multilevel SEM with lavaan

Skip to first unread message

Helena Blackmore

Feb 10, 2020, 9:42:54 AM2/10/20
to lavaan

I am trying to build a SEM (3 predictors, 1 mediator, 1 outcome variable). The data comes from a repeated measures experiment, so all predictors are binary (currently coded as 0, 1; class is numeric).  (see a single level SEM plot below)

I want to build a SEM (mediation) model where several observations are nested within participant (varibale pp_number). 

My code looks as follows

model_bitter_mlm<-'    level: 1
                    bitter~ m*taste + l*colour+ j*descriptor
                                  expected_bitter~  k*colour+ h*descriptor
                      bitter~  i*expected_bitter
                    #indirect and total effects
                      hi := h*i
                      total1_within :=j+hi
                      total2_within := l+ki

                  level: 2
                    bitter~ g*taste + f*colour+ c*descriptor
                                  expected_bitter~  d*colour+ a*descriptor

                      bitter~  b*expected_bitter
                    #indirect and total effects
                      ab := a*b
                      total1_between :=c+(a*b)
                      total2_between := f+(d*b)
bitter.out_mlm<-sem(model_bitter_mlm, data=all_merged, cluster="pp_number")

summary(bitter.out_mlm, standardized=TRUE)

Problem is that the output doesn't look right. lavaan does not end normally and only after 1 iteration. 



Any idea what might be the problem? Thank you, I really struggle to find helpful resources online.

Terrence Jorgensen

Feb 11, 2020, 7:19:28 AM2/11/20
to lavaan
Any idea what might be the problem? 

lavaan does not yet fit ML-SEMs to categorical outcomes.  Although your binary variables are only the 3 predictors (right?), you are still telling lavaan to partition their variance into Level 1 and 2 components by modeling the binary predictors' effects at both levels.  That requires them to be random variables (i.e., fixed.x=FALSE).  I think you can only model them as fixed covariates by excluding them from the Level-2 model. See this example, adapted from ?Demo.twolevel

Demo.twolevel$g <- 1:0 # arbitrary binary variable
<- '
    level: 1
        fw =~ y1 + y2 + y3
        fw ~ g
    level: 2
        fb =~ y1 + y2 + y3
        fb ~ g              # if you comment this out, the problem goes away

<- sem(model, data = Demo.twolevel, cluster = "cluster")

But that would of course change the interpretation of the Level-2 coefficients because you would not be controlling for the same covariates.  If you are only interested in the Level-1 (within-subject) effects, you can just omit "level: 1" and "level: 2" from your model, and only specify a level-1 model in your syntax.  When you call lavaan with the cluster= argument, it will give you cluster-robust SEs and test statistics.  

If you need to model both levels of analysis, you should just switch to fitting separate univariate MLMs for each mediator/outcome, then use the mediation package to calculate the (in)direct effects with robust CIs (they have good algorithms).  Find examples in their vignette:

A limitation of both the cluster-robust single-level model AND a ML-SEM is the sphericity assumption, which is not necessarily tenable for repeated-measures data when they come from a longitudinal design (but it should be tenable if the repeated measures are experimental conditions whose order is counterbalanced across participants).  The only way (I can currently think of) around that limitation would be to switch the repeated measures to wide format and fit an autoregressive cross-lagged panel model, applying constraints to test whether effects remain constant over time (which is an assumption lavaan's ML-SEM would not allow you to test because it can only handle random intercepts).  But that is probably only feasible if you have relatively few repeated measures relative to your number of subjects.

Good luck,

Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Helena Blackmore

Feb 14, 2020, 4:59:16 AM2/14/20
Thank you! This is really helpful. I considered using the mediation package, but wasn't sure whether running the mediation analyses separately was ok. Thank you, I really appreciate your time and advice!


You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
Reply all
Reply to author
0 new messages