Mplus code for mediation, moderation, and moderated mediation models

Model 92: 2 or more mediators in series, 1 moderator, moderating all of the direct IV-DV path, IV-first mediator path, IV-second mediator path, first mediator-DV path, second mediator-DV path, and the path between mediators

Example Variables: 1 predictor X, 2 mediators M1 and M2, 1 moderator W, 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 + b3M1W + b4M2W + c1'X + c2'W + c3'XW
M1 = a01 + a1X + a3W + a4XW
M2 = a02 + a2X + a5W + a6XW + d1M1 + d2M1W

 

Algebra to calculate total, indirect and/or conditional effects by writing model as Y = a + bX:

Y = b0 + b1M1 + b2M2 + b3M1W + b4M2W + c1'X + c2'W + c3'XW
M1 = a01 + a1X + a3W + a4XW
M2 = a02 + a2X + a5W + a6XW + d1M1 + d2M1W


Hence... substituting in equations for M1 and M2

Y = b0 + b1(a01 + a1X + a3W + a4XW) + b2(a02 + a2X + a5W + a6XW + d1(a01 + a1X + a3W + a4XW) + d2(a01 + a1X + a3W + a4XW)W) + b3(a01 + a1X + a3W + a4XW)W + b4(a02 + a2X + a5W + a6XW + d1(a01 + a1X + a3W + a4XW) + d2(a01 + a1X + a3W + a4XW)W)W + c1'X + c2'W + c3'XW


Hence... multiplying out brackets
Y = b0 + a01b1 + a1b1X + a3b1W + a02b2 + a2b2X + a5b2W + a6b2XW + a01d1b2 + a1d1b2X + a3d1b2W + a4d1b2XW + a01d2b2W + a1d2b2XW + a3d2b2WW + a4d2b2XWW + a01b3W + a1b3XW + a3b3WW + a4b3XWW + a02b4W + a2b4XW + a5b4WW + a6b4XWW + a01d1b4W + a1d1b4XW + a3d1b4WW + a4d1b4XWW + a01d2b4WW + a1d2b4XWW + a3d2b4WWW + a4d2b4XWWW + c1'X + c2'W + c3'XW


Hence... grouping terms into form Y = a + bX

Y = b0 + a01b1 + a3b1W + a02b2 + a5b2W + a01d1b2 + a3d1b2W + a01d2b2W + a3d2b2WW + a01b3W + a3b3WW + a02b4W + a5b4WW + a01d1b4W + a3d1b4WW + a01d2b4WW + a3d2b4WWW + c2'W + (a1b1 + a4b1W + a2b2 + a6b2W + a1d1b2+ a4d1b2W + a1d2b2W + a4d2b2WW + a1b3W + a4b3WW + a2b4W + a6b4WW + a1d1b4W + a4d1b4WW + a1d2b4WW + a4d2b4WWW + c1' + c3'W)X


Hence...

Three indirect effects of X on Y, conditional on W:

(a1 + a4W)(b1 + b3W), (a2 + a6W)(b2 + b4W), (a1 + a4W)(d1 + d2W)(b2 + b4W)

One direct effect of X on Y:

c1' + c3'W

 

Mplus code for the model:

! Predictor variable - X
! Mediator variable(s) M1, M2
! Moderator variable(s) - W
! Outcome variable - Y

USEVARIABLES = X M1 M2 W Y XW M1W M2W;

! Create interaction term
! Note that it has to be placed at end of USEVARIABLES subcommand above

DEFINE:
   XW = X*W;
   M1W = M1*W;
   M2W = M2*W;

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 M1W (b3);
   Y ON M2W (b4);

   Y ON X (cdash1);
   Y ON W (cdash2);
   Y ON XW (cdash3);

   M1 ON X (a1);
   M2 ON X (a2);
   M1 ON W (a3);
   M1 ON XW (a4);
   M2 ON W (a5);
   M2 ON XW (a6);

   M2 ON M1 (d1);

   M2 ON M1W (d2);

! Use model constraint subcommand to test simple slopes
! You need to pick low, medium and high moderator values,
! for example, of 1 SD below mean, mean, 1 SD above mean
! Also calc total effects at lo, med, hi values of moderator

MODEL CONSTRAINT:
   NEW(LOW_W MED_W HIGH_W
   LWa1b1 MWa1b1 HWa1b1
   LWa2b2 MWa2b2 HWa2b2
   LWa1d1b2 MWa1d1b2 HWa1d1b2
   IMM_ALW IMM_AMW IMM_AHW IMM_BLW IMM_BMW IMM_BHW IMM_CLW IMM_CMW IMM_CHW
   DIR_LW DIR_MW DIR_HW
   TOT_LOWW TOT_MEDW TOT_HIW);

   LOW_W = #LOWW;   ! replace #LOWW in the code with your chosen low value of W
   MED_W = #MEDW;   ! replace #MEDW in the code with your chosen medium value of W
   HIGH_W = #HIGHW;   ! replace #HIGHW in the code with your chosen high value of W

! Now calc indirect and total effects for each value of W

! Conditional indirect effects of X on Y via M1 only given values of W

   LWa1b1 = (a1 + a4*LOW_W)*(b1 + b3*LOW_W);
   MWa1b1 = (a1 + a4*MED_W)*(b1 + b3*MED_W);
   HWa1b1 = (a1 + a4*HIGH_W)*(b1 + b3*HIGH_W);

! Conditional indirect effects of X on Y via M2 only given values of W

   LWa2b2 = (a2 + a6*LOW_W)*(b2 + b4*LOW_W);
   MWa2b2 = (a2 + a6*MED_W)*(b2 + b4*MED_W);
   HWa2b2 = (a2 + a6*HIGH_W)*(b2 + b4*HIGH_W);

! Conditional indirect effects of X on Y via M1 and M2 given values of W

   LWa1d1b2 = (a1 + a4*LOW_W)*(d1 + d2*LOW_W)*(b2 + b4*LOW_W);
   MWa1d1b2 = (a1 + a4*MED_W)*(d1 + d2*MED_W)*(b2 + b4*MED_W);
   HWa1d1b2 = (a1 + a4*HIGH_W)*(d1 + d2*HIGH_W)*(b2 + b4*HIGH_W);

! Indices of Moderated Mediation (conditional on W)

   IMM_ALW = a1*b3 + a4*b1 + a4*b3*LOW_W;
   IMM_AMW = a1*b3 + a4*b1 + a4*b3*MED_W;
   IMM_AHW = a1*b3 + a4*b1 + a4*b3*HIGH_W;

   IMM_BLW = a2*b4 + a6*b2 + a6*b4*LOW_W;
   IMM_BMW = a2*b4 + a6*b2 + a6*b4*MED_W;
   IMM_BHW = a2*b4 + a6*b2 + a6*b4*HIGH_W;

   IMM_CLW = a1*d1*b3 + a1*d2*b2 + a4*d1*b2
     + a1*d2*b3*LOW_W + a4*d1*b3*LOW_W + a4*d2*b2*LOW_W + a4*d2*b3*LOW_W*LOW_W;
   IMM_CMW = a1*d1*b3 + a1*d2*b2 + a4*d1*b2 ;
     + a1*d2*b3*MED_W + a4*d1*b3*MED_W + a4*d2*b2*MED_W + a4*d2*b3*MED_W*MED_W;
   IMM_CHW = a1*d1*b3 + a1*d2*b2 + a4*d1*b2 ;
     + a1*d2*b3*HIGH_W + a4*d1*b3*HIGH_W + a4*d2*b2*HIGH_W + a4*d2*b3*HIGH_W*HIGH_W;

! Conditional direct effects of X on Y given values of W

   DIR_LW = cdash1 + cdash3*LOW_W;
   DIR_MW = cdash + cdash3*MED_W;;
   DIR_HW = cdash + cdash3*HIGH_W;;

! Conditional total effects of X on Y given values of W

   TOT_LOWW = LWa1d1b2 + LWa2b2 + LWa1b1 + DIR_LW;
   TOT_MEDW = MWa1d1b2 + MWa2b2 + MWa1b1 + DIR_MW;
   TOT_HIW = HWa1d1b2 + HWa2b2 + HWa1b1 + DIR_HW;

! Use loop plot to plot total effect of X on Y for low, med, high values of W
! 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(LOMOD MEDMOD HIMOD);

   LOOP(XVAL,1,5,0.1);

   LOMOD = TOT_LOWW*XVAL;
   MEDMOD = TOT_MEDW*XVAL;
   HIMOD = TOT_HIW*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

Home
Statistical Consultancy
Data Management
Public Training Courses
Inhouse & Bespoke Training
Links & Resources
About Us & Contact Details