﻿figure it out - a statistical consultancy from the Institute of Work Psychology, University of Sheffield

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

Model 41 (latent variable version): 1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 1 moderating both the IV- Mediator path and the direct IV-DV path, 2 moderating both the Mediator-DV path and the direct IV-DV path

Example Variables: 1 latent predictor X measured by 4 observed variables X1-X4, 1 latent mediator M measured by 4 observed variables M1-M4, 3 latent moderators W, V, and Q, each measured by sets of 4 observed variables W1-W4, V1-V4, and Q1-Q4 respectively, 1 latent outcome Y measured by 4 observed variables Y1-Y4

Preliminary notes:

The code below assumes that

• The latent IV (factor X) is measured by continuous observed variables X1-X4.
• Any latent moderator(s) (factors W, V, Q, Z) are measured by continuous observed variables W1-W4, Z1-Z4, V1-V4, Q1-Q4 respectively.
• Any latent mediator(s) (factor M, or factors M1, M2, etc.) are measured by continuous observed variables M1-M4 or M1_1-M1-4, M2_1-M2_4 respectively.
• The latent outcome Y is measured by continuous observed variables Y1-Y4.

Model Diagram (factor indicator variables omitted for space/clarity reasons):

Statistical Diagram (factor indicator variables omitted for space/clarity reasons):

Model Equation(s):

Y = b0 + b1M + b2MV + b3MQ + c1'X + c2'W + c3'XW + c4'V + c5'Q + c6'XV + c7'XQ
M = a0 + a1X + a2W + a3XW

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

Y = b0 + b1M + b2MV + b3MQ + c1'X + c2'W + c3'XW + c4'V + c5'Q + c6'XV + c7'XQ
M = a0 + a1X + a2W + a3XW

Hence... substituting in equation for M

Y = b0 + b1(a0 + a1X + a2W + a3XW) + b2(a0 + a1X + a2W + a3XW)V + b3(a0 + a1X + a2W + a3XW)Q + c1'X + c2'W + c3'XW + c4'V + c5'Q + c6'XV + c7'XQ

Hence... multiplying out brackets

Y = b0 + a0b1 + a1b1X + a2b1W + a3b1XW + a0b2V + a1b2XV + a2b2WV + a3b2XWV + a0b3Q + a1b3XQ + a2b3WQ + a3b3XWQ + c1'X + c2'W + c3'XW + c4'V + c5'Q + c6'XV + c7'XQ

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

Y = (b0 + a0b1 + a2b1W + a0b2V + a2b2WV + a0b3Q + a2b3WQ + c2'W + c4'V + c5'Q) + (a1b1 + a3b1W + a1b2V + a3b2WV + a1b3Q + a3b3WQ + c1' + c3'W + c6'V + c7'Q)X

Hence...

One indirect effect(s) of X on Y, conditional on W, V, Q:

a1b1 + a3b1W + a1b2V + a3b2WV + a1b3Q + a3b3WQ = (a1 + a3W)(b1 + b2V + b3Q)

One direct effect of X on Y, conditional on W, V, Q:

c1' + c3'W + c6'V + c7'Q

Mplus code for the model:

! Latent predictor variable X measured by X1-X4
! Latent mediator M measured by 4 observed variables M1-M4
! Latent moderators W, V, and Q, each measured by sets of 4 observed variables W1-W4, V1-V4, and Q1-Q4 respectively
! Latent outcome variable Y measured by Y1-Y4

USEVARIABLES = X1 X2 X3 X4 M1 M2 M3 M4
W1 W2 W3 W4 V1 V2 V3 V4 Q1 Q2 Q3 Q4
Y1 Y2 Y3 Y4;

ANALYSIS:
TYPE = GENERAL RANDOM;
ESTIMATOR = ML;
ALGORITHM = INTEGRATION;

! In model statement first state measurement model
! Then create any latent interactions required
! Then state structural model naming each path and intercept using parentheses

MODEL:

! Measurement model
! Identify moderator factors by fixing variance = 1 (instead of first loading)
! This makes these factors standardised
X BY X1 X2 X3 X4;
M BY M1 M2 M3 M4;
W BY W1* W2 W3 W4;
V BY V1* V2 V3 V4;
Q BY Q1* Q2 Q3 Q4;
Y BY Y1 Y2 Y3 Y4;

W@1;   V@1;   Q@1;

! Create latent interactions
MV | M XWITH V;
MQ | M XWITH Q;
XW | X XWITH W;
XV | X XWITH V;
XQ | X XWITH Q;

! Fit structural model and name parameters
! Note that intercepts of M, Y are fixed = 0 since they are latent vars
! so no code to state and name them as parameters
Y ON M (b1);
Y ON MV (b2);
Y ON MQ (b3);

Y ON X (cdash1);
Y ON W (cdash2);
Y ON XW (cdash3);
Y ON V (cdash4);
Y ON Q (cdash5);
Y ON XV (cdash6);
Y ON XQ (cdash7);

M ON X (a1);
M ON W (a2);
M ON XW (a3);

! Use model constraint subcommand to test conditional indirect effects
! You need to pick low, medium and high moderator values for W, V, Q
! for example, of 1 SD below mean, mean, 1 SD above mean

! 3 moderators, 3 values for each, gives 27 combinations
! arbitrary naming convention for conditional indirect and total effects used below:
! HWMVLQ = high value of W, medium value of V and low value of Q, etc.

MODEL CONSTRAINT:
NEW(LOW_W MED_W HIGH_W LOW_V MED_V HIGH_V LOW_Q MED_Q HIGH_Q
ILWLVLQ IMWLVLQ IHWLVLQ ILWMVLQ IMWMVLQ IHWMVLQ
ILWHVLQ IMWHVLQ IHWHVLQ
ILWLVMQ IMWLVMQ IHWLVMQ ILWMVMQ IMWMVMQ IHWMVMQ
ILWHVMQ IMWHVMQ IHWHVMQ
ILWLVHQ IMWLVHQ IHWLVHQ ILWMVHQ IMWMVHQ IHWMVHQ
ILWHVHQ IMWHVHQ IHWHVHQ
DLWLVLQ DMWLVLQ DHWLVLQ DLWMVLQ DMWMVLQ DHWMVLQ
DLWHVLQ DMWHVLQ DHWHVLQ
DLWLVMQ DMWLVMQ DHWLVMQ DLWMVMQ DMWMVMQ DHWMVMQ
DLWHVMQ DMWHVMQ DHWHVMQ
DLWLVHQ DMWLVHQ DHWLVHQ DLWMVHQ DMWMVHQ DHWMVHQ
DLWHVHQ DMWHVHQ DHWHVHQ
TLWLVLQ TMWLVLQ THWLVLQ TLWMVLQ TMWMVLQ THWMVLQ
TLWHVLQ TMWHVLQ THWHVLQ
TLWLVMQ TMWLVMQ THWLVMQ TLWMVMQ TMWMVMQ THWMVMQ
TLWHVMQ TMWHVMQ THWHVMQ
TLWLVHQ TMWLVHQ THWLVHQ TLWMVHQ TMWMVHQ THWMVHQ
TLWHVHQ TMWHVHQ THWHVHQ);

LOW_W = -1;   ! -1 SD below mean value of W
MED_W = 0;   ! mean value of W
HIGH_W = 1;   ! +1 SD above mean value of W

LOW_V = -1;   ! -1 SD below mean value of V
MED_V = 0;   ! mean value of V
HIGH_V = 1;   ! +1 SD above mean value of V

LOW_Q = -1;   ! -1 SD below mean value of Q
MED_Q = 0;   ! mean value of Q
HIGH_Q = 1;   ! +1 SD above mean value of Q

! Calc conditional indirect effects for each combination of moderator values

ILWLVLQ = a1*b1 + a3*b1*LOW_W + a1*b2*LOW_V + a3*b2*LOW_W*LOW_V +
a1*b3*LOW_Q + a3*b3*LOW_W*LOW_Q;
IMWLVLQ = a1*b1 + a3*b1*MED_W + a1*b2*LOW_V + a3*b2*MED_W*LOW_V +
a1*b3*LOW_Q + a3*b3*MED_W*LOW_Q;
IHWLVLQ = a1*b1 + a3*b1*HIGH_W + a1*b2*LOW_V + a3*b2*HIGH_W*LOW_V +
a1*b3*LOW_Q + a3*b3*HIGH_W*LOW_Q;

ILWMVLQ = a1*b1 + a3*b1*LOW_W + a1*b2*MED_V + a3*b2*LOW_W*MED_V +
a1*b3*LOW_Q + a3*b3*LOW_W*LOW_Q;
IMWMVLQ = a1*b1 + a3*b1*MED_W + a1*b2*MED_V + a3*b2*MED_W*MED_V +
a1*b3*LOW_Q + a3*b3*MED_W*LOW_Q;
IHWMVLQ = a1*b1 + a3*b1*HIGH_W + a1*b2*MED_V + a3*b2*HIGH_W*MED_V +
a1*b3*LOW_Q + a3*b3*HIGH_W*LOW_Q;

ILWHVLQ = a1*b1 + a3*b1*LOW_W + a1*b2*HIGH_V + a3*b2*LOW_W*HIGH_V +
a1*b3*LOW_Q + a3*b3*LOW_W*LOW_Q;
IMWHVLQ = a1*b1 + a3*b1*MED_W + a1*b2*HIGH_V + a3*b2*MED_W*HIGH_V +
a1*b3*LOW_Q + a3*b3*MED_W*LOW_Q;
IHWHVLQ = a1*b1 + a3*b1*HIGH_W + a1*b2*HIGH_V + a3*b2*HIGH_W*HIGH_V +
a1*b3*LOW_Q + a3*b3*HIGH_W*LOW_Q;

ILWLVMQ = a1*b1 + a3*b1*LOW_W + a1*b2*LOW_V + a3*b2*LOW_W*LOW_V +
a1*b3*MED_Q + a3*b3*LOW_W*MED_Q;
IMWLVMQ = a1*b1 + a3*b1*MED_W + a1*b2*LOW_V + a3*b2*MED_W*LOW_V +
a1*b3*MED_Q + a3*b3*MED_W*MED_Q;
IHWLVMQ = a1*b1 + a3*b1*HIGH_W + a1*b2*LOW_V + a3*b2*HIGH_W*LOW_V +
a1*b3*MED_Q + a3*b3*HIGH_W*MED_Q;

ILWMVMQ = a1*b1 + a3*b1*LOW_W + a1*b2*MED_V + a3*b2*LOW_W*MED_V +
a1*b3*MED_Q + a3*b3*LOW_W*MED_Q;
IMWMVMQ = a1*b1 + a3*b1*MED_W + a1*b2*MED_V + a3*b2*MED_W*MED_V +
a1*b3*MED_Q + a3*b3*MED_W*MED_Q;
IHWMVMQ = a1*b1 + a3*b1*HIGH_W + a1*b2*MED_V + a3*b2*HIGH_W*MED_V +
a1*b3*MED_Q + a3*b3*HIGH_W*MED_Q;

ILWHVMQ = a1*b1 + a3*b1*LOW_W + a1*b2*HIGH_V + a3*b2*LOW_W*HIGH_V +
a1*b3*MED_Q + a3*b3*LOW_W*MED_Q;
IMWHVMQ = a1*b1 + a3*b1*MED_W + a1*b2*HIGH_V + a3*b2*MED_W*HIGH_V +
a1*b3*MED_Q + a3*b3*MED_W*MED_Q;
IHWHVMQ = a1*b1 + a3*b1*HIGH_W + a1*b2*HIGH_V + a3*b2*HIGH_W*HIGH_V +
a1*b3*MED_Q + a3*b3*HIGH_W*MED_Q;

ILWLVHQ = a1*b1 + a3*b1*LOW_W + a1*b2*LOW_V + a3*b2*LOW_W*LOW_V +
a1*b3*HIGH_Q + a3*b3*LOW_W*HIGH_Q;
IMWLVHQ = a1*b1 + a3*b1*MED_W + a1*b2*LOW_V + a3*b2*MED_W*LOW_V +
a1*b3*HIGH_Q + a3*b3*MED_W*HIGH_Q;
IHWLVHQ = a1*b1 + a3*b1*HIGH_W + a1*b2*LOW_V + a3*b2*HIGH_W*LOW_V +
a1*b3*HIGH_Q + a3*b3*HIGH_W*HIGH_Q;

ILWMVHQ = a1*b1 + a3*b1*LOW_W + a1*b2*MED_V + a3*b2*LOW_W*MED_V +
a1*b3*HIGH_Q + a3*b3*LOW_W*HIGH_Q;
IMWMVHQ = a1*b1 + a3*b1*MED_W + a1*b2*MED_V + a3*b2*MED_W*MED_V +
a1*b3*HIGH_Q + a3*b3*MED_W*HIGH_Q;
IHWMVHQ = a1*b1 + a3*b1*HIGH_W + a1*b2*MED_V + a3*b2*HIGH_W*MED_V +
a1*b3*HIGH_Q + a3*b3*HIGH_W*HIGH_Q;

ILWHVHQ = a1*b1 + a3*b1*LOW_W + a1*b2*HIGH_V + a3*b2*LOW_W*HIGH_V +
a1*b3*HIGH_Q + a3*b3*LOW_W*HIGH_Q;
IMWHVHQ = a1*b1 + a3*b1*MED_W + a1*b2*HIGH_V + a3*b2*MED_W*HIGH_V +
a1*b3*HIGH_Q + a3*b3*MED_W*HIGH_Q;
IHWHVHQ = a1*b1 + a3*b1*HIGH_W + a1*b2*HIGH_V + a3*b2*HIGH_W*HIGH_V +
a1*b3*HIGH_Q + a3*b3*HIGH_W*HIGH_Q;

! Calc conditional direct effects for each combination of moderator values

DLWLVLQ = cdash1 + cdash3*LOW_W + cdash6*LOW_V + cdash7*LOW_Q;
DMWLVLQ = cdash1 + cdash3*MED_W + cdash6*LOW_V + cdash7*LOW_Q;
DHWLVLQ = cdash1 + cdash3*HIGH_W + cdash6*LOW_V + cdash7*LOW_Q;

DLWMVLQ = cdash1 + cdash3*LOW_W + cdash6*MED_V + cdash7*LOW_Q;
DMWMVLQ = cdash1 + cdash3*MED_W + cdash6*MED_V + cdash7*LOW_Q;
DHWMVLQ = cdash1 + cdash3*HIGH_W + cdash6*MED_V + cdash7*LOW_Q;

DLWHVLQ = cdash1 + cdash3*LOW_W + cdash6*HIGH_V + cdash7*LOW_Q;
DMWHVLQ = cdash1 + cdash3*MED_W + cdash6*HIGH_V + cdash7*LOW_Q;
DHWHVLQ = cdash1 + cdash3*HIGH_W + cdash6*HIGH_V + cdash7*LOW_Q;

DLWLVMQ = cdash1 + cdash3*LOW_W + cdash6*LOW_V + cdash7*MED_Q;
DMWLVMQ = cdash1 + cdash3*MED_W + cdash6*LOW_V + cdash7*MED_Q;
DHWLVMQ = cdash1 + cdash3*HIGH_W + cdash6*LOW_V + cdash7*MED_Q;

DLWMVMQ = cdash1 + cdash3*LOW_W + cdash6*MED_V + cdash7*MED_Q;
DMWMVMQ = cdash1 + cdash3*MED_W + cdash6*MED_V + cdash7*MED_Q;
DHWMVMQ = cdash1 + cdash3*HIGH_W + cdash6*MED_V + cdash7*MED_Q;

DLWHVMQ = cdash1 + cdash3*LOW_W + cdash6*HIGH_V + cdash7*MED_Q;
DMWHVMQ = cdash1 + cdash3*MED_W + cdash6*HIGH_V + cdash7*MED_Q;
DHWHVMQ = cdash1 + cdash3*HIGH_W + cdash6*HIGH_V + cdash7*MED_Q;

DLWLVHQ = cdash1 + cdash3*LOW_W + cdash6*LOW_V + cdash7*HIGH_Q;
DMWLVHQ = cdash1 + cdash3*MED_W + cdash6*LOW_V + cdash7*HIGH_Q;
DHWLVHQ = cdash1 + cdash3*HIGH_W + cdash6*LOW_V + cdash7*HIGH_Q;

DLWMVHQ = cdash1 + cdash3*LOW_W + cdash6*MED_V + cdash7*HIGH_Q;
DMWMVHQ = cdash1 + cdash3*MED_W + cdash6*MED_V + cdash7*HIGH_Q;
DHWMVHQ = cdash1 + cdash3*HIGH_W + cdash6*MED_V + cdash7*HIGH_Q;

DLWHVHQ = cdash1 + cdash3*LOW_W + cdash6*HIGH_V + cdash7*HIGH_Q;
DMWHVHQ = cdash1 + cdash3*MED_W + cdash6*HIGH_V + cdash7*HIGH_Q;
DHWHVHQ = cdash1 + cdash3*HIGH_W + cdash6*HIGH_V + cdash7*HIGH_Q;

! Calc conditional total effects for each combination of moderator values

TLWLVLQ = ILWLVLQ + DLWLVLQ;
TMWLVLQ = IMWLVLQ + DMWLVLQ;
THWLVLQ = IHWLVLQ + DHWLVLQ;

TLWMVLQ = ILWMVLQ + DLWMVLQ;
TMWMVLQ = IMWMVLQ + DMWMVLQ;
THWMVLQ = IHWMVLQ + DHWMVLQ;

TLWHVLQ = ILWHVLQ + DLWHVLQ;
TMWHVLQ = IMWHVLQ + DMWHVLQ;
THWHVLQ = IHWHVLQ + DHWHVLQ;

TLWLVMQ = ILWLVMQ + DLWLVMQ;
TMWLVMQ = IMWLVMQ + DMWLVMQ;
THWLVMQ = IHWLVMQ + DHWLVMQ;

TLWMVMQ = ILWMVMQ + DLWMVMQ;
TMWMVMQ = IMWMVMQ + DMWMVMQ;
THWMVMQ = IHWMVMQ + DHWMVMQ;

TLWHVMQ = ILWHVMQ + DLWHVMQ;
TMWHVMQ = IMWHVMQ + DMWHVMQ;
THWHVMQ = IHWHVMQ + DHWHVMQ;

TLWLVHQ = ILWLVHQ + DLWLVHQ;
TMWLVHQ = IMWLVHQ + DMWLVHQ;
THWLVHQ = IHWLVHQ + DHWLVHQ;

TLWMVHQ = ILWMVHQ + DLWMVHQ;
TMWMVHQ = IMWMVHQ + DMWMVHQ;
THWMVHQ = IHWMVHQ + DHWMVHQ;

TLWHVHQ = ILWHVHQ + DLWHVHQ;
TMWHVHQ = IMWHVHQ + DMWHVHQ;
THWHVHQ = IHWHVHQ + DHWHVHQ;

! Use loop plot to plot conditional indirect effect of X on Y for each combination of low, med, high moderator values
! Could be edited to show conditional direct or conditional total effects instead
! NOTE - values from -3 to 3 in LOOP() statement since
! X is factor with mean set at default of 0

PLOT(PLWLVLQ PMWLVLQ PHWLVLQ PLWMVLQ PMWMVLQ PHWMVLQ
PLWHVLQ PMWHVLQ PHWHVLQ
PLWLVMQ PMWLVMQ PHWLVMQ PLWMVMQ PMWMVMQ PHWMVMQ
PLWHVMQ PMWHVMQ PHWHVMQ
PLWLVHQ PMWLVHQ PHWLVHQ PLWMVHQ PMWMVHQ PHWMVHQ
PLWHVHQ PMWHVHQ PHWHVHQ);

LOOP(XVAL,-3,3,0.1);

PLWLVLQ = ILWLVLQ*XVAL;
PMWLVLQ = IMWLVLQ*XVAL;
PHWLVLQ = IHWLVLQ*XVAL;

PLWMVLQ = ILWMVLQ*XVAL;
PMWMVLQ = IMWMVLQ*XVAL;
PHWMVLQ = IHWMVLQ*XVAL;

PLWHVLQ = ILWHVLQ*XVAL;
PMWHVLQ = IMWHVLQ*XVAL;
PHWHVLQ = IHWHVLQ*XVAL;

PLWLVMQ = ILWLVMQ*XVAL;
PMWLVMQ = IMWLVMQ*XVAL;
PHWLVMQ = IHWLVMQ*XVAL;

PLWMVMQ = ILWMVMQ*XVAL;
PMWMVMQ = IMWMVMQ*XVAL;
PHWMVMQ = IHWMVMQ*XVAL;

PLWHVMQ = ILWHVMQ*XVAL;
PMWHVMQ = IMWHVMQ*XVAL;
PHWHVMQ = IHWHVMQ*XVAL;

PLWLVHQ = ILWLVHQ*XVAL;
PMWLVHQ = IMWLVHQ*XVAL;
PHWLVHQ = IHWLVHQ*XVAL;

PLWMVHQ = ILWMVHQ*XVAL;
PMWMVHQ = IMWMVHQ*XVAL;
PHWMVHQ = IHWMVHQ*XVAL;

PLWHVHQ = ILWHVHQ*XVAL;
PMWHVHQ = IMWHVHQ*XVAL;
PHWHVHQ = IHWHVHQ*XVAL;

PLOT:
TYPE = plot2;

OUTPUT:
CINT;

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.figureitout.org.uk

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