Reputation: 814
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
?
Upvotes: 12
Views: 6661
Reputation: 591
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.
Upvotes: 0
Reputation: 1
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")
Upvotes: 0
Reputation: 1230
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
Upvotes: 6
Reputation: 14450
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))
Upvotes: 22