Skip to content
svgrdd-plot

Regression discontinuity

Does the outcome jump where the running variable crosses the cutoff?

Demo data · seed 20261101 · 82 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Read the dots as binned means on each side of the cutoff.
  2. Look for a jump in the fit lines at the cutoff.
  3. Compare the jump with the bands around it.
  1. Callout 1: The jump at the cutoff: the estimated effect.
  2. Callout 2: The local fit below the cutoff.
  3. Callout 3: Bins further from the cutoff matter less.

Watch out

  • If people can manipulate the running variable around the cutoff, the design fails. Check the density.

Defaults

Cutoff
0
The running variable's value where treatment changes.
Range -1,000,000,000 to 1,000,000,000, step 0.1

Pinned in the code

  • Draws the run's numbers as given (computed by the Python/R engine); the chart computes nothing statistical.
  • Bins are the engine's mimicking-variance (rdrobust `rdplot`) or evenly spaced bins; the lines are the local polynomial fits within the bandwidth with their robust bias-corrected bands.
  • Rows with kind = fit (or with fit_x and fit_y and no kind) draw the fits; the legend's jump is the difference of the two fits nearest the cutoff — the engine's estimate is authoritative.
# Regression discontinuity — Codesplash Chart Lab (recipe "rdd-plot")
# controls: cutoff=0
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("rdd-plot-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["side"] = pd.Categorical(df["side"].astype("string"), categories=["below (Democrat lost)", "above (Democrat won)"])
df["kind"] = pd.Categorical(df["kind"].astype("string"), categories=["bin", "fit"])

_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")
def levels_of(s):
    """Declared level order (categoricals), else order of first appearance."""
    if isinstance(s.dtype, pd.CategoricalDtype):
        return [v for v in s.cat.categories if (s == v).any()]
    return list(dict.fromkeys(s.dropna().astype(str)))
X = "margin of victory (pp)"
Y = "vote share next election (%)"
SIDE = "side"
FX = "fit_x"
FY = "fit_y"
FL = "fit_low"
FH = "fit_high"
KIND = "kind"
CUTOFF = 0
side = df[SIDE].astype(str) if SIDE else np.where(df[X].fillna(df[FX] if FX else 0) >= CUTOFF, "above", "below")
sides = levels_of(df[SIDE]) if SIDE else ["below", "above"]
fig, ax = plt.subplots(figsize=(8, 4.5))
for i, s in enumerate(sides):
    col = PALETTE[i % len(PALETTE)]
    b = df[(side == str(s)) & df[X].notna() & df[Y].notna()]
    ax.scatter(b[X], b[Y], s=16, color=col, alpha=0.8, label=str(s))
    if FX and FY:
        f = df[(side == str(s)) & df[FX].notna()].sort_values(FX)
        if FL and FH:
            ax.fill_between(f[FX], f[FL], f[FH], color=col, alpha=0.18, lw=0)
        ax.plot(f[FX], f[FY], color=col, linewidth=2)
ax.axvline(CUTOFF, color=REF, linestyle="--", linewidth=1)
ax.set_xlabel(X)
ax.set_ylabel(Y)
ax.legend(fontsize=8)
plt.show()

Needs pandas, numpy, matplotlib · reads rdd-plot-demo.csv · app theme from the optional codesplash helper