Re: [R] question about Principal Component Analysis in R?

From: Bjørn-Helge Mevik <bhs2_at_mevik.net>
Date: Tue 28 Feb 2006 - 19:52:53 EST

Michael wrote:

>> pca=prcomp(training_data, center=TRUE, scale=FALSE, retx=TRUE);
>
> Then I want to rotate the test data set using the
>
>> d1=scale(test_data, center=TRUE, scale=FALSE) %*% pca$rotation;
>> d2=predict(pca, test_data, center=TRUE, scale=FALSE);
>
> these two values are different
>
>> min(d2-d1)
> [1] -1.976152
>> max(d2-d1)
> [1] 1.535222

This is because you have subtracted a different means vector. You should use the coloumn means of the training data (as predict does; see the last line of stats:::predict.prcomp):

d1=scale(test_data, center=pca$center, scale=FALSE) %*% pca$rotation;

-- 
Bjørn-Helge Mevik

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Tue Feb 28 20:02:52 2006

This archive was generated by hypermail 2.1.8 : Tue 28 Feb 2006 - 21:08:48 EST