R Intermediate analysis - loading

I am trying to conduct a mediation analysis in R using a mediation package. I looked at the documentation on how to do this and read the examples provided by R (i.e. I already completed the β€œexample (mediator)”). Despite this, I cannot get the simplest mediation. Ideally, I would like to do a bootstrap procedure, a la Preacher and Hayes (2004).

Here is the code I'm 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 processing is called age , the result is called zpers1 , and the pick 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 argued that the variable (in particular, the processing variable) does not exist. However, running names (desirdata) show that the variable is there, and it is named correctly, like all other variables. The first two models (model.m and model.y) work fine, and the result looks as it should. This is just a mediation model that I cannot run. As far as I can tell, I did not make a typo, and I checked it a hundred times.

Thoughts?

+6
source share
5 answers

When I read the examples in the documentation, model.m for the mediator model will have a different result than for the main regression object model.y . Since you did not describe the background and what types of data it is difficult to be very sure of this, but are wondering if you want 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 used it with the help of the formula and data objects, since some regression functions are broken only into given vectors. Typing errors is also easier.

+1
source

Your models are incorrect. model.m should predict the pick from IV and model.y should predict the DV from pick 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) 
+2
source

Try the MBESS package. The preacher recommends this, and you can simply use the mediation function. If you want bootstrap to just make sure it says bootstrap = TRUE. B is the number of boot files.

 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) 
+1
source

I ran into the same problem with simulated data, so I ran debug (broker) and found where the problem is. I believe the problem is with the code part [treat = "age", mediator = "zdesir1"]. If you attach data, you should not run into this problem. Alternatively, you can use [treat = "desirdata1 $ age", mediator = "desirdata1 $ zdesir1"], which should solve the problem.

0
source

Ok try the following:

 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, an intermediary model (model.m) must have an intermediary as a result.

0
source

Source: https://habr.com/ru/post/925726/


All Articles