【问题标题】:prcomp : PCA residuals not zeroprcomp : PCA 残差不为零
【发布时间】:2014-05-12 07:12:47
【问题描述】:

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

【问题讨论】:

    标签: r pca


    【解决方案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
    

    【讨论】:

    • 感谢您的回答。完全愚蠢,我忘了转置我的矩阵....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多