[R] predict.prcomp: 'newdata' does not have the correct number of columns

Gad Abraham gabraham at csse.unimelb.edu.au
Sun May 18 04:44:43 CEST 2008


Hi,

I'm doing PCA on wide matrices and I don't understand why calling 
predict.prcomp on it throws an error:

 > x1 <- matrix(rnorm(100), 5, 20)
 > x2 <- matrix(rnorm(100), 5, 20)
 > p <- prcomp(x1)
 > predict(p, x2)
Error in predict.prcomp(p, x2) :
   'newdata' does not have the correct number of columns
 > dim(x2)
[1]  5 20
 > dim(p$rotation)
[1] 20  5

All predict.prcomp really does is x2 %*% p$rotation, which is a 
perfectly legal operation that should yield a 5x5 matrix:

 > dim(x2 %*% p$rotation)
[1] 5 5

Thanks,
Gad

-- 
Gad Abraham
Dept. CSSE and NICTA
The University of Melbourne
Parkville 3010, Victoria, Australia
email: gabraham at csse.unimelb.edu.au
web: http://www.csse.unimelb.edu.au/~gabraham



More information about the R-help mailing list