1
$\begingroup$

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?

$\endgroup$
4
  • 1
    $\begingroup$ 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. $\endgroup$
    – ttnphns
    Apr 27, 2015 at 6:43
  • $\begingroup$ @ttnphns Thanks so much. This makes complete sense. $\endgroup$
    – GaidinD
    Apr 27, 2015 at 11:42
  • $\begingroup$ @ttnphns: Your comment is a full and complete answer to this question. Consider posting it as such... $\endgroup$
    – amoeba
    Apr 27, 2015 at 18:55
  • $\begingroup$ You might also be interested in the concept of Partial Least Squares. $\endgroup$
    – John
    Apr 27, 2015 at 19:02

1 Answer 1

2
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – amoeba
    Apr 27, 2015 at 19:22
  • $\begingroup$ 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 $\endgroup$
    – Aksakal
    Apr 27, 2015 at 19:44
  • $\begingroup$ 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. $\endgroup$
    – amoeba
    Apr 27, 2015 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.