Mplus code for mediation, moderation, and moderated mediation models

Model 5 (latent variable version): 1 or more mediators, in parallel if multiple, 1 moderator of direct IV-DV path only

Example Variables: 1 latent predictor X measured by 4 observed variables X1-X4; 1 latent mediator M measured by 4 observed variables M1-M4, 1 latent moderator W measured by 4 observed variables W1-W4, 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 + c1'X + c2'W + c3'XW
M = a0 + a1X

 

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


Hence... substituting in equation for M

Y = b0 + b1(a0 + a1X) + c1'X + c2'W + c3'XW


Hence... multiplying out brackets

Y = b0 + a0b1 + a1b1X + c1'X + c2'W + c3'XW


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

Y = (b0 + a0b1 + c2'W) + (a1b1 + c1' + c3'W)X


Hence...

One indirect effect of X on Y:

a1b1

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

c1' + c3'W

 

Mplus code for the model:

! Latent predictor variable X measured by X1-X4
! Latent mediator M measured by M1-M4
! Latent moderator W measured by W1-W4
! Latent outcome variable Y measured by Y1-Y4

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 factor by fixing variance = 1 (instead of first loading)
! This makes this factor 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);

! 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

! Since we have standardised factors, this is simple - use moderator values of -1, 0, 1 !

! Also calculate total effects at lo, med, hi values of moderator

MODEL CONSTRAINT:
   NEW(LOW_W MED_W HIGH_W a1b1 DIR_LO DIR_MED DIR_HI TOT_LO TOT_MED TOT_HI);

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

! Now calc indirect effect - and conditional direct effects for each value of W

   a1b1 = a1*b1;

   DIR_LO = cdash1 + cdash3*LOW_W;
   DIR_MED = cdash1 + cdash3*MED_W;
   DIR_HI = cdash1 + cdash3*HIGH_W;

   TOT_LO = DIR_LO + a1b1;
   TOT_MED = DIR_MED + a1b1;
   TOT_HI = DIR_HI + a1b1;

! Use loop plot to plot total effects of X on Y for low, med, high values of W
! 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 = TOT_LO*XVAL;
   MEDMOD = TOT_MED*XVAL;
   HIMOD = TOT_HI*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.offbeat.group.shef.ac.uk/FIO/mplusmedmod.htm

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