# Doubly robust estimation with AIPW
# https://tidyecology.com/posts/doubly-robust-estimation-aipw/
#
# 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-14

## ---- 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))

## ---- dgp ----
expit <- function(z) 1 / (1 + exp(-z))
pa <- c(a0=-0.5, a1=0.6, a2=0.4, a3=0.5, aq=0.35)          # true propensity
pb <- c(b0=-0.6, bD=log(2), b1=0.7, b2=0.5, b3=0.6, bq=0.5) # true outcome

gen <- function(n) {
  x1 <- rnorm(n); x2 <- rnorm(n); x3 <- rbinom(n, 1, 0.5)
  ps <- expit(pa["a0"] + pa["a1"]*x1 + pa["a2"]*x2 + pa["a3"]*x3 + pa["aq"]*x1^2)
  d  <- rbinom(n, 1, ps)
  py <- expit(pb["b0"] + pb["bD"]*d + pb["b1"]*x1 + pb["b2"]*x2 + pb["b3"]*x3 + pb["bq"]*x1^2)
  y  <- rbinom(n, 1, py)
  data.frame(y, d, x1, x2, x3)
}
true_ate <- function(dat) {
  l1 <- pb["b0"]+pb["bD"]*1+pb["b1"]*dat$x1+pb["b2"]*dat$x2+pb["b3"]*dat$x3+pb["bq"]*dat$x1^2
  l0 <- pb["b0"]+pb["bD"]*0+pb["b1"]*dat$x1+pb["b2"]*dat$x2+pb["b3"]*dat$x3+pb["bq"]*dat$x1^2
  as.numeric(mean(expit(l1)) - mean(expit(l0)))
}

## ---- estimators ----
gcomp <- function(om, dat)
  mean(predict(om, transform(dat, d=1), type="response")) -
  mean(predict(om, transform(dat, d=0), type="response"))

ipw <- function(pm, dat) {                          # normalised (Hajek) ATE
  e <- predict(pm, type="response"); d <- dat$d; y <- dat$y
  sum((d/e)*y)/sum(d/e) - sum(((1-d)/(1-e))*y)/sum((1-d)/(1-e))
}
aipw <- function(pm, om, dat) {
  e  <- predict(pm, type="response"); d <- dat$d; y <- dat$y
  m1 <- predict(om, transform(dat, d=1), type="response")
  m0 <- predict(om, transform(dat, d=0), type="response")
  psi <- (d*(y-m1)/e + m1) - ((1-d)*(y-m0)/(1-e) + m0)
  list(ate = mean(psi), se = sd(psi)/sqrt(length(psi)))
}
fit_all <- function(dat) list(
  om_c = glm(y ~ d + x1 + x2 + x3 + I(x1^2), binomial, dat),   # correct outcome
  om_w = glm(y ~ d + x1 + x2 + x3,           binomial, dat),   # wrong outcome
  pm_c = glm(d ~ x1 + x2 + x3 + I(x1^2),      binomial, dat),  # correct propensity
  pm_w = glm(d ~ x1 + x2 + x3,                binomial, dat))  # wrong propensity

## ---- estimates ----
set.seed(167)
n <- 2000
dat <- gen(n)
f <- fit_all(dat)
e_c <- predict(f$pm_c, type="response")
TA <- true_ate(dat)

g_c <- gcomp(f$om_c, dat); g_w <- gcomp(f$om_w, dat)
i_c <- ipw(f$pm_c, dat);   i_w <- ipw(f$pm_w, dat)
a_cc <- aipw(f$pm_c, f$om_c, dat); a_cw <- aipw(f$pm_c, f$om_w, dat)
a_wc <- aipw(f$pm_w, f$om_c, dat); a_ww <- aipw(f$pm_w, f$om_w, dat)

data.frame(
  estimator = c("g-comp correct","g-comp wrong","IPW correct","IPW wrong",
                "AIPW ok/ok","AIPW ok/wrong","AIPW wrong/ok","AIPW wrong/wrong"),
  estimate  = round(c(g_c,g_w,i_c,i_w,a_cc$ate,a_cw$ate,a_wc$ate,a_ww$ate), 4),
  error     = round(c(g_c,g_w,i_c,i_w,a_cc$ate,a_cw$ate,a_wc$ate,a_ww$ate) - TA, 4))

## ---- mc ----
set.seed(16700)
nsim <- 1000
cols <- c("gc_w","ipw_w","aipw_cc","aipw_cw","aipw_wc","aipw_ww")
E <- matrix(NA_real_, nsim, length(cols), dimnames = list(NULL, cols))
tr <- numeric(nsim); cov_cc <- cov_cw <- cov_wc <- 0L
for (s in 1:nsim) {
  ds <- gen(n); ta <- true_ate(ds); tr[s] <- ta
  ff <- fit_all(ds)
  E[s,"gc_w"]  <- gcomp(ff$om_w, ds)
  E[s,"ipw_w"] <- ipw(ff$pm_w, ds)
  acc <- aipw(ff$pm_c, ff$om_c, ds); E[s,"aipw_cc"] <- acc$ate
  acw <- aipw(ff$pm_c, ff$om_w, ds); E[s,"aipw_cw"] <- acw$ate
  awc <- aipw(ff$pm_w, ff$om_c, ds); E[s,"aipw_wc"] <- awc$ate
  E[s,"aipw_ww"] <- aipw(ff$pm_w, ff$om_w, ds)$ate
  if (ta >= acc$ate-1.96*acc$se && ta <= acc$ate+1.96*acc$se) cov_cc <- cov_cc+1L
  if (ta >= acw$ate-1.96*acw$se && ta <= acw$ate+1.96*acw$se) cov_cw <- cov_cw+1L
  if (ta >= awc$ate-1.96*awc$se && ta <= awc$ate+1.96*awc$se) cov_wc <- cov_wc+1L
}
mtruth <- mean(tr)
bias <- colMeans(E) - mtruth
round(rbind(mean = colMeans(E), bias = bias, sd = apply(E, 2, sd)), 4)

## ---- fig-bias ----
lab <- c(gc_w="g-computation (wrong outcome)", ipw_w="IPW (wrong propensity)",
         aipw_ww="AIPW (both wrong)", aipw_cc="AIPW (both correct)",
         aipw_cw="AIPW (propensity ok, outcome wrong)", aipw_wc="AIPW (outcome ok, propensity wrong)")
ord <- c("gc_w","ipw_w","aipw_ww","aipw_cc","aipw_cw","aipw_wc")
b <- data.frame(key=ord, bias=bias[ord], se=apply(E,2,sd)[ord]/sqrt(nsim))
b$label <- factor(lab[ord], levels=lab[ord])
b$kind  <- ifelse(abs(b$bias) < 0.01, "consistent", "biased")
p_bias <- ggplot(b, aes(bias, label, colour=kind)) +
  geom_vline(xintercept=0, colour=te$label, linewidth=0.5) +
  geom_pointrange(aes(xmin=bias-1.96*se, xmax=bias+1.96*se), linewidth=0.9, size=0.6) +
  scale_colour_manual(values=c(consistent=te$mown, biased=te$abandoned), guide="none") +
  scale_x_continuous(limits=c(-0.01, 0.06)) +
  labs(x="mean bias in the estimated ATE (over 1000 datasets)", y=NULL,
       title="AIPW stays centred when either nuisance model is correct",
       subtitle="The bias returns only when both nuisance models are wrong") +
  theme_te() + theme(plot.title.position = "plot")
p_bias

## ---- thumb ----
ggsave("thumbnail.png",
       p_bias + labs(title = NULL, subtitle = NULL) +
         theme(axis.title.x = element_text(size = 9.5),
               plot.margin = margin(8, 14, 8, 8)),
       width = 7.5, height = 5.4, dpi = 100, bg = te$paper)

## ---- coverage ----
c(both_correct = cov_cc, ps_ok_out_wrong = cov_cw, out_ok_ps_wrong = cov_wc) / nsim

## ---- fig-dist ----
dd <- rbind(data.frame(ate=E[,"aipw_cc"], est="AIPW (both correct)"),
            data.frame(ate=E[,"gc_w"],    est="g-computation (wrong outcome)"),
            data.frame(ate=E[,"ipw_w"],   est="IPW (wrong propensity)"))
dd$est <- factor(dd$est, levels=c("AIPW (both correct)","g-computation (wrong outcome)","IPW (wrong propensity)"))
ggplot(dd, aes(ate, fill=est, colour=est)) +
  geom_density(alpha=0.32, linewidth=0.7) +
  geom_vline(xintercept=mtruth, linetype="dashed", colour=te$ink) +
  annotate("text", x=mtruth, y=Inf, label="true ATE", vjust=1.6, hjust=1.08, size=3, colour=te$ink) +
  scale_y_continuous(expand=expansion(mult=c(0.05, 0.15))) +
  scale_fill_manual(values=c("AIPW (both correct)"=te$mown,
                             "g-computation (wrong outcome)"=te$abandoned,
                             "IPW (wrong propensity)"=te$grazed), name=NULL) +
  scale_colour_manual(values=c("AIPW (both correct)"=te$forest,
                               "g-computation (wrong outcome)"=te$abandoned,
                               "IPW (wrong propensity)"=te$low), name=NULL) +
  labs(x="estimated ATE (risk difference)", y="density",
       title="AIPW with both models correct centres on the truth",
       subtitle="Both misspecified single-model estimators are shifted away from the true ATE") +
  theme_te() + theme(legend.position="top")
