Extracting output from principal function in psych package as a data frame
Asked Answered
T

2

12

When I use the principal function, like in the following code, I get a nice table which gives all the standardized loadings, as well as a table with the eigenvalues and the proportion and cumulative proportion explained.

rotatedpca <- principal(PCFdataset, nfactors = 8, rotate = "varimax", scores = T)

I would like to export this output to an excel file (using WriteXLS), but I can only do that for dataframes, and rotatedpca is not a dataframe and cannot be coerced into one it seems. I am able to extract the standardized loadings by using the following code:

loadings<-as.data.frame(unclass(rotatedpca$loadings))

But I cannot figure out how to access the other information that normally displays when I simply call the principal function, in particular the eigenvalues and the proportion and cumulative variance explained. I tried rotatedcpa$values, but that returns what looks like the eigenvalues for all 12 original variables as factors without rotation, which I don't understand. And I haven't been able to figure out any way to even try to extract the variance explained values. How can I simply create a dataframe that looks like the R output I get below from the principal function, for example?

                       RC2  RC3  RC8  RC1  RC4  RC5  RC6  RC7
SS loadings           1.52 1.50 1.45 1.44 1.01 1.00 0.99 0.98
Proportion Var        0.13 0.12 0.12 0.12 0.08 0.08 0.08 0.08
Cumulative Var        0.13 0.25 0.37 0.49 0.58 0.66 0.74 0.82
Proportion Explained  0.15 0.15 0.15 0.15 0.10 0.10 0.10 0.10
Cumulative Proportion 0.15 0.31 0.45 0.60 0.70 0.80 0.90 1.00

Thanks for reading my post!

Tortoni answered 28/6, 2013 at 18:23 Comment(5)
can you dput() your data or some other data to make this reproducible? what package is principal in? i would check names(unclass(rotatedpca)) or str(rotatedpca)Scission
@JakeBurkhead This is using the psych package, as noted in the post title.Pacification
@HongOoi sorry, my reading comprehension is bad... :)Scission
I was (sort of) mistaken. It appears to actually be generated inside print.psych which is an absolute beast of a function. But if you sift through it long enough, so you should find it there somewhere.Ocular
...and then you'll be sent off to psych:::print.psych.fa, where about midway down you'll see the code that prints out that last piece.Ocular
A
11

I have just added this feature to the latest (as of today) release of psych 1.3.10.11. If you either

 f3 <- fa(Thurstone,3) 
   #or
   p3 <- principal(Thurstone,3)
   #then
   p <- print(f3)
   p # will give you
    p
   $Vaccounted
                            MR1       MR2       MR3
   SS loadings           2.6411150 1.8621522 1.4951831
  Proportion Var        0.2934572 0.2069058 0.1661315
  Cumulative Var        0.2934572 0.5003630 0.6664945
  Proportion Explained  0.4402995 0.3104389 0.2492616
  Proportion            0.4402995 0.7507384 1.0000000

In general, if you have suggestions or questions re the psych package, you will get a faster answer if you contact me directly.

Bill

Altricial answered 12/10, 2013 at 23:23 Comment(0)
Z
5

Why not this:

 capture.output( print(rotatedpca), file="pc.txt")

You can read desired portions into Excel using its Text to Columns... function off the /Data menu. Or you can just paste it into an open blank Excel document and select the rows you want to convert. Use the "fixed" option that will probably be offered automagically.

Zosi answered 28/6, 2013 at 20:34 Comment(2)
Oh thanks @HongOoi , I thought it is a typo, G and T are close ;-)Evie
Thanks, @DWin! This definitely works. I was hoping for something more elegant, but it looks like the psych package output just isn't written in a way that will let me automate what I want to do as much as I would like... :)Tortoni

© 2022 - 2024 — McMap. All rights reserved.