set.seed(11)
n <- 100 # sample size
# 1. Predictor
X <- rnorm(n)
# 2. Mediator (Path a = 0.5)
M <- 0.5 * X + rnorm(n)
# 3. Outcome (Path b = 0.7, Path c = 0)
Y <- 0.7 * M + rnorm(n)
dat <- data.frame(X = X, Y = Y, M = M)Mediation analysis (Yuan & MacKinnon, 2009) allows researchers to investigate the mechanism by which an independent variable (X) influences a dependent variable (Y). Rather than just asking “Does X affect Y?”, mediation asks “Does X affect Y through an intermediate variable M?”
Common examples include:
In this vignette, we demonstrate how to estimate a simple mediation model using {INLAvaan}. We will fit a standard three-variable mediation model:
In a mediation model, the Total Effect represents the overall impact of X on Y, ignoring the specific pathway. It answers the question: “If I change X, how much does Y change in total, regardless of whether it goes through M or not?”.
To verify that {INLAvaan} recovers the correct parameters, we simulate data where the “truth” is known. The logic is as follows: Generate…
Critically, we do not add X to the generation of Y. This means the true direct effect (c) is 0, and the relationship is fully mediated. We expect our model to estimate a ≈ 0.5, b ≈ 0.7, and the indirect effect ab ≈ 0.35. The direct effect c should be close to zero.
set.seed(11)
n <- 100 # sample size
# 1. Predictor
X <- rnorm(n)
# 2. Mediator (Path a = 0.5)
M <- 0.5 * X + rnorm(n)
# 3. Outcome (Path b = 0.7, Path c = 0)
Y <- 0.7 * M + rnorm(n)
dat <- data.frame(X = X, Y = Y, M = M)The standard lavaan syntax for a mediation model is straightforward (note the use of the := operator to define the indirect effect as a new parameter.):
mod <- "
# Direct effect (path c)
Y ~ c*X
# Mediator paths (path a and b)
M ~ a*X
Y ~ b*M
# Define Indirect effect (a*b)
ab := a*b
# Define Total effect
total := c + (a*b)
"The model is fit using asem(). The meanstructure = TRUE argument is supplied to estimate intercepts for the variables.
library(INLAvaan)
fit <- asem(mod, dat, meanstructure = TRUE)
#> ✔ Found the posterior mode in 6 iterations [27ms]
#> ✔ Applied VB mean correction; max |δ| = 0.28σ [44ms]
#> ✔ Fitted 7 skew-normal posterior marginals [56ms]
#> ⠙ Drawing from the posterior and summarising.
#> ✔ Summarised 1,000 draws from the posterior [178ms]
#> ℹ Computed posterior fit measures: PPP, DIC.The user may wish to specify different prior distributions for the parameters. See the relevant section in the Get started vignetted for further details.
The summary output provides the posterior mean, standard deviation, and 95% credible intervals for all paths.
summary(fit)
#> INLAvaan 0.4.2 ended normally after 6 iterations
#>
#> Estimator BAYES
#> Optimization method NLMINB
#> Number of model parameters 7
#>
#> Number of observations 100
#>
#> Model Test (User Model):
#>
#> Marginal log-likelihood -308.480
#> PPP (Chi-square) 0.519
#>
#> Information Criteria:
#>
#> Deviance (DIC) 568.325
#> Effective parameters (pD) 6.713
#>
#> Parameter Estimates:
#>
#> Marginalisation method SKEWNORM
#> Marginal scan LINEAR
#> VB correction ONESTEP
#>
#> Regressions:
#> Estimate SD 2.5% 97.5% NMAD Prior
#> Y ~
#> X (c) -0.060 0.118 -0.291 0.171 0.000 normal(0,10)
#> M ~
#> X (a) 0.525 0.108 0.314 0.736 0.000 normal(0,10)
#> Y ~
#> M (b) 0.771 0.099 0.577 0.964 0.000 normal(0,10)
#>
#> Intercepts:
#> Estimate SD 2.5% 97.5% NMAD Prior
#> .Y -0.071 0.098 -0.264 0.120 0.000 normal(0,32)
#> .M 0.126 0.098 -0.066 0.318 0.000 normal(0,32)
#>
#> Variances:
#> Estimate SD 2.5% 97.5% NMAD Prior
#> .Y 0.977 0.143 0.735 1.296 0.004 gamma(1,.5)[sd]
#> .M 0.998 0.146 0.752 1.322 0.004 gamma(1,.5)[sd]
#>
#> Defined Parameters:
#> Estimate SD 2.5% 97.5% NMAD Prior
#> ab 0.406 0.097 0.227 0.609
#> total 0.349 0.136 0.078 0.611Looking at the Regressions and Defined Parameters sections of the output:
M ~ X) estimated at 0.525 (true value 0.5).Y ~ M) estimated at 0.771 (true value 0.7).Y ~ X) estimated at -0.060. The 95% Credible Interval [-0.291, 0.171] includes zero, correctly identifying that there is no direct effect.The numbers of summary() are also in a data frame from parameterestimates(), with one row per parameter and the column names of lavaan’s parameterEstimates(), ready for your own tables. Here are the defined parameters with 90% credible intervals:
pe <- parameterestimates(fit, level = 0.90)
pe[pe$op == ":=", ]
#> lhs op rhs label est se ci.lower ci.upper
#> 10 ab := a*b ab 0.406 0.097 0.254 0.573
#> 11 total := c+(a*b) total 0.349 0.136 0.123 0.569