I am running a series of mediation analyses using R's mediation package. Because the models are extremely similar to each other I wrote a function where all that would change would be the mediating variable, the outcome variable, and the data set. The function is below:

    library(mediation)
    data("framing", package = "mediation")
    covList <- list("age", "educ", "gender", "income")

    meBrokenFunction <- function(MEDIATOR, OUTCOME, DATA) {

      treatOnMed <- lm(DATA[[MEDIATOR]] ~ treat + age + educ + gender + income, data = DATA)

      medOnOut <- glm(DATA[[OUTCOME]] ~ DATA[[MEDIATOR]] + treat + age + educ + gender + income, data = DATA, family = binomial("probit"))

      expt <- mediate(treatOnMed, medOnOut, sims = 100, 
                      treat = "treat", mediator = MEDIATOR, 
                      covariates = covList, robustSE = TRUE)
      expt
    }  
    set.seed(2019)
    test_first <- meBrokenFunction("emo", "cong_mesg", framing)

When I run this function I get the following error:

Error in `[.data.frame`(y.data, , mediator) : undefined columns selected 

However if I run the code without using the function I wrote, everything works as intended.

test_treatOnMed <- lm(emo ~ treat + age + educ + gender + income, 
                        data = framing)
test_treatOnOut <- glm(cong_mesg ~ treat + age + educ + gender + income,
                        data = framing, family = binomial("probit"))
test_medOnOut <- glm(cong_mesg ~ emo + treat + age + educ + gender + income,
                        data = framing, family = binomial("probit"))

test_second <- mediate(test_treatOnMed, test_medOnOut, sims = 100, 
                  treat = "treat", mediator = "emo", 
                  covariates = covList, robustSE = TRUE)

The error appears to be in the mediate function, specifically at mediator = MEDIATOR but I do not understand why it is not working or if I am approaching the problem incorrectly.

1 Answers

0
akrun On Best Solutions

In the formula, we may need paste instead of DATA[[MEDIATOR]]

lm(paste(MEDIATOR,  "~ treat + age + educ + gender + income"), data = DATA)

Similarly for the glm

-fullcode

meFixedFunction <- function(MEDIATOR, OUTCOME, DATA) {

      treatOnMed <- lm(paste(MEDIATOR,  
          "~ treat + age + educ + gender + income"), data = DATA)

      medOnOut <- glm(paste(OUTCOME, "~", MEDIATOR,
         "+ treat + age + educ + gender + income"), data = DATA, 
             family = binomial("probit"))

      expt <- mediate(treatOnMed, medOnOut, sims = 100, 
                      treat = "treat", mediator = MEDIATOR, 
                      covariates = covList, robustSE = TRUE)
      expt
    }  

-testing

set.seed(2019)
test_first <- meFixedFunction("emo", "cong_mesg", framing)