prcomp:PCA残差不为零

时间:2014-05-12 07:12:48

标签: r pca

我有3个变量,我使用prcomp运行PCA。我尝试使用负载和因子重建变量,但残差不为零。统计上(我可能在这里错了)我期望能够重建原始数据。我错过了什么吗?

test = read.table(text='0.8728891 0.7403704 0.6655271
0.8697503 0.7447901 0.6629487
0.8569866 0.7321241 0.6493666
0.8824890 0.7405750 0.6505887
0.8912246 0.7334331 0.6508194
0.8930270 0.7381421 0.6448108
0.8721081 0.7173891 0.6355404
0.8649705 0.7326563 0.6493313
0.8976412 0.7249211 0.6437649
0.9233625 0.7406451 0.6454023',sep=' ')

pca = prcomp(test,center=T,scale=F)
pca$x %*%  pca$rotation + matrix(1,nrow=nrow(test),ncol=1) %*% pca$center  - test 

V1            V2            V3
-0.0020186611  0.0071487188 -0.0240478838
-0.0004352159 -0.0005375912 -0.0262594828
0.0008042558 -0.0039840874 -0.0019352850
0.0009905100 -0.0053390749 -0.0067663626
-0.0008375576  0.0041104957  0.0016244986
0.0013586563 -0.0060476694  0.0036526104
0.0004278214  0.0009280342  0.0298641699
0.0005504918 -0.0026885505 -0.0009348334
-0.0011619165  0.0073130849  0.0185829183
0.0003216158 -0.0009033601  0.0062196504

1 个答案:

答案 0 :(得分:1)

我使用以下函数重建prcomp对象的数据:

#This function reconstructs a data set using a defined set of principal components.
#arguments "pca" is the pca object from prcomp, "pcs" is a vector of principal components
#to be used for reconstruction (default includes all pcs)
prcomp.recon <- function(pca, pcs=NULL){
  if(is.null(pcs)) pcs <- seq(pca$sdev)
  recon <- as.matrix(pca$x[,pcs]) %*% t(as.matrix(pca$rotation[,pcs]))
  if(pca$scale[1] != FALSE){
    recon <- scale(recon , center=FALSE, scale=1/pca$scale)
  }
  if(pca$center[1] != FALSE){
    recon <- scale(recon , center=-pca$center, scale=FALSE)
  }
  recon
}

我无法确切地知道您的代码究竟出了什么问题,但使用prcomp.recon函数会得到正确的结果:

> prcomp.recon(pca) - test
   V1 V2 V3
1   0  0  0
2   0  0  0
3   0  0  0
4   0  0  0
5   0  0  0
6   0  0  0
7   0  0  0
8   0  0  0
9   0  0  0
10  0  0  0