From bhx5 at mevik.net  Fri Nov 18 13:04:39 2005
From: bhx5 at mevik.net (bhx5@mevik.net)
Date: Fri, 18 Nov 2005 13:04:39 +0100 (CET)
Subject: [Rd] pr[in]comp: predict single observation when data has colnames
	(PR#8324)
Message-ID: <20051118120439.D8D8F22424@slim.kubism.ku.dk>

To my knowledge, this has not been reported previously, and doesn't
seem to have been changed in R-devel or R-patched.

If M is a matrix with coloumn names, and

mod <- prcomp(M)  # or princomp

then predicting a single observation (row) with predict() gives the
error

Error in scale.default(newdata, object$center, object$scale) : 
	length of 'center' must equal the number of columns of 'x'

This doesn't happen if M doesn't have coloumn names.

For instance:

> M <- matrix(rnorm(30), ncol = 3)
> mod <- prcomp(M[-1,])
> predict(mod, newdata = M[1,, drop = FALSE])
           PC1       PC2       PC3
[1,] -1.666191 -2.333012 -1.424587

> colnames(M) <- 1:3
> mod <- prcomp(M[-1,])
> predict(mod, newdata = M[1,, drop = FALSE])
Error in scale.default(newdata, object$center, object$scale) : 
	length of 'center' must equal the number of columns of 'x'


I believe the problem is the line

        newdata <- newdata[, nm]

in predict.prcomp (line 106 of prcomp.R) and predict.princomp (line 11
of princomp-add.R), which should probably be

        newdata <- newdata[, nm, drop = FALSE]



Version:
 platform = x86_64-unknown-linux-gnu
 arch = x86_64
 os = linux-gnu
 system = x86_64, linux-gnu
 status = 
 major = 2
 minor = 2.0
 year = 2005
 month = 10
 day = 06
 svn rev = 35749
 language = R

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

Search Path:
 .GlobalEnv, package:methods, package:stats, package:graphics, package:grDevices, package:utils, package:datasets, Autoloads, package:base

-- 
Bj?rn-Helge Mevik



[Rd] pr[in]comp: predict single observation when data has colnames (PR#8324)
bhx5@mevik.net