rcovariancepcaeigenvalueprincomp

quesstion about PCA R language


I'm a freshman in R. I want to show the relationship between convariance matrix Σ and eigenvectors and eigenvalues. I know that Σ can be factorized such that : ∃P, ∃D: Σ = P. D. P' with P the eigenvector matrix and D the diagonal matrix whose diagonal elements are the corresponding eigenvalues. But my result is not the same as covariance matrix.My Σ is equal to correlation matrix. Here is my code:

> data<-scale(swiss,center=T,scale=F)
> test<-princomp(data,cor=T)
> D=test$sdev
> Var=D^2
> Var
> Var=diag(Var)
> Loa=test$loadings
> Loa
>  Loa=Loa[1:6,1:6]
> sigma= Loa %*% Var %*% t(Loa)
> sigma
                  Fertility Agriculture Examination   Education   Catholic Infant.Mortality
Fertility         1.0000000  0.35307918  -0.6458827 -0.66378886  0.4636847       0.41655603
Agriculture       0.3530792  1.00000000  -0.6865422 -0.63952252  0.4010951      -0.06085861
Examination      -0.6458827 -0.68654221   1.0000000  0.69841530 -0.5727418      -0.11402160
Education        -0.6637889 -0.63952252   0.6984153  1.00000000 -0.1538589      -0.09932185
Catholic          0.4636847  0.40109505  -0.5727418 -0.15385892  1.0000000       0.17549591
Infant.Mortality  0.4165560 -0.06085861  -0.1140216 -0.09932185  0.1754959       1.00000000

> cov(data)
                 Fertility Agriculture Examination   Education   Catholic Infant.Mortality
Fertility        156.04250  100.169149  -64.366929  -79.729510  241.56320        15.156193
Agriculture      100.16915  515.799417 -124.392831 -139.657401  379.90438        -4.025851
Examination      -64.36693 -124.392831   63.646623   53.575856 -190.56061        -2.649537
Education        -79.72951 -139.657401   53.575856   92.456059  -61.69883        -2.781684
Catholic         241.56320  379.904376 -190.560611  -61.698830 1739.29454        21.318116
Infant.Mortality  15.15619   -4.025851   -2.649537   -2.781684   21.31812         8.483802
> 

Can anyone explain where is my probleme? thanks so much in advance.


Solution

  • You explicitly told princomp to use correlation matrix in this line:

    test<-princomp(data,cor=T)
    

    If you omit the parameter and just use test <- printcomp(data), it will use covariance matrix and you'll get the results(roughly) you're expecting.