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



The siteid values in the two data frames match up.

  • $\begingroup$ Am not sure whether the toc3 dataset in the mediator model is the same as stoc3 in the outcome model. Also, ensure the same model structure in both outcome and mediator models, i.e. have a multilevel model for the two models $\endgroup$
    – Richard
    Commented Aug 23, 2023 at 12:15
  • $\begingroup$ Where are toca3 and stoc3? $\endgroup$
    – swihart
    Commented Feb 6 at 18:28

2 Answers 2


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).

  • 1
    $\begingroup$ 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. $\endgroup$ Commented Jan 3, 2021 at 0:10
  • $\begingroup$ 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. $\endgroup$
    – Erik Ruzek
    Commented Jan 3, 2021 at 0:20

On line 721 of mediate.R, there is code that extracts the ids from the mediation model. In this instance, of a lm mediation model and a lmer outcome model, the line essentially does:

M.ID <- sort(as.vector(data.matrix(toca3["siteid"])))

and my guess is that length(M.ID) is longer than length(unique(toca3$siteid)) which cause this error message to be printed.

I'm tempted to edit the line, in a personal copy of my function, to

M.ID <- unique(sort(as.vector(data.matrix(toca3["siteid"]))))

which is a hack to get the lengths equal. However, this hack/edit causes a different error later in the function related to the differing sizes (leave alone whether methodologically such a hack/edit is fundamentally changing the theory in this application).

Error in v1[, NUM.Y] <- PredictM1[, NUM.M] :                                        |    }
   number of items to replace is not a multiple of replacement length

In my applied problem where I encountered this issue, I rethought the mediation model to get it to 1 id per row -- which involved me making a wide dataset for some variables that were repeated measures.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.