When I fit a mediation model using mediation::mediate, like this:



df <- data.frame(x = runif(100), 
                 x2 = runif(100),
                 y = runif(100), m = runif(100), 
                 p_id = sample(1:5, 100, replace = TRUE), 
                 item = sample(LETTERS, 100, replace = TRUE))

fit.totaleffect <- lmer(y ~ (1|item) + (1|p_id) + x + x2, data = df)

fit.mediator <- lmer(m ~ (1|item) + (1|p_id) + x + x2, data = df)

fit.dv <- lmer(y ~ (1|item) + (1|p_id) + x  + x2 + m, data = df)

results <- mediation::mediate(fit.mediator, fit.dv, treat=c('x1', 'x2'), mediator='m')

I get the error, "mediate does not support more than two levels per model".

In another answer, someone says:

"The mediate function in the mediation package takes only a binary mediator or a numeric mediator. In your case, it seems that your mediator is categorical but contains more than 2 levels. You can either convert it to numeric or dummy code it."

However, this does not apply to my data. My data seems to be suitable (the mediator is numeric), based on this.

So what is wrong?

(NB. my actual data doesn't raise the boundary (singular) warning, but otherwise has the same qualities as the dummy data above).

  • $\begingroup$ Hi, it's been a long time, but, if you can see my comment, could you please tell me how you solved this problem?Now I am encountering the same question, I need to do the mediation analysis with nested random effect model... It will help a lot if you could tell me how you fixed this question. $\endgroup$
    – Eve
    Apr 8 at 8:39

1 Answer 1


The two levels refers to the two random effects in your model (i.e., two levels in a multilevel model). mediate() doesn't support that. Nothing to do with your treatment variable.


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.