# Propensity scores and IPW
# https://tidyecology.com/posts/propensity-scores-and-ipw/
#
# 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-11-14

## ---- setup ----
library(ggplot2); library(dplyr); library(tidyr)
paper<-"#f5f4ee"; ink<-"#16241d"; body<-"#2c3a31"; forest<-"#275139"
sage<-"#93a87f"; line<-"#dad9ca"; faint<-"#5d6b61"; brick<-"#b5534e"
theme_te <- function(base=12) theme_minimal(base_size=base) +
  theme(plot.background=element_rect(fill=paper,colour=NA),
        panel.background=element_rect(fill=paper,colour=NA),
        panel.grid.major=element_line(colour=line,linewidth=0.3),
        panel.grid.minor=element_blank(),
        axis.text=element_text(colour=body), axis.title=element_text(colour=ink),
        plot.title=element_text(colour=ink,face="bold"),
        plot.subtitle=element_text(colour=faint), legend.position="top")

## ---- data ----
set.seed(4165)
n <- 2000; tau <- 1.0
X1<-rnorm(n); X2<-rnorm(n); X3<-rnorm(n); X4<-rnorm(n)
D <- rbinom(n, 1, plogis(0.5*X1 + 0.7*X2 - 0.6*X3 + 0.8*X4))   # non-random treatment
Y <- tau*D + 1.0*X1 + 0.8*X2 + 1.2*X3 + 0.9*X4 + rnorm(n, 0, 1)

## ---- naive ----
naive <- mean(Y[D==1]) - mean(Y[D==0])

## ---- ipw ----
ps <- glm(D ~ X1+X2+X3+X4, family=binomial)
e  <- fitted(ps)                       # propensity scores
w  <- D/e + (1-D)/(1-e)                 # inverse-probability weights
ipw <- coef(lm(Y ~ D, weights=w))["D"]
adj <- coef(lm(Y ~ D + X1+X2+X3+X4))["D"]   # outcome regression, for comparison

## ---- boot ----
set.seed(6165); B <- 600
bs <- t(sapply(1:B, function(b){
  i <- sample(n, n, replace=TRUE)
  d <- data.frame(Y=Y[i],D=D[i],X1=X1[i],X2=X2[i],X3=X3[i],X4=X4[i])
  nv <- mean(d$Y[d$D==1]) - mean(d$Y[d$D==0])
  ee <- fitted(glm(D~X1+X2+X3+X4, family=binomial, data=d))
  ww <- d$D/ee + (1-d$D)/(1-ee)
  c(naive=nv, ipw=unname(coef(lm(Y~D, weights=ww, data=d))["D"]))
}))
ci_naive <- quantile(bs[,"naive"], c(.025,.975))
ci_ipw   <- quantile(bs[,"ipw"],   c(.025,.975))

## ---- balance ----
smd <- function(x, D, w=rep(1,length(x))){
  mt <- weighted.mean(x[D==1], w[D==1]); mc <- weighted.mean(x[D==0], w[D==0])
  sp <- sqrt((var(x[D==1]) + var(x[D==0]))/2); (mt - mc)/sp
}
Xs  <- list(X1=X1, X2=X2, X3=X3, X4=X4)
bal <- data.frame(var=names(Xs),
  before = sapply(Xs, function(x) smd(x, D)),
  after  = sapply(Xs, function(x) smd(x, D, w)))

## ---- fig-balance ----
bl <- bal %>% mutate(before=abs(before), after=abs(after)) %>%
  pivot_longer(c(before,after), names_to="stage", values_to="smd") %>%
  mutate(stage=factor(stage, levels=c("before","after")))
ggplot(bl, aes(smd, var)) +
  geom_vline(xintercept=0.1, linetype="dashed", colour=faint, linewidth=0.4) +
  geom_line(aes(group=var), colour=line, linewidth=1.1) +
  geom_point(aes(colour=stage), size=3.2) +
  annotate("text", x=0.1, y=0.55, label="0.1 threshold", hjust=-0.05, colour=faint, size=3.1) +
  scale_colour_manual(values=c("before"=brick,"after"=forest),
                      labels=c("unweighted","after IPW"), name=NULL) +
  labs(x="absolute standardised mean difference", y=NULL,
       title="Weighting balances the confounders",
       subtitle="Each confounder moves from imbalanced (unweighted) to balanced (after IPW)") +
  theme_te()

## ---- overlap ----
e_range <- range(e); max_w <- max(w); ess <- sum(w)^2 / sum(w^2)

## ---- fig-overlap ----
ov <- data.frame(e=e, grp=factor(ifelse(D==1,"treated","control"), levels=c("control","treated")))
ggplot(ov, aes(e, fill=grp, colour=grp)) +
  geom_density(alpha=0.35, linewidth=0.6) +
  scale_fill_manual(values=c("control"=brick,"treated"=forest), name=NULL) +
  scale_colour_manual(values=c("control"=brick,"treated"=forest), name=NULL) +
  labs(x="estimated propensity score  P(treated | confounders)", y="density",
       title="Overlap: both groups appear across the propensity range",
       subtitle="Mass near 0 and 1 produces large weights and strains positivity") +
  theme_te()

## ---- missing ----
ps_mis <- glm(D ~ X1+X2+X3, family=binomial)          # X4 omitted
w_mis  <- D/fitted(ps_mis) + (1-D)/(1-fitted(ps_mis))
ipw_mis <- coef(lm(Y ~ D, weights=w_mis))["D"]

## ---- mc ----
set.seed(9165); nsim <- 400
mc <- t(sapply(1:nsim, function(s){
  X1<-rnorm(n);X2<-rnorm(n);X3<-rnorm(n);X4<-rnorm(n)
  D <- rbinom(n,1, plogis(0.5*X1+0.7*X2-0.6*X3+0.8*X4))
  Y <- tau*D + 1.0*X1 + 0.8*X2 + 1.2*X3 + 0.9*X4 + rnorm(n,0,1)
  nv <- mean(Y[D==1]) - mean(Y[D==0])
  ee <- fitted(glm(D~X1+X2+X3+X4, family=binomial)); ww <- D/ee + (1-D)/(1-ee)
  c(naive=nv, ipw=unname(coef(lm(Y~D, weights=ww))["D"]))
}))
