Model with multiple mediation/moderation effects

219 views
Skip to first unread message

blairmid

unread,
Feb 21, 2023, 1:46:52 PM2/21/23
to lavaan
Hi all,

I am trying to test the following model, and am curious if the code I have written is the appropriate way to represent these effects.  We are assuming that the moderators (Z and M) can affect the mediator (Y) and the DV. 

Model:

Screen Shot 2023-02-21 at 10.39.08 AM.png


Current code:
model <- '
# Regression equations
  M ~ aZ * Z + control_1 + control_2 + control_3
 
  Y ~ bM * M + bX * X + bXM * X:M + bZ * Z + bXZ * X:Z + control_1 + control_2 + control_3
 
  DV ~ cM * M + cX * X + cXM * X:M + cZ * Z + cXZ * X:Z + cY * Y + control_1 + control_2 + control_3




# Mean of X
  X ~ X.mean * 1

# Variance of X
  X ~~ X.var * X

# Effects on Mediator conditional on moderator
  ZtoM.SDlo := ( aZ )
  ZtoM.SDhi := ( aZ )

########## Y effects

# Indirect Effects Conditional on Moderator
  Y.indirect.SDlo := (bM + bXM * (X.mean - sqrt(X.var))) * ZtoM.SDlo
  Y.indirect.SDhi := (bM + bXM * (X.mean + sqrt(X.var))) * ZtoM.SDhi

# Index of Moderated Mediation
  index.mod.med: = ( aZ ) * bXM

# Covariance of Exogeneous Variables
  Enact_Condition ~~ Gender_code

# Direct Effects Conditional on Moderator
  Y.direct.SDlo := (bZ + bXZ * (X.mean - sqrt(X.var)))
  Y.direct.SDhi := (bZ + bXZ * (X.mean + sqrt(X.var)))

# Total Effects conditional on moderator
  Y.total.SDlo := Y.direct.SDlo + Y.indirect.SDlo
  Y.total.SDhi := Y.direct.SDhi + Y.indirect.SDhi

# Proportion Mediated Conditional on Moderator
  Y.prop.mediated.SDlo := Y.indirect.SDlo / Y.total.SDlo
  Y.prop.mediated.SDhi := Y.indirect.SDhi / Y.total.SDhi


########## DV effects

# Indirect Effects Conditional on Moderator (gender to belonging)
  DV.indirect.SDlo := (cX+ cY) * (bM + bXM * (X.mean - sqrt(X.var))) * ZtoM.SDlo
  DV.indirect.SDhi := (cX+ cY) * (bM + bXM * (X.mean + sqrt(X.var))) * ZtoM.SDhi

# Index of Moderated Mediation (alternativ test for sign. differences among different moderator levels)
  DV.index.mod.med: = ( aZ ) * bXM * cY

# Direct Effects Conditional on Moderator (gender to belonging)
  ZtoDV.direct.SDlo := (cZ + cXZ * (X.mean - sqrt(X.var)))
  ZtoDV.direct.SDhi := (cZ + cXZ * (X.mean + sqrt(X.var)))
 
# Direct Effects Conditional on Moderator (threat to belonging)
  MtoDV.direct.SDlo := (cM + cXM * (X.mean - sqrt(X.var)))
  MtoDV.direct.SDhi := (cM + cXM * (X.mean + sqrt(X.var)))

# Total Effects conditional on moderator
  DV.total.SDlo := ZtoDV.direct.SDlo + MtoDV.direct.SDlo + DV.indirect.SDlo
  DV.total.SDhi := ZtoDV.direct.SDhi + MtoDV.direct.SDhi + DV.indirect.SDhi
 
  # Proportion Mediated Conditional on Moderator (To match the output of mediate package)
  DV.prop.mediated.SDlo := DV.indirect.SDlo / DV.total.SDlo
  DV.prop.mediated.SDhi := DV.indirect.SDhi / DV.total.SDhi

'


 
model.fit <- sem(model = model,
                           data = data,
                           se = "bootstrap",
                           bootstrap = 500)

model.results <- parameterEstimates(model.fit,
                              boot.ci.type = "bca.simple",
                              level = .95, ci = TRUE,
                              standardized = FALSE)
model.results





I am also wondering if it would be possible to represent Z as a categorical variable with 4 categories, using 3 dummy variables, or if this would cause the calculations of the effects to be incorrect?

Alternatively, I am wondering if Z could be replaced with an interaction between two variables, as pictured below, or if this kind of model would be beyond the means of what lavaan can do?

Screen Shot 2023-02-21 at 10.44.16 AM.png

Thank you very very much for your help!




Stijn Debrouwere

unread,
Feb 22, 2023, 4:58:20 AM2/22/23
to lavaan
Well there's two problems here :-) First, whether the graphs represent the problem you're trying to solve and second, whether the graphs match the model specification.

I'll just focus on the first graph for now. To clarify, you expect:

* an effect from X on DV that may or may not be mediated by Y
* the effect of X on Y may differ depending on Z
* the moderating effect of Z may be mediated by M

If so then you're missing a X -> DV arrow to represent that the effect of X on the outcome may or may not be mediated.

When you say the moderators Z and M can affect the DV, do you mean indirectly through Y or directly as well? If the latter, then you should also have arrows from Z and M towards the Y -> DV arrow to indicate that they can moderate that relationship too.

Once we know the model you're interested in, we can have a look at the model specification.

Best,
Stijn

blairmid

unread,
Feb 22, 2023, 2:07:44 PM2/22/23
to lavaan
Thanks so much Stijn!

I had included that model as a more simple representation, but this might be a better representation of the effects we are predicting/interested in exploring:Screen Shot 2023-02-22 at 10.57.21 AM.png

We are proposing that the relationship between X and the DV is mediated by Y, but we are also interested in examining any direct effect of X on the DV. Then, with these two different paths we are hypothesizing a moderation effect in X's relationship with Y and with the DV. This moderation effect also includes a partial mediation (similarly to the X-->Y-->DV model), where Z is a direct moderator on the X-->Y link and on the X-->DV link and then also is an indirect moderator through M, so M is also a moderator of the X-->Y link and the X-->DV link, plus there is a link between Z and M. I tried to show this with the coloring, essentially that the moderation effect (Z via M) moderates the X-->Y relationship and the X-->DV relationship. Hopefully this makes sense, but I am happy to clarify further! Thank you again for your help!
Reply all
Reply to author
Forward
0 new messages