Mplus code for the mediation, moderation, and moderated mediation model templates from Andrew Hayes'
PROCESS analysis examples
Model 1d: 1 moderator [BASIC MODERATION], categorical moderator with > 2 categories
Example Variables: 1 predictor X, 2 dummy variables WD1 and WD2 representing 3 category moderator W, 1 outcome Y
Preliminary notes:
The code below assumes that
 The primary IV (variable X) is continuous or dichotomous
 The moderator (variable W) has 3 categories (13), and is therefore represented by two dummy variables WD1, WD2, coded such that when W = 1, then WD1 = 1, WD2 = 0; when W = 2, then WD1 = 0, WD2 = 1; when W = 3 (the reference category), then WD1 = 0, WD2 = 0.
 Any mediators (variable M, or M1, M2, etc.) are continuous and satisfy the assumptions of standard multiple regression. An example of how to handle a dichotomous mediator is given in model 4c.
 The DV (variable Y) is continuous and satisfies the assumptions of standard multiple regression. An example of how to handle a dichotomous DV is given in model 1e (i.e. a moderated logistic regression) and in model 4d (i.e. an indirect effect in a logistic regression).
Model Diagram:
Statistical Diagram:
Model Equation(s):
Y = b0 + b1X + b2WD1 + b3WD2 + b4XWD1 + b5XWD2
Algebra to calculate indirect and/or conditional effects by writing model as Y = a + bX:
Y = b0 + b1X + b2WD1 + b3WD2 + b4XWD1 + b5XWD2
Hence... grouping terms into form Y = a + bX
Y = (b0 + b2WD1 + b3WD2) + (b1 + b4WD1 + b5WD2)X
Hence...
One direct effect of X on Y, conditional on W:
b1 + b4WD1 + b5WD2
so inserting the values of 0 and 1 for moderator W gives....
when W = 1, then WD1 = 1, WD2 = 0, hence Y = (b0 + b2) + (b1 + b4)X
when W = 2, then WD1 = 0, WD2 = 1, hence Y = (b0 + b3) + (b1 + b5)X
when W = 3, then WD1 = 0, WD2 = 0, hence Y = b0 + b1X
Mplus code for the model:
! Predictor variable  X
! Mediator variable(s) – (not applicable)
! Moderator variable(s)  W, 3 categories, represented by dichotomous 0/1 dummy variables WD1, WD2
! Outcome variable  Y
USEVARIABLES = X WD1 WD2 Y XWD1 XWD2;
! Create interaction term
! Note that it has to be placed at end of USEVARIABLES subcommand above
DEFINE:
XWD1 = X*WD1;
XWD2 = X*WD2;
ANALYSIS:
TYPE = GENERAL;
ESTIMATOR = ML;
BOOTSTRAP = 10000;
! In model statement name each path and intercept using parentheses
MODEL:
[Y] (b0);
Y ON X (b1);
Y ON WD1 (b2);
Y ON WD2 (b3);
Y ON XWD1 (b4);
Y ON XWD2 (b5);
! Use model constraint subcommand to test simple slopes
! You need to insert your respective dummy variable values, 0 and 1, for each group of W
MODEL CONSTRAINT:
NEW(SIMP_W1 SIMP_W2 SIMP_W3);
! Now calc simple slopes for each group of W
SIMP_W1 = b1 + b4;
SIMP_W2 = b1 + b5;
SIMP_W3 = b1;
! Use loop plot to plot model for values of W = 0, W = 1
! NOTE  values of 1,5 in LOOP() statement need to be replaced by
! logical min and max limits of predictor X used in analysis
PLOT(LINE_W1 LINE_W2 LINE_W3);
LOOP(XVAL,1,5,0.1);
LINE_W1 = (b0 + b2) + (b1 + b4)*XVAL;
LINE_W2 = (b0 + b3) + (b1 + b5)*XVAL;
LINE_W3 = b0 + b1*XVAL;
PLOT:
TYPE = plot2;
OUTPUT:
STAND CINT(bcbootstrap);
Return to Model Template index.
To cite this page and/or any code used, please use:
Stride, C.B., Gardner, S., Catley, N. & Thomas, F.(2015) 'Mplus code for the mediation, moderation, and moderated mediation model templates from Andrew Hayes' PROCESS analysis examples', http://www.offbeat.group.shef.ac.uk/FIO/mplusmedmod.htm
