Mplus code for mediation, moderation, and moderated mediation models
Model 8 (latent variable version): 1 or more mediators, in parallel if multiple (example uses 1), 1 moderator moderating both the IVMediator path and direct IVDV 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 + c1'X + c2'W + c3'XW
M = a0 + a1X + a2W + a3XW
Algebra to calculate indirect and/or conditional effects by writing model as Y = a + bX:
Y = b0 + b1M + c1'X + c2'W + c3'XW
M = a0 + a1X + a2W + a3XW
Hence... substituting in equation for M
Y = b0 + b1(a0 + a1X + a2W + a3XW) + c1'X + c2'W + c3'XW
Hence... multiplying out brackets
Y = b0 + a0b1 + a1b1X + a2b1W + a3b1XW + c1'X + c2'W + c3'XW
Hence... grouping terms into form Y = a + bX
Y = (b0 + a0b1 + a2b1W + c2'W) + (a1b1 + a3b1W + c1' + c3'W)X
Hence...
One indirect effect(s) of X on Y, conditional on W:
a1b1 + a3b1W = (a1 + a3W)b1
One direct effect of X on Y, conditional on W:
c1' + c3'W
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
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 X (cdash1);
Y ON W (cdash2);
Y ON XW (cdash3);
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 DIR_LOWW DIR_MEDW DIR_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; IND_MEDW = a1*b1 + a3*b1*MED_W; IND_HIW = a1*b1 + a3*b1*HIGH_W; IMM = a3*b1;
! Calc conditional direct effects for each combination of moderator values
DIR_LOWW = cdash1 + cdash3*LOW_W; DIR_MEDW = cdash1 + cdash3*MED_W; DIR_HIW = cdash1 + cdash3*HIGH_W;
! Calc conditional total effects for each combination of moderator values
TOT_LOWW = IND_LOWW + DIR_LOWW;
TOT_MEDW = IND_MEDW + DIR_MEDW;
TOT_HIW = IND_HIW + DIR_HIW;
! 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:
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
