3

Suppose you have an outcome variable (Y; continuous), an independent variable (X; dummy), and a moderator (W; dummy). Suppose that you would like to test whether another variable (M; continuous) mediates the link between X and W. How would you go about coding this test in R (using lavaan)?

The closest post to mine is: Creating a first stage mediated moderation model, syntax issues

However, the offered answer deals with a question different from mine. My question is about mediating a moderation, whereas the answer deals with moderating a mediation.

Max Hackert
  • 131
  • 1
  • 4

1 Answers1

0

Assuming that both X and W are dummy variables, you can use the : operator:

library(lavaan)
#> This is lavaan 0.6-7
#> lavaan is BETA software! Please report any bugs.
df <- data.frame(id=1:301)
df$w <- dummies::dummy(HolzingerSwineford1939$school)[,1]
#> Warning in model.matrix.default(~x - 1, model.frame(~x - 1), contrasts = FALSE):
#> non-list contrasts argument ignored
df$x <- dummies::dummy(HolzingerSwineford1939$sex)[,1]
#> Warning in model.matrix.default(~x - 1, model.frame(~x - 1), contrasts = FALSE):
#> non-list contrasts argument ignored
df$y <- HolzingerSwineford1939$x9
df$m <- HolzingerSwineford1939$agemo

model <- "
#x9 will be your Y
#sex will be your X
#school will be your W
#agemo will be your M
y ~ x + w + c*x:w + b*m
m ~ a*x:w

# indirect effect (a*b)
ab := a*b
# total effect
total := c + (a*b)
"

fit <- sem(model = model, data = df)
summary(object = fit, std=T)
#> lavaan 0.6-7 ended normally after 33 iterations
#> 
#>   Estimator                                         ML
#>   Optimization method                           NLMINB
#>   Number of free parameters                          7
#>                                                       
#>   Number of observations                           301
#>                                                       
#> Model Test User Model:
#>                                                       
#>   Test statistic                                 0.041
#>   Degrees of freedom                                 2
#>   P-value (Chi-square)                           0.980
#> 
#> Parameter Estimates:
#> 
#>   Standard errors                             Standard
#>   Information                                 Expected
#>   Information saturated (h1) model          Structured
#> 
#> Regressions:
#>                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
#>   y ~                                                                   
#>     x                -0.131    0.161   -0.812    0.417   -0.131   -0.065
#>     w                -0.130    0.162   -0.805    0.421   -0.130   -0.065
#>     x:w        (c)    0.086    0.232    0.373    0.709    0.086    0.037
#>     m          (b)    0.008    0.017    0.478    0.633    0.008    0.027
#>   m ~                                                                   
#>     x:w        (a)   -0.238    0.465   -0.511    0.609   -0.238   -0.029
#> 
#> Variances:
#>                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
#>    .y                 1.010    0.082   12.268    0.000    1.010    0.995
#>    .m                11.865    0.967   12.268    0.000   11.865    0.999
#> 
#> Defined Parameters:
#>                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
#>     ab               -0.002    0.005   -0.349    0.727   -0.002   -0.001
#>     total             0.085    0.232    0.364    0.716    0.085    0.036

Created on 2021-03-16 by the reprex package (v0.3.0)

Sinval
  • 1,315
  • 1
  • 16
  • 25