2014-05-12 5 views
2

prcomp를 사용하여 PCA를 실행 한 변수가 3 개 있습니다. 로드 및 요소를 사용하여 변수를 재구성하려고했지만 잔차가 0이 아닙니다. 통계적으로 (내가 틀렸을 수도 있습니다) 원래 데이터를 재구성 할 수 있기를 기대했습니다. 내가 놓친 게 있니?prcomp : 0이 아닌 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

나는 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 
+0

답장을 보내 주셔서 감사합니다. 완전히 어리석은, 나는 매트릭스를 바꾸는 것을 잊었다 .... – hjw

관련 문제