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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
bountiful
  • 814
  • 1
  • 8
  • 22
  • 1
    `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. – Henrik Mar 23 '13 at 11:02
  • @Henrik apologies, should be fixed now. Needed to add `use="complete.obs"` to `cor`. – bountiful Mar 23 '13 at 11:17
  • 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". – William Revelle Oct 12 '13 at 22:16

4 Answers4

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))
Henrik
  • 14,202
  • 10
  • 68
  • 91
  • 1
    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? – emudrak Apr 10 '17 at 18:59
  • Please specify the package from which you're calling `xtable()` from. – sunitprasad1 Feb 11 '18 at 12:36
  • @sunitprasad1 library `xtable` as specified in the question. – Henrik Feb 11 '18 at 14:31
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

William Revelle
  • 1,200
  • 8
  • 15
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")
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.

r0bt
  • 383
  • 3
  • 12