Mplus code for mediation, moderation, and moderated mediation models

Model 49: 1 or more mediators, in parallel if multiple (example uses 1), 4 moderators, 2 of which moderate both the IV-Mediator path and the direct IV-DV path, with the other 2 moderating the Mediator-DV path

Example Variables: 1 predictor X, 1 mediator M, 4 moderators W, Z, V, Q, 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 + b1M + b2V + b3Q + b4MV + b5MQ + c1'X + c2'W + c3'Z + c4'XW + c5'XZ
M = a0 + a1X + a2W + a3Z + a4XW + a5XZ

 

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

Y = b0 + b1M + b2V + b3Q + b4MV + b5MQ + c1'X + c2'W + c3'Z + c4'XW + c5'XZ
M = a0 + a1X + a2W + a3Z + a4XW + a5XZ


Hence... substituting in equation for M

Y = b0 + b1(a0 + a1X + a2W + a3Z + a4XW + a5XZ) + b2V + b3Q + b4(a0 + a1X + a2W + a3Z + a4XW + a5XZ)V + b5(a0 + a1X + a2W + a3Z + a4XW + a5XZ)Q + c1'X + c2'W + c3'Z + c4'XW + c5'XZ


Hence... multiplying out brackets

Y = b0 + a0b1 + a1b1X + a2b1W + a3b1Z + a4b1XW + a5b1XZ + b2V + b3Q + a0b4V + a1b4XV + a2b4WV + a3b4ZV + a4b4XWV + a5b4XZV + a0b5Q + a1b5XQ + a2b5WQ + a3b5ZQ + a4b5XWQ + a5b5XZQ + c1'X + c2'W + c3'Z + c4'XW + c5'XZ


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

Y = (b0 + a0b1 + a2b1W + a3b1Z + b2V + b3Q + a0b4V + a2b4WV + a3b4ZV + a0b5Q + a2b5WQ + a3b5ZQ + c2'W + c3'Z) + (a1b1 + a4b1W + a5b1Z + a1b4V + a4b4WV + a5b4ZV + a1b5Q + a4b5WQ + a5b5ZQ + c1' + c4'W + c5'Z)X


Hence...

One indirect effect(s) of X on Y, conditional on W, Z, V, Q:

a1b1 + a4b1W + a5b1Z + a1b4V + a4b4WV + a5b4ZV + a1b5Q + a4b5WQ + a5b5ZQ = (a1 + a4W + a5Z)(b1 + b4V + b5Q)

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

c1' + c4'W + c5'Z

 

Mplus code for the model:

! Predictor variable - X
! Mediator variable(s) – M
! Moderator variable(s) – W, Z, V, Q
! Outcome variable - Y

USEVARIABLES = X M W Z V Q Y XW XZ MV MQ;

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

DEFINE:
   MQ = M*Q;
   MV = M*V;
   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 M (b1);
   Y ON V (b2);
   Y ON Q (b3);
   Y ON MV (b4);
   Y ON MQ (b5);

   Y ON X (cdash1);
   Y ON W (cdash2);
   Y ON Z (cdash3);
   Y ON XW (cdash4);
   Y ON XZ (cdash5);

   [M] (a0);
   M ON X (a1);
   M ON W (a2);
   M ON Z (a3);
   M ON XW (a4);
   M ON XZ (a5);

! Use model constraint subcommand to test conditional indirect effects
! You need to pick low, medium and high moderator values for W, Z, V, Q
! for example, of 1 SD below mean, mean, 1 SD above mean

! 4 moderators, 3 values for each, gives 81 combinations
! arbitrary naming convention for conditional indirect and total effects used below:
! HHML = high value of W, high value of Z, medium value of V and low value of Q.

MODEL CONSTRAINT:
    NEW(LOW_W MED_W HIGH_W LOW_Z MED_Z HIGH_Z LOW_V MED_V HIGH_V LOW_Q MED_Q HIGH_Q
    ILLLL IMLLL IHLLL ILMLL IMMLL IHMLL ILHLL IMHLL IHHLL
    ILLML IMLML IHLML ILMML IMMML IHMML ILHML IMHML IHHML
    ILLHL IMLHL IHLHL ILMHL IMMHL IHMHL ILHHL IMHHL IHHHL
    ILLLM IMLLM IHLLM ILMLM IMMLM IHMLM ILHLM IMHLM IHHLM
    ILLMM IMLMM IHLMM ILMMM IMMMM IHMMM ILHMM IMHMM IHHMM
    ILLHM IMLHM IHLHM ILMHM IMMHM IHMHM ILHHM IMHHM IHHHM
    ILLLH IMLLH IHLLH ILMLH IMMLH IHMLH ILHLH IMHLH IHHLH
    ILLMH IMLMH IHLMH ILMMH IMMMH IHMMH ILHMH IMHMH IHHMH
    ILLHH IMLHH IHLHH ILMHH IMMHH IHMHH ILHHH IMHHH IHHHH
    DLOW_LOZ DMEW_LOZ DHIW_LOZ DLOW_MEZ DMEW_MEZ DHIW_MEZ
    DLOW_HIZ DMEW_HIZ DHIW_HIZ
    TLLLL TMLLL THLLL TLMLL TMMLL THMLL TLHLL TMHLL THHLL
    TLLML TMLML THLML TLMML TMMML THMML TLHML TMHML THHML
    TLLHL TMLHL THLHL TLMHL TMMHL THMHL TLHHL TMHHL THHHL
    TLLLM TMLLM THLLM TLMLM TMMLM THMLM TLHLM TMHLM THHLM
    TLLMM TMLMM THLMM TLMMM TMMMM THMMM TLHMM TMHMM THHMM
    TLLHM TMLHM THLHM TLMHM TMMHM THMHM TLHHM TMHHM THHHM
    TLLLH TMLLH THLLH TLMLH TMMLH THMLH TLHLH TMHLH THHLH
    TLLMH TMLMH THLMH TLMMH TMMMH THMMH TLHMH TMHMH THHMH
    TLLHH TMLHH THLHH TLMHH TMMHH THMHH TLHHH TMHHH THHHH);

    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

    LOW_V = #LOWV;   ! replace #LOWV in the code with your chosen low value of V
    MED_V = #MEDV;   ! replace #MEDV in the code with your chosen medium value of V
    HIGH_V = #HIGHV;   ! replace #HIGHV in the code with your chosen high value of V

    LOW_Q = #LOWQ;   ! replace #LOWQ in the code with your chosen low value of Q
    MED_Q = #MEDQ;   ! replace #MEDQ in the code with your chosen medium value of Q
    HIGH_Q = #HIGHQ;   ! replace #HIGHQ in the code with your chosen high value of Q

! Calc conditional indirect effects for each combination of moderator values

    ILLLL = a1*b1 + a4*b1*LOW_W + a5*b1*LOW_Z + a1*b4*LOW_V +
     a4*b4*LOW_W*LOW_V + a5*b4*LOW_Z*LOW_V + a1*b5*LOW_Q +
     a4*b5*LOW_W*LOW_Q + a5*b5*LOW_Z*LOW_Q;
    IMLLL = a1*b1 + a4*b1*MED_W + a5*b1*LOW_Z + a1*b4*LOW_V +
     a4*b4*MED_W*LOW_V + a5*b4*LOW_Z*LOW_V + a1*b5*LOW_Q +
     a4*b5*MED_W*LOW_Q + a5*b5*LOW_Z*LOW_Q;
    IHLLL = a1*b1 + a4*b1*HIGH_W + a5*b1*LOW_Z + a1*b4*LOW_V +
     a4*b4*HIGH_W*LOW_V + a5*b4*LOW_Z*LOW_V + a1*b5*LOW_Q +
     a4*b5*HIGH_W*LOW_Q + a5*b5*LOW_Z*LOW_Q;

    ILMLL = a1*b1 + a4*b1*LOW_W + a5*b1*MED_Z + a1*b4*LOW_V +
     a4*b4*LOW_W*LOW_V + a5*b4*MED_Z*LOW_V + a1*b5*LOW_Q +
     a4*b5*LOW_W*LOW_Q + a5*b5*MED_Z*LOW_Q;
    IMMLL = a1*b1 + a4*b1*MED_W + a5*b1*MED_Z + a1*b4*LOW_V +
     a4*b4*MED_W*LOW_V + a5*b4*MED_Z*LOW_V + a1*b5*LOW_Q +
     a4*b5*MED_W*LOW_Q + a5*b5*MED_Z*LOW_Q;
    IHMLL = a1*b1 + a4*b1*HIGH_W + a5*b1*MED_Z + a1*b4*LOW_V +
     a4*b4*HIGH_W*LOW_V + a5*b4*MED_Z*LOW_V + a1*b5*LOW_Q +
     a4*b5*HIGH_W*LOW_Q + a5*b5*MED_Z*LOW_Q;

    ILHLL = a1*b1 + a4*b1*LOW_W + a5*b1*HIGH_Z + a1*b4*LOW_V +
     a4*b4*LOW_W*LOW_V + a5*b4*HIGH_Z*LOW_V + a1*b5*LOW_Q +
     a4*b5*LOW_W*LOW_Q + a5*b5*HIGH_Z*LOW_Q;
    IMHLL = a1*b1 + a4*b1*MED_W + a5*b1*HIGH_Z + a1*b4*LOW_V +
     a4*b4*MED_W*LOW_V + a5*b4*HIGH_Z*LOW_V + a1*b5*LOW_Q +
     a4*b5*MED_W*LOW_Q + a5*b5*HIGH_Z*LOW_Q;
    IHHLL = a1*b1 + a4*b1*HIGH_W + a5*b1*HIGH_Z + a1*b4*LOW_V +
     a4*b4*HIGH_W*LOW_V + a5*b4*HIGH_Z*LOW_V + a1*b5*LOW_Q +
     a4*b5*HIGH_W*LOW_Q + a5*b5*HIGH_Z*LOW_Q;

    ILLML = a1*b1 + a4*b1*LOW_W + a5*b1*LOW_Z + a1*b4*MED_V +
     a4*b4*LOW_W*MED_V + a5*b4*LOW_Z*MED_V + a1*b5*LOW_Q +
     a4*b5*LOW_W*LOW_Q + a5*b5*LOW_Z*LOW_Q;
    IMLML = a1*b1 + a4*b1*MED_W + a5*b1*LOW_Z + a1*b4*MED_V +
     a4*b4*MED_W*MED_V + a5*b4*LOW_Z*MED_V + a1*b5*LOW_Q +
     a4*b5*MED_W*LOW_Q + a5*b5*LOW_Z*LOW_Q;
    IHLML = a1*b1 + a4*b1*HIGH_W + a5*b1*LOW_Z + a1*b4*MED_V +
     a4*b4*HIGH_W*MED_V + a5*b4*LOW_Z*MED_V + a1*b5*LOW_Q +
     a4*b5*HIGH_W*LOW_Q + a5*b5*LOW_Z*LOW_Q;

    ILMML = a1*b1 + a4*b1*LOW_W + a5*b1*MED_Z + a1*b4*MED_V +
     a4*b4*LOW_W*MED_V + a5*b4*MED_Z*MED_V + a1*b5*LOW_Q +
     a4*b5*LOW_W*LOW_Q + a5*b5*MED_Z*LOW_Q;
    IMMML = a1*b1 + a4*b1*MED_W + a5*b1*MED_Z + a1*b4*MED_V +
     a4*b4*MED_W*MED_V + a5*b4*MED_Z*MED_V + a1*b5*LOW_Q +
     a4*b5*MED_W*LOW_Q + a5*b5*MED_Z*LOW_Q;
    IHMML = a1*b1 + a4*b1*HIGH_W + a5*b1*MED_Z + a1*b4*MED_V +
     a4*b4*HIGH_W*MED_V + a5*b4*MED_Z*MED_V + a1*b5*LOW_Q +
     a4*b5*HIGH_W*LOW_Q + a5*b5*MED_Z*LOW_Q;

    ILHML = a1*b1 + a4*b1*LOW_W + a5*b1*HIGH_Z + a1*b4*MED_V +
     a4*b4*LOW_W*MED_V + a5*b4*HIGH_Z*MED_V + a1*b5*LOW_Q +
     a4*b5*LOW_W*LOW_Q + a5*b5*HIGH_Z*LOW_Q;
    IMHML = a1*b1 + a4*b1*MED_W + a5*b1*HIGH_Z + a1*b4*MED_V +
     a4*b4*MED_W*MED_V + a5*b4*HIGH_Z*MED_V + a1*b5*LOW_Q +
     a4*b5*MED_W*LOW_Q + a5*b5*HIGH_Z*LOW_Q;
    IHHML = a1*b1 + a4*b1*HIGH_W + a5*b1*HIGH_Z + a1*b4*MED_V +
     a4*b4*HIGH_W*MED_V + a5*b4*HIGH_Z*MED_V + a1*b5*LOW_Q +
     a4*b5*HIGH_W*LOW_Q + a5*b5*HIGH_Z*LOW_Q;

    ILLHL = a1*b1 + a4*b1*LOW_W + a5*b1*LOW_Z + a1*b4*HIGH_V +
     a4*b4*LOW_W*HIGH_V + a5*b4*LOW_Z*HIGH_V + a1*b5*LOW_Q +
     a4*b5*LOW_W*LOW_Q + a5*b5*LOW_Z*LOW_Q;
    IMLHL = a1*b1 + a4*b1*MED_W + a5*b1*LOW_Z + a1*b4*HIGH_V +
     a4*b4*MED_W*HIGH_V + a5*b4*LOW_Z*HIGH_V + a1*b5*LOW_Q +
     a4*b5*MED_W*LOW_Q + a5*b5*LOW_Z*LOW_Q;
    IHLHL = a1*b1 + a4*b1*HIGH_W + a5*b1*LOW_Z + a1*b4*HIGH_V +
     a4*b4*HIGH_W*HIGH_V + a5*b4*LOW_Z*HIGH_V + a1*b5*LOW_Q +
     a4*b5*HIGH_W*LOW_Q + a5*b5*LOW_Z*LOW_Q;

    ILMHL = a1*b1 + a4*b1*LOW_W + a5*b1*MED_Z + a1*b4*HIGH_V +
     a4*b4*LOW_W*HIGH_V + a5*b4*MED_Z*HIGH_V + a1*b5*LOW_Q +
     a4*b5*LOW_W*LOW_Q + a5*b5*MED_Z*LOW_Q;
    IMMHL = a1*b1 + a4*b1*MED_W + a5*b1*MED_Z + a1*b4*HIGH_V +
     a4*b4*MED_W*HIGH_V + a5*b4*MED_Z*HIGH_V + a1*b5*LOW_Q +
     a4*b5*MED_W*LOW_Q + a5*b5*MED_Z*LOW_Q;
    IHMHL = a1*b1 + a4*b1*HIGH_W + a5*b1*MED_Z + a1*b4*HIGH_V +
     a4*b4*HIGH_W*HIGH_V + a5*b4*MED_Z*HIGH_V + a1*b5*LOW_Q +
     a4*b5*HIGH_W*LOW_Q + a5*b5*MED_Z*LOW_Q;

    ILHHL = a1*b1 + a4*b1*LOW_W + a5*b1*HIGH_Z + a1*b4*HIGH_V +
     a4*b4*LOW_W*HIGH_V + a5*b4*HIGH_Z*HIGH_V + a1*b5*LOW_Q +
     a4*b5*LOW_W*LOW_Q + a5*b5*HIGH_Z*LOW_Q;
    IMHHL = a1*b1 + a4*b1*MED_W + a5*b1*HIGH_Z + a1*b4*HIGH_V +
     a4*b4*MED_W*HIGH_V + a5*b4*HIGH_Z*HIGH_V + a1*b5*LOW_Q +
     a4*b5*MED_W*LOW_Q + a5*b5*HIGH_Z*LOW_Q;
    IHHHL = a1*b1 + a4*b1*HIGH_W + a5*b1*HIGH_Z + a1*b4*HIGH_V +
     a4*b4*HIGH_W*HIGH_V + a5*b4*HIGH_Z*HIGH_V + a1*b5*LOW_Q +
     a4*b5*HIGH_W*LOW_Q + a5*b5*HIGH_Z*LOW_Q;

    ILLLM = a1*b1 + a4*b1*LOW_W + a5*b1*LOW_Z + a1*b4*LOW_V +
     a4*b4*LOW_W*LOW_V + a5*b4*LOW_Z*LOW_V + a1*b5*MED_Q +
     a4*b5*LOW_W*MED_Q + a5*b5*LOW_Z*MED_Q;
    IMLLM = a1*b1 + a4*b1*MED_W + a5*b1*LOW_Z + a1*b4*LOW_V +
     a4*b4*MED_W*LOW_V + a5*b4*LOW_Z*LOW_V + a1*b5*MED_Q +
     a4*b5*MED_W*MED_Q + a5*b5*LOW_Z*MED_Q;
    IHLLM = a1*b1 + a4*b1*HIGH_W + a5*b1*LOW_Z + a1*b4*LOW_V +
     a4*b4*HIGH_W*LOW_V + a5*b4*LOW_Z*LOW_V + a1*b5*MED_Q +
     a4*b5*HIGH_W*MED_Q + a5*b5*LOW_Z*MED_Q;

    ILMLM = a1*b1 + a4*b1*LOW_W + a5*b1*MED_Z + a1*b4*LOW_V +
     a4*b4*LOW_W*LOW_V + a5*b4*MED_Z*LOW_V + a1*b5*MED_Q +
     a4*b5*LOW_W*MED_Q + a5*b5*MED_Z*MED_Q;
    IMMLM = a1*b1 + a4*b1*MED_W + a5*b1*MED_Z + a1*b4*LOW_V +
     a4*b4*MED_W*LOW_V + a5*b4*MED_Z*LOW_V + a1*b5*MED_Q +
     a4*b5*MED_W*MED_Q + a5*b5*MED_Z*MED_Q;
    IHMLM = a1*b1 + a4*b1*HIGH_W + a5*b1*MED_Z + a1*b4*LOW_V +
     a4*b4*HIGH_W*LOW_V + a5*b4*MED_Z*LOW_V + a1*b5*MED_Q +
     a4*b5*HIGH_W*MED_Q + a5*b5*MED_Z*MED_Q;

    ILHLM = a1*b1 + a4*b1*LOW_W + a5*b1*HIGH_Z + a1*b4*LOW_V +
     a4*b4*LOW_W*LOW_V + a5*b4*HIGH_Z*LOW_V + a1*b5*MED_Q +
     a4*b5*LOW_W*MED_Q + a5*b5*HIGH_Z*MED_Q;
    IMHLM = a1*b1 + a4*b1*MED_W + a5*b1*HIGH_Z + a1*b4*LOW_V +
     a4*b4*MED_W*LOW_V + a5*b4*HIGH_Z*LOW_V + a1*b5*MED_Q +
     a4*b5*MED_W*MED_Q + a5*b5*HIGH_Z*MED_Q;
    IHHLM = a1*b1 + a4*b1*HIGH_W + a5*b1*HIGH_Z + a1*b4*LOW_V +
     a4*b4*HIGH_W*LOW_V + a5*b4*HIGH_Z*LOW_V + a1*b5*MED_Q +
     a4*b5*HIGH_W*MED_Q + a5*b5*HIGH_Z*MED_Q;

    ILLMM = a1*b1 + a4*b1*LOW_W + a5*b1*LOW_Z + a1*b4*MED_V +
     a4*b4*LOW_W*MED_V + a5*b4*LOW_Z*MED_V + a1*b5*MED_Q +
     a4*b5*LOW_W*MED_Q + a5*b5*LOW_Z*MED_Q;
    IMLMM = a1*b1 + a4*b1*MED_W + a5*b1*LOW_Z + a1*b4*MED_V +
     a4*b4*MED_W*MED_V + a5*b4*LOW_Z*MED_V + a1*b5*MED_Q +
     a4*b5*MED_W*MED_Q + a5*b5*LOW_Z*MED_Q;
    IHLMM = a1*b1 + a4*b1*HIGH_W + a5*b1*LOW_Z + a1*b4*MED_V +
     a4*b4*HIGH_W*MED_V + a5*b4*LOW_Z*MED_V + a1*b5*MED_Q +
     a4*b5*HIGH_W*MED_Q + a5*b5*LOW_Z*MED_Q;

    ILMMM = a1*b1 + a4*b1*LOW_W + a5*b1*MED_Z + a1*b4*MED_V +
     a4*b4*LOW_W*MED_V + a5*b4*MED_Z*MED_V + a1*b5*MED_Q +
     a4*b5*LOW_W*MED_Q + a5*b5*MED_Z*MED_Q;
    IMMMM = a1*b1 + a4*b1*MED_W + a5*b1*MED_Z + a1*b4*MED_V +
     a4*b4*MED_W*MED_V + a5*b4*MED_Z*MED_V + a1*b5*MED_Q +
     a4*b5*MED_W*MED_Q + a5*b5*MED_Z*MED_Q;
    IHMMM = a1*b1 + a4*b1*HIGH_W + a5*b1*MED_Z + a1*b4*MED_V +
     a4*b4*HIGH_W*MED_V + a5*b4*MED_Z*MED_V + a1*b5*MED_Q +
     a4*b5*HIGH_W*MED_Q + a5*b5*MED_Z*MED_Q;

    ILHMM = a1*b1 + a4*b1*LOW_W + a5*b1*HIGH_Z + a1*b4*MED_V +
     a4*b4*LOW_W*MED_V + a5*b4*HIGH_Z*MED_V + a1*b5*MED_Q +
     a4*b5*LOW_W*MED_Q + a5*b5*HIGH_Z*MED_Q;
    IMHMM = a1*b1 + a4*b1*MED_W + a5*b1*HIGH_Z + a1*b4*MED_V +
     a4*b4*MED_W*MED_V + a5*b4*HIGH_Z*MED_V + a1*b5*MED_Q +
     a4*b5*MED_W*MED_Q + a5*b5*HIGH_Z*MED_Q;
    IHHMM = a1*b1 + a4*b1*HIGH_W + a5*b1*HIGH_Z + a1*b4*MED_V +
     a4*b4*HIGH_W*MED_V + a5*b4*HIGH_Z*MED_V + a1*b5*MED_Q +
     a4*b5*HIGH_W*MED_Q + a5*b5*HIGH_Z*MED_Q;

    ILLHM = a1*b1 + a4*b1*LOW_W + a5*b1*LOW_Z + a1*b4*HIGH_V +
     a4*b4*LOW_W*HIGH_V + a5*b4*LOW_Z*HIGH_V + a1*b5*MED_Q +
     a4*b5*LOW_W*MED_Q + a5*b5*LOW_Z*MED_Q;
    IMLHM = a1*b1 + a4*b1*MED_W + a5*b1*LOW_Z + a1*b4*HIGH_V +
     a4*b4*MED_W*HIGH_V + a5*b4*LOW_Z*HIGH_V + a1*b5*MED_Q +
     a4*b5*MED_W*MED_Q + a5*b5*LOW_Z*MED_Q;
    IHLHM = a1*b1 + a4*b1*HIGH_W + a5*b1*LOW_Z + a1*b4*HIGH_V +
     a4*b4*HIGH_W*HIGH_V + a5*b4*LOW_Z*HIGH_V + a1*b5*MED_Q +
     a4*b5*HIGH_W*MED_Q + a5*b5*LOW_Z*MED_Q;

    ILMHM = a1*b1 + a4*b1*LOW_W + a5*b1*MED_Z + a1*b4*HIGH_V +
     a4*b4*LOW_W*HIGH_V + a5*b4*MED_Z*HIGH_V + a1*b5*MED_Q +
     a4*b5*LOW_W*MED_Q + a5*b5*MED_Z*MED_Q;
    IMMHM = a1*b1 + a4*b1*MED_W + a5*b1*MED_Z + a1*b4*HIGH_V +
     a4*b4*MED_W*HIGH_V + a5*b4*MED_Z*HIGH_V + a1*b5*MED_Q +
     a4*b5*MED_W*MED_Q + a5*b5*MED_Z*MED_Q;
    IHMHM = a1*b1 + a4*b1*HIGH_W + a5*b1*MED_Z + a1*b4*HIGH_V +
     a4*b4*HIGH_W*HIGH_V + a5*b4*MED_Z*HIGH_V + a1*b5*MED_Q +
     a4*b5*HIGH_W*MED_Q + a5*b5*MED_Z*MED_Q;

    ILHHM = a1*b1 + a4*b1*LOW_W + a5*b1*HIGH_Z + a1*b4*HIGH_V +
     a4*b4*LOW_W*HIGH_V + a5*b4*HIGH_Z*HIGH_V + a1*b5*MED_Q +
     a4*b5*LOW_W*MED_Q + a5*b5*HIGH_Z*MED_Q;
    IMHHM = a1*b1 + a4*b1*MED_W + a5*b1*HIGH_Z + a1*b4*HIGH_V +
     a4*b4*MED_W*HIGH_V + a5*b4*HIGH_Z*HIGH_V + a1*b5*MED_Q +
     a4*b5*MED_W*MED_Q + a5*b5*HIGH_Z*MED_Q;
    IHHHM = a1*b1 + a4*b1*HIGH_W + a5*b1*HIGH_Z + a1*b4*HIGH_V +
     a4*b4*HIGH_W*HIGH_V + a5*b4*HIGH_Z*HIGH_V + a1*b5*MED_Q +
     a4*b5*HIGH_W*MED_Q + a5*b5*HIGH_Z*MED_Q;

    ILLLH = a1*b1 + a4*b1*LOW_W + a5*b1*LOW_Z + a1*b4*LOW_V +
     a4*b4*LOW_W*LOW_V + a5*b4*LOW_Z*LOW_V + a1*b5*HIGH_Q +
     a4*b5*LOW_W*HIGH_Q + a5*b5*LOW_Z*HIGH_Q;
    IMLLH = a1*b1 + a4*b1*MED_W + a5*b1*LOW_Z + a1*b4*LOW_V +
     a4*b4*MED_W*LOW_V + a5*b4*LOW_Z*LOW_V + a1*b5*HIGH_Q +
     a4*b5*MED_W*HIGH_Q + a5*b5*LOW_Z*HIGH_Q;
    IHLLH = a1*b1 + a4*b1*HIGH_W + a5*b1*LOW_Z + a1*b4*LOW_V +
     a4*b4*HIGH_W*LOW_V + a5*b4*LOW_Z*LOW_V + a1*b5*HIGH_Q +
     a4*b5*HIGH_W*HIGH_Q + a5*b5*LOW_Z*HIGH_Q;

    ILMLH = a1*b1 + a4*b1*LOW_W + a5*b1*MED_Z + a1*b4*LOW_V +
     a4*b4*LOW_W*LOW_V + a5*b4*MED_Z*LOW_V + a1*b5*HIGH_Q +
     a4*b5*LOW_W*HIGH_Q + a5*b5*MED_Z*HIGH_Q;
    IMMLH = a1*b1 + a4*b1*MED_W + a5*b1*MED_Z + a1*b4*LOW_V +
     a4*b4*MED_W*LOW_V + a5*b4*MED_Z*LOW_V + a1*b5*HIGH_Q +
     a4*b5*MED_W*HIGH_Q + a5*b5*MED_Z*HIGH_Q;
    IHMLH = a1*b1 + a4*b1*HIGH_W + a5*b1*MED_Z + a1*b4*LOW_V +
     a4*b4*HIGH_W*LOW_V + a5*b4*MED_Z*LOW_V + a1*b5*HIGH_Q +
     a4*b5*HIGH_W*HIGH_Q + a5*b5*MED_Z*HIGH_Q;

    ILHLH = a1*b1 + a4*b1*LOW_W + a5*b1*HIGH_Z + a1*b4*LOW_V +
     a4*b4*LOW_W*LOW_V + a5*b4*HIGH_Z*LOW_V + a1*b5*HIGH_Q +
     a4*b5*LOW_W*HIGH_Q + a5*b5*HIGH_Z*HIGH_Q;
    IMHLH = a1*b1 + a4*b1*MED_W + a5*b1*HIGH_Z + a1*b4*LOW_V +
     a4*b4*MED_W*LOW_V + a5*b4*HIGH_Z*LOW_V + a1*b5*HIGH_Q +
     a4*b5*MED_W*HIGH_Q + a5*b5*HIGH_Z*HIGH_Q;
    IHHLH = a1*b1 + a4*b1*HIGH_W + a5*b1*HIGH_Z + a1*b4*LOW_V +
     a4*b4*HIGH_W*LOW_V + a5*b4*HIGH_Z*LOW_V + a1*b5*HIGH_Q +
     a4*b5*HIGH_W*HIGH_Q + a5*b5*HIGH_Z*HIGH_Q;

    ILLMH = a1*b1 + a4*b1*LOW_W + a5*b1*LOW_Z + a1*b4*MED_V +
     a4*b4*LOW_W*MED_V + a5*b4*LOW_Z*MED_V + a1*b5*HIGH_Q +
     a4*b5*LOW_W*HIGH_Q + a5*b5*LOW_Z*HIGH_Q;
    IMLMH = a1*b1 + a4*b1*MED_W + a5*b1*LOW_Z + a1*b4*MED_V +
     a4*b4*MED_W*MED_V + a5*b4*LOW_Z*MED_V + a1*b5*HIGH_Q +
     a4*b5*MED_W*HIGH_Q + a5*b5*LOW_Z*HIGH_Q;
    IHLMH = a1*b1 + a4*b1*HIGH_W + a5*b1*LOW_Z + a1*b4*MED_V +
     a4*b4*HIGH_W*MED_V + a5*b4*LOW_Z*MED_V + a1*b5*HIGH_Q +
     a4*b5*HIGH_W*HIGH_Q + a5*b5*LOW_Z*HIGH_Q;

    ILMMH = a1*b1 + a4*b1*LOW_W + a5*b1*MED_Z + a1*b4*MED_V +
     a4*b4*LOW_W*MED_V + a5*b4*MED_Z*MED_V + a1*b5*HIGH_Q +
     a4*b5*LOW_W*HIGH_Q + a5*b5*MED_Z*HIGH_Q;
    IMMMH = a1*b1 + a4*b1*MED_W + a5*b1*MED_Z + a1*b4*MED_V +
     a4*b4*MED_W*MED_V + a5*b4*MED_Z*MED_V + a1*b5*HIGH_Q +
     a4*b5*MED_W*HIGH_Q + a5*b5*MED_Z*HIGH_Q;
    IHMMH = a1*b1 + a4*b1*HIGH_W + a5*b1*MED_Z + a1*b4*MED_V +
     a4*b4*HIGH_W*MED_V + a5*b4*MED_Z*MED_V + a1*b5*HIGH_Q +
     a4*b5*HIGH_W*HIGH_Q + a5*b5*MED_Z*HIGH_Q;

    ILHMH = a1*b1 + a4*b1*LOW_W + a5*b1*HIGH_Z + a1*b4*MED_V +
     a4*b4*LOW_W*MED_V + a5*b4*HIGH_Z*MED_V + a1*b5*HIGH_Q +
     a4*b5*LOW_W*HIGH_Q + a5*b5*HIGH_Z*HIGH_Q;
    IMHMH = a1*b1 + a4*b1*MED_W + a5*b1*HIGH_Z + a1*b4*MED_V +
     a4*b4*MED_W*MED_V + a5*b4*HIGH_Z*MED_V + a1*b5*HIGH_Q +
     a4*b5*MED_W*HIGH_Q + a5*b5*HIGH_Z*HIGH_Q;
    IHHMH = a1*b1 + a4*b1*HIGH_W + a5*b1*HIGH_Z + a1*b4*MED_V +
     a4*b4*HIGH_W*MED_V + a5*b4*HIGH_Z*MED_V + a1*b5*HIGH_Q +
     a4*b5*HIGH_W*HIGH_Q + a5*b5*HIGH_Z*HIGH_Q;

    ILLHH = a1*b1 + a4*b1*LOW_W + a5*b1*LOW_Z + a1*b4*HIGH_V +
     a4*b4*LOW_W*HIGH_V + a5*b4*LOW_Z*HIGH_V + a1*b5*HIGH_Q +
     a4*b5*LOW_W*HIGH_Q + a5*b5*LOW_Z*HIGH_Q;
    IMLHH = a1*b1 + a4*b1*MED_W + a5*b1*LOW_Z + a1*b4*HIGH_V +
     a4*b4*MED_W*HIGH_V + a5*b4*LOW_Z*HIGH_V + a1*b5*HIGH_Q +
     a4*b5*MED_W*HIGH_Q + a5*b5*LOW_Z*HIGH_Q;
    IHLHH = a1*b1 + a4*b1*HIGH_W + a5*b1*LOW_Z + a1*b4*HIGH_V +
     a4*b4*HIGH_W*HIGH_V + a5*b4*LOW_Z*HIGH_V + a1*b5*HIGH_Q +
     a4*b5*HIGH_W*HIGH_Q + a5*b5*LOW_Z*HIGH_Q;

    ILMHH = a1*b1 + a4*b1*LOW_W + a5*b1*MED_Z + a1*b4*HIGH_V +
     a4*b4*LOW_W*HIGH_V + a5*b4*MED_Z*HIGH_V + a1*b5*HIGH_Q +
     a4*b5*LOW_W*HIGH_Q + a5*b5*MED_Z*HIGH_Q;
    IMMHH = a1*b1 + a4*b1*MED_W + a5*b1*MED_Z + a1*b4*HIGH_V +
     a4*b4*MED_W*HIGH_V + a5*b4*MED_Z*HIGH_V + a1*b5*HIGH_Q +
     a4*b5*MED_W*HIGH_Q + a5*b5*MED_Z*HIGH_Q;
    IHMHH = a1*b1 + a4*b1*HIGH_W + a5*b1*MED_Z + a1*b4*HIGH_V +
     a4*b4*HIGH_W*HIGH_V + a5*b4*MED_Z*HIGH_V + a1*b5*HIGH_Q +
     a4*b5*HIGH_W*HIGH_Q + a5*b5*MED_Z*HIGH_Q;

    ILHHH = a1*b1 + a4*b1*LOW_W + a5*b1*HIGH_Z + a1*b4*HIGH_V +
     a4*b4*LOW_W*HIGH_V + a5*b4*HIGH_Z*HIGH_V + a1*b5*HIGH_Q +
     a4*b5*LOW_W*HIGH_Q + a5*b5*HIGH_Z*HIGH_Q;
    IMHHH = a1*b1 + a4*b1*MED_W + a5*b1*HIGH_Z + a1*b4*HIGH_V +
     a4*b4*MED_W*HIGH_V + a5*b4*HIGH_Z*HIGH_V + a1*b5*HIGH_Q +
     a4*b5*MED_W*HIGH_Q + a5*b5*HIGH_Z*HIGH_Q;
    IHHHH = a1*b1 + a4*b1*HIGH_W + a5*b1*HIGH_Z + a1*b4*HIGH_V +
     a4*b4*HIGH_W*HIGH_V + a5*b4*HIGH_Z*HIGH_V + a1*b5*HIGH_Q +
     a4*b5*HIGH_W*HIGH_Q + a5*b5*HIGH_Z*HIGH_Q;

! Calc conditional direct effects for each combination of moderator values

    DLOW_LOZ = cdash1 + cdash4*LOW_W + cdash5*LOW_Z;
    DMEW_LOZ = cdash1 + cdash4*MED_W + cdash5*LOW_Z;
    DHIW_LOZ = cdash1 + cdash4*HIGH_W + cdash5*LOW_Z;

    DLOW_MEZ = cdash1 + cdash4*LOW_W + cdash5*MED_Z;
    DMEW_MEZ = cdash1 + cdash4*MED_W + cdash5*MED_Z;
    DHIW_MEZ = cdash1 + cdash4*HIGH_W + cdash5*MED_Z;

    DLOW_HIZ = cdash1 + cdash4*LOW_W + cdash5*HIGH_Z;
    DMEW_HIZ = cdash1 + cdash4*MED_W + cdash5*HIGH_Z;
    DHIW_HIZ = cdash1 + cdash4*HIGH_W + cdash5*HIGH_Z;

! Calc conditional total effects for each combination of moderator values

    TLLLL = ILLLL + DLOW_LOZ;
    TMLLL = IMLLL + DMEW_LOZ;
    THLLL = IHLLL + DHIW_LOZ;

    TLMLL = ILMLL + DLOW_MEZ;
    TMMLL = IMMLL + DMEW_MEZ;
    THMLL = IHMLL + DHIW_MEZ;

    TLHLL = ILHLL + DLOW_HIZ;
    TMHLL = IMHLL + DMEW_HIZ;
    THHLL = IHHLL + DHIW_HIZ;

    TLLML = ILLML + DLOW_LOZ;
    TMLML = IMLML + DMEW_LOZ;
    THLML = IHLML + DHIW_LOZ;

    TLMML = ILMML + DLOW_MEZ;
    TMMML = IMMML + DMEW_MEZ;
    THMML = IHMML + DHIW_MEZ;

    TLHML = ILHML + DLOW_HIZ;
    TMHML = IMHML + DMEW_HIZ;
    THHML = IHHML + DHIW_HIZ;

    TLLHL = ILLHL + DLOW_LOZ;
    TMLHL = IMLHL + DMEW_LOZ;
    THLHL = IHLHL + DHIW_LOZ;

    TLMHL = ILMHL + DLOW_MEZ;
    TMMHL = IMMHL + DMEW_MEZ;
    THMHL = IHMHL + DHIW_MEZ;

    TLHHL = ILHHL + DLOW_HIZ;
    TMHHL = IMHHL + DMEW_HIZ;
    THHHL = IHHHL + DHIW_HIZ;

    TLLLM = ILLLM + DLOW_LOZ;
    TMLLM = IMLLM + DMEW_LOZ;
    THLLM = IHLLM + DHIW_LOZ;

    TLMLM = ILMLM + DLOW_MEZ;
    TMMLM = IMMLM + DMEW_MEZ;
    THMLM = IHMLM + DHIW_MEZ;

    TLHLM = ILHLM + DLOW_HIZ;
    TMHLM = IMHLM + DMEW_HIZ;
    THHLM = IHHLM + DHIW_HIZ;

    TLLMM = ILLMM + DLOW_LOZ;
    TMLMM = IMLMM + DMEW_LOZ;
    THLMM = IHLMM + DHIW_LOZ;

    TLMMM = ILMMM + DLOW_MEZ;
    TMMMM = IMMMM + DMEW_MEZ;
    THMMM = IHMMM + DHIW_MEZ;

    TLHMM = ILHMM + DLOW_HIZ;
    TMHMM = IMHMM + DMEW_HIZ;
    THHMM = IHHMM + DHIW_HIZ;

    TLLHM = ILLHM + DLOW_LOZ;
    TMLHM = IMLHM + DMEW_LOZ;
    THLHM = IHLHM + DHIW_LOZ;

    TLMHM = ILMHM + DLOW_MEZ;
    TMMHM = IMMHM + DMEW_MEZ;
    THMHM = IHMHM + DHIW_MEZ;

    TLHHM = ILHHM + DLOW_HIZ;
    TMHHM = IMHHM + DMEW_HIZ;
    THHHM = IHHHM + DHIW_HIZ;

    TLLLH = ILLLH + DLOW_LOZ;
    TMLLH = IMLLH + DMEW_LOZ;
    THLLH = IHLLH + DHIW_LOZ;

    TLMLH = ILMLH + DLOW_MEZ;
    TMMLH = IMMLH + DMEW_MEZ;
    THMLH = IHMLH + DHIW_MEZ;

    TLHLH = ILHLH + DLOW_HIZ;
    TMHLH = IMHLH + DMEW_HIZ;
    THHLH = IHHLH + DHIW_HIZ;

    TLLMH = ILLMH + DLOW_LOZ;
    TMLMH = IMLMH + DMEW_LOZ;
    THLMH = IHLMH + DHIW_LOZ;

    TLMMH = ILMMH + DLOW_MEZ;
    TMMMH = IMMMH + DMEW_MEZ;
    THMMH = IHMMH + DHIW_MEZ;

    TLHMH = ILHMH + DLOW_HIZ;
    TMHMH = IMHMH + DMEW_HIZ;
    THHMH = IHHMH + DHIW_HIZ;

    TLLHH = ILLHH + DLOW_LOZ;
    TMLHH = IMLHH + DMEW_LOZ;
    THLHH = IHLHH + DHIW_LOZ;

    TLMHH = ILMHH + DLOW_MEZ;
    TMMHH = IMMHH + DMEW_MEZ;
    THMHH = IHMHH + DHIW_MEZ;

    TLHHH = ILHHH + DLOW_HIZ;
    TMHHH = IMHHH + DMEW_HIZ;
    THHHH = IHHHH + DHIW_HIZ;

! Use loop plot to plot conditional indirect effect of X on Y for each combination of low, med, high moderator values
! Could be edited to show conditional direct or conditional total effects instead
! 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(PLLLL PMLLL PHLLL PLMLL PMMLL PHMLL PLHLL PMHLL PHHLL
    PLLML PMLML PHLML PLMML PMMML PHMML PLHML PMHML PHHML
    PLLHL PMLHL PHLHL PLMHL PMMHL PHMHL PLHHL PMHHL PHHHL
    PLLLM PMLLM PHLLM PLMLM PMMLM PHMLM PLHLM PMHLM PHHLM
    PLLMM PMLMM PHLMM PLMMM PMMMM PHMMM PLHMM PMHMM PHHMM
    PLLHM PMLHM PHLHM PLMHM PMMHM PHMHM PLHHM PMHHM PHHHM
    PLLLH PMLLH PHLLH PLMLH PMMLH PHMLH PLHLH PMHLH PHHLH
    PLLMH PMLMH PHLMH PLMMH PMMMH PHMMH PLHMH PMHMH PHHMH
    PLLHH PMLHH PHLHH PLMHH PMMHH PHMHH PLHHH PMHHH PHHHH);

    LOOP(XVAL,1,5,0.1);

    PLLLL = ILLLL*XVAL;
    PMLLL = IMLLL*XVAL;
    PHLLL = IHLLL*XVAL;

    PLMLL = ILMLL*XVAL;
    PMMLL = IMMLL*XVAL;
    PHMLL = IHMLL*XVAL;

    PLHLL = ILHLL*XVAL;
    PMHLL = IMHLL*XVAL;
    PHHLL = IHHLL*XVAL;

    PLLML = ILLML*XVAL;
    PMLML = IMLML*XVAL;
    PHLML = IHLML*XVAL;

    PLMML = ILMML*XVAL;
    PMMML = IMMML*XVAL;
    PHMML = IHMML*XVAL;

    PLHML = ILHML*XVAL;
    PMHML = IMHML*XVAL;
    PHHML = IHHML*XVAL;

    PLLHL = ILLHL*XVAL;
    PMLHL = IMLHL*XVAL;
    PHLHL = IHLHL*XVAL;

    PLMHL = ILMHL*XVAL;
    PMMHL = IMMHL*XVAL;
    PHMHL = IHMHL*XVAL;

    PLHHL = ILHHL*XVAL;
    PMHHL = IMHHL*XVAL;
    PHHHL = IHHHL*XVAL;

    PLLLM = ILLLM*XVAL;
    PMLLM = IMLLM*XVAL;
    PHLLM = IHLLM*XVAL;

    PLMLM = ILMLM*XVAL;
    PMMLM = IMMLM*XVAL;
    PHMLM = IHMLM*XVAL;

    PLHLM = ILHLM*XVAL;
    PMHLM = IMHLM*XVAL;
    PHHLM = IHHLM*XVAL;

    PLLMM = ILLMM*XVAL;
    PMLMM = IMLMM*XVAL;
    PHLMM = IHLMM*XVAL;

    PLMMM = ILMMM*XVAL;
    PMMMM = IMMMM*XVAL;
    PHMMM = IHMMM*XVAL;

    PLHMM = ILHMM*XVAL;
    PMHMM = IMHMM*XVAL;
    PHHMM = IHHMM*XVAL;

    PLLHM = ILLHM*XVAL;
    PMLHM = IMLHM*XVAL;
    PHLHM = IHLHM*XVAL;

    PLMHM = ILMHM*XVAL;
    PMMHM = IMMHM*XVAL;
    PHMHM = IHMHM*XVAL;

    PLHHM = ILHHM*XVAL;
    PMHHM = IMHHM*XVAL;
    PHHHM = IHHHM*XVAL;

    PLLLH = ILLLH*XVAL;
    PMLLH = IMLLH*XVAL;
    PHLLH = IHLLH*XVAL;

    PLMLH = ILMLH*XVAL;
    PMMLH = IMMLH*XVAL;
    PHMLH = IHMLH*XVAL;

    PLHLH = ILHLH*XVAL;
    PMHLH = IMHLH*XVAL;
    PHHLH = IHHLH*XVAL;

    PLLMH = ILLMH*XVAL;
    PMLMH = IMLMH*XVAL;
    PHLMH = IHLMH*XVAL;

    PLMMH = ILMMH*XVAL;
    PMMMH = IMMMH*XVAL;
    PHMMH = IHMMH*XVAL;

    PLHMH = ILHMH*XVAL;
    PMHMH = IMHMH*XVAL;
    PHHMH = IHHMH*XVAL;

    PLLHH = ILLHH*XVAL;
    PMLHH = IMLHH*XVAL;
    PHLHH = IHLHH*XVAL;

    PLMHH = ILMHH*XVAL;
    PMMHH = IMMHH*XVAL;
    PHMHH = IHMHH*XVAL;

    PLHHH = ILHHH*XVAL;
    PMHHH = IMHHH*XVAL;
    PHHHH = IHHHH*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.figureitout.org.uk

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