[Rd] predict.prcomp: wrong check for matrix dimensions (PR#11482)

gabraham at csse.unimelb.edu.au gabraham at csse.unimelb.edu.au
Sun May 18 11:15:07 CEST 2008


Full_Name: Gad Abraham
Version: 2.7.0
OS: Ubuntu Linux
Submission from: (NULL) (59.167.148.182)


Description:
============

> 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

Solution:
=========

Change
   p <- NCOL(object$rotation)
to
   p <- NROW(object$rotation)

This bug is still present in R-patched_2008-05-16.

> sessionInfo()
R version 2.7.0 (2008-04-22) 
x86_64-unknown-linux-gnu 

locale:
LC_CTYPE=en_AU.UTF-8;LC_NUMERIC=C;LC_TIME=en_AU.UTF-8;LC_COLLATE=en_AU.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_AU.UTF-8;LC_PAPER=en_AU.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_AU.UTF-8;LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base



More information about the R-devel mailing list