Mplus code for mediation, moderation, and moderated mediation models
Model 15 (latent variable version): 1 or more mediators, in parallel if multiple (example uses 1), 1 moderator of both MediatorDV 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 V measured by 4 observed variables V1V4, 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 + b2MV + c1'X + c2'V + c3'XV
M = a0 + a1X
Algebra to calculate indirect and/or conditional effects by writing model as Y = a + bX:
Y = b0 + b1M + b2MV + c1'X + c2'V + c3'XV
M = a0 + a1X
Hence... substituting in equation for M
Y = b0 + b1(a0 + a1X) + b2(a0 + a1X)V + c1'X + c2'V + c3'XV
Hence... multiplying out brackets
Y = b0 + a0b1 + a1b1X + a0b2V + a1b2XV + c1'X + c2'V + c3'XV
Hence... grouping terms into form Y = a + bX
Y = (b0 + a0b1 + c2'V + a0b2V) + (a1b1 + a1b2V + c1' + c3'V)X
Hence...
One indirect effect(s) of X on Y, conditional on V:
a1b1 + a1b2V = a1(b1 + b2V)
One direct effect of X on Y, conditional on V:
c1' + c3'V
Mplus code for the model:
! Latent predictor variable X measured by X1X4
! Latent mediator M measured by 4 observed variables M1M4
! Latent moderator V measured by 4 observed variables V1V4
! Latent outcome variable Y measured by Y1Y4
USEVARIABLES = X1 X2 X3 X4 M1 M2 M3 M4 V1 V2 V3 V4 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;
V BY V1* V2 V3 V4;
Y BY Y1 Y2 Y3 Y4;
V@1;
! Create latent interactions
MV  M XWITH V;
XV  X XWITH V;
! 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 X (cdash1);
Y ON V (cdash2);
Y ON XV (cdash3);
M ON X (a1);
! Use model constraint subcommand to test conditional indirect effects
! You need to pick low, medium and high moderator values for V
! 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_V MED_V HIGH_V IND_LOWV IND_MEDV IND_HIV IMM DIR_LOWV DIR_MEDV DIR_HIV TOT_LOWV TOT_MEDV TOT_HIV);
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
! Calc conditional indirect effects for each combination of moderator values ! and index of moderated mediation
IND_LOWV = a1*b1 + a1*b2*LOW_V; IND_MEDV = a1*b1 + a1*b2*MED_V; IND_HIV = a1*b1 + a1*b2*HIGH_V; IMM = a1*b3;
! Calc conditional direct effects for each combination of moderator values
DIR_LOWV = cdash1 + cdash3*LOW_V; DIR_MEDV = cdash1 + cdash3*MED_V; DIR_HIV = cdash1 + cdash3*HIGH_V;
! Calc conditional total effects for each combination of moderator values
TOT_LOWV = IND_LOWV + DIR_LOWV;
TOT_MEDV = IND_MEDV + DIR_MEDV;
TOT_HIV = IND_HIV + DIR_HIV;
! 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_LOWV*XVAL; MEDMOD = IND_MEDV*XVAL; HIMOD = IND_HIV*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
