# The synthetic control method
# https://tidyecology.com/posts/synthetic-control-method/
#
# 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); library(dplyr); library(tidyr)
set.seed(521)
J <- 12; Tn <- 20; T0 <- 13; tvec <- 1:Tn
F1 <- scale(cumsum(rnorm(Tn, 0.04, 0.6)))[, 1]          # common smooth component
F2 <- sin(2 * pi * tvec / 9)                            # cyclical component
trend <- (tvec - mean(tvec)) / Tn                       # centred trend
Fmat <- cbind(F1, F2, trend)
Ld <- matrix(0, J, 3)                                    # donor loadings, two clusters
Ld[1:3, ]  <- cbind(rnorm(3, 1.2, 0.12), rnorm(3, 0.6, 0.12), rnorm(3, 1.4, 0.2))   # neighbours: rising
Ld[4:12, ] <- cbind(rnorm(9, 0.1, 0.35), rnorm(9, 0.4, 0.3), rnorm(9, -0.2, 0.4))   # flatter
mud <- c(runif(3, 5.2, 5.8), runif(9, 3.6, 4.4))
donors <- sapply(1:J, function(j) mud[j] + Fmat %*% Ld[j, ] + rnorm(Tn, 0, 0.12))
colnames(donors) <- paste0("D", 1:J)
wtrue <- c(0.5, 0.3, 0.2); mix <- 1:3                    # treated resembles donors 1-3
Lt <- colSums(wtrue * Ld[mix, ]); mut <- sum(wtrue * mud[mix])
treat0 <- as.numeric(mut + Fmat %*% Lt + rnorm(Tn, 0, 0.12))     # untreated potential outcome
eff_true <- ifelse(tvec >= T0, 0.30 * (tvec - T0 + 1), 0)        # growing effect after T0
treated <- treat0 + eff_true

## ---- solve ----
pre <- 1:(T0 - 1); post <- T0:Tn
Dpre <- donors[pre, , drop = FALSE]; ypre <- treated[pre]
D <- crossprod(Dpre) + diag(1e-8, J)               # tiny ridge guards against near-collinear donors
A <- cbind(1, diag(J)); b0 <- c(1, rep(0, J))      # weights sum to one and are non-negative
w_sc <- quadprog::solve.QP(D, crossprod(Dpre, ypre), A, b0, meq = 1)$solution   # exact quadratic programme
w_sc[w_sc < 1e-6] <- 0; w_sc <- w_sc / sum(w_sc)
names(w_sc) <- colnames(donors)
synth <- as.numeric(donors %*% w_sc)      # synthetic control path
naive <- rowMeans(donors)                 # plain donor average, for contrast

## ---- fig-fit ----
te <- list(ink="#16241d",body="#2c3a31",forest="#275139",label="#46604a",sage="#93a87f",paper="#f5f4ee",
           line="#dad9ca",faint="#5d6b61",grazed="#cda23f",mown="#2f8f63",aband="#b5534e")
th <- theme_minimal(base_size = 12) + theme(text = element_text(colour = te$body),
  plot.title = element_text(colour = te$ink, face = "bold"), plot.subtitle = element_text(colour = te$faint, size = rel(0.9)),
  axis.title = element_text(colour = te$label), axis.text = element_text(colour = te$faint), panel.grid.minor = element_blank(),
  panel.grid.major = element_line(colour = te$line, linewidth = 0.3), legend.position = "bottom",
  legend.title = element_blank(), legend.text = element_text(colour = te$faint, size = rel(0.8)),
  plot.background = element_rect(fill = te$paper, colour = NA), panel.background = element_rect(fill = te$paper, colour = NA))
don_long <- data.frame(t = rep(tvec, J), y = as.vector(donors), d = rep(colnames(donors), each = Tn))
df1 <- pivot_longer(data.frame(t = tvec, Treated = treated, Synthetic = synth, `Donor average` = naive, check.names = FALSE),
                    -t, names_to = "series", values_to = "y")
df1$series <- factor(df1$series, levels = c("Treated", "Synthetic", "Donor average"))
ggplot() +
  geom_line(data = don_long, aes(t, y, group = d), colour = te$line, linewidth = 0.35) +
  geom_vline(xintercept = T0 - 0.5, colour = te$faint, linetype = "22", linewidth = 0.5) +
  geom_line(data = df1, aes(t, y, colour = series, linetype = series), linewidth = 0.9) +
  geom_point(data = subset(df1, series == "Treated"), aes(t, y), colour = te$ink, size = 1.3) +
  annotate("text", x = T0 - 0.3, y = min(donors) + 0.2, label = "intervention", hjust = 0, colour = te$faint, size = 3) +
  scale_colour_manual(values = c("Treated" = te$ink, "Synthetic" = te$mown, "Donor average" = te$grazed)) +
  scale_linetype_manual(values = c("Treated" = "solid", "Synthetic" = "solid", "Donor average" = "4212")) +
  labs(x = "Year", y = "Native-diversity index",
       title = "The synthetic control matches the pre-period; the donor average does not",
       subtitle = "Grey: individual donors. The synthetic mix tracks the treated region before the intervention;\nthe plain average sits well below.") + th

## ---- gaps ----
gap_sc <- treated - synth; gap_nv <- treated - naive
att_sc <- mean(gap_sc[post]); att_nv <- mean(gap_nv[post]); att_true <- mean(eff_true[post])
postdev_sc <- max(abs(gap_sc[post] - eff_true[post]))

## ---- fig-gap ----
df2 <- pivot_longer(data.frame(t = tvec, `Synthetic control gap` = gap_sc, `Donor-average gap` = gap_nv,
                               `True effect` = eff_true, check.names = FALSE), -t, names_to = "series", values_to = "y")
df2$series <- factor(df2$series, levels = c("Synthetic control gap", "Donor-average gap", "True effect"))
ggplot(df2, aes(t, y, colour = series, linetype = series)) +
  annotate("rect", xmin = T0 - 0.5, xmax = Tn + 0.5, ymin = -Inf, ymax = Inf, fill = te$sage, alpha = 0.12) +
  geom_hline(yintercept = 0, colour = te$faint, linewidth = 0.4) +
  geom_vline(xintercept = T0 - 0.5, colour = te$faint, linetype = "22", linewidth = 0.5) +
  geom_line(linewidth = 0.9) + geom_point(size = 1.2) +
  scale_colour_manual(values = c("Synthetic control gap" = te$mown, "Donor-average gap" = te$grazed, "True effect" = te$forest)) +
  scale_linetype_manual(values = c("Synthetic control gap" = "solid", "Donor-average gap" = "4212", "True effect" = "31")) +
  labs(x = "Year", y = "Treated minus counterfactual",
       title = "The gap is the estimated effect: clean pre-period, honest post-period",
       subtitle = "The synthetic-control gap is near zero before the intervention and tracks the true effect after;\nthe donor-average gap is biased throughout.") + th
