Tine Harpøth
Tine Harpøth

Reputation: 69

ERROR in R: `[.data.frame`(m.data, , treat) : undefined columns selected - running mediation

#Create subset of a dataset

df <- subset(dat,select = c(id,obs,day_clos,posaff,er89,qol1))

### remove rows with missing values on a variable

df <- subset(df, !is.na(day_clos))

df <- subset(df, !is.na(er89))

df <- subset(df, !is.na(qol1))

df <- subset(df,!is.na(posaff))

any(is.na(df)) ## returns FALSE

Then my data looks like this

   id obs day_clos   posaff     er89 qol1

   1   0 16966.61 2.000000 2.785714    3

   1   1 16967.79 1.666667 2.785714    4

   1   2 16968.82 1.666667 3.142857    3

   1   3 16969.76 1.166667 3.071429    4

   1   4 16970.95 2.083333 3.000000    4

   1   5 16971.75 1.416667 2.857143    4

model.Y <- lm(qol1 ~ posaff,df)

summary(model.Y)

model.M <- lm(qol1 ~ er89, df)

summary(model.M)

#### There is no problem running the regression analyses, however: 

results <- mediate(model.M, model.Y, treat="posaff", mediator="er89",  boot=TRUE, sims=500)

Returns error message: [.data.frame(m.data, , treat) : undefined columns selected

Any one know how to fix this?

Upvotes: 5

Views: 9144

Answers (2)

valentina vellani
valentina vellani

Reputation: 11

I think that I have what you suggested but it is still giving the same error message.

model.mediator <- lmer(PercAccuracy~factor(Rep1) +
                      (factor(Rep1)| ParticipantPublicID),
                    data = data, REML=FALSE , control = control_params)
summary(model.mediator)


model.outcome <- lmer(Sharing~factor(Rep1) +PercAccuracy+
                      (factor(Rep1)+PercAccuracy| ParticipantPublicID),
                    data = data, REML=FALSE , control = control_params)
summary(model.outcome )

effectModel<-mediate(model.mediator, model.outcome, treat = "Rep1", mediator="PercAccuracy")
summary(effectModel)

Upvotes: 0

Terru_theTerror
Terru_theTerror

Reputation: 5017

Variables used in treat and mediator must be presents in both models:

treat a character string indicating the name of the treatment variable used in the models.
The treatment can be either binary (integer or a two-valued factor) or continuous
(numeric).
mediator a character string indicating the name of the mediator variable used in the models

Source

A trivial working example:

library("mediation")

db<-data.frame(y=c(1,2,3,4,5,6,7,8,9),x1=c(9,8,7,6,5,4,3,2,1),x2=c(9,9,7,7,5,5,3,3,1),x3=c(1,1,1,1,1,1,1,1,1))

model.M <- lm(x2 ~ x1+x3,db)
model.Y <- lm(y ~ x1+x2+x3)

results <- mediate(model.M, model.Y, treat="x1", mediator="x2", boot=TRUE, sims=500)

Upvotes: 4

Related Questions