Question

I am attempting to do a mediation analysis in R using the mediate package. I have looked at the documentation on how to do this, and have read through the examples provided by R (i.e., I've already run "example(mediate)"). Despite this, I cannot get the simplest mediation to run. Ideally, I'd like to do a bootstrapping procedure, a la Preacher & Hayes (2004).

Here's the code that I am trying to run:

model.m <- lm(desirdata1$zpers1 ~ desirdata1$zdesir1 + desirdata1$age)
model.y <- lm(desirdata1$zpers1 ~ desirdata1$age)
age1test <- mediate(model.m, model.y,treat="age", mediator="zdesir1",
                    boot=TRUE, sims=50)

Note that the dataset is called desirdata, the treatment is called age, the outcome is called zpers1 and the mediator is called "zdesir1". When I run this, I get the following error:

Error in `[.data.frame`(m.data, , treat) : undefined columns selected

It seems to be claiming that a variable (specifically, the treatment variable) does not exist. However, running names(desirdata) shows that the variable is there, and it is named correctly, as are all of the other variables. The first two models (model.m and model.y) run fine, and the output looks as it should. It's only the mediation model that I can't get to run. I haven't made a typo, as far as I can tell, and I've checked this a hundred times.

Thoughts?

Was it helpful?

Solution

As I read the examples in the documentation, the model.m for the mediator model will have different outcome than that for the main regression object model.y. Since you haven't described the background and what sorts of data it's hard to be very certain of this, but wondering if you meant to type:

model.m <- lm(zdesir1 ~  age, data=desirdata1)
model.y <- lm(zpers1 ~ age, , data=desirdata1 )
age1test <- mediate(model.m, model.y,treat="age", mediator="zdesir1",
                    boot=TRUE, sims=50)

I cast it using formula and data objects, since some regression functions break down when just given vectors. Also makes it easier to see typos.

OTHER TIPS

Your models are not correct. model.m should predict the mediator from the IV, and model.y should predict the DV from the mediator and IV.

model.m <- lm(desirdata1$zdesir1 ~ desirdata1$age)
model.y <- lm(desirdata1$zpers1 ~ desirdata1$zdesir1 + desirdata1$age)
age1test <- mediate(model.m, model.y, treat="age", mediator="zdesir1", boot=TRUE, sims=50)

Try the MBESS package. Preacher recommends it and you can just use the mediation function. If you want bootstrap just make sure it says bootstrap=TRUE. B is the number of bootstraps.

mediation(x, mediator, dv, S = NULL, N = NULL, x.location.S = NULL,
mediator.location.S = NULL, dv.location.S = NULL, mean.x = NULL,
mean.m = NULL, mean.dv = NULL, conf.level = 0.95,
bootstrap = FALSE, B = 1000, which.boot="both", save.bs.replicates=FALSE)

I ran into the same problem with simulated data so I ran debug(mediate) and found where the problem was. I believe the problem is with the [treat="age", mediator="zdesir1"] portion of the code. If you attach the data, you shouldn't run into that problem. Alternatively, you can use [treat="desirdata1$age", mediator="desirdata1$zdesir1"] that should solve the problem.

Well try this:

model.m <- lm(zdesir1 ~ age, data=desirdata1)
model.y <- lm(zpers1 ~ age + zdesir1, data=desirdata1)
age1test <- mediate(model.m, model.y,treat="age", mediator="zdesir1",
                    boot=TRUE, sims=50)

To make it simple, the mediator model (model.m) should have the mediator as the outcome.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top