Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Mediation Analysis -- Bootstrapping

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?

like image 953
Jess A. Avatar asked Sep 18 '12 21:09

Jess A.


1 Answers

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.

like image 106
IRTFM Avatar answered Oct 01 '22 09:10

IRTFM