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

Model 1: 1 moderator [BASIC MODERATION]

Example Variables: 1 predictor X, 1 moderator W, 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 for this model after the main code.
  • Any mediators (variable M, or M1, M2, etc.) are assumed to be continuous
  • The DV (variable Y) is assumed to be continuous and to satisfy the assumptions of standard multiple regression - an example of how to handle a dichotomous DV is given for this model at the bottom of the code (i.e. a moderated logistic regression) and for model 4 (i.e. an indirect effect in a logistic regression).

 

Model Diagram:

 

Statistical Diagram:

 

Model Equation(s):

Y = b0 + b1X + b2W + b3XW

 

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

Y = b0 + b1X + b2W + b3XW


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

Y = (b0 + b2W) + (b1 + b3W)X


Hence...

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

b1 + b3W

 

Mplus code for the model:

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

USEVARIABLES = X W Y XW;

! Create interaction term
! Note that it has to be placed at end of USEVARIABLES subcommand above

DEFINE:
   XW = X*W;

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 XW (b3);

! 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

MODEL CONSTRAINT:
   NEW(LOW_W MED_W HIGH_W SIMP_LO SIMP_MED SIMP_HI);

   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

! Now calc simple slopes for each value of W

   SIMP_LO = b1 + b3*LOW_W;
   SIMP_MED = b1 + b3*MED_W;
   SIMP_HI = b1 + b3*HIGH_W;

! Use loop plot to plot model for low, med, high values of W
! 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(LOMOD MEDMOD HIMOD);
   LOOP(XVAL,1,5,0.1);
   LOMOD = (b0 + b2*LOW_W) + (b1 + b3*LOW_W)*XVAL;
   MEDMOD = (b0 + b2*MED_W) + (b1 + b3*MED_W)*XVAL;
   HIMOD = (b0 + b2*HIGH_W) + (b1 + b3*HIGH_W)*XVAL;

PLOT:
   TYPE = plot2;

OUTPUT:
   STAND CINT(bcbootstrap);

 

Editing required for dichotomous moderator scenario

First, to make life easier when interpreting results, make sure your dichotomous moderator is coded with values of 0 and 1.

The model constraint and loop plotting now needs to be edited to reflect that fact that, rather than wanting to test and plot slopes at low, medium and high values of your moderator, you simply want to test and plot slopes at values of 0 and 1 for your moderator. Hence you need just two equations for each of the simple slopes and plots sections. Thinking about this in terms of equations:

Y = (b0 + b2W) + (b1 + b3W)X

so plugging in values of 0 and 1 for moderator W gives....

W = 0, Y = b0 + b1X; W = 1, Y = (b0 + b2) + (b1 + b3)X

The code edits we need are...

MODEL CONSTRAINT:
   NEW(LOW_W HIGH_W SIMP_LO SIMP_HI);

   LOW_W = 0;
   HIGH_W = 1;

! Now calc simple slopes for each value of W

   SIMP_LO = b1 + b3*LOW_W;
   SIMP_HI = b1 + b3*HIGH_W;

! Use loop plot to plot model for low, med, high values of W
! 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(LOMOD HIMOD);
   LOOP(XVAL,1,5,0.1);
   LOMOD = (b0 + b2*LOW_W) + (b1 + b3*LOW_W)*XVAL;
   HIMOD = (b0 + b2*HIGH_W) + (b1 + b3*HIGH_W)*XVAL;

PLOT:
   TYPE = plot2;

OUTPUT:
   STAND CINT(bcbootstrap);

If you are feeling really confident you could simplify this to:

MODEL CONSTRAINT:
   NEW(SIM_MOD0 SIM_MOD1);
   SIM_MOD0 = b1;
   SIM_MOD1 = b1 + b3;

! Use loop plot to plot model for values of W = 0, W = 1
! 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(MOD0 MOD1);

   LOOP(XVAL,1,5,0.1);

   MOD0 = b0 + b1*XVAL;
   MOD1 = (b0 + b2) + (b1 + b3)*XVAL;

PLOT:
   TYPE = plot2;

OUTPUT:
   STAND CINT(bcbootstrap);

 

Equations and code for the dichotomous DV scenario (moderated logistic regression)

 

Model Equation(s):

logit(Y) = b0 + b1X + b2W + b3XW

 

Albegra to calculate indirect and/or conditional effects by writing model as logit(Y) = a + bX:

logit(Y) = b0 + b1X + b2W + b3XW


Hence... grouping terms into form logit(Y) = a + bX

logit(Y) = (b0 + b2W) + (b1 + b3W)X


Hence...

One direct effect of X on logit(Y), conditional on W:

b1 + b3W


Hence, writing as an odds ratio...

The multiplicative effect of X on the odds of Y, conditional on W:

exp(b1 + b3W) = exp(b1)*exp(b3W)

 

Mplus code for the model:

! Predictor variable - X
! Mediator variable(s) (not applicable)
! Moderator variable(s) - W
! Outcome variable - Y - a dichotomous outcome, coded 0/1

USEVARIABLES = X W Y XW;

CATEGORICAL = Y;

! Create interaction term
! Note that it has to be placed at end of USEVARIABLES subcommand above

DEFINE:
   XW = X*W;

ANALYSIS:
   TYPE = GENERAL;
   ESTIMATOR = ML;

! In model statement name each path and intercept using parentheses

MODEL:
   [Y$1] (b0);
   Y ON X (b1);
   Y ON W (b2);
   Y ON XW (b3);

! 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

MODEL CONSTRAINT:
   NEW(LOW_W MED_W HIGH_W OR_LO OR_MED OR_HI);

   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

! Now calc conditional odds ratios for each value of W

   OR_LO = exp(b1 + b3*LOW_W);
   OR_MED = exp(b1 + b3*MED_W);
   OR_HI = exp(b1 + b3*HIGH_W);

! Use loop plot to plot predicted probabilities by X
! conditional on low, med, high values of W

! 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(PLOMOD PMEDMOD PHIMOD);
   LOOP(XVAL,1,5,0.1);
   PLOMOD = 1/(1 + exp(-1*((b0 + b2*LOW_W) + (b1 + b3*LOW_W)*XVAL)));
   PMEDMOD = 1/(1 + exp(-1*((b0 + b2*MED_W) + (b1 + b3*MED_W)*XVAL)));
   PHIMOD = 1/(1 + exp(-1*((b0 + b2*HIGH_W) + (b1 + b3*HIGH_W)*XVAL)));

PLOT:
   TYPE = plot2;

OUTPUT:
   STAND;

 

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