Skip to contents

Creates a publication-ready RD plot with optimal binning, local polynomial fit, confidence bands, and a cutoff line. Wraps rdrobust::rdplot from the rdrobust package with enhanced ggplot2 aesthetics. When rdrobust is not installed a simple local polynomial via lm is used instead.

Usage

rd_plot(
  y,
  x,
  cutoff = 0,
  nbins = NULL,
  poly_order = 4L,
  ci_shade = TRUE,
  title = NULL,
  xlab = "Running Variable",
  ylab = "Outcome",
  cutoff_color = "red",
  color_sides = TRUE,
  rdplot_args = list()
)

Arguments

y

Numeric vector. Outcome variable.

x

Numeric vector. Running variable. Values are shifted internally so the cutoff is at zero.

cutoff

Numeric. RD cutoff value on the original scale. Default 0.

nbins

Integer or NULL. Number of bins per side. If NULL (default), MSE-optimal binning from rdrobust::rdplot is used.

poly_order

Integer. Polynomial order for the smooth fit lines. Default 4.

ci_shade

Logical. Add confidence band shading around the polynomial fit. Default TRUE.

title

Character or NULL. Plot title.

xlab

Character. X-axis label. Default "Running Variable".

ylab

Character. Y-axis label. Default "Outcome".

cutoff_color

Character. Color of the vertical cutoff line. Default "red".

color_sides

Logical. Use different colors for the control (left) and treated (right) sides. Default TRUE.

rdplot_args

List. Additional named arguments forwarded to rdrobust::rdplot (ignored when rdrobust is not available).

Value

A ggplot2 object.

References

Calonico, S., Cattaneo, M. D., and Titiunik, R. (2015). "rdrobust: An R Package for Robust Nonparametric Inference in Regression-Discontinuity Designs." R Journal, 7(1), 38-51.

Examples

if (FALSE) { # \dontrun{
library(rdrobust)
data(rdrobust_RDsenate)
rd_plot(
  y      = rdrobust_RDsenate$vote,
  x      = rdrobust_RDsenate$margin,
  cutoff = 0,
  title  = "RD: Senate Vote Share",
  xlab   = "Democratic Vote Share Margin",
  ylab   = "Democratic Vote Share (next election)"
)
} # }