# Meta-regression with moderators
# https://tidyecology.com/posts/meta-regression-moderators/
#
# 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-05-24

## ---- setup ----
library(ggplot2)
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE,
                      fig.width = 7.5, fig.height = 4.5, dpi = 100)
pal <- list(ink = "#16241d", body = "#2c3a31", forest = "#275139",
            label = "#46604a", sage = "#93a87f", paper = "#f5f4ee",
            line = "#dad9ca", faint = "#5d6b61", gold = "#cda23f",
            mown = "#2f8f63", warn = "#b5534e", low = "#c9b458", high = "#1d5b4e")
theme_te <- function(base_size = 12) {
  theme_minimal(base_size = base_size) +
    theme(panel.grid.minor = element_blank(),
          panel.grid.major = element_line(colour = pal$line, linewidth = 0.3),
          plot.background = element_rect(fill = pal$paper, colour = NA),
          panel.background = element_rect(fill = pal$paper, colour = NA),
          axis.text = element_text(colour = pal$body),
          axis.title = element_text(colour = pal$ink),
          plot.title = element_text(colour = pal$ink, face = "bold"),
          plot.subtitle = element_text(colour = pal$faint),
          legend.position = "bottom",
          legend.text = element_text(colour = pal$body),
          legend.title = element_text(colour = pal$ink))
}

## ---- sci-tex-helper ----
sci_tex <- function(x, digits = 2) {
  s <- formatC(x, format = "e", digits = digits)
  paste0("$", sub("e.*", "", s), " \\times 10^{", as.integer(sub(".*e", "", s)), "}$")
}

## ---- simulate ----
sim_study <- function(theta, ni) {
  x1 <- rnorm(ni, 0, 1); x2 <- rnorm(ni, theta, 1)
  sp <- sqrt(((ni - 1) * sd(x1)^2 + (ni - 1) * sd(x2)^2) / (2 * ni - 2))
  d  <- (mean(x2) - mean(x1)) / sp
  J  <- 1 - 3 / (4 * (2 * ni - 2) - 1)
  g  <- J * d
  vd <- (2 * ni) / (ni * ni) + d^2 / (2 * (2 * ni))
  c(g = g, v = J^2 * vd)
}
set.seed(153)
k <- 25; b0 <- 0.10; b1 <- 0.35; tau_r <- 0.15
x <- scale(runif(k, -2, 2))[, 1]              # standardised moderator
n <- sample(15:120, k, replace = TRUE)
theta_i <- b0 + b1 * x + rnorm(k, 0, tau_r)   # true effect depends on x
gv <- t(mapply(sim_study, theta_i, n))
g  <- gv[, "g"]; v <- gv[, "v"]

## ---- meta-regression ----
mreg <- function(y, v, X) {
  reml <- function(t2) {
    W   <- 1 / (v + t2); XtW <- t(X * W)
    b   <- solve(XtW %*% X, XtW %*% y); r <- y - X %*% b
    -0.5 * sum(log(v + t2)) - 0.5 * log(det(XtW %*% X)) - 0.5 * sum(W * r^2)
  }
  t2  <- optimize(reml, c(0, 10), maximum = TRUE)$maximum
  W   <- 1 / (v + t2); XtW <- t(X * W)
  Vb  <- solve(XtW %*% X); b <- Vb %*% (XtW %*% y)
  list(b = as.vector(b), se = sqrt(diag(Vb)), tau2 = t2, Vb = Vb)
}
X   <- cbind(1, x)
fit <- mreg(g, v, X)
QM  <- (fit$b[2] / fit$se[2])^2                 # Wald test for the slope
pQM <- pchisq(QM, 1, lower.tail = FALSE)
c(intercept = fit$b[1], slope = fit$b[2], se_slope = fit$se[2], QM = QM, p = pQM)

## ---- r2-analog ----
tau2_tot <- mreg(g, v, matrix(1, k, 1))$tau2    # total heterogeneity, same REML fit
tau2_res <- fit$tau2                            # residual after moderator
R2       <- max(0, (tau2_tot - tau2_res) / tau2_tot)
c(tau2_total = tau2_tot, tau2_residual = tau2_res, R2_analog = R2)

## ---- fig-mr-bubble ----
xg  <- seq(min(x), max(x), length.out = 100)
Xg  <- cbind(1, xg)
fitv <- as.vector(Xg %*% fit$b)
sef  <- sqrt(rowSums((Xg %*% fit$Vb) * Xg))
band <- data.frame(x = xg, y = fitv, lo = fitv - 1.96 * sef, hi = fitv + 1.96 * sef)
bub  <- data.frame(x = x, g = g, prec = 1 / (v + tau2_res))
ggplot(bub, aes(x, g)) +
  geom_ribbon(data = band, aes(x, ymin = lo, ymax = hi), inherit.aes = FALSE,
              fill = pal$sage, alpha = 0.30) +
  geom_line(data = band, aes(x, y), inherit.aes = FALSE,
            colour = pal$forest, linewidth = 0.9) +
  geom_point(aes(size = prec), colour = pal$forest, alpha = 0.55) +
  scale_size_area(max_size = 8, guide = "none") +
  labs(x = "moderator (standardised)", y = "Hedges' g",
       title = "Meta-regression: effect against a moderator") +
  theme_te()

## ---- type-one ----
freg <- function(y, v, X) {                     # fixed-effect meta-regression
  W <- 1 / v; XtW <- t(X * W); Vb <- solve(XtW %*% X); b <- Vb %*% (XtW %*% y)
  list(b = as.vector(b), se = sqrt(diag(Vb)))
}
type_one <- function(tt, nsim = 1000) {
  fx <- 0; rx <- 0
  for (s in seq_len(nsim)) {
    xx <- scale(runif(k, -2, 2))[, 1]; nn <- sample(15:120, k, replace = TRUE)
    thh <- b0 + 0 * xx + rnorm(k, 0, tt)        # NULL slope
    gg <- numeric(k); vv <- numeric(k)
    for (i in seq_len(k)) { r <- sim_study(thh[i], nn[i]); gg[i] <- r[1]; vv[i] <- r[2] }
    XX <- cbind(1, xx)
    ff <- freg(gg, vv, XX); if (abs(ff$b[2] / ff$se[2]) > 1.96) fx <- fx + 1
    rr <- tryCatch(mreg(gg, vv, XX), error = function(e) NULL)
    if (!is.null(rr) && abs(rr$b[2] / rr$se[2]) > 1.96) rx <- rx + 1
  }
  c(fixed = fx / nsim, random = rx / nsim)
}
set.seed(4242)
grid <- c(0, 0.1, 0.2, 0.3)
t1tab <- as.data.frame(t(sapply(grid, type_one)))
t1tab$tau <- grid
round(t1tab[, c("tau", "fixed", "random")], 3)

## ---- fig-mr-typeone ----
tdat <- rbind(
  data.frame(tau = grid, rate = t1tab$fixed, model = "Fixed-effect meta-reg"),
  data.frame(tau = grid, rate = t1tab$random, model = "Random-effects meta-reg"))
ggplot(tdat, aes(tau, rate, colour = model)) +
  geom_hline(yintercept = 0.05, linetype = "dashed", colour = pal$faint) +
  geom_line(linewidth = 0.8) + geom_point(size = 2.2) +
  scale_colour_manual(values = c("Fixed-effect meta-reg" = pal$warn,
                                 "Random-effects meta-reg" = pal$forest), name = NULL) +
  labs(x = "residual between-study SD", y = "false-positive rate",
       title = "Null moderator: false positives") +
  theme_te()
