Re: [R] body of non-visible function

From: Sundar Dorai-Raj <sundar.dorai-raj_at_pdf.com>
Date: Fri 06 May 2005 - 01:30:30 EST

Anna Oganyan wrote on 5/5/2005 7:42 AM:
> 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.
>
> 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.
>
> Thanks.
> Anna
>

Hi Anna,

Use getAnywhere("princomp.default") or stats:::princomp.default. This has to do with princomp.default not being exported in the stats package NAMESPACE. As for the difference, the scores are based on centering the columns of data.matrix before determining the principal components. For example:

 > X <- as.matrix(USArrests)
 > Xc <- sweep(X, 2, colMeans(X), "-")
 > pc <- princomp(X, scores = TRUE) # inappropriate, see ?princomp
 > str(pc$scores)

  num [1:50, 1:4] -64.8 -92.8 -124.1 -18.3 -107.4 ...

Finally, "data.matrix" is a function in the base package. I would avoid using it as a variable name.

--sundar



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 Fri May 06 02:10:51 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:36 EST