Mplus code for mediation, moderation, and moderated mediation models
Model 58 (latent variable version): 1 or more mediators, in parallel if multiple (example uses 1), 1 moderators, which moderates both the IV Mediator path and the MediatorDV path
Example Variables:
1 latent predictor X measured by 4 observed variables X1X4, 1 latent mediator M measured by 4 observed variables M1M4, 1 latent moderator W measured by 4 observed variables W1W4, 1 latent outcome Y measured by 4 observed variables Y1Y4
Preliminary notes:
The code below assumes that
 The latent IV (factor X) is measured by continuous observed variables X1X4.
 Any latent moderator(s) (factors W, V, Q, Z) are measured by continuous observed variables W1W4, Z1Z4, V1V4, Q1Q4 respectively.
 Any latent mediator(s) (factor M, or factors M1, M2, etc.) are measured by continuous observed variables M1M4 or M1_1M14, M2_1M2_4 respectively.
 The latent outcome Y is measured by continuous observed variables Y1Y4.
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 + b2W + b3MW + c'X
M = a0 + a1X + a2W + a3XW
Algebra to calculate indirect and/or conditional effects by writing model as Y = a + bX:
Y = b0 + b1M + b2W + b3MW + c'X
M = a0 + a1X + a2W + a3XW
Hence... substituting in equation for M
Y = b0 + b1(a0 + a1X + a2W + a3XW) + b2W + b3(a0 + a1X + a2W + a3XW)W + c'X
Hence... multiplying out brackets
Y = b0 + a0b1 + a1b1X + a2b1W + a3b1XW + b2W + a0b3W + a1b3XW + a2b3WW + a3b3XWW + c'X
Hence... grouping terms into form Y = a + bX
Y = (b0 + a0b1 + a2b1W + b2W + a0b3W + a2b3WW) + (a1b1 + a3b1W + a1b3W + a3b3WW + c')X
Hence...
One indirect effect(s) of X on Y, conditional on W:
a1b1 + a3b1W + a1b3W + a3b3WW = (a1 + a3W)(b1 + b3W)
One direct effect of X on Y:
c'
Mplus code for the model:
! Latent predictor variable X measured by X1X4
! Latent mediator M measured by 4 observed variables M1M4
! Latent moderator W measured by 4 observed variables W1W4
! Latent outcome variable Y measured by Y1Y4
USEVARIABLES = X1 X2 X3 X4 M1 M2 M3 M4 W1 W2 W3 W4 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;
Y BY Y1 Y2 Y3 Y4;
W@1;
! Create latent interactions
MW  M XWITH W;
XW  X XWITH W;
! 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 W (b2);
Y ON MW (b3);
Y ON X (cdash);
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
! for example, of 1 SD below mean, mean, 1 SD above mean
! 1 moderator, 3 values for it
! arbitrary naming convention for conditional indirect and total effects used below:
! MED_Q = medium value of Q, etc.
MODEL CONSTRAINT:
NEW(LOW_W MED_W HIGH_W IND_LOWW IND_MEDW IND_HIW IMM_LOW IMM_MEDW IMM_HIW TOT_LOWW TOT_MEDW TOT_HIW);
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
! Calc conditional indirect effects for each combination of moderator values ! and index of moderated mediation
IND_LOWW = a1*b1 + a3*b1*LOW_W + a1*b3*LOW_W + a3*b3*LOW_W*LOW_W; IND_MEDW = a1*b1 + a3*b1*MED_W + a1*b3*MED_W + a3*b3*MED_W*MED_W; IND_HIW = a1*b1 + a3*b1*HIGH_W + a1*b3*HIGH_W + a3*b3*HIGH_W*HIGH_W; IMM_LOW = a3*b1 + a1*b3 + a3*b3*LOW_W; IMM_MEDW = a3*b1 + a1*b3 + a3*b3*MED_W; IMM_HIW = a3*b1 + a1*b3 + a3*b3*HIGH_W;
! Calc conditional total effects for each combination of moderator values
TOT_LOWW = IND_LOWW + cdash;
TOT_MEDW = IND_MEDW + cdash;
TOT_HIW = IND_HIW + cdash;
! 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(LOMOD MEDMOD HIMOD);
LOOP(XVAL,3,3,0.1);
LOMOD = IND_LOWW*XVAL; MEDMOD = IND_MEDW*XVAL; HIMOD = IND_HIW*XVAL;
PLOT:
TYPE = plot2;
OUTPUT:
STAND 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
