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) doesnt show it. In particular, I guess I have a very nave question Id 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
> 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)) Andy> Thanks. > Anna > > ______________________________________________ > R-help at 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 > > >
getS3method("princomp", "default"). getAnywhere("princomp.default") The first of these is in the See Also of ?methods. On Thu, 5 May 2005, Anna Oganyan wrote:> Hello, > Is there any possibility in R to see the body of thenon-visible> function, for > exampleprincomp ?> If I do : > >> methods(princomp) > > so, I get that princomp.default and princomp.formula are non-visible > functions and > body(princomp.default) doesnt show it. > > In particular, I guess I have a very na?ve question& > Id 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.You have forgotten to centre your data. It may be more helpful to look at the predict method. -- 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
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) doesnt show it. > > In particular, I guess I have a very nave question > Id 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 ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:50] "Alabama" "Alaska" "Arizona" "Arkansas" ... ..$ : chr [1:4] "Comp.1" "Comp.2" "Comp.3" "Comp.4" > Xl <- Xc %*% loadings(pc) > str(Xl) num [1:50, 1:4] -64.8 -92.8 -124.1 -18.3 -107.4 ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:50] "Alabama" "Alaska" "Arizona" "Arkansas" ... ..$ : chr [1:4] "Comp.1" "Comp.2" "Comp.3" "Comp.4" > sum(abs(Xl - pc$scores)) [1] 4.97999e-12 > Finally, "data.matrix" is a function in the base package. I would avoid using it as a variable name. --sundar