I’m working on confirmatory factor analysis (CFA) in R and have a couple of questions:
-
My model only uses some of the variables from my original dataset (x1, x2, x3, x5, x8, x9 out of x1-x9). When I run cfa() with just these variables, R throws an error. I’ve been creating a new CSV file with only the needed variables as a workaround. Is there an easier way to select specific variables for CFA?
-
After getting my model fit, how can I present the output in a nicer format than R’s default? I’d like to create a clean table for CFA results and other analyses like KMO.
Any help would be great! I’m still learning how to handle these issues in R.
yo ava, for selecting variables try using dplyr::select(). it’s super easy:
your_data %>% select(x1, x2, x3, x5, x8, x9)
For nicer output, check out the sjPlot package. it’s awesome for making pretty tables from cfa results. just do:
sjPlot::tab_model(your_cfa_model)
hope that helps! lmk if u need anything else
I’ve found that using the lavaan package for CFA in R can simplify both variable selection and output presentation. For your first issue, you can specify your model using only the variables you need:
model ← ‘factor =~ x1 + x2 + x3 + x5 + x8 + x9’
fit ← cfa(model, data = your_dataset)
This approach lets you work with your full dataset without creating new files.
For nicer output, the semPlot package is quite useful:
library(semPlot)
semPaths(fit, ‘std’, layout = ‘tree’)
This creates a visual representation of your CFA model with standardized coefficients. For tabular output, the broom package can help:
library(broom)
tidy(fit)
This gives you a clean, formatted table of your CFA results. Combining these methods should improve your workflow and presentation significantly.
Hey Ava_Books! Great questions about CFA in R. I’ve been dabbling with it too and can totally relate to the struggle.
For your first question, have you tried using the subset function? Something like:
cfa_data ← subset(your_dataset, select = c(x1, x2, x3, x5, x8, x9))
Then you can use cfa_data in your cfa() function. Might save you the hassle of creating new CSV files each time.
As for prettifying the output, have you looked into the ‘stargazer’ package? It’s a game-changer for making nice tables from R output. You could try something like:
library(stargazer)
stargazer(your_cfa_model, type = ‘text’)
This should give you a more polished table. You can even export it to HTML or LaTeX if you’re feeling fancy!
What kind of specific info are you looking to include in your tables? I’m curious about how others are presenting their CFA results too. Have you seen any good examples in papers or presentations that you’re trying to emulate?