R Mediation Analysis -- Bootstrapping
Asked Answered
L

5

7

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?

Lorenzo answered 18/9, 2012 at 21:26 Comment(3)
I'm guessing you meant to write "mediation package"?Firebird
It's looking for age, but it finds desirdata1$age. Not quite the same pickle (I think this is what @Dwin mean with "breaking down" comment).Yamada
DWin--yes, mediation package. Damn. And thanks for the input, Roman.Lorenzo
F
2

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.

Firebird answered 18/9, 2012 at 21:59 Comment(1)
DWin-- Thanks, and your interpretation was correct I think, but I still can't get this to run. I tried your version, and now I the same error, but claiming that the the mediator doesn't exist: Error in [.data.frame(y.data, , mediator) : undefined columns selected. Any other ideas?Lorenzo
A
2

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)
Angi answered 30/6, 2014 at 15:44 Comment(0)
A
1

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)
Armory answered 19/9, 2012 at 5:59 Comment(1)
Alright, I'm giving this a shot as another alternative. I am still having some trouble though, if you have any thoughts, please feel free to share. When I try to run this, I get an error indicating that the number of rows of matrices must match: Error in cbind(x, mediator, dv) : number of rows of matrices must match (see arg 3). However, I already excluded any missing data, and the n for each of the variables is the same (13044). Any ideas on what it might be upset about?Lorenzo
V
0

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.

Venireman answered 11/1, 2013 at 0:29 Comment(0)
P
0

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.

Phillipphillipe answered 7/6, 2013 at 7:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.