Multilevel equivalent of random intercept lme4 model

245 views
Skip to first unread message

Felix Schönbrodt

unread,
Mar 30, 2022, 11:26:48 AM3/30/22
to lavaan
Dear all,

I currently familiarize myself with multilevel SEM, but I did not manage to create the equivalent of the following lme4 random intercept path model in lavaan:

library(lme4)
mlm1 <- lmer(Reaction ~ Days + (1 | Subject), sleepstudy)
summary(mlm1)


From my understanding, this should be possible in multilevel lavaan?

Thanks, Felix

Thorsten Liu

unread,
Mar 30, 2022, 1:09:07 PM3/30/22
to lavaan
Dear Felix,

If I understand correctly, the model you write could be written by only lavaan. The idea is you assumed every day's reaction as an item. 
Certainly, you need to constrain something, for example, the residual. 

But I am not sure if this is what you want or not?

TUO

Felix Schönbrodt

unread,
Mar 31, 2022, 1:57:29 AM3/31/22
to lavaan
I can rebuild a plain linear regression as a path model:

lm1 <- lm(Reaction ~ Days, sleepstudy)
summary(lm1)

model <- "
Reaction ~ Days
"

s1 <- sem(model, sleepstudy, meanstructure=TRUE)
summary(s1)

But how can I add the random intercepts (in this demo data set, there are repeated measures for each person and I want to add a random intercept for "Subject"):

mlm1 <- lmer(Reaction ~ Days + (1 | Subject), sleepstudy)
summary(mlm1)

model2 <- '
    level: 1
        Reaction ~ Days
    level: 2
        ????
'

s2 <- sem(model2, sleepstudy, meanstructure=TRUE, cluster="Subject")
summary(s2)

Maybe I am missing something obvious ...

Marco Tullio Liuzza

unread,
Mar 31, 2022, 2:40:39 AM3/31/22
to lav...@googlegroups.com
Hi Felix,
I am not sure, but if I correctly understood the purpose of the cluster argument in lavaan, that should do the job.
Something like:

s1 <- sem(model, sleepstudy, meanstructure=TRUE, cluster = “Subject")

Best,
MT

-- 
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 lavaan+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/ca4fd6b7-7b75-40b1-8895-71ccc7836d3cn%40googlegroups.com.

Felix Schönbrodt

unread,
Mar 31, 2022, 2:46:06 AM3/31/22
to lavaan
Thanks! To my understanding, this gives corrected standard errors, but not an estimate of the random intercept variance (which I would like to have; I haven't made that clear).

Marco Tullio Liuzza

unread,
Mar 31, 2022, 2:47:53 AM3/31/22
to lav...@googlegroups.com
I see. Then I am not sure, and I am looking forward to reading a response as well, as I would like to do the same :-)

Terrence Jorgensen

unread,
Mar 31, 2022, 5:19:45 PM3/31/22
to lavaan
how can I add the random intercepts for "Subject"?

mlm1 <- lmer(Reaction ~ Days + (1 | Subject), sleepstudy)
summary(mlm1)

model2 <- '
    level: 1
        Reaction ~ Days
    level: 2
        ????
'

s2 <- sem(model2, sleepstudy, meanstructure=TRUE, cluster="Subject")

Estimate the Level-2 variance of Reaction (its Level-2 component is the random intercepts)

model2 <- '
    level: 1
        Reaction ~ Days
        Reaction ~~ var.L1*Reaction
   level: 2
        Reaction ~~ var.L2*Reaction
'

Note that by only include Days in the Level-1 model, you are estimating the conflated effect of Days, in both the MLM and MLSEM.  In lmer(), decomposing the effects would require calculating cluster means for Days, then cluster-mean-centering Days (manifest covariate approach).  But MLSEM can use the latent covariate approach, automatically decomposing Level-1 predictors into their level-specific components.  Then you can define the contextual effect and test whether the effect differs across levels.  Also, the R-squared at each level will then correspond to the pseudo-R-squared per level calculated from residual-variance reduction in MLM (although the Level-2 R-squared will be attenuated in MLM due to unreliability of cluster-mean estimates; Lüdtke et al., 2008, 2011).

model2 <- '
    level: 1
        Reaction ~ b1*Days
        Reaction ~~ var.L1*Reaction
   level: 2
        Reaction ~ b2*Days
        Reaction ~~ var.L2*Reaction

  contextual := b2 - b1 # contextual effect
'

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

Schönbrodt, Felix

unread,
Apr 1, 2022, 1:25:27 AM4/1/22
to lav...@googlegroups.com
Estimate the Level-2 variance of Reaction (its Level-2 component is the random intercepts)

model2 <- '
    level: 1
        Reaction ~ Days
        Reaction ~~ var.L1*Reaction
   level: 2
        Reaction ~~ var.L2*Reaction
'

Thanks Terrence - that’s what I was looking for!

Note that by only include Days in the Level-1 model, you are estimating the conflated effect of Days, in both the MLM and MLSEM.  In lmer(), decomposing the effects would require calculating cluster means for Days, then cluster-mean-centering Days (manifest covariate approach).  But MLSEM can use the latent covariate approach, automatically decomposing Level-1 predictors into their level-specific components.  Then you can define the contextual effect and test whether the effect differs across levels.  Also, the R-squared at each level will then correspond to the pseudo-R-squared per level calculated from residual-variance reduction in MLM (although the Level-2 R-squared will be attenuated in MLM due to unreliability of cluster-mean estimates; Lüdtke et al., 2008, 2011).

model2 <- '
    level: 1
        Reaction ~ b1*Days
        Reaction ~~ var.L1*Reaction
   level: 2
        Reaction ~ b2*Days
        Reaction ~~ var.L2*Reaction

  contextual := b2 - b1 # contextual effect

This gives me the following error:

Error in S[1, 1] : incorrect number of dimensions

Any ideas?

-Felix


Terrence Jorgensen

unread,
Apr 2, 2022, 2:57:12 AM4/2/22
to lavaan
Error in S[1, 1] : incorrect number of dimensions

Any ideas?

Not sure what causes it, but when you set fixed.x=FALSE to treat Days as random (not really the case), you will get results.  However, the Level-2 parameters don't have SEs and the estimates are wacky, so I don't think it converges on an actual solution.  At first, I thought the problem might be the low Level-2 N=18, but the real problem is that because everyone had exactly the same Days values, there is no Level-2 variance of the Days "cluster" means:

aggregate(Days ~ Subject, data = sleepstudy, mean) # all are 4.5 (Days == 1:9)

So in this particular example, there is no opportunity for the Level-1 predictor to explain Level-2 variance, which confuses the optimizer when we try.

Felix Schönbrodt

unread,
Apr 4, 2022, 3:39:40 AM4/4/22
to lavaan
OK, thanks!

Yves Rosseel

unread,
Apr 6, 2022, 6:50:47 AM4/6/22
to lav...@googlegroups.com
On 4/2/22 08:57, Terrence Jorgensen wrote:
> Error in S[1, 1] : incorrect number of dimensions
>
> Any ideas?

This is caused by the zero variance of 'Days' at the between level. The
github version of lavaan will handle this now more gracefully (and
produce a warning).

Yves.
Reply all
Reply to author
Forward
0 new messages