关于PCA R语言的问题

时间:2015-05-24 13:17:07

标签: r covariance pca eigenvalue princomp

我是R的新生。我想展示协方差矩阵Σ与特征向量和特征值之间的关系。   我知道Σ可以分解为:∃P,∃D:Σ= P. D. P'与P 特征向量矩阵和D对角元素的对角矩阵 相应的特征值。但是我的结果与协方差矩阵不同。我的Σ等于相关矩阵。  这是我的代码:

> 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
> 

任何人都可以解释我的问题在哪里?非常感谢。

1 个答案:

答案 0 :(得分:1)

您明确告诉$.post("url", {body: 'hi'}, function(response) { .. });在此行中使用 cor 关系矩阵:

princomp

如果省略参数并只使用test<-princomp(data,cor=T) ,它将使用协方差矩阵,您将获得您期望的结果(粗略)。