kosukeimai / mediation

R package mediation
58 stars 29 forks source link

Fitting an outcome model that is within subject (model.y, using lmer) and a simple model for the mediator (`model.m`, using lm)? #50

Closed robertour closed 1 year ago

robertour commented 1 year ago

I have what seems a simple case that cannot properly fit.

I have a dependent variable that is measured 10 times per subject. However, the mediator is only once after the intervention (there are two groups: Control and Interventions)

I thought that the following would work:

model_m <- lm(mediator ~ group, data=unique (df) )
model_dv <- lmer(dv ~ group + mediator  + (1 | subj), data = df)
mediate(model_m, model_dv, treat='group', mediator='mediator', boot=F)

To be sure: model_m is fit using lm because the mediator is only measured once (note that data=unique(df)), whereas model_dv is fitted using lmer because it is measured 10 times (within subject).

It triggers this error, which makes sense: one group has 10 times more rows.

Instead, I tried the following:

model_m <- lmer(mediator ~ group + (1|subj), data=df )
model_dv <- lmer(dv ~ group + mediator  + (1 | subj), data = df)
mediate(model_m, model_dv, treat='group', mediator='mediator', boot=F)

This works except that model.m is incorrect because it is fitted in 10 times the measurements that actually were taken. As a consequence, there are several warnings displayed (e.g., model failed to converge which makes sense), *the estimates are the same* as the ones obtained with lm but the the standard errors are smaller***.

Is it OK to still the model_m (fitted with lmer) even though some of the statistics are wrong? In other words, which statistics of the lmerMod object are used inside the mediate function? (I can then check if they are all the same and it is just about the format of the data)

robertour commented 1 year ago

I found that there is an example in the mediate pagacke:

library(lme4)
library(mediation)

data("school", package = "mediation")
data("student", package = "mediation")

med.fit <- lm(smorale ~ free , data = school)
out.fit <- lmer(late ~ free + smorale + (1 | SCH_ID), data = student)

med.out <- mediate(med.fit, out.fit, treat = "free", mediator = "smorale", control.value = 3, treat.value = 4, sims = 100)
summary(med.out)

I just made sure that my data looked as similar as possible to the example. It is difficult to say what change did the trick, but here are a few things that I noticed: