Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.LNX.4.61.0505051649510.21164@gannet.stats>
Date: 2005-05-05T15:54:17Z
From: Brian Ripley
Subject: body of non-visible function
In-Reply-To: <39B6DDB9048D0F4DAD42CB26AAFF0AFA076ED5@usctmx1106.merck.com>

On Thu, 5 May 2005, Liaw, Andy wrote:

>> From: Anna Oganyan
>>
>> Hello,
>> Is there any possibility in R to see the body of the "non-visible"
>> function, for
>> example "princomp"?
>> If I do :
>>
>> > methods(princomp)
>>
>> so, I get that princomp.default and princomp.formula are non-visible
>> functions and
>> body(princomp.default) doesn't show it.
>
> You may want to learn about getAnywhere() and getS3method().
>
>
>> In particular, I guess I have a very na?ve question...
>> I'd like to see how scores calculation is implemented in the function
>> princomp. Because when I multiply my data matrix on the
>> matrix of loadings
>> >data.matrix %*% princomp(data.matrix, scores=T)$loadings
>>
>> I get different result than just doing
>>
>> >princomp(data.matrix, scores=T)$scores.
>
> Variables are centered and scaled, so you want to do
>
>  scale(data.matrix) %*% loadings(princomp(data.matrix))

Only centred.  Had this been princomp(data.matrix, cor=TRUE, scores=TRUE)
they would have been scaled.  It is better to follow my hint of looking at
predict.princomp:

     object <- princomp(data.matrix)
     scale(data.matrix, object$center, object$scale) %*% object$loadings

BTW, data.matrix is an R function, so not a good choice of name.

-- 
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