# Checking for publication bias
# https://tidyecology.com/posts/publication-bias-funnel-egger/
#
# 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),
          strip.text = element_text(colour = pal$ink, face = "bold"),
          legend.position = "bottom",
          legend.text = element_text(colour = pal$body),
          legend.title = element_text(colour = pal$ink))
}

## ---- 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)
}
wmean <- function(y, v) sum(y / v) / sum(1 / v)
mu <- 0.30

## unbiased: keep all 30 studies
set.seed(50)
ku <- 30; nu <- sample(10:90, ku, replace = TRUE)
gv <- t(mapply(sim_study, rnorm(ku, mu, 0.05), nu))
gu <- gv[, "g"]; seu <- sqrt(gv[, "v"])

## biased: simulate a pool, publish significant-positive for sure, else with p = 0.08
set.seed(302)
pool <- 260; np <- sample(10:90, pool, replace = TRUE)
gv <- t(mapply(sim_study, rnorm(pool, mu, 0.05), np))
gp <- gv[, "g"]; sep <- sqrt(gv[, "v"])
published <- (gp / sep > 1.64) | (runif(pool) < 0.08)
idx <- which(published)[1:32]
gb <- gp[idx]; seb <- sep[idx]
c(pooled_unbiased = wmean(gu, seu^2), pooled_biased = wmean(gb, seb^2))

## ---- fig-pb-funnel ----
fdat <- rbind(
  data.frame(g = gu, se = seu, set = "Unbiased (all studies)"),
  data.frame(g = gb, se = seb, set = "Filtered (selective publication)"))
fdat$set <- factor(fdat$set, levels = c("Unbiased (all studies)",
                                         "Filtered (selective publication)"))
poolv <- c("Unbiased (all studies)" = wmean(gu, seu^2),
           "Filtered (selective publication)" = wmean(gb, seb^2))
sey  <- seq(0, max(fdat$se), length.out = 40)
edge <- do.call(rbind, lapply(names(poolv), function(nm) rbind(
  data.frame(x = poolv[nm] - 1.96 * sey, se = sey, set = nm, side = "l"),
  data.frame(x = poolv[nm] + 1.96 * sey, se = sey, set = nm, side = "r"))))
edge$set <- factor(edge$set, levels = levels(fdat$set))
cen <- data.frame(x = as.numeric(poolv), set = factor(names(poolv), levels = levels(fdat$set)))
ggplot(fdat, aes(g, se)) +
  geom_line(data = edge, aes(x, se, group = side), linetype = "dashed",
            colour = pal$faint, linewidth = 0.4) +
  geom_vline(data = cen, aes(xintercept = x), colour = pal$sage, linewidth = 0.4) +
  geom_point(colour = pal$forest, alpha = 0.7, size = 1.9) +
  scale_y_reverse() +
  facet_wrap(~ set) +
  labs(x = "Hedges' g", y = "standard error",
       title = "Funnel plots: symmetric versus asymmetric") +
  theme_te()

## ---- egger ----
egger <- function(g, se) {
  m <- lm(I(g / se) ~ I(1 / se))
  s <- summary(m)$coefficients
  c(intercept = s[1, 1], se = s[1, 2], t = s[1, 3], p = s[1, 4])
}
eu <- egger(gu, seu)
eb <- egger(gb, seb)
rbind(unbiased = eu, biased = eb)

## ---- trim-fill ----
trimfill <- function(g, se) {
  v <- se^2; k <- length(g); mu0 <- wmean(g, v)
  side <- if (sum(sign(g - mu0)) >= 0) 1 else -1     # 1 = excess on the right
  yy <- side * g; ord <- order(yy)
  k0 <- 0; k0o <- -1; mu <- mu0; it <- 0
  while (k0 != k0o && it < 100) {
    it <- it + 1; k0o <- k0
    keep <- if (k0 > 0) ord[1:(k - k0)] else ord
    mu  <- wmean(g[keep], v[keep]); cen <- side * mu
    dd  <- yy - cen; r <- rank(abs(dd)); Tn <- sum(r[dd > 0])
    k0  <- max(0, round((4 * Tn - k * (k + 1)) / (2 * k - 1)))
  }
  cen <- side * mu
  if (k0 > 0) {
    ex <- ord[(k - k0 + 1):k]
    ga <- c(g, side * (2 * cen - yy[ex])); sa <- c(se, se[ex])
    imp <- (k + 1):(k + k0)
  } else { ga <- g; sa <- se; imp <- integer(0) }
  list(k0 = k0, mu_naive = wmean(g, v), mu_fill = wmean(ga, sa^2),
       ga = ga, sa = sa, imp = imp)
}
tu <- trimfill(gu, seu)
tb <- trimfill(gb, seb)
rbind(unbiased = c(k0 = tu$k0, naive = tu$mu_naive, filled = tu$mu_fill),
      biased   = c(k0 = tb$k0, naive = tb$mu_naive, filled = tb$mu_fill))

## ---- fig-pb-trimfill ----
tf <- data.frame(g = tb$ga, se = tb$sa,
                 kind = ifelse(seq_along(tb$ga) %in% tb$imp, "imputed", "observed"))
sey2 <- seq(0, max(tf$se), length.out = 40)
edge2 <- rbind(
  data.frame(x = tb$mu_fill - 1.96 * sey2, se = sey2, side = "l"),
  data.frame(x = tb$mu_fill + 1.96 * sey2, se = sey2, side = "r"))
ggplot(tf, aes(g, se, colour = kind)) +
  geom_line(data = edge2, aes(x, se, group = side), inherit.aes = FALSE,
            linetype = "dashed", colour = pal$faint, linewidth = 0.4) +
  geom_vline(xintercept = tb$mu_naive, colour = pal$faint,
             linetype = "dashed", linewidth = 0.5) +
  geom_vline(xintercept = tb$mu_fill, colour = pal$forest,
             linetype = "dashed", linewidth = 0.6) +
  geom_point(size = 2.1, alpha = 0.8) +
  scale_y_reverse() +
  scale_colour_manual(values = c(observed = pal$forest, imputed = pal$warn),
                      name = NULL) +
  labs(x = "Hedges' g", y = "standard error",
       title = "Trim-and-fill: imputing the missing studies") +
  theme_te()
