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

Model 4c: 1 or more mediators, in parallel if multiple (example uses 1) [BASIC MEDIATION], dichotomous mediator

Example Variables: 1 predictor X, 1 mediator M, 1 outcome Y

Preliminary notes:

The code below assumes that

  • The primary IV (variable X) is continuous or dichotomous
  • The mediator (variable M) is dichotomous.
  • 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 + b1M + c'X
logit(M) = a0 + a1X


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

Y = b0 + b1M + c'X
logit(M) = a0 + a1X

The problem we now have is that we are working on different scales across the two equations. Mplus doesn't automatically acknowledge this (the MODEL INDIRECT command does not yet work in the dichtomous mediator scenario) and adjust for us. So we have to program the method of McKinnon & Dwyer (1993), which adjusts our regression coefficients so that they are on the same scale, to enable the standard decomposition of the total effect into direct and indirect effects i.e. a1*b1 + cdash, to hold. For those who haven't got access to this paper, the maths is kindly outlined by Nathaniel Kerr...


adjusted a1 = a1*SD(X)/(sqrt(a1*a1*Var(X) + pi_squared/3))
adjusted b1 = b1*SD(M)/(sqrt(cdash*cdash*Var(X) + b1*b1*Var(M) + 2*b1*cdash*Cov(X,M)))

Hence, when using Mplus, we first need to calculate the sample standard deviations/variances of X and M, and their covariance. This is most easily done by placing these variables into a USEVARIABLES = command, leaving the model section blank, and adding SAMPSTAT; to the output options.

Having calculated those value, you can use the following code...


Mplus code for the model:

! Predictor variable - X
! Mediator variable(s) M
! Moderator variable(s) - none
! Outcome variable - Y




! In model statement name each path using parentheses

   Y ON M (b1);

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

   M ON X (a1);

! Use model constraint to calculate adjusted indirect effect
! Need to precalculate sample SDs and Covar of X and M, and input as #'d values below

   NEW(SDX SDM COVXM pi adja1 adjb1 adjind);

   SDX = #SDX;
   SDM = #SDM;
   pi = 3.141592653589793;

   adja1 = a1*SDX/(sqrt((a1*a1*SDX*SDX) + ((pi*pi)/3)));
   adjb1 = b1*SDM/(sqrt((cdash*cdash*SDX*SDX) + (b1*b1*SDM*SDM) +

   adjind = adja1*adjb1;

   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',

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