[R] PCA scores

Gavin Simpson gavin.simpson at ucl.ac.uk
Fri Apr 16 19:47:50 CEST 2010


On Fri, 2010-04-16 at 10:23 -0700, phoebe kong wrote:
> Hi all,
> 
> I have a difficulty to calculate the PCA scores. The PCA scores I calculated
> doesn't match with the scores generated by R,
> 
> mypca<-princomp(mymatrix, cor=T)
> 
> myscore<-as.matrix(mymatrix)%*%as.matrix(mypca$loadings)
> 
> Does anybody know how the mypca$scores were calculated? Is my formula not
> correct?

You need to apply the centring and scaling done because you set 'cor =
TRUE' in your princomp call. Here's an example using the inbuilt 'swiss'
data set.

data(swiss)
pc <- princomp(swiss, cor = TRUE)
my.scr <- with(pc, scale(swiss, center = center, scale = scale) %*% 
                   loadings(pc))
all.equal(my.scr, pc$scores)

You can see all of this in the princomp code if you look closely:

getAnywhere(princomp.default)

HTH

> 
> Thanks a lot!
> 
> Phoebe
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list