I am trying to fit a moderation mediation model (please see the figure) enter image description here

So I have a clasic mediation model and a moderator for the association between the mediator and the outcome.

I want to fit this model in lavaan (or another package) and I know how to do the mediation model but I am struggling to add the moderator. Please see here the code for the mediation model but any guidance about how to include the moderator would be really appreciated:

X <- rnorm(100)
M <- 0.5*X + rnorm(100)
Y <- 0.7*M + rnorm(100)
W <- 0.3*M + rnorm(100)
data <- data.frame(X = X, Y = Y, M = M, W = W)
data$MW <- data$M * data$W

model <- ' # direct effect
             Y ~ c*X
           # mediator
            Y ~ b*M
            M ~ a*X

           # indirect effect (a*b)
             ab := a*b
           # total effect
             total := c + (a*b)
             Y ~~ Y
             M ~~ M
fit <- sem(model, data = data)
summary(fit, standardized=T)

Plot <- semPaths(fit, whatLabels = "est",
                 sizeMan = 10,
                 edge.label.cex = 1.15,
                 style = "ram",
                 nCharNodes = 0, nCharEdges = 0)

I would also need to know how to get robust estimations since it is a twin sample and some of the participants are related

Thank you so much in advance!

  • 4
    $\begingroup$ lavaan doesn't allow you to include product terms, so you will have to do that yourself. Center the moderator and include it and its product with the mediator in the model for the outcome. If you center it at its mean (assuming it is continuous), you will still get something like the average indirect effect (e.g., as recommended by Barrett et al. (2019). You will need to create the product term yourself as a new variable before including it in the model. $\endgroup$
    – Noah
    Commented Feb 15 at 16:35

1 Answer 1


I'm struggling with similar issue, however different then your, I have a moderation effect on the path from X to Y. I could not solve my deep issue, but here is certain suggestion for you.

To run this analysis in lavaan, before the analysis you should center your MOD and MED variables. then calculate the interaction term of MOD:MED, it doesn't matter where do you calculate, I prefer SPSS since I'm beginner in R. But special for moderated mediation model, it is suggested to calculate index of moderated mediation. Index of moderated mediation helps researchers understand how the indirect effect of an independent variable on an outcome variable through a mediator variable is contingent upon the level or condition of a moderator variable.

Additionally, at the beginning, I was also interested in model 14 of Hayes. With time, I read too many things about my analyses since there will be multigroup comparison in mine, and I learned using model 15 instead of model 14, and model 8 instead of model 7 is more useful than the single moderation effect. I forgot the proper articles but here is a tutorial about modmed and reasons why we should choose model 8 or model 15. Lastly, If you interested in only with modmed, instead of lavaan you can use special package by Hayes. This package is available in this website and you can run the process model by this package.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.