Mplus code for the mediation, moderation, and moderated mediation model templates from Andrew Hayes' PROCESS analysis examples

Model 2: 2 moderators, 2-way interactions with predictor only

Example Variables: 1 predictor X, 2 moderators W, Z, 1 outcome Y

Preliminary notes:

The code below assumes that

  • The primary IV (variable X) is continuous or dichotomous
  • Any moderators (variables W, V, Q, Z) are continuous, though the only adaptation required to handle dichotomous moderators is in the MODEL CONSTRAINT: and loop plot code - an example of how to do this is given in model 1b. Handling categorical moderators with > 2 categories is demonstrated in model 1d.
  • Any mediators (variable M, or M1, M2, etc.) are continuous and satisfy the assumptions of standard multiple regression. An example of how to handle a dichotomous mediator is given in model 4c.
  • The DV (variable Y) is continuous and satisfies the assumptions of standard multiple regression. An example of how to handle a dichotomous DV is given in model 1e (i.e. a moderated logistic regression) and in model 4d (i.e. an indirect effect in a logistic regression).

 

Model Diagram:

 

Statistical Diagram:

 

Model Equation(s):

Y = b0 + b1X + b2W + + b3Z + b4XW + b5XZ

 

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

Y = b0 + b1X + b2W + b3Z + b4XW + b5XZ


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

Y = (b0 + b2W + b3Z) + (b1 + b4W + b5Z)X


Hence...

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

b1 + b4W + b5Z

 

Mplus code for the model:

! Predictor variable - X
! Mediator variable(s) (not applicable)
! Moderator variable(s) - W, Z
! Outcome variable - Y

USEVARIABLES = X W Y XW XZ;

! Create interaction terms
! Note as new vars they have to be placed at end of USEVARIABLES subcommand above

DEFINE:
   XW = X*W;
   XZ = X*Z;

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

! In model statement name each path, and intercept, using parentheses

MODEL:
   [Y] (b0);
   Y ON X (b1);
   Y ON W (b2);
   Y ON Z (b3);
   Y ON XW (b4);
   Y ON XZ (b5);

! Use model constraint subcommand to test simple slopes
! You need to pick low, medium and high moderator values for both W and Z,
! for example, of 1 SD below mean, mean, 1 SD above mean

! 2 moderators, 3 values for each, gives 9 combinations
! arbitrary naming convention for simple slopes used below:
! MEW_LOZ = medium value of W and low value of Z, etc.

MODEL CONSTRAINT:
   NEW(LOW_W MED_W HIGH_W LOW_Z MED_Z HIGH_Z LOW_LOZ MEW_LOZ HIW_LOZ
   LOW_MEZ MEW_MEZ HIW_MEZ LOW_HIZ MEW_HIZ HIW_HIZ);

   LOW_W = #LOWW;   ! replace #LOWW in the code with your chosen low value of W
   MED_W = #MEDW;   ! replace #MEDW in the code with your chosen medium value of W
   HIGH_W = #HIGHW;   ! replace #HIGHW in the code with your chosen high value of W

   LOW_Z = #LOWZ;   ! replace #LOWZ in the code with your chosen low value of Z
   MED_Z = #MEDZ;   ! replace #MEDZ in the code with your chosen medium value of Z
   HIGH_Z = #HIGHZ;   ! replace #HIGHZ in the code with your chosen high value of Z

! Now calc simple slopes for each value of W and Z

   LOW_LOZ = b1 + b4*LOW_W + b5*LOW_Z;
   MEW_LOZ = b1 + b4*MED_W + b5*LOW_Z;
   HIW_LOZ = b1 + b4*HIGH_W + b5*LOW_Z;

   LOW_MEZ = b1 + b4*LOW_W + b5*MED_Z;
   MEW_MEZ = b1 + b4*MED_W + b5*MED_Z;
   HIW_MEZ = b1 + b4*HIGH_W + b5*MED_Z;

   LOW_HIZ = b1 + b4*LOW_W + b5*HIGH_Z;
   MEW_HIZ = b1 + b4*MED_W + b5*HIGH_Z;
   HIW_HIZ = b1 + b4*HIGH_W + b5*HIGH_Z;

! Use loop plot to plot model for all combinations of low, med, high values of W and Z
! NOTE - values of 1,5 in LOOP() statement need to be replaced by
! logical min and max limits of predictor X used in analysis

   PLOT(PLOW_LOZ PMEW_LOZ PHIW_LOZ PLOW_MEZ PMEW_MEZ PHIW_MEZ
   PLOW_HIZ PMEW_HIZ PHIW_HIZ);

   LOOP(XVAL,1,5,0.1);

   PLOW_LOZ = (b0 + b2*LOW_W + b3*LOW_Z) + LOW_LOZ*XVAL;
   PMEW_LOZ = (b0 + b2*MED_W + b3*LOW_Z) + MEW_LOZ*XVAL;
   PHIW_LOZ = (b0 + b2*HIGH_W + b3*LOW_Z) + HIW_LOZ*XVAL;

   PLOW_MEZ = (b0 + b2*LOW_W + b3*MED_Z) + LOW_MEZ*XVAL;
   PMEW_MEZ = (b0 + b2*MED_W + b3*MED_Z) + MEW_MEZ*XVAL;
   PHIW_MEZ = (b0 + b2*HIGH_W + b3*MED_Z) + HIW_MEZ*XVAL;

   PLOW_HIZ = (b0 + b2*LOW_W + b3*HIGH_Z) + LOW_HIZ*XVAL;
   PMEW_HIZ = (b0 + b2*MED_W + b3*HIGH_Z) + MEW_HIZ*XVAL;
   PHIW_HIZ = (b0 + b2*HIGH_W + b3*HIGH_Z) + HIW_HIZ*XVAL;

PLOT:
   TYPE = plot2;

OUTPUT:
   STAND CINT(bcbootstrap);

 

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