How to use JSL to save T² from PCA platform (or how to calculate it)Hi All,
I am trying to get some JSL code working to save T² from the Principal Component platform, like how you can do it by calling <
I know that the call << Save T Square will work with multivariate control chart, but it doesn't seem to work with the PCA or Multivariate platforms. I need to use the PCA platform to generate the T² formula because in the PCA platform, you can actually specify the number of PCs to use in the calculation, it's not the normal T² statistic, like the Mahalanobis distance squared. Even the call << T²(1, Save T Square) sent to the report window doesn't work.
1. Is it possible to use scripts to specify the number of PCs to use and save the T² formula to the data table? So far, I have not found an option for this.
2. If not, how to best calculate it? I know the calculation is defined here, but the formula that's saved to data table using the red hot-button seems a bit different.
According to the saved formula (manual save) T² = Vec Quadratic(S, X)= Diag(X*S*X`), where X is the standardized vector of data, X` is its transpose, and S is the inverse covariance matrix. This is the standard T², the square of the Mahalanobis distance.
However, according to the documentation for the ith T² value from the PCA platform, T_i² = [X_ci*P_a]*L-1*[P`_a*X`_ci], where X_ci is the ith standardized observation, P_a is a matrix containing the first "a" eigenvalues (i.e. possibly not all of them), and L-1 is a diagonal matrix containing the first A eigenvalues. In principle, this should be equal to Vec Quadratic(L-1, [X_ci*P_a]), except instead of using the inverse covariance matrix, S, it's using the inverse axa eigenvalue matrix of the first "a" PCs, and X is now X_ci*P_a]. Or so, it would appear.
It seems rather complicated and unnecessary to re-engineer this problem in order to save the n-principal component T² when JMP already calculates all this for you. I just want to automate the saving process, but I can't seem to get option 1 to work, which means option 2 is the next best bet. Even then, trying to recover the formula in order to make a New Column with a formula in it seems a bit much.
I'm attaching a modified data table of the Iris.jmp file that shows the saved T² formulas from the multivariate platform (and how T² = Mahal.Dist.²), and then T² from the PCA platform where the formula is determined using all 4 PCs and then only to PCs. As you can see by looking at the formulas, the S matrix in the Vec Quadratic(S,X) is different for the T²s calculated using 2 PCs vs 4 PCs.
If anyone has insight into how to script getting the T² with a different number of PCs or how to properly formulate the column formula to calculate the T², I would be grateful for that insight.
Thanks for your feedback and thoughts!,
DS
AnalyticsData TableScriptingRe: How to use JSL to save T² from PCA platform (or how to calculate it)The capability to save T square is only available from the control chart platform, so your first approach is not possible at the moment.
You might search the Wish List here in the JMP Community to see if someone has already asked for it. If so, vote for it (kudo). If not, enter a new request.
Re: How to use JSL to save T² from PCA platform (or how to calculate it)Please see the attached script for the way to calculate T square. It was provided by the JMP developer of these platforms.
Re: How to use JSL to save T² from PCA platform (or how to calculate it)Hi @markbailey ,
Thanks for your feedback and including the JSL from the developer of the platform. Well, as it turns out, the solution to what I was after (option 1) is actually possible with JSL!! And so much simpler, too. I just needed to see it in the code that you sent. The critical part is here:
Outlier Analysis( 1, Number of Components( N ), Save T Square ),
By changing "N" in the Number of Components input, and adding the Save T Square, it will actually save the T² formula to the data table for the N components that you define. I didn't know that within the Outlier Analysis() call, you could send it the request to "Save T Square". Doing that along with the N components does exactly what I was hoping to do, thanks for the help! No need to put in a Wish List or anything, JMP can already do it for you!
I'm attaching the Iris_mod.jmp data table with scripts to that calculate the T² value for 2, 3, and 4 PCs. It is good to know how to efficiently calculate the T² value should I ever need to. The code I was just writing now to do the calculation was rather long and cumbersome, although I believe it would have worked once I got everything straightened out. However, I prefer the above solution -- it's only a few extra characters of code!
Upon closer inspection of the code you sent, I have noticed that the T² calculated from the PCA platform and the "manual" calculation in the code are different for the same number of PCs. Any clue as to why? Either the "manual" calculation is "wrong" or performed differently than how the PCA does it, or something else is going on. I'm also attaching another data table Iris_mod_different_T2.jmp that shows the T² results for the same number of PCs is different from the PCA platform and the "manual" calculation. Until further notice, I prefer to use the T² formula from the PCA platform. I would like to know if there something different about the "manual" calculation and why the values are not the same.
Thanks again!,
DS
P.S. If anyone is interested, the code I've been working on is below. It is an interactive code that allows the user to select the columns to perform the PCA on and then dynamically select the number of PCs to use for extracting out the DModX and T² formulas. You can even save the selected number of PCs to the data table if you want for other analyses.
A big thanks to @jthi for help with some other aspects of this code in order to get things to work as intended, like <
Names Default To Here( 1 );
//Clear Symbols();
//Function to choose the data table to model
choose_data_table = Function( {},
list = {};
For( i = 1, i <= N Table(), i++,
list[i] = Data Table( i ) << get name
);
win = New Window( "Select a data table",
<<MODAL></MODAL> PCRptOutlier << Save DModX( N_PCs );
If( savePCs == 1,
PCRptOutlier << Save Principal Components( N_PCs )
);
PCRptOutlier<<CLOSEWINDOW> 0, Try( choose_data_table, Throw( "No data found" ) )
);
ModMetrics;
);
Firstwin;</CLOSEWINDOW>