svgsynth-gap
Synthetic control gaps
Did the treated unit diverge from its synthetic control more than placebo units did?
How to read it
- Read the coloured line as the treated unit's gap from its synthetic control.
- Grey lines are placebo units: what gaps happen by chance.
- A real effect drops outside the grey after the intervention.
- Callout 1: The intervention.
- Callout 2: The treated unit diverges from the placebos.
- Callout 3: Before: a small gap means a good match.
Watch out
- A poor pre-period fit undermines the comparison: check pre-period gaps first.
Defaults
- Intervention time
- 0
- When the treatment began.
- Range -10,000,000,000,000 to 10,000,000,000,000, step 1
- Mark the intervention
- On
- Draw a vertical line at the intervention.
- Drop placebos with pre-RMSPE > k × treated (0 = keep all)
- 5
- Hide placebo units that fit poorly before treatment, since they tell you little.
- Range 0 to 50, step 1
Pinned in the code
- Draws the run's numbers as given (computed by the Python/R engine); the chart computes nothing statistical.
- RMSPE is computed from the gaps as sent: pre-period t < intervention, post-period t ≥ intervention; the p-value is the treated unit's rank of post/pre RMSPE among the units shown divided by their number (Abadie, Diamond & Hainmueller 2010).
- Placebos whose pre-period fit is poor (pre-RMSPE above k times the treated unit's; k = 5 in the original study) are dropped from the display and the rank.
# Synthetic control gaps — Codesplash Chart Lab (recipe "synth-gap")
# controls: intervention=0, show_intervention=true, max_pre=5
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]
df = pd.read_csv("synth-gap-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["state"] = pd.Categorical(df["state"].astype("string"), categories=["California", "State 01", "State 02", "State 03", "State 04", "State 05", "State 06", "State 07", "State 08", "State 09", "State 10", "State 11", "State 12", "State 13", "State 14", "State 15", "State 16", "State 17", "State 18", "State 19", "State 20", "State 21", "State 22", "State 23", "State 24", "State 25", "State 26", "State 27", "State 28", "State 29", "State 30"])
_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"]) # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"]) # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
T = "years since program"
GAP = "gap in packs per capita"
UNIT = "state"
TR = "treated"
INTERVENTION = 0
fig, ax = plt.subplots(figsize=(8, 4.5))
ax.axhline(0, color="grey", linewidth=0.8)
for unit, d in df.dropna(subset=[T, GAP]).sort_values(T).groupby(UNIT, observed=True):
treated = bool(d[TR].astype(bool).any())
ax.plot(d[T], d[GAP], color=PALETTE[0] if treated else "grey", alpha=1 if treated else 0.35, linewidth=2.4 if treated else 1, label=f"{unit} (treated)" if treated else None, zorder=3 if treated else 1)
if INTERVENTION is not None:
ax.axvline(INTERVENTION, color=REF, linestyle="--", linewidth=1)
ax.set_xlabel(T)
ax.set_ylabel(GAP)
ax.legend(fontsize=8)
plt.show()Needs pandas, numpy, matplotlib · reads synth-gap-demo.csv · app theme from the optional codesplash helper
# Synthetic control gaps — Codesplash Chart Lab (recipe "synth-gap")
# controls: intervention=0, show_intervention=true, max_pre=5
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else px.colors.qualitative.Plotly
df = pd.read_csv("synth-gap-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["state"] = pd.Categorical(df["state"].astype("string"), categories=["California", "State 01", "State 02", "State 03", "State 04", "State 05", "State 06", "State 07", "State 08", "State 09", "State 10", "State 11", "State 12", "State 13", "State 14", "State 15", "State 16", "State 17", "State 18", "State 19", "State 20", "State 21", "State 22", "State 23", "State 24", "State 25", "State 26", "State 27", "State 28", "State 29", "State 30"])
_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"]) # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"]) # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
T = "years since program"
GAP = "gap in packs per capita"
UNIT = "state"
TR = "treated"
INTERVENTION = 0
fig = go.Figure()
for unit, d in df.dropna(subset=[T, GAP]).sort_values(T).groupby(UNIT, observed=True):
treated = bool(d[TR].astype(bool).any())
fig.add_scatter(x=d[T], y=d[GAP], mode="lines", name=f"{unit} (treated)" if treated else str(unit), showlegend=treated,
line=dict(color=PALETTE[0] if treated else "rgba(128,128,128,0.35)", width=2.5 if treated else 1))
fig.add_hline(y=0, line=dict(color="grey", width=1))
if INTERVENTION is not None:
fig.add_vline(x=INTERVENTION, line=dict(color=REF, dash="dash", width=1))
fig.update_layout(xaxis_title=T, yaxis_title=GAP)
fig.show()Needs pandas, numpy, plotly · reads synth-gap-demo.csv · app theme from the optional codesplash helper
# Synthetic control gaps — Codesplash Chart Lab (recipe "synth-gap")
# controls: intervention=0, show_intervention=true, max_pre=5
library(ggplot2)
has_cs <- isTRUE(requireNamespace("codesplashr", quietly = TRUE)) # optional: the app's chart theme
theme_cs <- if (has_cs) codesplashr::theme_codesplash(dark = FALSE) else theme_minimal()
cs_cat <- if (has_cs) codesplashr::cs_palette(dark = FALSE)$categorical else grDevices::palette.colors(8, "Okabe-Ito")
if (has_cs) update_geom_defaults("text", list(colour = codesplashr::cs_palette(dark = FALSE)$foreground))
df <- read.csv("synth-gap-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
df[["treated"]] <- as.logical(df[["treated"]])
df[["state"]] <- factor(df[["state"]], levels = c("California", "State 01", "State 02", "State 03", "State 04", "State 05", "State 06", "State 07", "State 08", "State 09", "State 10", "State 11", "State 12", "State 13", "State 14", "State 15", "State 16", "State 17", "State 18", "State 19", "State 20", "State 21", "State 22", "State 23", "State 24", "State 25", "State 26", "State 27", "State 28", "State 29", "State 30"))
tok <- if (has_cs) codesplashr::cs_palette(dark = FALSE) else list()
seq_ramp <- if (is.null(tok$sequential)) c("#eff6fd", "#1a469d") else tok$sequential # low → high
div_ramp <- if (is.null(tok$diverging)) c("#c13e2e", "#f3f5f8", "#0859ac") else tok$diverging # −, 0, +
ref_col <- if (is.null(tok$reference)) "#4e5661" else tok$reference
grey_col <- if (is.null(tok$dim)) "#ced1d6" else tok$dim
t_col <- "years since program"
gap_col <- "gap in packs per capita"
unit_col <- "state"
tr_col <- "treated"
intervention <- 0
d <- data.frame(t = df[[t_col]], gap = df[[gap_col]], unit = as.character(df[[unit_col]]), treated = as.logical(df[[tr_col]]))
d <- d[!is.na(d$t) & !is.na(d$gap), ]
p <- ggplot(d, aes(t, gap, group = unit)) +
geom_hline(yintercept = 0, colour = "grey50") +
geom_line(data = d[!d$treated, ], colour = "grey50", alpha = 0.35) +
geom_line(data = d[d$treated, ], colour = cs_cat[1], linewidth = 1.2) +
{ if (!is.null(intervention)) geom_vline(xintercept = intervention, colour = ref_col, linetype = "dashed") } +
labs(x = t_col, y = gap_col) + theme_cs
print(p)Needs ggplot2 · reads synth-gap-demo.csv · app theme from the optional codesplashr helper