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

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun May 18 08:33:19 CEST 2008


This is not to do with 'wide' matrices' but to do with the lack of column 
names.

There is a bug -- the definition of 'p' should have NROW and not NCOL.

On Sun, 18 May 2008, Gad Abraham wrote:

> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list