0

I am doing mediation analysis in R. My mediator is a binary variable, so model.m is run by glm.

set.seed(2022)

data = data.frame(ID = as.character(seq(1,100,1)),group = seq(1,100,1),
                  iris[1:100,],day1 = rnorm(100), day2 = rnorm(100), day3 = rnorm(100))
data$med = as.factor(ifelse(data$Petal.Width>0.3&data$Petal.Width<1.2,1,0))

data_long <- data %>% 
  pivot_longer(cols = day1:day3, names_to = "day", values_to = "y") %>%
  mutate(day = as.numeric(gsub("day", "", day))) %>%
  dplyr::select(ID, day, Sepal.Length, everything()) %>% 
  as.data.frame(.)

formula <- as.formula(Sepal.Length ~ Species + med + Petal.Length + Sepal.Width + day + (day | group))
out.fit <- lme4::lmer(formula = formula, 
                      data = data_long,
                      control = lme4::lmerControl(optimizer = "bobyqa",
                                                  optCtrl = list(maxfun = 2e5)))

formula_2 <- as.formula(med ~ Species + Petal.Length + Sepal.Width)

med.fit <- glm(formula = formula_2,
               data = data,
               family = "binomial")


result <- mediate(med.fit, out.fit, treat = "Species", 
                  treat.value = "setosa", control.value = "versicolor", 
                  mediator = "med",
                  sims = 100)

However, after I ran the above piece of code, I kept getting error:

Error in factor(PredictMt, levels = 1:m, labels = m.levels) : object 'm' not found

I checked the original source code and found that m.levels in the source code should be a variable and there isn't an object called m. In other words, m should not be an object. I don'r understand why the program recognize m as an object.

Also, the code runs totally fine on the continuous mediator. The following code works:

    formula <- as.formula(Sepal.Length ~ Species + Petal.Width + Petal.Length + Sepal.Width + day + (day | group))
    out.fit <- lme4::lmer(formula = formula, 
                          data = data_long,
                          control = lme4::lmerControl(optimizer = "bobyqa",
                                                      optCtrl = list(maxfun = 2e5)))
    
    formula_2 <- as.formula(Petal.Width ~ Species + Petal.Length + Sepal.Width)
    
    med.fit <- lm(formula = formula_2,
                  data = data)

result <- mediate(med.fit, out.fit, treat = "Species", 
                  treat.value = "setosa", control.value = "versicolor", 
                  mediator = "Petal.Width",
                  sims = 100)
Dianafreedom
  • 391
  • 1
  • 13
  • 1
    Can you make this question more reproducible? You don't specify non-base packages (`mediate`?), and we don't have any of your variables. Please make this question *reproducible* by including sample *unambiguous* data (e.g., `data.frame(x=...,y=...)` or the output from `dput(head(x))`), non-base packages used here, and the intended output given the sample input. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jan 18 '22 at 16:18
  • @Dianafreedom Here's the line in the source code where `m` is created: https://github.com/kosukeimai/mediation/blob/36e9cd63a964cadc3b9ae8b786f8bc714854079f/R/mediate.R#L745. One of these two lines is likely responsible for the error: https://github.com/kosukeimai/mediation/blob/36e9cd63a964cadc3b9ae8b786f8bc714854079f/R/mediate.R#L2190; https://github.com/kosukeimai/mediation/blob/36e9cd63a964cadc3b9ae8b786f8bc714854079f/R/mediate.R#L2436. – Greg Jan 18 '22 at 16:22
  • @Dianafreedom From the error message, we know `m` does not exist as of those lines i linked. Since `m` was to be created in the conditional block [`if(!(isMer.y & !isMer.m)){...}`](https://github.com/kosukeimai/mediation/blob/36e9cd63a964cadc3b9ae8b786f8bc714854079f/R/mediate.R#L739-L746), we know this condition `!(isMer.y & !isMer.m)` must evaluate to `FALSE`; so `isMer.y` must be `TRUE` and `isMer.m` must be `FALSE`. – Greg Jan 18 '22 at 16:37
  • @Dianafreedom Now those two variables were created [here](https://github.com/kosukeimai/mediation/blob/36e9cd63a964cadc3b9ae8b786f8bc714854079f/R/mediate.R#L547-L548): `isMer.y <- inherits(model.y, "merMod")` and `isMer.m <- inherits(model.m, "merMod")`. So from the perspective of `mediate()`, you've unexpectedly supplied either a `model.m` that is not of class [`merMod`](https://www.rdocumentation.org/packages/lme4/versions/1.1-27.1/topics/merMod-class), or a `model.y` that *is* of that class. – Greg Jan 18 '22 at 16:37
  • @Greg Thanks for your comment! I checked: my model.y belongs to `merMod` and my model.m belongs to `glm`. However, I don't think it should be a problem, as the the same code runs well when the mediator is continuous. – Dianafreedom Jan 24 '22 at 01:52

1 Answers1

1

I found the crux of the issue:

In mediate.R, you can find "For binary response models, the 'mediator' must be a numeric variable with values 0 or 1 as opposed to a factor."

After I added data$med = as.numeric(data$med)-1, problem solved.

Dianafreedom
  • 391
  • 1
  • 13