Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

mediation::mediate does not support more than two levels per model

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

library(mediation)

set.seed(30)

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

The code that generates the error is:

  out.group <- names(model.y@flist)
    n.out <- length(out.group)
    if (n.out > 1) {
      stop("mediate does not support more than two levels per model")
    }

If we look at model.y@flist you’ll see that it has the following values:

> fit.dv@flist
$item
  [1] U A A F A Z L B V W C C X P J W X Z A Z A B O N J T K M L A S I V O B Q B T F M S L A N B
 [46] O P A Y K U T T Z R O E Y M A B Z S Y U A V Z F J H K X D K V H G S S W E T S M I O Z W R
 [91] H N Q M C U T O Y R
Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

$p_id
  [1] 3 3 1 3 3 3 1 1 2 4 1 5 4 1 3 3 4 2 1 4 2 4 1 2 4 4 1 5 5 2 2 1 4 1 1 5 3 3 3 2 5 5 1 5 3
 [46] 1 2 1 3 5 2 5 4 3 4 3 5 3 2 5 1 1 2 2 5 2 2 1 2 2 1 3 2 3 3 2 4 5 3 5 4 1 3 4 4 1 1 5 5 3
 [91] 5 1 4 3 3 4 4 3 2 5
Levels: 1 2 3 4 5

attr(,"assign")
[1] 1 2

The "levels" in the error refer not to the levels of a factor, but to the random effects in the model. The error is getting tripped because there is more than one random effect (indicating more than two hierarchical levels, because the first level is accounted for by the idiosyncratic observation-level error).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading