# Sensitivity to unmeasured confounding
# https://tidyecology.com/posts/sensitivity-to-unmeasured-confounding/
#
# 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-10-08

## ---- setup ----
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", message = FALSE, warning = FALSE,
                      dev = "png", dpi = 110, fig.width = 8, fig.height = 4.4)
library(ggplot2)
te <- list(ink="#16241d", body="#2c3a31", forest="#275139", label="#46604a",
           paper="#f5f4ee", line="#dad9ca", faint="#5d6b61", grazed="#cda23f",
           mown="#2f8f63", abandoned="#b5534e", high="#1d5b4e", low="#c9b458")
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$faint), axis.title=element_text(colour=te$body),
          plot.title=element_text(colour=te$ink,face="bold"), plot.subtitle=element_text(colour=te$body),
          legend.text=element_text(colour=te$body), legend.title=element_text(colour=te$body),
          plot.margin=margin(10,12,10,10))
}
expit <- function(z) 1 / (1 + exp(-z))

## ---- 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)), "}$")
}

## ---- dgp ----
expit <- function(z) 1 / (1 + exp(-z))
pa <- c(a0=-0.2, ax=0.5, au=log(1.7))                # treatment: u odds ratio 1.7
pb <- c(b0=-0.8, bD=log(1.8), bx=0.5, bu=log(2.0))   # outcome: u odds ratio 2.0, true effect 1.8

gen <- function(n) {
  x <- rnorm(n); u <- rbinom(n, 1, 0.5)
  d <- rbinom(n, 1, expit(pa["a0"] + pa["ax"]*x + pa["au"]*u))
  y <- rbinom(n, 1, expit(pb["b0"] + pb["bD"]*d + pb["bx"]*x + pb["bu"]*u))
  data.frame(y, d, x, u)
}
true_rr <- function(dat) {                           # true marginal risk ratio (over x and u)
  l1 <- pb["b0"]+pb["bD"]*1+pb["bx"]*dat$x+pb["bu"]*dat$u
  l0 <- pb["b0"]+pb["bD"]*0+pb["bx"]*dat$x+pb["bu"]*dat$u
  as.numeric(mean(expit(l1))/mean(expit(l0)))
}
gcomp_rr <- function(model, dat) {                   # marginal RR by standardisation
  mean(predict(model, transform(dat, d=1), type="response")) /
  mean(predict(model, transform(dat, d=0), type="response"))
}
evalue      <- function(rr) { r <- ifelse(rr < 1, 1/rr, rr); as.numeric(r + sqrt(r*(r-1))) }
bias_factor <- function(rud, ruy) as.numeric(rud*ruy/(rud+ruy-1))

## ---- observed ----
set.seed(169)
n <- 3000
dat <- gen(n)
m_x <- glm(y ~ d + x, binomial, dat)                 # adjusts for x only; u is unmeasured
RR  <- gcomp_rr(m_x, dat)
set.seed(1690); B <- 500; bs <- numeric(B)
for (b in 1:B) { idx <- sample.int(n, n, TRUE); db <- dat[idx, ]
  bs[b] <- gcomp_rr(glm(y ~ d + x, binomial, db), db) }
ci <- quantile(bs, c(.025, .975))
TR <- true_rr(dat)
c(true_RR = TR, observed_RR = RR, lower = ci[[1]], upper = ci[[2]])

## ---- evalues ----
ev_pt <- evalue(RR)
ev_ci <- evalue(ci[[1]])                             # applied to the confidence limit nearest the null
c(evalue_point = ev_pt, evalue_ci = ev_ci)

## ---- fig-evalue ----
E <- ev_pt
a <- seq(RR+0.002, 4, 0.01); bb <- RR*(a-1)/(a-RR)
curve <- rbind(data.frame(rud=a, ruy=bb), data.frame(rud=bb, ruy=a))
curve <- curve[curve$rud<=4 & curve$ruy<=4 & curve$ruy>=1 & curve$rud>=1, ]
RUD <- max(sapply(0:1, function(k) mean(dat$u[dat$d==1]==k) / mean(dat$u[dat$d==0]==k)))
RUY <- max(sapply(0:1, function(dd)
  mean(dat$y[dat$u==1 & dat$d==dd]) / mean(dat$y[dat$u==0 & dat$d==dd])))
ggplot(curve[order(curve$rud), ], aes(rud, ruy)) +
  geom_ribbon(data=data.frame(x=a, lo=pmin(bb,4)), aes(x=x, ymin=lo, ymax=4),
              inherit.aes=FALSE, fill=te$abandoned, alpha=0.10) +
  geom_line(linewidth=1, colour=te$abandoned) +
  geom_point(aes(x=E, y=E), colour=te$forest, size=3.6, shape=18) +
  geom_point(aes(x=RUD, y=RUY), colour=te$ink, size=3) +
  annotate("text", x=E, y=E, label=paste0("  E-value ", sprintf('%.2f', E)), hjust=0, size=3, colour=te$forest) +
  annotate("text", x=RUD, y=RUY, label="  u (measured strength)", hjust=0, size=3, colour=te$ink) +
  annotate("text", x=3.4, y=3.6, label="confounders here\nexplain the effect away", size=3, colour=te$abandoned) +
  coord_cartesian(xlim=c(1,4), ylim=c(1,4)) +
  labs(x="confounder-treatment risk ratio", y="confounder-outcome risk ratio",
       title="How strong would an unmeasured confounder have to be?",
       subtitle=paste0("To explain away the observed RR of ", sprintf('%.2f', RR),
                       ", a confounder must reach the curve")) +
  theme_te()

## ---- calibrate ----
BFu <- bias_factor(RUD, RUY)
c(RR_ud = RUD, RR_uy = RUY, bias_factor = BFu, observed_RR = RR)
c(u_can_explain_point = BFu >= RR, u_reaches_evalue = (RUD >= ev_pt && RUY >= ev_pt))

## ---- rosenbaum ----
ps <- glm(d ~ x, binomial, dat); lps <- qlogis(fitted(ps))
it <- which(dat$d==1); ic <- which(dat$d==0)
ord <- it[order(lps[it], decreasing=TRUE)]; avail <- rep(TRUE, length(ic)); cal <- 0.2*sd(lps)
tp <- cp <- integer(0)
for (t in ord) { dd <- abs(lps[ic]-lps[t]); dd[!avail] <- Inf; j <- which.min(dd)
  if (is.finite(dd[j]) && dd[j] <= cal) { avail[j] <- FALSE; tp <- c(tp,t); cp <- c(cp,ic[j]) } }
bcnt <- sum(dat$y[tp]==1 & dat$y[cp]==0)              # pairs: treated has event, control does not
ccnt <- sum(dat$y[tp]==0 & dat$y[cp]==1)              # the reverse
m <- bcnt + ccnt
p_one <- pbinom(bcnt-1, m, 0.5, lower.tail=FALSE)     # McNemar, one sided, no hidden bias

p_upper <- function(g) pbinom(bcnt-1, m, g/(1+g), lower.tail=FALSE)   # worst-case p at bias g
gammas <- seq(1, 6, 0.01); pu <- sapply(gammas, p_upper)
gstar <- gammas[which(pu > 0.05)[1]]
c(pairs = length(tp), discordant = m, b = bcnt, c = ccnt, mcnemar_p = p_one, gamma_star = gstar)

## ---- fig-rosenbaum ----
sens <- data.frame(gamma=gammas, p=pu)
ggplot(sens, aes(gamma, p)) +
  geom_hline(yintercept=0.05, linetype="dashed", colour=te$label) +
  geom_vline(xintercept=gstar, colour=te$abandoned, linewidth=0.7) +
  geom_vline(xintercept=exp(pa["au"]), colour=te$forest, linewidth=0.7, linetype="dotted") +
  geom_line(linewidth=1, colour=te$ink) +
  annotate("text", x=gstar + 0.45, y=0.55, label=paste0("Gamma* = ", sprintf('%.2f', gstar)), hjust=0, size=3, colour=te$abandoned) +
  annotate("text", x=exp(pa["au"]), y=0.82, label=paste0("u's bias ", sprintf('%.1f', exp(pa["au"])), "  "), hjust=1, size=3, colour=te$forest) +
  annotate("text", x=4.5, y=0.09, label="0.05", size=3, colour=te$label) +
  coord_cartesian(xlim=c(1,5), ylim=c(0,1)) +
  labs(x="hidden-bias parameter Gamma", y="worst-case one-sided p-value",
       title="The matched result withstands hidden bias up to Gamma*",
       subtitle="The assignment bias from u sits below Gamma*, so a confounder like u does not overturn it") +
  theme_te()

## ---- mc ----
set.seed(16900); nsim <- 300; eb <- tb <- numeric(nsim)
for (s in 1:nsim) { ds <- gen(n); tb[s] <- true_rr(ds)
  eb[s] <- gcomp_rr(glm(y ~ d + x, binomial, ds), ds) }
c(mean_true_RR = mean(tb), mean_observed_RR = mean(eb), bias = mean(eb) - mean(tb))
