2
votes

Given a data matrix M, pc=prcomp(M) provides pc$rotation (a matrix of eigenvectors) and pc$x, the scores of the original variables in the pca space. However, the scores I obtain don't match inner products computed "by hand."

For instance, if I have matrix

m1=matrix(c(1,2,3,4,4,8,7,9,5,3,2,11),byrow=TRUE,nrow=3)

pctest=prcomp(m1) returns the following for pctest$x, pctest$rotation, respectively:

Rotation:
          PC1        PC2        PC3
    [1,] -0.3751603  0.3133237 -0.5240612
    [2,] -0.5810952 -0.4802203  0.5681371
    [3,] -0.3471051 -0.5836868 -0.6211215
    [4,] -0.6333255  0.5749142  0.1295694


pctest$x
           PC1       PC2           PC3
     [1,]  5.11167 -1.326545 -1.110223e-16
     [2,] -4.05543 -2.728072 -1.942890e-15
     [3,] -1.05624  4.054616  2.831069e-15

Now, the score of variable 1 on PCA axis 2 (for instance) should just be the inner product of m1[1,] on pctest$rotation[,2], which is

        m1[1,]%*%pctest$rotation[,2]
        [,1]
        [1,] -0.09852071

Rather than pctest$x[1,2], which is -1.3265

Is this just a matter of scaling, or is $x returning something other than the projections of the original variables onto the PCA axes?

1
+1 for a clear and concise question with reproducible code.Paul Hiemstra

1 Answers

3
votes

Reading the documentation for ?prcomp answers your question: The calculation is done by a singular value decomposition of the (centered and possibly scaled) data matrix...

You would need to center and scale m1[1,] before you can multiply it with the corresponding PCA vector. This can be readily observed by setting the center and scale arguments to FALSE after which:

all.equal(m1 %*% pctest$rotation, pctest$x)
[1] TRUE