Java_Coder_01
Java_Coder_01

Reputation: 13

Printing in R - PCA rotation components

I did a PCA in R and I am trying to print the rotation components. I was pretty much trying to understand a snippet I found online and I would really appreciate if somebody could help me with it. Please see below the snippet I found online:

    require(stats) 
    prcomp(top2, scale=TRUE) 
    summary(prcomp(top2, scale=TRUE)) 

    for (i in 1:15) { 
    top4[[i]] <- sort(survey.prcomp$rotation[,i], decreasing=TRUE)[1:4]} 
    top4

I am trying to print top 15 principal components and I get the "top4 object not found error". I am pretty new to R and would appreciate it if somebody could please explain it.

The snippet can be found at https://www.casact.org/pubs/forum/10spforum/Francis_Flynn.pdf

Thanks a lot!

Upvotes: 0

Views: 810

Answers (2)

Karolis Koncevičius
Karolis Koncevičius

Reputation: 9656

The snippet you found does not work because there is no declared "survey.prcomp" object. "top4" is missing as well. I assume the authors missed this line:

survey.prcomp <- prcomp(top2, scale=TRUE)

And also this one:

top4 <- list()

Then, if your aim is to get first 15 rotation vectors, you can do so with survey.prcomp$rotation[,1:15]

The snippet you pasted does something different. It returns, for each of 15 main principal components, top 4 variables that have the most influence on the loadings (rotations).

Upvotes: 1

Giuseppe Romagnuolo
Giuseppe Romagnuolo

Reputation: 3402

In the snippet you pasted a series of variables are accessed but never assigned hence the error.

top2, survey.prcomp and top4 are never assigned, in the document you have attached the author seem have omitted those lines.

Upvotes: 0

Related Questions