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:

- Psychology: Does a therapy (X) reduce anxiety (M), which in turn improves sleep quality (Y)?
- Medicine: Does a new drug (X) lower blood pressure (M), thereby decreasing the risk of heart attack (Y)?

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

- a: The effect of X on M.
- b: The effect of M on Y.
- c: The direct effect of X on Y.
- a×b: The indirect effect (the mediation effect).

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?”.

## Data Simulation

To verify that `{INLAvaan}` recovers the correct parameters, we simulate data where the “truth” is known. The logic is as follows: Generate…

1. X normally;
2. M dependent on X with a coefficient of 0.5; and
3. Y dependent only on M with a coefficient of 0.7.

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 a b≈ 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)

## Model Specification and Fit

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)
    #> ℹ Mode finding and Hessian computation.
    #> ✔ Posterior mode and Hessian. [55ms]
    #>
    #> ℹ Performing VB correction.
    #> ✔ VB correction; mean |δ| = 0.099σ. [41ms]
    #>
    #> ⠙ Fitting 0/7 skew-normal marginals.
    #> ✔ Fit 7/7 skew-normal marginals. [53ms]
    #>
    #> ℹ Adjusting copula correlations (NORTA).
    #> ✔ Adjust copula correlations (NORTA). [6ms]
    #>
    #> ⠙ Posterior sampling and summarising.
    #> ✔ Summarise 1000 posterior draws. [324ms]
    #>
    #> ℹ Fit measures: PPP, DIC, LOO, WAIC.

The user may wish to specify different prior distributions for the parameters. See the relevant section in the Get started vignetted for further details.

## Results

The summary output provides the posterior mean, standard deviation, and 95% credible intervals for all paths.

    summary(fit)
    #> INLAvaan 0.3.1 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                    -312.078
    #>    PPP (Chi-square)                              0.613
    #>
    #> Information Criteria:
    #>
    #>    Deviance (DIC)                              568.304
    #>    Effective parameters (pD)                     6.693
    #>
    #> Parameter Estimates:
    #>
    #>    Marginalisation method                     SKEWNORM
    #>    VB correction                                  TRUE
    #>
    #> 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.263    0.121    0.000    normal(0,32)
    #>    .M                 0.126    0.099   -0.068    0.319    0.000    normal(0,32)
    #>
    #> Variances:
    #>                    Estimate       SD     2.5%    97.5%     NMAD    Prior
    #>    .Y                 0.977    0.143    0.735    1.297    0.004 gamma(1,.5)[sd]
    #>    .M                 0.998    0.146    0.752    1.324    0.004 gamma(1,.5)[sd]
    #>
    #> Defined Parameters:
    #>                    Estimate       SD     2.5%    97.5%     NMAD    Prior
    #>     ab                0.406    0.097    0.215    0.597
    #>     total             0.349    0.136    0.083    0.615

Looking at the Regressions and Defined Parameters sections of the output:

- Both intercepts are non-significant, since we simulated data with true means of zero.
- Path a (`M ~ X`) estimated at 0.525 (true value 0.5).
- Path b (`Y ~ M`) estimated at 0.771 (true value 0.7).
- Path c (`Y ~ X`) estimated at -0.060. The 95% Credible Interval [-0.291, 0.171] includes zero, correctly identifying that there is no direct effect.
- Indirect Effect a b estimated at 0.406 (true value 0.35). The interval [0.215, 0.597] does not cross zero, indicating significant mediation.
- Total Effect estimated at 0.349.
  - This is the sum of the direct and indirect effects (c+a b).
  - It tells us that a 1-unit increase in X leads to a total increase of roughly 0.349 in Y.
  - Note: In this simulation, even though the direct effect is non-significant (close to zero), the total effect is significant because the mechanism via M is strong. This illustrates a “full mediation” scenario: X affects Y, but only because of M.

## References

Yuan, Ying, and David P. MacKinnon. 2009. “Bayesian Mediation Analysis.” Psychological Methods 14 (4): 301–22. https://doi.org/10.1037/a0016972.