# How do you get the principal components of one matrix along the principal directions of another matrix?

I have a data matrix, A, on which I have performed principal component analysis (PCA) using the prcomp function in R. This gives me the rotation (eigenvector) field and the x (rotated data) field.

Now I have another data matrix, B. I want to find the principal components of B along the principal directions of the rotation of A obtained above. How would I go about doing this?

So far, I've just calculated the two prcomp's separately and used a vector projection to project the principal components of B along the eigenvectors of A. Is this correct?

• Am I right that I'm understanding you as that you want just to compute PC scores for the new, incoming data points? If yes then do this. By the "rotated data" you mean principal component scores, don't you? They are given by AV (V is your eigenvectors from decomposing A). If you then do BV you'll get more component scores: the scores that are B rotated by V. You don't need to do PCA on B at all. If A was centered or standardized prior its decomposing you should do the same with B, and it is wise to use the mean or st.dev. taken from A at that action. – ttnphns Apr 27 '15 at 6:43
• @ttnphns Thanks so much. This makes complete sense. – GaidinD Apr 27 '15 at 11:42
• @ttnphns: Your comment is a full and complete answer to this question. Consider posting it as such... – amoeba Apr 27 '15 at 18:55
• You might also be interested in the concept of Partial Least Squares. – John Apr 27 '15 at 19:02

You get the coefficients from PCA. These coefficients are multiplied by your observation matrix to obtain the components. So, multiply rotation by the new observation matrix instead. Don't forget to center it.

Here's the code.

Run PCA and see how the score matrix is obtained from the original data and the rotation. Note, that I'm NOT centering, and you probably should.

> x=matrix(c(1,2,3,2,4,5.5),3,2)
> x
[,1] [,2]
[1,]    1  2.0
[2,]    2  4.0
[3,]    3  5.5
> r=prcomp(x,retx=1,center=FALSE)
> r$rotation PC1 PC2 [1,] -0.4666132 0.8844615 [2,] -0.8844615 -0.4666132 > r$x
PC1         PC2
[1,] -2.235536 -0.04876479
[2,] -4.471072 -0.09752958
[3,] -6.264378  0.08701220
> x %*% r$rotation PC1 PC2 [1,] -2.235536 -0.04876479 [2,] -4.471072 -0.09752958 [3,] -6.264378 0.08701220  Now, apply the same rotation to the different data (again, see that I am NOT centering). > y=matrix(c(1,2,3,2,4,6.5),3,2) > y [,1] [,2] [1,] 1 2.0 [2,] 2 4.0 [3,] 3 6.5 > y %*% r$rotation
PC1         PC2
[1,] -2.235536 -0.04876479
[2,] -4.471072 -0.09752958
[3,] -7.148839 -0.37960095


Note the similarity of the new scores.

By the way, this is used a lot in forecasting with PCA. We obtain the rotation on historical data, then apply it to new data.

• This is a very concise answer (even more concise than @ttnphns'es comment above). Consider supplementing it by some explicit formulas and/or by an R expert, this would make it much more clear. – amoeba Apr 27 '15 at 19:22
• This isn't R forum, so I don't think the code samples are necessary, but added just in case. I don't always code in R, and don't always remember the syntax – Aksakal Apr 27 '15 at 19:44
• I agree that code samples are not necessary, but they are often helpful (be they in R, Matlab, Python, or any other well-known language). Thanks, +1. – amoeba Apr 27 '15 at 19:54