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

Model 81 (latent variable version): 3 or more mediators, both in parallel and in series

Example Variables: 1 latent predictor X measured by 4 observed variables X1-X4, 3 latent mediators M1, M2, and M3, , each measured by 4 observed variables M1_1-M1_4, M2_1-M2_4, and M3_1-M3_4 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:

Statistical Diagram:

Model Equation(s):

Y = b0 + b1M1 + b2M2 + b3M3 + c'X
M1 = a01 + a1X
M2 = a02 + a2X + d1M1
M3 = a03 + a3X + d2M1

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

Y = b0 + b1M1 + b2M2 + b3M3 + c'X
M1 = a01 + a1X
M2 = a02 + a2X + d1M1
M3 = a03 + a3X + d2M1

Hence... substituting in equations for M1 into Y, M2 and M3

Y = b0 + b1(a01 + a1X) + b2M2 + b3M3 + c'X
M2 = a02 + a2X + d1(a01 + a1X)
M3 = a03 + a3X + d2(a01 + a1X)

Hence... substituting in equations for M3 into Y

Y = b0 + b1(a01 + a1X) + b2(a02 + a2X + d1(a01 + a1X)) + b3(a03 + a3X + d2(a01 + a1X)) + c'X

Hence... multiplying out brackets

Y = b0 + a01b1 + a1b1X + a02b2 + a2b2X + a01b2d1 + a1b2d1X + a03b3 + a3b3X + a01b3d2 + a1b3d2X + c'X

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

Y = (b0 + a01b1 + a02b2 + a01b2d1 + a03b3 + a01b3d2) + (a1b1 + a2b2 + a3b3 + a1b2d1 + a1b3d2 + c')X

Hence...

Five indirect effects of X on Y:

a1b1, a2b2, a3b3, a1d1b2, a1d2b3

One direct effect of X on Y:

c'

Mplus code for the model:

! Latent predictor variable X measured by X1-X4
! Latent mediator variables M1, M2 and M3, measured by M1_1-M1_4, M2_1-M2_4 and M3_1-M3_4 respectively
! Moderator variable(s) - none
! Latent outcome variable Y measured by Y1-Y4

USEVARIABLES = X1 X2 X3 X4 M1_1 M1_2 M1_3 M1_4 M2_1 M2_2 M2_3 M2_4 M3_1 M3_2 M3_3 M3_4 Y1 Y2 Y3 Y4;

ANALYSIS:
TYPE = GENERAL;
ESTIMATOR = ML;
BOOTSTRAP = 10000;

! In model statement first state measurement model
! Then state structural model naming each path and intercept using parentheses

MODEL:
! Measurement model
X BY X1 X2 X3 X4;
M1 BY M1_1 M1_2 M1_3 M1_4;
M2 BY M2_1 M2_2 M2_3 M2_4;
M3 BY M3_1 M3_2 M3_3 M3_4;
Y BY Y1 Y2 Y3 Y4;

! Fit structural model and name parameters
Y ON M1 (b1);
Y ON M2 (b2);
Y ON M3 (b3);

Y ON X (cdash);   ! direct effect of X on Y

M1 ON X (a1);
M2 ON X (a2);
M3 ON X (a3);

M2 ON M1 (d1);
M3 ON M1 (d2);

! Use model constraint to calculate specific indirect paths and total indirect effect

MODEL CONSTRAINT:
NEW(a1b1 a2b2 a3b3 a1d1b2 a1d2b3 TOTALIND TOTAL);
a1b1 = a1*b1;   ! Specific indirect effect of X on Y via M1 only
a2b2 = a2*b2;   ! Specific indirect effect of X on Y via M2 only
a3b3 = a3*b3;   ! Specific indirect effect of X on Y via M3 only
a1d1b2 = a1*d1*b2;   ! Specific indirect effect of X on Y via M1 and M2
a1d2b3 = a1*d2*b3;   ! Specific indirect effect of X on Y via M1 and M3
TOTALIND = a1b1 + a2b2 + a3b3 + a1d1b2 + a1d2b3;   ! Total indirect effect of X on Y via M1, M2, M3
TOTAL = a1b1 + a2b2 + a3b3 + a1d1b2 + a1d2b3 + cdash;   ! Total effect of X on Y

OUTPUT:
STAND CINT(bcbootstrap);

Editing required for testing indirect effect using alternative MODEL INDIRECT: subcommand

MODEL INDIRECT: offers an alternative to MODEL CONSTRAINT: for models containing indirect effects, where these are not moderated. To instead use MODEL INDIRECT: to test this model, you would edit the code above as follows:

First, you can remove the naming of parameters using parentheses in the MODEL: command, i.e. you just need:

MODEL:
Y ON X M1 M2 M3;
M1 ON X;
M2 ON M1 X;
M3 ON M1 X;

Second, replace the MODEL CONSTRAINT: subcommand with the following MODEL INDIRECT: subcommand:

MODEL INDIRECT:
Y IND X;

Leave the OUTPUT: command unchanged.