Using lavaan for SEM/CFA with Categorical Control Variables

Running lavaan SEM with latent factors and nominal controls (gender, family). Matrix inversion error occurs. Code:

fit <- sem('F =~ a+b', df)

Hey SurfingWave, I totally get the frustration with these matrix inversion errors—it seems like lavaan sometimes doesn’t get along with categorical predictors out of the box. I had a similar hiccup once and it turned out the issue was with how the nominal variables were being handled. I ended up double-checking how gender and family were coded in my data. They were factors in R, but sometimes lavaan still treats them as numeric if there’s something off with the levels. Have you tried recoding them with something like as.factor() explicitly before fitting the model? Also, there might be the option to use a different estimator or adjust the settings for polychoric correlations if you’re treating them as ordered. How did you handle these variables originally, and have you experimented with different estimator settings in lavaan? It’s been a bit of trial and error for me, so I’m curious to know if that approach helps on your end too. Cheers! :blush: