lavaan ERROR Some between-level (only) variables have non-zero variance at the within-level

957 views
Skip to first unread message

Sandra Jaworeck

unread,
May 13, 2020, 9:16:32 AM5/13/20
to lavaan
Hello, everyone,

i try to do a multilevel analysis (with mediator) with the help of lavaan. I have defined my model as follows:

model <-'

level:1
SRH  ~ 1

level:2
SRH ~ b * sunshine + c * D_lat
sunshine ~ a * D_lat

SRH ~~ SRH
sunshine ~~ sunshine
D_lat ~~ D_lat

SRH ~~ sunshine
SRH ~~ D_lat
D_lat ~~ sunshine


indirect := a*b
direct := c
total := indirect + direct'

If I want to fit:

fit <- sem(model,
           data = sun,
           cluster = "country",
           ordered = c("SRH", "D_lat")
           )

then I get the following "error": Error in lav_data_full(data = data, group = group, cluster = cluster, : lavaan ERROR Some between-level (only) variables have non-zero variance at the within-level. Please double-check your data.

My sunshine variable has more than one expression per country, because different months were surveyed and therefore variance in my level 2 variable sunshine occurs obviously. So all this is supposed to be like this. The question ist how can I communicate this to lavaan? Or am I missing something? With lme4 and brms it was no problem to calculate a multi-level model with my data.

I have spent several hours trying to research it myself and unfortunately have not found anything. I would be very happy if one of you could help me. Thanks al lot!

Yves Rosseel

unread,
May 13, 2020, 2:12:29 PM5/13/20
to lav...@googlegroups.com
On 5/13/20 3:16 PM, Sandra Jaworeck wrote:
> My sunshine variable has more than one expression per country, because
> different months were surveyed and therefore variance in my level 2
> variable sunshine occurs obviously. So all this is supposed to be like
> this. The question ist how can I communicate this to lavaan?

Move sunshine to the level: 1 section.

Note that multilevel SEM currently (0.6-5/0.6-6) does not work with
ordered/categorical data!

Yves.

Sandra Jaworeck

unread,
May 13, 2020, 2:20:59 PM5/13/20
to lavaan
Thanks a lot Yves.

I have only one more question: If I move sunshine to the level 1 my model is not correct specified or it doesn't make any difference?

Sandra

Yves Rosseel

unread,
May 13, 2020, 2:29:42 PM5/13/20
to lav...@googlegroups.com
I don't believe it will make a difference. The 'strict' distinction
between 'level 1' and 'level 2' variables can sometimes be an obstacle.
The lmer() function eg doesn't care...

Yves.

Sandra Jaworeck

unread,
May 14, 2020, 1:14:07 AM5/14/20
to lavaan
Thank you! I will just check it with other models and write again if it makes a difference.

Sandra
Message has been deleted

Yves Rosseel

unread,
May 15, 2020, 11:05:28 AM5/15/20
to lav...@googlegroups.com
> Note that multilevel SEM currently (0.6-5/0.6-6) does not work with
> ordered/categorical data!

To be clear: no version of lavaan (up to 0.6-6, now on CRAN) handles
multilevel SEM with ordinal data yet.

Xian Zhao

unread,
Aug 23, 2020, 11:28:24 PM8/23/20
to lavaan
My question is related. I ran this actor-partner interdependence moderation model. 
H = husband, W = wife, x and y are continous variables, m = moderator (binory, 1/0 coded)
H_m means the moderator variable for husbands. 

Here is the interesting thing:

When I ran the model with R-4.0.2, I encountered the same error "Some between-level (only) variables have non-zero variance at the within-level. Please double-check your data."

When I ran the model with R-3.5.3, I don't have any errors. 

But due to some reasons, I need to run it with R-4.0.2, can someone please help me with it? 

"model<-'
level:1

H_y~H_x+W_x
W_y~W_x+H_x

H_y~~W_y

level:2

H_y~H_m:H_x+H_m:W_x+W_m:W_x+W_m:H_x+H_m+W_m+H_x+W_x
W_y~H_m:H_x+H_m:W_x+W_m:W_x+W_m:H_x+H_m+W_m+H_x+W_x


H_y~~W_y

'
library(lavaan)
fit <- sem(model, data = data_wide, cluster="Couple_ID") 
summary(fit, standardized = TRUE, fit.measures = TRUE,ci=TRUE)


Yves Rosseel

unread,
Aug 30, 2020, 10:58:29 AM8/30/20
to lav...@googlegroups.com
On 8/24/20 5:28 AM, Xian Zhao wrote:
> *When I ran the model with R-4.0.2, I encountered the same error* "Some
> between-level (only) variables have non-zero variance at the
> within-level. Please double-check your data."

It is not so much the R version, but the lavaan version that matters
here. In recent versions of lavaan, there is check to verify if the
level-2 variables are indeed level-2. If not, you get the error message
above.

Simply put: he 'level: 2' section should NOT contain any variables that
are not constant within clusters.

Yves.

Xian Zhao

unread,
Sep 2, 2020, 12:28:10 AM9/2/20
to lav...@googlegroups.com
Thank you so much, Yves. This really helps! 

--

You received this message because you are subscribed to a topic in the Google Groups "lavaan" group.

To unsubscribe from this topic, visit https://groups.google.com/d/topic/lavaan/5CaX_CkyWOs/unsubscribe.

To unsubscribe from this group and all its topics, send an email to lavaan+un...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/0512881d-548b-8cb2-2178-d6f9265edb4f%40gmail.com.

--

Regards,

Xian Zhao
Post-doc Fellow
University of Toronto


Reply all
Reply to author
Forward
0 new messages