# Confounding and backdoor adjustment
# https://tidyecology.com/posts/confounding-and-backdoor-adjustment/
#
# 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-07

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

## ---- confounded-fit ----
set.seed(159)
n   <- 300
bX  <- 0.40                 # true causal effect of grazing on richness
bZ  <- 0.60                 # soil productivity -> richness
aZX <- 0.80                 # soil productivity -> grazing
Z <- rnorm(n)
X <- aZX * Z + rnorm(n, 0, 0.6)
Y <- bX * X + bZ * Z + rnorm(n, 0, 1)

m_naive <- lm(Y ~ X)        # travels the backdoor X <- Z -> Y
m_adj   <- lm(Y ~ X + Z)    # blocks it by conditioning on Z

est_naive <- coef(m_naive)["X"]; ci_naive <- confint(m_naive)["X", ]
est_adj   <- coef(m_adj)["X"];   ci_adj   <- confint(m_adj)["X", ]

## ---- analytic ----
naive_pop <- bX + bZ * aZX / (aZX^2 + 0.6^2)   # population cov(X, Z) / var(X)

## ---- fig-estimates ----
set.seed(1590)
n2 <- 300
Xm <- rnorm(n2)
M  <- 0.7 * Xm + rnorm(n2, 0, 0.7)          # X -> M
Ym <- 0.3 * Xm + 0.6 * M + rnorm(n2, 0, 1)  # X -> Y (direct) and M -> Y
total_true <- 0.3 + 0.7 * 0.6
m_med <- lm(Ym ~ Xm + M)                     # conditions on a mediator
est_med <- coef(m_med)["Xm"]; ci_med <- confint(m_med)["Xm", ]

fig1 <- data.frame(
  model = factor(c("Y ~ X\n(naive)", "Y ~ X + Z\n(backdoor)", "Y ~ X + M\n(mediator)"),
                 levels = c("Y ~ X\n(naive)", "Y ~ X + Z\n(backdoor)", "Y ~ X + M\n(mediator)")),
  est = c(est_naive, est_adj, est_med),
  lo  = c(ci_naive[1], ci_adj[1], ci_med[1]),
  hi  = c(ci_naive[2], ci_adj[2], ci_med[2]),
  kind = c("wrong", "right", "wrong"))
ggplot(fig1, aes(model, est, colour = kind)) +
  scale_x_discrete() +
  annotate("segment", x = 0.6, xend = 2.4, y = bX, yend = bX, linetype = "dashed", colour = ink) +
  annotate("segment", x = 2.6, xend = 3.4, y = total_true, yend = total_true,
           linetype = "dotted", colour = ink, linewidth = 0.8) +
  geom_errorbar(aes(ymin = lo, ymax = hi), width = 0.16, linewidth = 0.8) +
  geom_point(size = 3) +
  scale_colour_manual(values = c(right = green, wrong = red), guide = "none") +
  labs(x = NULL, y = "estimated effect of grazing on richness",
       title = "Only the backdoor set recovers the causal effect",
       subtitle = "dashed = true grazing effect (0.40)\ndotted = total effect in the mediator simulation (0.72)") +
  theme_te()

## ---- coverage ----
set.seed(20159)
nsim <- 2000
est_n <- est_a <- numeric(nsim)
cov_n <- cov_a <- logical(nsim)
for (i in seq_len(nsim)) {
  Zi <- rnorm(n); Xi <- aZX * Zi + rnorm(n, 0, 0.6)
  Yi <- bX * Xi + bZ * Zi + rnorm(n)
  mn <- lm(Yi ~ Xi); ma <- lm(Yi ~ Xi + Zi)
  est_n[i] <- coef(mn)["Xi"]; est_a[i] <- coef(ma)["Xi"]
  cn <- confint(mn)["Xi", ]; ca <- confint(ma)["Xi", ]
  cov_n[i] <- bX >= cn[1] && bX <= cn[2]
  cov_a[i] <- bX >= ca[1] && bX <= ca[2]
}

## ---- fig-coverage ----
dd <- rbind(data.frame(est = est_n, estimator = "naive Y ~ X"),
            data.frame(est = est_a, estimator = "backdoor Y ~ X + Z"))
ggplot(dd, aes(est, fill = estimator, colour = estimator)) +
  geom_density(alpha = 0.35, linewidth = 0.7) +
  geom_vline(xintercept = bX, linetype = "dashed", colour = ink) +
  scale_fill_manual(values = c("naive Y ~ X" = red, "backdoor Y ~ X + Z" = green)) +
  scale_colour_manual(values = c("naive Y ~ X" = red, "backdoor Y ~ X + Z" = green)) +
  labs(x = "estimated effect across 2000 simulated datasets", y = "density",
       title = "The naive estimator is biased in every sample",
       fill = NULL, colour = NULL) +
  theme_te()
