Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 <- 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. <- 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(,, treat = "Spatial",
                  mediator = "TotCorr")

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

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

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

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

#This is just an overall summary

# 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.


Thank you!

share|improve this question

migrated from Jul 7 '13 at 1:23

This question came from our site for professional and enthusiast programmers.

What is it that you actually need help with? It's hard to tell what your actual question is here. If its a code question, can you provide a toy data frame to work with? We don't know what your Masters data looks like. An example of output that you're expecting to see would also be helpful (if it differs from the actual output). If its a more general "hey am I using this statistical technique correctly" question, consider adding the [statistics], or more relevant tags. – Manetheran Jul 5 '13 at 3:32
Ah sorry, yes I just don't think this is the correct way to do this. Is there a way to do this type of ModMed where you get some sort of comparison of the mediation effect across conditions. – Andrew Taylor Jul 5 '13 at 4:19
Unfortunately im not well versed in mediator variables so i can't help you. – Manetheran Jul 5 '13 at 4:24
The code above works perfectly fine, technically, but what it's doing is separating the dataset by condition and doing four separate mediation analyses. I was thinking there should be a better way to do this. – Andrew Taylor Jul 5 '13 at 4:24
Well thank you for letting me know I should add some extra information! – Andrew Taylor Jul 5 '13 at 4:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.