psych - Getting factor loadings as data.frame for LaTeX export
Asked Answered
S

4

12

I am using the psych package's fa command for factor analysis, and so have an object of class fa. I can query the loadings with fac$loadings, but I want to only extract the table containing the loadings, so I can use xtable (or similar) to convert it into LaTeX format.

Example code:

library(psych)
library(xtable)
data(bfi)
fac <- fa(r=cor(bfi, use="complete.obs"), nfactors=5, fm="ml", rotate="none")
fac$loadings
ld <- someMagicalFunction(fac$loadings)
xtable(ld)

Can anyone tell me what I can use for someMagicalFunction?

Shoreless answered 23/3, 2013 at 10:51 Comment(3)
fac <- fa(r=cor(bfi), nfactors=5, fm="ml", rotate="none") gives me a lot of error messages (e.g., Something is seriously wrong the correlation matrix.) and does not produce anything.Discolor
@Discolor apologies, should be fixed now. Needed to add use="complete.obs" to cor.Shoreless
Actually, just say fa(bfi,nfactors=5, fm="ml",rotate="none"). That will find the pairwise correlation matrix without needing to do it your self. As of version 1.3.10.11, fa will take the use parameter if you want to specify use="complete".Outcurve
D
22

When you look at fac$loading, you see that it is a S3 object. Removing the class attribute gives you a matrix which can then be passed to xtable:

str(fac$loadings)
class(fac$loadings)

xtable(unclass(fac$loadings))
Discolor answered 23/3, 2013 at 11:21 Comment(3)
This command works to get the actual loadings matrix, but how can you capture the bottom half of the print.loadings() command, which gives the SS loadings and the Proportion and cumulative Variance?Reluctivity
Please specify the package from which you're calling xtable() from.Icebox
@Icebox library xtable as specified in the question.Discolor
O
6

That works fine.

An alternative is to use the fa2latex function in psych:

Using your example:

library(psych)
fac <- fa(bfi,5)
fa2latex(fac)

will give you an APA ready LaTeX table.

Bill

Outcurve answered 9/6, 2013 at 18:16 Comment(0)
D
0

The result of xtable is in HTML language. If you want to save it as a file, you can use:

print.xtable(x, type="HTML", file="table.html")
Devolution answered 11/10, 2019 at 12:48 Comment(0)
H
0

Another alternative is to call fac$Vaccounted that will pull the proportional variance, cumulative variance, etc that can then be put into a df or kable object:

fac$Vaccounted %>% kable()

Do the same with fac$weights to access the loadings:

fac$weights

That should cover all the output you needed.

Halfway answered 10/5, 2022 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.