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?
age
, but it findsdesirdata1$age
. Not quite the same pickle (I think this is what @Dwin mean with "breaking down" comment). – Yamada