I’m trying to set up a structural equation model (SEM) in R using lavaan. My goal is to model an interaction between a categorical predictor and a continuous moderator that I’ve created through confirmatory factor analysis (CFA). Here’s what I’m aiming for:
- Recreate a two-way ANOVA in SEM
- Include a CFA-derived moderator to test with each factor
I’ve set up my data with:
- A dependent variable (DV) from 3 items
- A moderator (MOD) from 3 items
- Two categorical factors (FAC1 and FAC2) and their interaction (FAC12)
My basic model works fine, but when I add the interaction term (FAC1:MOD), I get an error about the variance-covariance matrix not being positive definite.
Is this approach possible in lavaan? Are there any workarounds? For example, could I extract the CFA-calculated MOD values, create the interaction outside SEM, then use it in the path analysis?
I’d really appreciate any guidance on how to properly model this interaction in lavaan or if there’s a better way to approach this problem. Thanks!
Hey there, Owen_Galaxy!
Your question’s got me super curious about SEM modeling. Have you considered trying out a two-step approach? 
I’m wondering if you could run your CFA first, grab those factor scores for your moderator, and then play around with the interactions outside of lavaan? It might help dodge that pesky positive definite matrix error you’re running into.
What if you tried something like this:
- Run your CFA model
- Use lavPredict() to snag those MOD factor scores
- Create your interaction terms manually (FAC1:MOD, FAC2:MOD)
- Toss those new interaction variables into your path analysis model
It’s not as slick as doing everything in one go, but it might just do the trick! 

Have you experimented with this approach at all? I’d be super interested to hear if it works out for you or if you’ve found any other cool workarounds. Keep us posted on how it goes!
I’ve encountered similar challenges with lavaan. One approach that’s worked for me is extracting the factor scores for your moderator using lavPredict() after running the CFA. Then you can create the interaction term manually outside of SEM.
Here’s a rough outline of the steps:
- Run your CFA model
- Use lavPredict() to get factor scores for MOD
- Create interaction terms (FAC1:MOD, FAC2:MOD) using these scores
- Include these new interaction variables in your path analysis model
This method often sidesteps issues with the variance-covariance matrix. It’s not as elegant as handling everything within SEM, but it’s pragmatic and gets the job done.
If you’re set on keeping everything within lavaan, you might need to look into more complex model specifications or consider alternative SEM packages that handle these interactions more smoothly.
Hope this helps point you in a useful direction!
hey, i’ve dealt with similar issues. have u tried extracting the mod scores from ur cfa first? u can use lavPredict() to get factor scores, then make the interaction manually. might avoid that matrix error. worth a shot before redoing everything. good luck!