graph LR
X((X)) -->|a| M((M))
M -->|b| Y((Y))
X -->|c| Y
Mediation analysis (Yuan and 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:
graph LR
X((X)) -->|a| M((M))
M -->|b| Y((Y))
X -->|c| Y
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)
#> ℹ Finding posterior mode.
#> ✔ Finding posterior mode. [10ms]
#>
#> ℹ Computing the Hessian.
#> ✔ Computing the Hessian. [26ms]
#>
#> ℹ Performing VB correction.
#> ✔ VB correction; mean |δ| = 0.012σ. [49ms]
#>
#> ⠙ Fitting skew normal to 0/7 marginals.
#> ✔ Fitting skew normal to 7/7 marginals. [61ms]
#>
#> ⠙ Computing ppp and DIC.
#> ✔ Computing ppp and DIC. [110ms]
#> 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.2.2 ended normally after 5 iterations
#>
#> Estimator BAYES
#> Optimization method NLMINB
#> Number of model parameters 7
#>
#> Number of observations 100
#>
#> Model Test (User Model):
#>
#> Marginal log-likelihood -311.858
#> PPP (Chi-square) 0.604
#>
#> Information Criteria:
#>
#> Deviance (DIC) 568.665
#> Effective parameters (pD) 6.876
#>
#> Parameter Estimates:
#>
#> Marginalisation method SKEWNORM
#> VB correction TRUE
#>
#> Regressions:
#> Estimate SD 2.5% 97.5% NMAD Prior
#> Y ~
#> X (c) -0.060 0.120 -0.296 0.176 0.001 normal(0,10)
#> M ~
#> X (a) 0.525 0.109 0.311 0.740 0.000 normal(0,10)
#> Y ~
#> M (b) 0.771 0.101 0.573 0.968 0.000 normal(0,10)
#>
#> Intercepts:
#> Estimate SD 2.5% 97.5% NMAD Prior
#> .Y -0.071 0.100 -0.267 0.126 0.000 normal(0,32)
#> .M 0.126 0.100 -0.071 0.322 0.000 normal(0,32)
#>
#> Variances:
#> Estimate SD 2.5% 97.5% NMAD Prior
#> .Y 0.976 0.145 0.732 1.299 0.004 gamma(1,.5)[sd]
#> .M 0.997 0.147 0.749 1.326 0.004 gamma(1,.5)[sd]
#>
#> Defined Parameters:
#> Estimate SD 2.5% 97.5% NMAD Prior
#> ab 0.403 0.097 0.246 0.596
#> total 0.347 0.128 0.101 0.592Looking 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.296, 0.176] includes zero, correctly identifying that there is no direct effect.