# Mediation package error message with group level mediator (bug?)

I am trying to use the mediation package with multilevel data and a group level mediator. I am getting a "groups do not match between mediator and outcome models" error message.
However, I have checked multiple times and the group definitely DO match (I provided some code snippets below). Has anyone else run into this problem and have a possible solution?

#Here is the code that results in the error message

med.fit2 <- lm(Z_lin~ANY_FK,data=toca3)

out.fit2 <- lmer(post_neq~Z_lin+ANY_FK+(1|siteid),data=stoc3)

med.out2 <- mediate(med.fit2, out.fit2, treat = "ANY_FK", mediator="Z_lin",sims=1000)

#To verify that groups matched I run the following code

table(unique(toca3$siteid)) table(unique(stoc3$siteid))


The siteid values in the two data frames match up.

## 1 Answer

Unfortunately, a group-level variable cannot be an outcome in lme4 models. Multilevel models require that the outcome be measured at the lowest level of the data hierarchy. The multilevel model partitions the variance in the outcome across the various levels - within-group, between-group, etc. Predictors at each level can be used to explain variance at their respective levels.

In order to test for mediation in which the mediator or the ultimate outcome is at the group level, you need to use a structural equation package capable of handling multilevel data. lavaan can do it (I'm pretty sure) and Mplus can do it (for sure).

• Thanks, I am aware of the SEM possibilities, but I'd like to be able to use the sensitivity analysis functions in "mediation". The "mediation" R package can handle this type of multi-level mediation model. See vignette here:cran.r-project.org/web/packages/mediation/vignettes/… . The toca3 dataset has only group level variables and the model for the mediator is fit using lm(), not lmer(). I was hoping someone else had run into this problem with the mediation package and had a solution or work around. Jan 3 at 0:10
• You can run sensitivity analyses in SEM. See Muthen's recent paper on causal mediation. I'm also not sure the mediation package will work here. My guess is that the problem is that the sample size is different across the models. I think SEM software is your best bet. Merge the group level variables into your stoc3 data and it will work. Jan 3 at 0:20