2015-05-24 64 views
1

我是R大一新生。我想展示信息矩阵Σ与特征向量和特征值之间的关系。 我知道Σ可以因式分解使得:∃P,∃D:Σ= P.D.P'是特征向量矩阵,D是对角线元素的对角元素是对应的特征值。但是我的结果与协方差矩阵不一样。我的Σ等于相关矩阵。 这里是我的代码:关于PCA R语言的问题

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

谁能解释哪里是我的probleme?非常感谢。

回答

1

您明确告诉princomp使用心病关系矩阵在这一行:

test<-princomp(data,cor=T) 

如果省略该参数,只使用test <- printcomp(data),它将使用协方差矩阵,你会得到的结果(约)你在期待。

+0

你是对的。非常感谢@ Anton –

+0

不客气。如果你对答案满意,就接受它,这样它就可以解决问题。 – Anton