# Mediation and bootstrapped indirect effects
# https://tidyecology.com/posts/mediation-bootstrap-indirect-effects/
#
# The R code of this Tidy Ecology tutorial, chunk by chunk, in the order of the post.
# Run it top to bottom in a fresh R session. Code the post shows but does not run
# is commented out. The prose, and the numbers it quotes, are in the post itself.
# Post last modified: 2026-09-27

## ---- setup ----
library(ggplot2)

te <- list(paper="#f5f4ee", card="#ffffff", ink="#16241d", body="#2c3a31",
           forest="#275139", label="#46604a", sage="#93a87f", line="#dad9ca",
           muted="#5d6b61", warm="#b5534e", gold="#cda23f")

theme_te <- function(base_size = 12) {
  theme_minimal(base_size = base_size) +
    theme(plot.background  = element_rect(fill = te$paper, colour = NA),
          panel.background = element_rect(fill = te$paper, colour = NA),
          panel.grid.major = element_line(colour = te$line, linewidth = 0.3),
          panel.grid.minor = element_blank(),
          axis.text  = element_text(colour = te$muted, size = base_size * 0.85),
          axis.title = element_text(colour = te$body),
          plot.title = element_text(colour = te$ink, face = "bold", size = base_size * 1.1),
          plot.subtitle = element_text(colour = te$muted, size = base_size * 0.9))
}

knitr::opts_chunk$set(fig.align = "center", dev = "png", dpi = 150)

## ---- helpers ----
ols <- function(X, y) {
  XtX <- crossprod(X); beta <- solve(XtX, crossprod(X, y))
  resid <- y - X %*% beta; s2 <- sum(resid^2) / (nrow(X) - ncol(X))
  list(beta = as.numeric(beta), se = sqrt(diag(solve(XtX)) * s2))
}
med_fit <- function(x, m, y) {
  e1 <- ols(cbind(1, x), m)              # m ~ x     gives a
  e2 <- ols(cbind(1, x, m), y)           # y ~ x + m gives c' and b
  a  <- unname(e1$beta[2]); se_a <- unname(e1$se[2])
  b  <- unname(e2$beta[3]); se_b <- unname(e2$se[3])
  ind <- a * b
  sob <- sqrt(b^2 * se_a^2 + a^2 * se_b^2)   # first-order Sobel (delta method)
  c(a = a, b = b, cprime = unname(e2$beta[2]), indirect = ind, sobelSE = sob)
}

## ---- data ----
a_t <- 0.35; b_t <- 0.35; cp_t <- 0.20
ind_true <- a_t * b_t
n <- 50

set.seed(157)
x <- rnorm(n)
m <- a_t * x + sqrt(1 - a_t^2) * rnorm(n)
y <- cp_t * x + b_t * m + rnorm(n)

fit <- med_fit(x, m, y)
prop_med <- unname(fit["indirect"] / (fit["cprime"] + fit["indirect"]))

## ---- sobel ----
sobelSE <- unname(fit["sobelSE"])
sob_lo  <- unname(fit["indirect"]) - 1.96 * sobelSE
sob_hi  <- unname(fit["indirect"]) + 1.96 * sobelSE

## ---- bootstrap ----
boot_ind <- function(x, m, y, B) {
  n <- length(x); out <- numeric(B)
  for (i in seq_len(B)) {
    idx <- sample.int(n, n, replace = TRUE)
    e1 <- ols(cbind(1, x[idx]), m[idx])
    e2 <- ols(cbind(1, x[idx], m[idx]), y[idx])
    out[i] <- e1$beta[2] * e2$beta[3]
  }
  out
}
set.seed(2157)
bd <- boot_ind(x, m, y, 5000)
bt_lo <- unname(quantile(bd, 0.025)); bt_hi <- unname(quantile(bd, 0.975))
skew  <- mean((bd - mean(bd))^3) / sd(bd)^3

## ---- fig-dist ----
xs <- seq(min(bd) - 0.02, max(bd) + 0.02, length.out = 400)
nd <- data.frame(x = xs, dens = dnorm(xs, mean = fit["indirect"], sd = sobelSE))
ci <- data.frame(method = c("Sobel (normal)", "Bootstrap (percentile)"),
                 lo = c(sob_lo, bt_lo), hi = c(sob_hi, bt_hi),
                 y = c(-0.9, -1.9), col = c(te$warm, te$forest))

ggplot() +
  geom_histogram(data = data.frame(bd), aes(bd, after_stat(density)),
                 bins = 45, fill = te$sage, colour = NA, alpha = 0.85) +
  geom_line(data = nd, aes(x, dens), colour = te$warm, linewidth = 0.9) +
  geom_vline(xintercept = 0, colour = te$muted, linewidth = 0.4, linetype = "22") +
  geom_vline(xintercept = fit["indirect"], colour = te$ink, linewidth = 0.5) +
  geom_segment(data = ci, aes(x = lo, xend = hi, y = y, yend = y), colour = ci$col, linewidth = 1.4) +
  geom_point(data = ci, aes(lo, y), colour = ci$col, size = 1.6) +
  geom_point(data = ci, aes(hi, y), colour = ci$col, size = 1.6) +
  geom_text(data = ci, aes((lo + hi)/2, y + 0.55, label = method), colour = ci$col, size = 3.0) +
  annotate("text", x = max(bd) * 0.7, y = max(nd$dens) * 0.8,
           label = "red curve: Sobel normal\n(symmetric, clips the right tail)",
           colour = te$warm, size = 2.9, hjust = 0, lineheight = 0.9) +
  labs(title = "The indirect effect a times b has a skewed sampling distribution",
       subtitle = sprintf("Bootstrap draws (green) lean right (skewness %.2f); the Sobel normal (red) misfits the tails", skew),
       x = "indirect effect  a x b", y = "density") +
  coord_cartesian(ylim = c(-2.4, NA), clip = "off") +
  theme_te(12) + theme(axis.text.y = element_blank())

## ---- coverage ----
set.seed(31557)
nsim <- 800; B <- 600
cov_sob <- 0L; cov_bt <- 0L
w_sob <- numeric(nsim); w_bt <- numeric(nsim)
for (s in seq_len(nsim)) {
  xs <- rnorm(n)
  ms <- a_t * xs + sqrt(1 - a_t^2) * rnorm(n)
  ys <- cp_t * xs + b_t * ms + rnorm(n)
  f  <- med_fit(xs, ms, ys)
  lo <- unname(f["indirect"]) - 1.96 * unname(f["sobelSE"])
  hi <- unname(f["indirect"]) + 1.96 * unname(f["sobelSE"])
  w_sob[s] <- hi - lo
  if (ind_true >= lo && ind_true <= hi) cov_sob <- cov_sob + 1L
  bb  <- boot_ind(xs, ms, ys, B)
  blo <- unname(quantile(bb, 0.025)); bhi <- unname(quantile(bb, 0.975))
  w_bt[s] <- bhi - blo
  if (ind_true >= blo && ind_true <= bhi) cov_bt <- cov_bt + 1L
}
cov_sob <- 100 * cov_sob / nsim
cov_bt  <- 100 * cov_bt / nsim

## ---- fig-coverage ----
cv <- data.frame(method = factor(c("Sobel (normal)", "Bootstrap (percentile)"),
                                 levels = c("Sobel (normal)", "Bootstrap (percentile)")),
                 coverage = c(cov_sob, cov_bt), width = c(mean(w_sob), mean(w_bt)))
ggplot(cv, aes(method, coverage, fill = method)) +
  geom_hline(yintercept = 95, colour = te$ink, linetype = "22", linewidth = 0.5) +
  geom_col(width = 0.55) +
  geom_text(aes(label = sprintf("%.1f%%", coverage)), vjust = 1.6, size = 3.8, colour = te$paper) +
  geom_text(aes(label = sprintf("mean width\n%.3f", width)), y = 46, size = 3.0,
            colour = te$paper, lineheight = 0.9) +
  annotate("text", x = Inf, y = 95, label = "nominal 95%", colour = te$ink, size = 3.0,
           hjust = 1.05, vjust = -0.5) +
  scale_fill_manual(values = c(`Sobel (normal)` = te$warm, `Bootstrap (percentile)` = te$forest),
                    guide = "none") +
  coord_cartesian(ylim = c(0, 100)) +
  labs(title = sprintf("Coverage of nominal 95%% intervals over %d simulated studies", nsim),
       subtitle = sprintf("n = %d per study, true indirect = %.3f; the Sobel interval undercovers", n, ind_true),
       x = NULL, y = "actual coverage (%)") +
  theme_te(12)
