 ## Mplus code for mediation, moderation, and moderated mediation models

Model 504: 3 or more mediators, both in parallel and in series, 2 moderators, 1 moderating paths between predictor and mediator, the second moderating paths between mediators, and between mediator and DV

Example Variables: 1 predictor X, 3 mediators M1, M2, and M3, 2 moderators W, V, 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 + b4V + b5M1V + c'X
M1 = a01 + a1X + a4W + a5XW
M2 = a02 + a2X
M3 = a03 + a3X + d1M1 + d2M2 + d3V + d4M1V

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

Y = b0 + b1M1 + b2M2 + b3M3 + b4V + b5M1V + c'X
M1 = a01 + a1X + a4W + a5XW
M2 = a02 + a2X
M3 = a03 + a3X + d1M1 + d2M2 + d3V + d4M1V

Hence... substituting in equations for M1 and M2 into Y and M3

Y = Y = b0 + b1(a01 + a1X + a4W + a5XW) + b2(a02 + a2X) + b3M3 + b4V + b5(a01 + a1X + a4W + a5XW)V + c'X
M3 = a03 + a3X + d1(a01 + a1X + a4W + a5XW) + d2(a02 + a2X) + d3V + d4(a01 + a1X + a4W + a5XW)V

Hence... substituting in equations for M3 into Y

Y = b0 + b1(a01 + a1X + a4W + a5XW) + b2(a02 + a2X) + b3(a03 + a3X + d1(a01 + a1X + a4W + a5XW) + d2(a02 + a2X) + d3V + d4(a01 + a1X + a4W + a5XW)V) + b4V + b5(a01 + a1X + a4W + a5XW)V + c'X

Hence... multiplying out brackets

Y = b0 + a01b1 + a1b1X + a4b1W + a5b1XW + a02b2 + a2b2X + a03b3 + a3b3X + a01d1b3 + a1d1b3X + a4d1b3W + a5d1b3XW + a02d2b3 + a2d2b3X + b3d3V + a01d4b3V + a1d4b3XV + a4d4b3WV + a5d4b3XWV + b4V + a01b5V + a1b5XV + a4b5WV + a5b5XWV + c'X

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

Y = (b0 + a01b1 + a4b1W + a02b2 + a03b3 + a01d1b3 + a4d1b3W + a02d2b3 + b3d3V + a01d4b3V + a4d4b3WV + b4V + a01b5V + a4b5WV) + (a1b1 + a5b1W + a2b2 + a3b3 + a1d1b3 + a5d1b3W + a2d2b3 + a1d4b3V + a5d4b3WV + a1b5V + a5b5WV + c')X

Hence...

Five indirect effects of X on Y:

a1b1 + a5b1W + a1b5V + a5b5WV, a2b2, a3b3, a2d2b3, a1b3d1 + a5d1b3W + a1d4b3V + a5d4b3WV

One direct effect of X on Y:

c'

Mplus code for the model:

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

USEVARIABLES = X M1 M2 M3 W V Y XW M1V;

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 V (b4);
Y ON M1V (b5);

Y ON X (cdash);   ! direct effect of X on Y

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

M3 ON M1 (d1);
M3 ON M2 (d2);
M3 ON V (d3);
M3 ON M1V (d4);

! 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 LOW_V MED_V HIGH_V
a1b1LWLV a1b1MWLV a1b1HWLV a1b1LWMV a1b1MWMV a1b1HWMV
a1b1LWHV a1b1MWHV a1b1HWHV
a2b2 a3b3 a2d2b3
TI_LWLV TI_MWLV TI_HWLV TI_LWMV TI_MWMV TI_HWMV
TI_LWHV TI_MWHV TI_HWHV
TOT_LWLV TOT_MWLV TOT_HWLV TOT_LWMV TOT_MWMV TOT_HWMV
TOT_LWHV TOT_MWHV TOT_HWHV);

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

LOW_V = #LOWV;   ! replace #LOWV in the code with your chosen low value of V
MED_V = #MEDV;   ! replace #MEDV in the code with your chosen medium value of V
HIGH_V = #HIGHV;   ! replace #HIGHV in the code with your chosen high value of V

! Now calc specific indirect effects for each value of W and V
a1b1LWLV = a1*b1 + a5*b1*LOW_W + a1*b5*LOW_V + a5*b5*LOW_W*LOW_V;
a1b1MWLV = a1*b1 + a5*b1*MED_W + a1*b5*LOW_V + a5*b5*MED_W*LOW_V;
a1b1HWLV = a1*b1 + a5*b1*HIGH_W + a1*b5*LOW_V + a5*b5*HIGH_W*LOW_V;
a1b1LWMV = a1*b1 + a5*b1*LOW_W + a1*b5*MED_V + a5*b5*LOW_W*MED_V;
a1b1MWMV = a1*b1 + a5*b1*MED_W + a1*b5*MED_V + a5*b5*MED_W*MED_V;
a1b1HWMV = a1*b1 + a5*b1*HIGH_W + a1*b5*MED_V + a5*b5*HIGH_W*MED_V;
a1b1LWHV = a1*b1 + a5*b1*LOW_W + a1*b5*HIGH_V + a5*b5*LOW_W*HIGH_V;
a1b1MWHV = a1*b1 + a5*b1*MED_W + a1*b5*HIGH_V + a5*b5*MED_W*HIGH_V;
a1b1HWHV = a1*b1 + a5*b1*HIGH_W + a1*b5*HIGH_V + a5*b5*HIGH_W*HIGH_V;

a2b2 = a2*b2;

a3b3 = a3*b3;

a2d2b3 = a2*d2*b3;

adbLWLV = a1*d1*b3 + a5*d1*b3*LOW_W + a1*d4*b3*LOW_V + a5*d4*b3*LOW_W*LOW_V;
adbMWLV = a1*d1*b3 + a5*d1*b3*MED_W + a1*d4*b3*LOW_V + a5*d4*b3*MED_W*LOW_V;
adbHWLV = a1*d1*b3 + a5*d1*b3*HIGH_W + a1*d4*b3*LOW_V + a5*d4*b3*HIGH_W*LOW_V;
adbLWMV = a1*d1*b3 + a5*d1*b3*LOW_W + a1*d4*b3*MED_V + a5*d4*b3*LOW_W*MED_V;
adbMWMV = a1*d1*b3 + a5*d1*b3*MED_W + a1*d4*b3*MED_V + a5*d4*b3*MED_W*MED_V;
adbHWMV = a1*d1*b3 + a5*d1*b3*HIGH_W + a1*d4*b3*MED_V + a5*d4*b3*HIGH_W*MED_V;
adbLWHV = a1*d1*b3 + a5*d1*b3*LOW_W + a1*d4*b3*HIGH_V + a5*d4*b3*LOW_W*HIGH_V;
adbMWHV = a1*d1*b3 + a5*d1*b3*MED_W + a1*d4*b3*HIGH_V + a5*d4*b3*MED_W*HIGH_V;
adbHWHV = a1*d1*b3 + a5*d1*b3*HIGH_W + a1*d4*b3*HIGH_V + a5*d4*b3*HIGH_W*HIGH_V;

! Now calc total indirect effects for each value of W and V
TI_LWLV = a1b1LWLV + a2b2 + a3b3 + a2d2b3 + adbLWLV;
TI_MWLV = a1b1MWLV + a2b2 + a3b3 + a2d2b3 + adbMWLV;
TI_HWLV = a1b1HWLV + a2b2 + a3b3 + a2d2b3 + adbHWLV;
TI_LWMV = a1b1LWMV + a2b2 + a3b3 + a2d2b3 + adbLWMV;
TI_MWMV = a1b1MWMV + a2b2 + a3b3 + a2d2b3 + adbMWMV;
TI_HWMV = a1b1HWMV + a2b2 + a3b3 + a2d2b3 + adbHWMV;
TI_LWHV = a1b1LWHV + a2b2 + a3b3 + a2d2b3 + adbLWHV;
TI_MWHV = a1b1MWHV + a2b2 + a3b3 + a2d2b3 + adbMWHV;
TI_HWHV = a1b1HWHV + a2b2 + a3b3 + a2d2b3 + adbHWHV;

! Now calc total effects for each value of W and V
TOT_LWLV = TI_LWLV + cdash;
TOT_MWLV = TI_MWLV + cdash;
TOT_HWLV = TI_HWLV + cdash;
TOT_LWMV = TI_LWMV + cdash;
TOT_MWMV = TI_MWMV + cdash;
TOT_HWMV = TI_HWMV + cdash;
TOT_LWHV = TI_LWHV + cdash;
TOT_MWHV = TI_MWHV + cdash;
TOT_HWHV = TI_HWHV + cdash;

! Use loop plot to plot total indirect 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(PTI_LWLV PTI_MWLV PTI_HWLV PTI_LWMV PTI_MWMV PTI_HWMV
PTI_LWHV PTI_MWHV PTI_HWHV);

LOOP(XVAL,1,5,0.1);

PTI_LWLV = TI_LWLV*XVAL;
PTI_MWLV = TI_MWLV*XVAL;
PTI_HWLV = TI_HWLV*XVAL;
PTI_LWMV = TI_LWMV*XVAL;
PTI_MWMV = TI_MWMV*XVAL;
PTI_HWMV = TI_HWMV*XVAL;
PTI_LWHV = TI_LWHV*XVAL;
PTI_MWHV = TI_MWHV*XVAL;
PTI_HWHV = TI_HWHV*XVAL;

PLOT:
TYPE = plot2;

OUTPUT:
STAND CINT(bcbootstrap);