Question

I have 3 variables on which I ran PCA using prcomp. I tried to reconstruct the variables using the loadings and factors but residuals is not zero. Statistically (I might be wrong here) I was expecting to be able to reconstruct the original data. Am I missing something?

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
Was it helpful?

Solution

I use the following function for reconstructing data from a prcomp object:

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

I couldn't figure out exactly what was wrong with your code, but using the prcomp.recon function gives the right result:

> 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top