I’m working on a Confirmatory Factor Analysis (CFA) project using the Lavaan package in R. I’ve successfully extracted the shared variance among my observed variables to represent broader factors. However, I’m stumped on how to get the unique variance for each indicator.
The summary output shows mean variances, but I can’t seem to extract these like I do with factor scores. I’ve tried various approaches, but nothing seems to work. Here’s a simplified version of what I’ve done so far:
library(lavaan)
# Define the model
model <- '
factor1 =~ item1 + item2 + item3
factor2 =~ item4 + item5 + item6
'
# Fit the model
fit <- cfa(model, data = my_data)
# Extract factor scores
scores <- lavPredict(fit)
# But how do I get individual residual variances?
Any tips on extracting these residual variances would be greatly appreciated. I’m particularly interested in getting them for each case, not just the overall means. Thanks in advance for any help!
hey charlotte91, i’ve dealt with this before. try using the residuals()
function from lavaan. it’ll give u a matrix of residuals for each observed variable. something like:
res_matrix <- residuals(fit, type="normalized")
the square of these gives variances. hope this helps!
Hey Charlotte91!
I’ve been playing around with Lavaan too, and I totally get your frustration. Have you tried the fitted()
function yet? It’s been a game-changer for me!
fitted_values <- fitted(fit)
observed_values <- my_data[,c('item1', 'item2', 'item3', 'item4', 'item5', 'item6')]
residuals <- observed_values - fitted_values
residual_variances <- apply(residuals^2, 2, mean)
This gives you the residual variances for each indicator. But here’s the cool part - if you want case-by-case stuff, just skip that last line and work with the residuals
directly!
What are you using these for anyway? I’m super curious about your project now!
Have you considered other approaches like exploratory factor analysis? Sometimes I find that helps me understand my data better before jumping into CFA.
Let me know if this works for you or if you need anything else explained. Always happy to chat more about this stuff!
I’ve encountered this issue in my CFA work as well. Here’s what worked for me:
You can use the inspect()
function from lavaan to extract the residual variances. Try this:
residual_variances <- inspect(fit, 'residuals')
This will give you a named vector of residual variances for each indicator. If you need case-by-case residuals, you might want to look into the resid()
function:
case_residuals <- resid(fit)
This returns a matrix where rows are cases and columns are indicators. Square these values to get variances.
Remember, these are estimates based on your model fit, so interpret with caution. Hope this helps with your analysis!