Mplus code for mediation, moderation, and moderated mediation models
This webpage contains links to Mplus code for testing different configurations of mediation, moderation and moderatedmediation models, including those corresponding in type to the 76 configurations listed and indexed by Andrew Hayes in the documentation for his SPSS PROCESS macro and seminal text Introduction to Mediation, Moderation, and Conditional Process Analysis (Hayes, 2013), which I strongly recommend reading as a primer before undertaking such analyses.
Note that Mplus, whilst obviously requiring extra labour and skills, provides a completely flexible modelling environment that enables you to test unlimited configurations of moderation and mediation, not just the 76 PROCESS model types. Specifically, PROCESS is limited to a single primary IV and DV, continuous mediators, either parallel or serial mediators but not both, and dichotomous or continuous outcomes, where the assumptions of standard multiple regression/logistic regression as appropriate are satisfied. Mplus, on the other hand, can handle all of these scenarios, and can also handle models with multiple IVs and DVs, mediators in serial or in parallel, and models with nonnormal DVs. Also, unlike PROCESS, it can handle mediation and moderation where the data structure is multilevel, and can incorporate latent variables.
Therefore, in recognition of this:
 Further types of models that can't (yet!) be fitted by PROCESS have been added (models 77 onwards), such as mediation with both serial and parallel mediators, moderated serial mediation, and moderated mediation with both serial and parallel mediators, and I will continue to add a selection of model types in the months and years ahead.
 For each model I have also produced the code required for when constructs are measured by latent variables as
opposed to observed variables.
However producing examples of every modelling scenario that Mplus can handle would be a task of infinite length! For a few examples and some great papers regarding multilevel mediation modelling, see Kris Preacher's website. But my best advice is to try to understand the code I have provided
as well as copying it  that way you will soon learn how to adapt it to your own ends...
For each of the models I have provided diagrams, the model equations, and most relevantly, the Mplus code for the requisite DEFINE:, ANALYSIS:, MODEL:, and OUTPUT: principal commands, as well as a preceding USEVARIABLES: subcommand that lists my hypothetical variables. To apply these examples to your data you will need to write the DATA: and VARIABLE: commands and change my hypothetical variable names to match yours.
We have found that, compared to the PROCESS macro, the more complex models can take a considerable amount of time to run (upwards of an hour), especially when bootstrapping is used (we tested the models using a relatively powerful laptop with an 8i processor). Mplus also struggles to fit models (i.e. you get convergence failures) where measures are on scales with high variance  where this is the case, rescaling predictors e.g. standardising them usually solves the problem.
This code and guidance to mediation and moderation testing is designed for people with some basic previous
knowledge of Mplus. Before trying to use this code you need beginner's Mplus skills, specifically to know how to read your data into Mplus, how missing data is coded and treated, how models are estimated, how different outcome distributions are specified, how the BY, ON, WITH, and XWITH statements, and the @ and () symbols work, and how MODEL CONSTRAINT: enables functions of parameters to be tested.
To that end (shameless plug time!) I run occasional public courses on the basics of Mplus and on testing mediation, moderation and moderatedmediation models using Mplus or SPSS, and also offer these on an inhouse basis  though there are one or two good books on Mplus (I recommend Christian Geiser's 'Data Analysis with Mplus'), and a few other course providers run similar intro courses. Note also that from
July 2016, statmodel.com (the programmers of Mplus) will be selling a book outlining the Mplus coding for various regression scenarios, including mediation models.
Anyway, I hope that you find this code helpful. Corrections to any code errors are very much welcomed. Best of luck with your analyses.
These pages were developed by Dr Chris Stride, with assistance from Sarah Gardner (programming/checking), Nick Catley (equation expansion/checking) and Ffion Thomas (diagram drawing, based on the original PROCESS diagrams by Andrew Hayes). To cite this page and/or any code used, please use:
Stride C.B., Gardner S., Catley. N. & Thomas, F.(2015) 'Mplus code for mediation, moderation, and moderated mediation models', http://www.offbeat.group.shef.ac.uk/FIO/mplusmedmod.htm
Model Template Selection
The model templates 176 linked to below match the equivalently numbered models associated with Andrew Hayes’ PROCESS macro, though with two adaptations:

First, where Andrew Hayes’ templates specify a model and equation generalised from 1 to multiple mediators, then, for the purposes of providing specific example code in Mplus that matches a diagram, my code and diagrams have been written for a model with 2 mediators in mediatoronly models (4 and 6) and 1 mediator in moderated mediation models (though the code could, of course, be edited to extend to as many mediators as is desired).

Second, all my models and code exclude covariates  though of course these could easily be added by specifying them as predictors of the outcome and mediators by adding extra ON statements.
You can select the appropriate model to match the analysis you wish to perform by browsing the descriptions of the configurations in the Model Index table below and then clicking on the 'Link to code' column. Further model configurations (over and above the PROCESS 'set' of 76) that I have written code for are numbered from 77 onwards.
Model Index
All models have one primary IV and one DV. For the purposes of the calculation of indirect effects and conditional effects:
 The primary IV (variable X) is assumed to be continuous or dichotomous (or, for the latent variable model versions, a continuous factor)
 Moderators (variables W,V,Q,Z) are assumed to be continuous (or, for the latent variable model versions, continuous factors), though the only adaptation required to handle observed dichotomous moderators is in MODEL CONSTRAINT: and loop plot code  an example of how to do this is given for model 1 (simple moderation)
 Mediators (variable M, or M1, M2, etc. in the multiple mediator examples) are assumed to be continuous (or, for the latent variable model versions, continuous factors)
 The DV (variable Y) is assumed to be continuous (or, for the latent variable model versions, a continuous factor) and to satisfy the assumptions of standard multiple regression  an example of how to handle an observed dichotomous DV is given for model 1 (i.e. a moderated logistic regression) and
for model 4 (i.e. an indirect effect in a logistic regression).
If you would rather look through all the models at once, or need to work offline, then rather than using the index
and individual files below, you can instead download the index and files as a pdf file (large!).
Model number 
Arrangement of mediators and moderators 
Number of distinct mediators 
Number of distinct moderators 
Code for observed variable example 
Code for latent variable example 
1a 
1 moderator [BASIC MODERATION], continuous moderator 
0 
1 
Mplus code 
Mplus code 
1b 
1 moderator [BASIC MODERATION], dichotomous moderator 
0 
1 
Mplus code 
Coming soon 
1c 
1 moderator [BASIC MODERATION], dichotomous moderator (using multigroup method) 
0 
1 
Mplus code 
Coming soon 
1d 
1 moderator [BASIC MODERATION], categorical moderator with > 2 categories 
0 
1 
Mplus code 
Coming soon 
1e 
1 moderator [BASIC MODERATION], dichotomous outcome (logistic regression) 
0 
1 
Mplus code 
Coming soon 
2 
2 moderators, 2way interactions with predictor only 
0 
2 
Mplus code 
Mplus code 
3 
2 moderators, all 2way and 3way interactions 
0 
2 
Mplus code 
Mplus code 
4a 
1 mediator [BASIC MEDIATION] 
1 
0 
Mplus code 
Mplus code 
4b 
2 mediators in parallel [BASIC MEDIATION] 
1+ 
0 
Mplus code 
Mplus code 
4c 
1 or more mediators, in parallel if multiple (example uses 1) [BASIC MEDIATION], dichotomous mediator 
1+ 
0 
Mplus code 
Coming soon 
4d 
1 or more mediators, in parallel if multiple (example uses 1) [BASIC MEDIATION], dichotomous outcome 
1+ 
0 
Mplus code 
Coming soon 
5 
1 or more mediators, in parallel if multiple, 1 moderator of direct IVDV path only 
1+ 
1 
Mplus code 
Mplus code 
6 
2 or more mediators, in series (example uses 2) 
2+ 
0 
Mplus code 
Mplus code 
7 
1 or more mediators, in parallel if multiple (example uses 1), 1 moderator of IVMediator path only 
1+ 
1 
Mplus code 
Mplus code 
8 
1 or more mediators, in parallel if multiple (example uses 1), 1 moderator moderating both the IVMediator
path and direct IVDV path 
1+ 
1 
Mplus code 
Mplus code 
9 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators both moderating the IVMediator
path only 
1+ 
2 
Mplus code 
Mplus code 
10 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators both moderating both the IV
Mediator path and direct IVDV path 
1+ 
2 
Mplus code 
Mplus code 
11 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators both moderating the IVMediator
path only, all 2way and 3way interactions 
1+ 
2 
Mplus code 
Mplus code 
12 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators both moderating the IVMediator
path and direct IVDV path, all 2way and 3way interactions 
1+ 
2 
Mplus code 
Mplus code 
13 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating the IV
Mediator path, 3way interaction, 1 also moderating direct IVDV path 
1+ 
2 
Mplus code 
Mplus code 
14 
1 or more mediators, in parallel if multiple (example uses 1), 1 moderator of MediatorDV path only 
1+ 
1 
Mplus code 
Mplus code 
15 
1 or more mediators, in parallel if multiple (example uses 1), 1 moderator of both MediatorDV and direct
IVDV path 
1+ 
1 
Mplus code 
Mplus code 
16 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators both moderating the MediatorDV path only 
1+ 
2 
Mplus code 
Mplus code 
17 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators both moderating both the MediatorDV and direct IVDV path 
1+ 
2 
Mplus code 
Mplus code 
18 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators both moderating the MediatorDV path only, all 2way and 3way interactions 
1+ 
2 
Mplus code 
Mplus code 
19 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators both moderating the MediatorDV
path and direct IVDV path, all 2way and 3way interactions 
1+ 
2 
Mplus code 
Mplus code 
20 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating the Mediator
DV path, 3way interaction, 1 also moderating direct IVDV path 
1+ 
2 
Mplus code 
Mplus code 
21 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, 1 moderating the IVMediator path, 1 moderating the MediatorDV path 
1+ 
2 
Mplus code 
Mplus code 
22 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, 1 moderating the IVMediator path and direct IVDV path, 1 moderating the MediatorDV path 
1+ 
2 
Mplus code 
Mplus code 
23 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 2 moderating the IVMediator
path, 1 moderating the MediatorDV path 
1+ 
3 
Mplus code 
Mplus code 
24 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 2 moderating both the IV
Mediator path and direct IVDV path, 1 moderating the MediatorDV path 
1+ 
3 
Mplus code 
Mplus code 
25 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 2 moderating the IVMediator
path with all 2way and 3way interactions, 1 moderating the MediatorDV path 
1+ 
3 
Mplus code 
Coming soon 
26 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 2 moderating both the IV
Mediator path and direct IVDV path with all 2way and 3way interactions, 1 moderating the MediatorDV path 
1+ 
3 
Mplus code 
Coming soon 
27 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 2 moderating the IVMediator
path with all 2way and 3way interactions, one of which also moderates the direct IVDV path, 1 moderating the MediatorDV path 
1+ 
3 
Mplus code 
Coming soon 
28 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, 1 moderating the IVMediator
path, 1 moderating the MediatorDV path and direct IVDV path 
1+ 
2 
Mplus code 
Coming soon 
29 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, 1 moderating the IVMediator
path, 1 moderating the MediatorDV path, both moderating the direct IVDV path 
1+ 
2 
Mplus code 
Coming soon 
30 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 2 moderating the IVMediator
path, 1 moderating both the MediatorDV path and the direct IVDV path 
1+ 
3 
Mplus code 
Coming soon 
31 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 2 moderating both the IV
Mediator path and the direct IVDV path, 1 moderating both the MediatorDV path and the direct IVDV path 
1+ 
3 
Mplus code 
Coming soon 
32 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 2 moderating the IVMediator
path with all 2way and 3way interactions, 1 moderating both the MediatorDV path and the direct IVDV path 
1+ 
3 
Mplus code 
Coming soon 
33 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 2 moderating both the IV
Mediator path and the direct IVDV path with all 2way and 3way interactions, 1 moderating both the MediatorDV path and the direct
IVDV path 
1+ 
3 
Mplus code 
Coming soon 
34 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 2 moderating the IVMediator
path with all 2way and 3way interactions, one of which also moderates the direct IVDV path, 1 moderating the MediatorDV path and
the direct IVDV path 
1+ 
3 
Mplus code 
Coming soon 
35 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 1 moderating the IVMediator
path, 2 moderating the MediatorDV path 
1+ 
3 
Mplus code 
Coming soon 
36 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 1 moderating the IVMediator
path, 2 moderating both the MediatorDV path and the IVDV path 
1+ 
3 
Mplus code 
Coming soon 
37 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 1 moderating the IVMediator
path, 2 moderating the MediatorDV path with all 2way and 3way interactions 
1+ 
3 
Mplus code 
Coming soon 
38 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 1 moderating the IVMediator
path, 2 moderating both the MediatorDV path and the IVDV path, with all 2way and 3way interactions 
1+ 
3 
Mplus code 
Coming soon 
39 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 1 moderating the IVMediator
path, 2 moderating the MediatorDV path with all 2way and 3way interactions, 1 of which also moderates the direct IVDV path 
1+ 
3 
Mplus code 
Coming soon 
40 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 1 moderating both the IV
Mediator path and the direct IVDV path, 2 moderating the MediatorDV path 
1+ 
3 
Mplus code 
Coming soon 
41 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 1 moderating both the IV
Mediator path and the direct IVDV path, 2 moderating both the MediatorDV path and the direct IVDV path 
1+ 
3 
Mplus code 
Coming soon 
42 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 1 moderating both the IV
Mediator path and the direct IVDV path, 2 moderating the MediatorDV path all 2way and 3way interactions 
1+ 
3 
Mplus code 
Coming soon 
43 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 1 moderating both the IV
Mediator path and the direct IVDV path, 2 moderating both the MediatorDV path and the direct IVDV path with all 2way and 3way
interactions 
1+ 
3 
Mplus code 
Coming soon 
44 
1 or more mediators, in parallel if multiple (example uses 1), 3 moderators, 1 moderating both the IV
Mediator path and the direct IVDV path, 2 moderating the MediatorDV path with all 2way and 3way interactions, 1 of which also
moderates the direct IVDV path 
1+ 
3 
Mplus code 
Coming soon 
45 
1 or more mediators, in parallel if multiple (example uses 1), 4 moderators, 2 of which moderate the IV
Mediator path, with the other 2 moderating the MediatorDV path 
1+ 
4 
Mplus code 
Coming soon 
46 
1 or more mediators, in parallel if multiple (example uses 1), 4 moderators, 2 of which moderate the IV
Mediator path with all 2way and 3way interactions, with the other 2 moderating the MediatorDV path 
1+ 
4 
Mplus code 
Coming soon 
47 
1 or more mediators, in parallel if multiple (example uses 1), 4 moderators, 2 of which moderate the IV
Mediator path, with the other 2 moderating the MediatorDV path with all 2way and 3way interactions 
1+ 
4 
Mplus code 
Coming soon 
48 
1 or more mediators, in parallel if multiple (example uses 1), 4 moderators, 2 of which moderate the IV
Mediator path with all 2way and 3way interactions, with the other 2 moderating the MediatorDV path with all 2way and 3way
interactions 
1+ 
4 
Mplus code 
Coming soon 
49 
1 or more mediators, in parallel if multiple (example uses 1), 4 moderators, 2 of which moderate both the
IVMediator path and the direct IVDV path, with the other 2 moderating the MediatorDV path 
1+ 
4 
Mplus code 
Coming soon 
50 
1 or more mediators, in parallel if multiple (example uses 1), 4 moderators, 2 of which moderate the IV
Mediator path, with the other 2 moderating both the MediatorDV path and the direct IVDV path 
1+ 
4 
Mplus code 
Coming soon 
51 
1 or more mediators, in parallel if multiple (example uses 1), 4 moderators, 2 of which moderate both the
IVMediator path and the direct IVDV path with all 2way and 3way interactions, with the other 2 moderating the MediatorDV path 
1+ 
4 
Mplus code 
Coming soon 
52 
1 or more mediators, in parallel if multiple (example uses 1), 4 moderators, 2 of which moderate the IV
Mediator path with all 2way and 3way interactions, with the other 2 moderating both the MediatorDV path and the direct IVDV path 
1+ 
4 
Mplus code 
Coming soon 
53 
1 or more mediators, in parallel if multiple (example uses 1), 4 moderators, 2 of which moderate both the
IVMediator path and the direct IVDV path, with the other 2 moderating the MediatorDV path with all 2way and 3way interactions 
1+ 
4 
Mplus code 
Coming soon 
54 
1 or more mediators, in parallel if multiple (example uses 1), 4 moderators, 2 of which moderate the IV
Mediator path, with the other 2 moderating both the MediatorDV path and the direct IVDV path with all 2way and 3way interactions 
1+ 
4 
Mplus code 
Coming soon 
55 
1 or more mediators, in parallel if multiple (example uses 1), 4 moderators, 2 of which moderate both the
IVMediator path and the direct IVDV path with all 2way and 3way interactions, with the other 2 moderating the MediatorDV path
with all 2way and 3way interactions 
1+ 
4 
Mplus code 
Coming soon 
56 
1 or more mediators, in parallel if multiple (example uses 1), 4 moderators, 2 of which moderate the IV
Mediator path with all 2way and 3way interactions, with the other 2 moderating both the MediatorDV path and the direct IVDV path
with all 2way and 3way interactions 
1+ 
4 
Mplus code 
Coming soon 
57 
1 or more mediators, in parallel if multiple (example uses 1), 4 moderators, 2 of which moderate both the
IVMediator path and the direct IVDV path with all 2way and 3way interactions, with the other 2 moderating both the MediatorDV
path and the direct IVDV path with all 2way and 3way interactions 
1+ 
4 
Mplus code 
Coming soon 
58 
1 or more mediators, in parallel if multiple (example uses 1), 1 moderators, which moderates both the IV
Mediator path and the MediatorDV path 
1+ 
1 
Mplus code 
Coming soon 
59 
1 or more mediators, in parallel if multiple (example uses 1), 1 moderators, which moderates all of the
IVMediator path, the MediatorDV path and the direct IVDV path 
1+ 
1 
Mplus code 
Coming soon 
60 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating both the IV
Mediator path, 1 of which also moderates the MediatorDV path 
1+ 
2 
Mplus code 
Coming soon 
61 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating both the IV
Mediator path, 1 of which also moderates both the MediatorDV path and the direct IVDV path 
1+ 
2 
Mplus code 
Coming soon 
62 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating both the IV
Mediator path, 1 of which also moderates the MediatorDV path, the with the other moderating the direct IVDV path 
1+ 
2 
Mplus code 
Coming soon 
63 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating both the IV
Mediator path and the direct IVDV path, 1 of which also moderates the MediatorDV path 
1+ 
2 
Mplus code 
Coming soon 
64 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating both the
MediatorDV path, 1 of which also moderates the IVMediator path 
1+ 
2 
Mplus code 
Coming soon 
65 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating both the
MediatorDV path, 1 of which also moderates both the IVMediator path and the direct IVDV path 
1+ 
2 
Mplus code 
Coming soon 
66 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating both the
MediatorDV path, 1 of which also moderates the IVMediator path, the with the other moderating the direct IVDV path 
1+ 
2 
Mplus code 
Coming soon 
67 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating both the
MediatorDV path and the direct IVDV path, 1 of which also moderates the IVMediator path 
1+ 
2 
Mplus code 
Coming soon 
68 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating the IV
Mediator path with all 2way and 3way interactions, 1 of which also moderates the MediatorDV path 
1+ 
2 
Mplus code 
Coming soon 
69 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating both of the
IVMediator path and the direct IVDV path, with all 2way and 3way interactions, 1 of which also moderates the MediatorDV path 
1+ 
2 
Mplus code 
Coming soon 
70 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating the Mediator
DV path with all 2way and 3way interactions, 1 of which also moderates the IVMediator path 
1+ 
2 
Mplus code 
Coming soon 
71 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating both of the
MediatorDV path and the direct IVDV path, with all 2way and 3way interactions, 1 of which also moderates the IVMediator path 
1+ 
2 
Mplus code 
Coming soon 
72 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating both the IV
Mediator path and the MediatorDV path, with all 2way and 3way interactions 
1+ 
2 
Mplus code 
Coming soon 
73 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating each of the
IVMediator path, the MediatorDV path and the direct IVDV path, with all 2way and 3way interactions 
1+ 
2 
Mplus code 
Coming soon 
74 
1 or more mediators, in parallel if multiple (example uses 1), IV also moderates the MediatorDV path 
1+ 
(1) 
Mplus code 
Coming soon 
75 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating both the IV
Mediator path and the MediatorDV path 
1+ 
2 
Mplus code 
Coming soon 
76 
1 or more mediators, in parallel if multiple (example uses 1), 2 moderators, both moderating each of the
IVMediator path, the MediatorDV path and the direct IVDV path 
1+ 
2 
Mplus code 
Coming soon 
77 
3 or more mediators, both in parallel and in series 
3+ 
0 
Mplus code 
Coming soon 
78 
2 or more mediators, in series, 1 moderator moderating path between mediators 
2+ 
1 
Mplus code 
Coming soon 
79 
3 or more mediators, both in parallel and in series, 2 moderators, 1 moderating paths between predictor and mediator, the second moderating paths between mediators, and between mediator and DV 
3+ 
2 
Mplus code 
Coming soon 
80 
1 mediator, predictor has nonlinear effect on mediator and outcome 
1 
0 
Mplus code 
Coming soon 
