MR and mediation

Author

Gibran Hemani

Published

November 11, 2026

Background

MR and mediation are asymptotically equivalent when there is no confounding and horizontal pleiotropy, and no measurement error.

b_iv = b_med + c / a

where c is the horizontal pleiotropy effect and a is the SNP-exposure effect.

but when there’s confounding IV isn’t affected but

b_med = b + u1 * u2 / (1 + u1^2)

where u1 and u2 are the confounding effects on the exposure and outcome, respectively.

As sample size increases, the bias in b_med gets more precise.

This simulation illustrates the parameter space in which inference would be wrong in b_med but correct in b_iv.

# 1. Data-generating model
dgp <- function(n = 1000, beta = 0.3,
                u_x = 1, u_y = -0.6,
                alpha = 0.6, c = 0) {

  g <- rnorm(n)
  u <- rnorm(n)

  x <- alpha * g + u_x * u + rnorm(n)
  y <- beta * x + u_y * u + c * g + rnorm(n)

  data.frame(g, x, y)
}

# 2. Fit mediation and IV models
estimate <- function(d) {

  # Mediation: Y ~ X + G
  med <- summary(lm(y ~ x + g, data = d))$coefficients

  # IV: Wald ratio, equivalent to just-identified 2SLS
  g <- d$g - mean(d$g)
  x <- d$x - mean(d$x)
  y <- d$y - mean(d$y)

  denom <- sum(g * x)
  b_iv <- sum(g * y) / denom

  # Robust IV standard error (not naive second-stage SE)
  resid_iv <- y - b_iv * x
  se_iv <- sqrt(sum((g * resid_iv)^2)) / abs(denom)
  p_iv <- 2 * pnorm(-abs(b_iv / se_iv))

  c(
    beta_med = med["x", "Estimate"],
    p_med = med["x", "Pr(>|t|)"],
    c_prime = med["g", "Estimate"],
    beta_iv = b_iv,
    p_iv = p_iv
  )
}

# 3. Example: complete cancellation in mediation
set.seed(1)
d <- dgp(n = 10000, beta = 0.3,
         u_x = 1, u_y = -0.6,
         alpha = 0.6, c = 0)

round(estimate(d), 3)
beta_med    p_med  c_prime  beta_iv     p_iv 
  -0.011    0.170    0.189    0.302    0.000 
# 4. Explore confounding parameter space
set.seed(2)

pars <- list(beta = 0.3, alpha = 0.6, c = 0)
n_values <- c(500, 1000, 2000)
n_sims <- 50  # Increase for more precise probabilities

ux <- seq(-2, 2, by = 0.25)
uy <- seq(-2, 2, by = 0.25)
grid <- expand.grid(u_x = ux, u_y = uy)

summarise_estimates <- function(est, beta_true) {
  na_real <- as.numeric(NA)

  med_null <- est["p_med", ] >= 0.05
  med_wrong <- est["p_med", ] < 0.05 & est["beta_med", ] * beta_true < 0
  med_bad <- med_null | med_wrong

  iv_right_sign <- est["beta_iv", ] * beta_true > 0

  c(
    fn_med = mean(med_null),
    fn_iv = mean(est["p_iv", ] >= 0.05),
    wrong_sign_med = mean(med_wrong),
    mean_beta_med = mean(est["beta_med", ]),
    frac_med_bad = mean(med_bad),

    # Conditional sign accuracy of MR/IV
    frac_iv_right_given_med_bad = if (sum(med_bad) == 0) na_real else mean(iv_right_sign[med_bad]),
    frac_iv_right_given_med_null = if (sum(med_null) == 0) na_real else mean(iv_right_sign[med_null]),
    frac_iv_right_given_med_wrong = if (sum(med_wrong) == 0) na_real else mean(iv_right_sign[med_wrong]),

    # Store counts so we can pool across grid points later
    n_med_bad = sum(med_bad),
    n_iv_right_med_bad = sum(iv_right_sign & med_bad),
    n_med_null = sum(med_null),
    n_iv_right_med_null = sum(iv_right_sign & med_null),
    n_med_wrong = sum(med_wrong),
    n_iv_right_med_wrong = sum(iv_right_sign & med_wrong)
  )
}

simulate_grid_for_n <- function(n, grid, n_sims, pars) {
  out <- matrix(NA_real_, nrow(grid), 14)
  colnames(out) <- c(
    "fn_med",
    "fn_iv",
    "wrong_sign_med",
    "mean_beta_med",
    "frac_med_bad",
    "frac_iv_right_given_med_bad",
    "frac_iv_right_given_med_null",
    "frac_iv_right_given_med_wrong",
    "n_med_bad",
    "n_iv_right_med_bad",
    "n_med_null",
    "n_iv_right_med_null",
    "n_med_wrong",
    "n_iv_right_med_wrong"
  )

  for (i in seq_len(nrow(grid))) {
    est <- replicate(n_sims, estimate(
      dgp(
        n = n,
        beta = pars$beta,
        alpha = pars$alpha,
        c = pars$c,
        u_x = grid$u_x[i],
        u_y = grid$u_y[i]
      )
    ))

    out[i, ] <- summarise_estimates(est, beta_true = pars$beta)
  }

  cbind(
    data.frame(n = n, u_x = grid$u_x, u_y = grid$u_y),
    as.data.frame(out)
  )
}

grid_by_n <- do.call(
  rbind,
  lapply(n_values, simulate_grid_for_n,
         grid = grid, n_sims = n_sims, pars = pars)
)


# 5. Visualise how sample size affects MR sign accuracy

cols <- colorRampPalette(c("white", "gold", "firebrick"))(100)

op <- par(mfrow = c(1, length(n_values)))

for (n in n_values) {
  n_i <- n
  gsub <- subset(grid_by_n, n == n_i)
  z <- matrix(gsub$frac_iv_right_given_med_bad, nrow = length(ux))

  image(
    ux, uy, z,
    col = cols, zlim = c(0, 1),
    xlab = "u_x", ylab = "u_y",
    main = paste0(
      "MR right sign | mediation null/wrong\n(n = ",
      n,
      ")"
    )
  )

  # Black curve: population mediation estimate = 0
  contour(
    ux, uy,
    outer(ux, uy, function(a, b) pars$beta + a * b / (1 + a^2)),
    levels = 0, add = TRUE,
    drawlabels = FALSE, lwd = 2
  )
}

par(op)


# 6. Pooled probability across the entire grid

safe_ratio <- function(num, den) {
  if (den == 0) NA_real_ else num / den
}

pooled <- do.call(
  rbind,
  lapply(n_values, function(n) {
    n_i <- n
    gsub <- subset(grid_by_n, n == n_i)

    data.frame(
      n = n_i,
      frac_iv_right_given_med_bad =
        safe_ratio(sum(gsub$n_iv_right_med_bad), sum(gsub$n_med_bad)),
      frac_iv_right_given_med_null =
        safe_ratio(sum(gsub$n_iv_right_med_null), sum(gsub$n_med_null)),
      frac_iv_right_given_med_wrong =
        safe_ratio(sum(gsub$n_iv_right_med_wrong), sum(gsub$n_med_wrong)),
      frac_med_bad = mean(gsub$frac_med_bad)
    )
  })
)

plot(
  pooled$n,
  pooled$frac_iv_right_given_med_bad,
  type = "b", pch = 19,
  ylim = c(0, 1),
  xlab = "Sample size (n)",
  ylab = "P(MR has correct sign)",
  main = "MR sign accuracy when mediation fails"
)
lines(pooled$n, pooled$frac_iv_right_given_med_null, type = "b", pch = 19, col = "steelblue")
lines(pooled$n, pooled$frac_iv_right_given_med_wrong, type = "b", pch = 19, col = "firebrick")
legend(
  "bottomright",
  legend = c("Mediation null or wrong sign", "Mediation null", "Mediation wrong sign"),
  col = c("black", "steelblue", "firebrick"),
  pch = 19, bty = "n"
)


sessionInfo()
R version 4.6.2 (2026-10-29)
Platform: aarch64-apple-darwin23
Running under: macOS Sequoia 15.2

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] htmlwidgets_1.6.4 compiler_4.6.2    fastmap_1.2.0     cli_3.6.6        
 [5] tools_4.6.2       htmltools_0.5.9   otel_0.2.0        yaml_2.3.12      
 [9] codetools_0.2-20  rmarkdown_2.31    knitr_1.51        jsonlite_2.0.0   
[13] xfun_0.57         digest_0.6.39     rlang_1.3.0       evaluate_1.0.5