Mplus code for mediation, moderation, and moderated mediation models
Model 82: 4 or more mediators, both in parallel and in series (parallel serial paths)
Example Variables: 1 predictor X, 4 mediators M1, M2, M3, M4, and 1 outcome Y
Preliminary notes:
The code below assumes that
- The primary IV (variable X) is continuous or dichotomous
- Any moderators (variables W, V, Q, Z) are continuous, though the only adaptation required to handle dichotomous moderators is in the MODEL CONSTRAINT: and loop plot code - an example of how to do this is given in model 1b. Handling categorical moderators with > 2 categories is demonstrated in
model 1d.
- 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 + b1M1 + b2M2 + b3M3 + b4M4 + c'X
M1 = a01 + a1X
M2 = a02 + a2X
M3 = a03 + a3X + d1M1
M4 = a04 + a4X + d2M2
 
Algebra to calculate total, indirect and/or conditional effects by writing model as Y = a + bX:
Y = b0 + b1M1 + b2M2 + b3M3 + b4M4 + c'X
M1 = a01 + a1X
M2 = a02 + a2X
M3 = a03 + a3X + d1M1
M4 = a04 + a4X + d2M2
Hence... substituting in equations for M1 and M2 into Y, M3 and M4
Y = b0 + b1(a01 + a1X) + b2(a02 + a2X) + b3M3 + b4M4 + c'X
M3 = a03 + a3X + d1(a01 + a1X)
M4 = a04 + a4X + d2(a02 + a2X)
Hence... substituting in equations for M3 and M4 into Y
Y = b0 + b1(a01 + a1X) + b2(a02 + a2X) + b3(a03 + a3X + d1(a01 + a1X)) + b4(a04 + a4X + d2(a02 + a2X)) + c'X
Hence... multiplying out brackets
Y = b0 + a01b1 + a1b1X + a02b2 + a2b2X + a03b3 + a3b3X + a01d1b3 + a1d1b3X + a04b4 + a4b4X + a02d2b4 + a2d2b4X + c'X
Hence... grouping terms into form Y = a + bX
Y = (b0 + a01b1 + a02b2 + a03b3 + a04b4 + a01d1b3 + a02d2b4) + (a1b1 + a2b2 + a3b3 + a4b4 + a1d1b3 + a2d2b4 + c')X
Hence...
Six indirect effects of X on Y:
a1b1, a2b2, a3b3, a4b4, a1d1b3, a2d2b4
One direct effect of X on Y:
c'
 
Mplus code for the model:
! Predictor variable - X
! Mediator variable(s) – M1, M2, M3, M4
! Moderator variable(s) - none
! Outcome variable - Y
USEVARIABLES = X M1 M2 M3 M4 Y;
ANALYSIS:
   TYPE = GENERAL;
   ESTIMATOR = ML;
   BOOTSTRAP = 10000;
! In model statement name each path using parentheses
MODEL:
   Y ON M1 (b1);
   Y ON M2 (b2);
   Y ON M3 (b3);
   Y ON M4 (b4);
   Y ON X (cdash);   ! direct effect of X on Y
   M1 ON X (a1);
   M2 ON X (a2);
   M3 ON X (a3);
   M4 ON X (a4);
   M3 ON M1 (d1);
   M4 ON M2 (d2);
! Use model constraint to calculate specific indirect paths and total indirect effect
MODEL CONSTRAINT:
   NEW(a1b1 a2b2 a3b3 a4b4 a1d1b3 a2d2b4 TOTALIND TOTAL);
   a1b1 = a1*b1;   ! Specific indirect effect of X on Y via M1 only
   a2b2 = a2*b2;   ! Specific indirect effect of X on Y via M2 only
   a3b3 = a3*b3;   ! Specific indirect effect of X on Y via M3 only
   a4b4 = a4*b4;   ! Specific indirect effect of X on Y via M4 only
   a1d1b3 = a1*d1*b3;   ! Specific indirect effect of X on Y via M1 and M3
   a2d2b4 = a2*d2*b4;   ! Specific indirect effect of X on Y via M2 and M4
   TOTALIND = a1b1 + a2b2 + a3b3 + a4b4 + a1d1b3 + a2d2b4;   ! Total indirect effect of X on Y via M1, M2, M3, M4
   TOTAL = a1b1 + a2b2 + a3b3 + a4b4 + a1d1b3 + a2d2b4 + cdash;   ! Total effect of X on Y
OUTPUT:
   STAND CINT(bcbootstrap);
 
Editing required for testing indirect effect using alternative MODEL INDIRECT: subcommand
MODEL INDIRECT: offers an alternative to MODEL CONSTRAINT: for models containing indirect effects, where these are
not moderated. To instead use MODEL INDIRECT: to test this model, you would edit the code above as follows:
First, you can remove the naming of parameters using parentheses in the MODEL: command, i.e. you just need:
MODEL:
   Y ON X M1 M2 M3 M4;
   M1 ON X;
   M2 ON X;
   M3 ON M1 X;
   M4 ON M2 X;
Second, replace the MODEL CONSTRAINT: subcommand with the following MODEL INDIRECT: subcommand:
   MODEL INDIRECT:
   Y IND X;
Leave the OUTPUT: command unchanged.
 
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
|