Solved – Moderated Mediation with categorical moderator

mediationr

Hi I'm trying to make the following moderated mediation but I'm missing the final step on testing differences between groups. The code below tests the indirect effects for each level of the categorical variable, but I have no clue how to test if the indirect effect differs between categories.

I want to test the indirect effect of "Spatial" on "MCUnd" through "TotCorr" in which "Condition" moderates the effect of "Spatial" on "TotCorr".

"Spatial", "TotCorr" and "MCUnd" are all continuous and "Condition" is categorical with four categories.

This is what I have:

# Now it's time to do the actual moderated mediation. First
# I created a model/object representing the treatment variable 
# (spatial ability) to the mediator variable (dynamic). As it
# is a moderated mediation, SA is interacting with condition in this model

medmod.fit <- lm(TotCorr ~ Spatial * Condition, data=Masters)

# I then create a model/object for the effect from mediator
# to Y (Multiple Choice Understanding scores), again, as it
# is a modmed, variables are crossed with condition.

outmod.fit <- lm(MCUnd ~ TotCorr + Spatial * Condition +
                         TotCorr * Condition, data=Masters)

# At this point I run one overall mediation analysis and four
# mediation analyses that split the data by condition. This is
# consistent with Tingley, Yamamoto, Koole, & Imai (2012)

medmod <- mediate(medmod.fit, outmod.fit, treat = "Spatial",
                  mediator = "TotCorr")

medmod.cond1 <- mediate(medmod.fit, outmod.fit, treat = "Spatial",
                        mediator = "TotCorr", covariates = list(Condition = 1))

medmod.cond2 <- mediate(medmod.fit, outmod.fit, treat = "Spatial",
                        mediator = "TotCorr", covariates = list(Condition = 2))

medmod.cond3 <- mediate(medmod.fit, outmod.fit, treat = "Spatial",
                        mediator = "TotCorr", covariates = list(Condition = 3))

medmod.cond4 <- mediate(medmod.fit, outmod.fit, treat = "Spatial",
                        mediator = "TotCorr", covariates = list(Condition = 4))

#This is just an overall summary
summary(medmod)

# This is a summary of the mediation effects for each of the four
# conditions. If there is moderated mediation, the mediation effects
# should be different across conditions. I do find that there
# mediation effects in conditions 2 and 3(moderate effect), but no
# mediation effect in conditions 1 and 4.

summary(medmod.cond1)
summary(medmod.cond2)
summary(medmod.cond3)
summary(medmod.cond4)

Thank you!

Best Answer

It has been a while since the OP, but for posterity and future readers, I will point out that the latest vignette for the mediation package on CRAN provides an example of moderated mediation in section 3.2, showing how to obtain pairwise comparisons of indirect effects between levels of Condition. So something like

med.init <- mediate(medmod.fit, outmod.fit, sims = 2,
                    treat = "Spatial", mediator = "TotCorr")
## Compare conditions 1 and 2
test.modmed(med.init, sims = 1000,
            covariates.1 = list(Condition = 1), 
            covariates.2 = list(Condition = 2))
## Compare conditions 1 and 3
test.modmed(med.init, sims = 1000,
            covariates.1 = list(Condition = 1), 
            covariates.2 = list(Condition = 3))
## and so on ...