Solved – Principal components using correlation matrix in R

pcar

My understanding is that prcomp and princomp work off the dataset itself (row of observations, across variables in the columns). Is there a function that will run a principal component analysis directly off a correlation or covariance matrix, without having the "raw" dataset?

Best Answer

You can use eigen(). For example:

> set.seed(3)
> x <- matrix(rnorm(18), ncol=3)
> x
           [,1]        [,2]       [,3]
[1,]  1.2243136 -0.48445511  0.9006247
[2,]  0.1998116 -0.74107266  0.8517704
[3,] -0.5784837  1.16061578  0.7277152
[4,] -0.9423007  1.01206712  0.7365021
[5,] -0.2037282 -0.07207847 -0.3521296
[6,] -1.6664748 -1.13678230  0.7055155

> prcomp(x)
Standard deviations:
[1] 1.0294417 0.9046837 0.4672911

Rotation:
             PC1         PC2         PC3
[1,] -0.84047203 -0.53902142  0.05534150
[2,]  0.53878561 -0.84219645 -0.02037687
[3,] -0.05759199 -0.01269102 -0.99825954

> eigen(cov(x))
$values
[1] 1.0597501 0.8184527 0.2183610

$vectors
            [,1]       [,2]        [,3]
[1,]  0.84047203 0.53902142 -0.05534150
[2,] -0.53878561 0.84219645  0.02037687
[3,]  0.05759199 0.01269102  0.99825954

So the eigenvalues of the covariance matrix are the squares of the standard deviations (i.e, variances) of the principal components and the principal components themselves are same as eigenvectors of covariance matrix (though signs may be opposite as they are here).