[R] How to set up number of prin comp.

Jari Oksanen jarioksa at sun3.oulu.fi
Fri Feb 25 14:37:25 CET 2005


On Fri, 2005-02-25 at 20:29 +0800, WeiQiang.Li at seagate.com wrote:
> Hi Bjrn-Helge,
> 
>         Thanks for your help.
> 
>         In my case, there are more variables in the matrix than the units, 
> so I have to use Prcomp with covariance to do PCA. The problem I am facing 
> is how to get fisrt 8 coefficients and scores and how to write the result 
> into text file. Thanks again.
> 
>         When I change princomp to prcomp below, I will NULL for pc$scores 
> & pc$loadings.
> 
>         X <- some matrix
>         pc <- prcomp(X)
>         pc$scores[,1:4]    # The four first score vectors
>         pc$loadings[,1:4]  # The four first loadings
> 
Three most useful commands are help(), str() and names().

The first tells you how to use prcomp() and how it names its results.
Try help(prcomp).
The second peeks into the result so you see what is in there. Try (with
your result) srt(pc).
The third tells you what names are available in your result.
The first (help) is the most useful of these commands, since it tells
you what these names and items are. If you read it, you should say:
pc$x[, 1:4] # The four first score vectors
pc$rotation[, 1:4] # The four first loadings

Also, loadings(pc) should work with prcomp.

I think I'll write functions as.prcomp.princomp and as.princomp.prcomp
someday. 

cheers, jari oksanen
-- 
Jari Oksanen <jarioksa at sun3.oulu.fi>




More information about the R-help mailing list