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

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Sun, 18 May 2008 07:33:19 +0100 (BST)

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

______________________________________________
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.
Received on Sun 18 May 2008 - 06:41:23 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 18 May 2008 - 13:30:38 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive