Mediate function. Mediation package: Error in `[.data.frame`(y.data, , mediator) : undefined columns selected

Hello,

Any idea why my data does not work with the "mediate" function (mediation package)?
My data is here

library("mediation")
#load the data:
data0 <- read_excel("D:/data_mediation.xlsx", sheet = "both_groups")
data<-as.data.frame(data0)
#My models:
med.fit <- glmer(BDI2 ~ GROUP2 + (1|SUBJECT), family = binomial(link = "logit"), data = data)
out.fit <- lmer(PSBA ~ GROUP2 + (1|SUBJECT), data = data)
med.out <- mediate(med.fit, out.fit, treat="GROUP2", mediator="BDI2", sims=500)

error mesage:
med.out <- mediate(med.fit, out.fit, treat="GROUP2", mediator="BDI2", sims=500)
Error in [.data.frame(y.data, , mediator) : undefined columns selected

i) In line with previous posts, no further packages with functions with the same name(meidate) are installed.
ii) In addition, the following toy sample works fine:

library("mediation")
db<data.frame(y=c(1,2,3,4,5,6,7,8,9),x1=c(9,8,7,6,5,4,3,2,1),x2=c(9,9,7,7,5,5,3,3,1),x3=c(1,1,1,1,1,1,1,1,1))
model.M <- lm(x2 ~ x1+x3,db)
model.Y <- lm(y ~ x1+x2+x3,db)
results <- mediate(model.M, model.Y, treat="x1", mediator="x2", boot=TRUE, sims=500

the problem lies in the missing mediator (m) variable in the "out.fit" model. Something like:

out.fit <- lmer(PSBA ~ BDI2 +GROUP2 +(1|SUBJECT), data = data)

See sintax in this vignette

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.