Search code examples
routputdimensionality-reductionfactor-analysis

Edit Output from Factor Analysis (R)


I am working on a project that involves performing factor analysis. Here is a chunk of example code:

fit <- factanal(mtcars, 2, rotation="varimax")
print(fit, digits=4, cutoff=.3, sort=TRUE)

The output for this code is way too long. I only want to extract out the bottom portion of the loadings output with the variances per factor and the hypothesis test after it.

If I try fit$loadings, I still get too much output and I lose the hypothesis test at the bottom. Does anyone know of a way to pull out these specific parts of the output?

Thank you!


Solution

  • This is a different hack, but it gets the job done:

    capture.output(print(fit, digits=4, cutoff=.3, sort=TRUE), file="temp.txt")
    cat(readLines("temp.txt")[23:30], sep="\n")
    #                Factor1 Factor2
    # SS loadings     4.4938  4.3567
    # Proportion Var  0.4085  0.3961
    # Cumulative Var  0.4085  0.8046
    # 
    # Test of the hypothesis that 2 factors are sufficient.
    # The chi square statistic is 68.57 on 34 degrees of freedom.
    # The p-value is 0.000405