From jari.oksanen at oulu.fi  Wed May 23 12:49:19 2012
From: jari.oksanen at oulu.fi (Jari Oksanen)
Date: Wed, 23 May 2012 10:49:19 +0000
Subject: [Rd] prcomp with previously scaled data: predict with 'newdata'
	wrong
Message-ID: <66C03CD1145C95448A4D73676DD9C2ED2C9B3A@nippu2.univ.yo.oulu.fi>

Hello folks,

it may be regarded as a user error to scale() your data prior to prcomp() instead of using its 'scale.' argument. However, it is a user thing that may happen and sounds a legitimate thing to do, but in that case predict() with 'newdata' can give wrong results:

x <- scale(USArrests)
sol <- prcomp(x)
all.equal(predict(sol), predict(sol, newdata=x))
## [1] "Mean relative difference: 0.9033485"

Predicting with the same data gives different results than the original PCA of the data.

The reason of this behaviour seems to be in these first lines of stats:::prcomp.default():

    x <- scale(x, center = center, scale = scale.)
    cen <- attr(x, "scaled:center")
    sc <- attr(x, "scaled:scale")

If input data 'x' have 'scaled:scale' attribute, it will be retained if scale() is called with argument "scale = FALSE" like is the case with default options in prcomp(). So scale(scale(x, scale = TRUE), scale = FALSE) will have the 'scaled:center' of the outer scale() (i.e, numerical zero), but the 'scaled:scale' of the inner scale(). 

Function princomp  finds the 'scale' directly instead of looking at the attributes of the input data, and works like expected:

 sol <- princomp(x)
all.equal(predict(sol), predict(sol, newdata=x))
## [1] TRUE

I don't have any nifty solution to this -- only checking the 'scale.' attribute and acting accordingly:

sc <- if (scale.) attr(x, "scaled:scale") else FALSE

Cheers, Jari Oksanen




[Rd] prcomp with previously scaled data: predict with 'newdata' wrong
Jari Oksanen