Reduced-form Analysis

Motivation and Learning Goals

Motivation
  • Structural estimation approach works best when clear empirical patterns motivate the structural model.
  • To find these patterns, we want to run standardized reduced-form analyses quickly.
  • Event-study plots, in particular, have become common in applied microeconomics.
Learning Goals
  • Learn how to estimate OLS, IV, and FE models in Julia.
  • Learn how to estimate DiD models, including event-study specifications, and RD models by calling R from Julia.

Generate the Panel Data

  • I generate an example dataset for this lecture note.
  • This example is inspired by the minimum-wage application in Callaway and Sant’Anna (2021).
    • The panel contains 1,200 counties observed from 2001 through 2010.
    • Counties first become treated in 2004, 2006, or 2008, or remain untreated.
    • Treatment cohorts are assigned at random.
    • The outcome is the teen employment rate, measured in percentage points.
Data-generating process
  • Let \(s_t=t-2001\) denote time since the first sample year.
  • The observed control variable is log population:

\[ X_{it} = 10+0.35\eta_i+0.012s_t+0.08\nu_{it}. \]

  • Without treatment, the teen employment rate is:

\[ Y_{it}(0) = 55+\alpha_i+1.5(X_{it}-10) +0.30s_t+0.05s_t^2+1.5\varepsilon_{it}. \]

  • The county effect is \(\alpha_i=2.5a_i\).
  • The shocks \(\eta_i\), \(\nu_{it}\), \(a_i\), and \(\varepsilon_{it}\) are independent standard normal variables.
  • Let \(G_i\) denote the first treatment year. Never-treated counties have \(G_i=0\).
  • The treatment effect is:

\[ \tau_{it} = \begin{cases} -1.0-0.5\min(t-G_i,3), & G_i>0 \text{ and } t\geq G_i,\\ 0, & \text{otherwise}. \end{cases} \]

  • The observed outcome is:

\[ Y_{it}=Y_{it}(0)+\tau_{it}. \]

Code
using DataFrames
using LinearAlgebra
using Plots
using Random
using Statistics

ENV["OMP_NUM_THREADS"] = "1"
"1"
Code
settings = (
    number_of_counties = 1_200,
    years = 2001:2010,
    treatment_cohorts = [0, 2004, 2006, 2008],
    seed = 2_026_072_501,
)

rng = MersenneTwister(settings.seed)
counties_per_cohort = (
    settings.number_of_counties ÷ length(settings.treatment_cohorts)
)
first_treatment = repeat(
    settings.treatment_cohorts,
    inner = counties_per_cohort,
)
shuffle!(rng, first_treatment)

baseline_log_population = 10 .+ 0.35 .* randn(
    rng,
    settings.number_of_counties,
)
county_effect = 2.5 .* randn(rng, settings.number_of_counties)

rows = NamedTuple[]
for county_id in 1:settings.number_of_counties
    cohort = first_treatment[county_id]
    for year in settings.years
        time_index = year - first(settings.years)
        treated = cohort > 0 && year >= cohort
        event_time = treated ? year - cohort : missing
        true_effect = treated ? -1.0 - 0.5 * min(event_time, 3) : 0.0

        log_population = (
            baseline_log_population[county_id] +
            0.012 * time_index +
            0.08 * randn(rng)
        )
        employment_without_treatment = (
            55.0 +
            county_effect[county_id] +
            1.5 * (log_population - 10.0) +
            0.30 * time_index +
            0.05 * time_index^2 +
            1.5 * randn(rng)
        )

        push!(
            rows,
            (
                county_id = county_id,
                year = year,
                first_treat = cohort,
                treated = treated,
                event_time = event_time,
                log_population = log_population,
                employment_rate = (
                    employment_without_treatment + true_effect
                ),
                true_effect = true_effect,
            ),
        )
    end
end

panel = DataFrame(rows)
first(panel, 8)
8×8 DataFrame
Row county_id year first_treat treated event_time log_population employment_rate true_effect
Int64 Int64 Int64 Bool Int64? Float64 Float64 Float64
1 1 2001 0 false missing 9.70939 54.7942 0.0
2 1 2002 0 false missing 9.81792 52.675 0.0
3 1 2003 0 false missing 9.61012 54.4526 0.0
4 1 2004 0 false missing 9.64185 54.1785 0.0
5 1 2005 0 false missing 9.86486 56.4984 0.0
6 1 2006 0 false missing 9.68214 54.8328 0.0
7 1 2007 0 false missing 9.91814 56.7328 0.0
8 1 2008 0 false missing 9.79773 59.2392 0.0
  • Treatment is absorbing: once a county is treated, it remains treated.
  • The data-generating process imposes no anticipation.
  • The true effect is -1 percentage point in the adoption year, becomes more negative for three years, and then remains constant.

Descriptive Statistics

variables = [:employment_rate, :treated, :log_population]

describe(
    panel,
    :mean,
    :std,
    :min,
    :q25,
    :median,
    :q75,
    :max;
    cols = variables,
)
3×8 DataFrame
Row variable mean std min q25 median q75 max
Symbol Float64 Float64 Real Float64 Float64 Float64 Real
1 employment_rate 57.1442 3.39686 45.6694 54.7968 57.1025 59.4044 73.1077
2 treated 0.375 0.484143 false 0.0 0.0 1.0 true
3 log_population 10.0667 0.372868 8.56905 9.82163 10.0645 10.3067 11.2522

Correlation

  • Rows and columns follow the variable order shown above.
  • These correlations are descriptive and should not be interpreted as causal effects.
correlation_matrix = round.(
    cor(Matrix{Float64}(select(panel, variables))),
    digits = 3,
)
correlation_table = DataFrame(correlation_matrix, variables)
insertcols!(correlation_table, 1, :variable => string.(variables))

correlation_table
3×4 DataFrame
Row variable employment_rate treated log_population
String Float64 Float64 Float64
1 employment_rate 1.0 0.116 0.213
2 treated 0.116 1.0 0.052
3 log_population 0.213 0.052 1.0
Show heatmap code
variable_labels = replace.(string.(variables), "_" => " ")

correlation_heatmap = heatmap(
    variable_labels,
    variable_labels,
    correlation_matrix;
    color = :RdBu,
    clims = (-1, 1),
    aspect_ratio = 1,
    yflip = true,
    xrotation = 20,
    colorbar_title = "Correlation",
)

correlation_annotations = [
    (
        column - 0.5,
        row - 0.5,
        text(
            string(correlation_matrix[row, column]),
            10,
            abs(correlation_matrix[row, column]) > 0.5 ? :white : :black,
        ),
    )
    for row in axes(correlation_matrix, 1)
    for column in axes(correlation_matrix, 2)
]
annotate!(correlation_heatmap, correlation_annotations)

correlation_heatmap
Figure 1: Correlation heatmap

Linear Regression: OLS, IV, and Fixed Effects (FE)

FixedEffectModels.jl

  • FixedEffectModels.jl estimates OLS and instrumental-variable models with high-dimensional fixed effects.
  • It covers the same core use case as R’s fixest and Stata’s reghdfe.
  • The benchmark below compares the reported runtimes of the listed commands, normalized to FixedEffectModels.jl.

Runtime of fixed-effects estimators relative to FixedEffectModels.jl.

Source: FixedEffectModels.jl benchmark by Matthieu Gomez.

  • A general formula with multiple endogenous regressors, instruments, and fixed effects is:
@formula(
    y ~
    x_exogenous +
    (x_endogenous_1 + x_endogenous_2 ~ instrument_1 + instrument_2) +
    fe(unit_id) +
    fe(time_id)
)
  • y is the outcome variable.
  • x_exogenous is an exogenous regressor in the coefficient part of the formula.
  • The left side of the inner ~ lists the endogenous regressors.
  • The right side of the inner ~ lists the excluded instruments. The model needs at least as many instruments as endogenous regressors.
  • fe(unit_id) and fe(time_id) absorb unit and time fixed effects.
  • Without the inner (endogenous ~ instruments) term, reg estimates OLS with the specified fixed effects.
  • The example below uses the panel generated above and estimates an OLS model with county and year fixed effects.
using FixedEffectModels
using RegressionTables

panel.treated_numeric = Float64.(panel.treated)

fe_formula = @formula(
    employment_rate ~
    treated_numeric +
    log_population +
    fe(county_id) +
    fe(year)
)
fe_model = reg(
    panel,
    fe_formula,
    Vcov.cluster(:county_id),
)

regtable(
    fe_model;
    render = HtmlTable(),
    labels = Dict(
        "treated_numeric" => "Treated",
        "log_population" => "Log population",
    ),
    print_clusters = true,
)
employment_rate
Treated -1.536***
(0.050)
Log population 1.411***
(0.182)
county_id Fixed Effects Yes
year Fixed Effects Yes
county_id Clustering Yes
N 12,000
R2 0.821
Within-R2 0.086

Standard Errors

  • Omit the third argument of reg, or use Vcov.simple(), for conventional homoskedastic standard errors.
  • Use Vcov.robust() for HC1 heteroskedasticity-robust standard errors.
  • Use Vcov.cluster(:county_id) for one-way clustering by county.
  • Use Vcov.cluster(:county_id, :year) for two-way clustering by county and year.

Calling R from Julia with RCall.jl

  • RCall.jl starts an R session from Julia.
  • R"..." runs one R expression, while R"""...""" runs multiple lines of R code.
  • @rput copies a Julia variable to R.
  • @rget copies an R variable to Julia.
  • rcopy converts an R object to a Julia value.
  • R packages must be installed in the R library used by RCall.jl.
using RCall

R"""
r_values <- c(1, 2, 3, 4)
r_mean <- mean(r_values)
""";

r_mean_with_rcopy = rcopy(R"r_mean")
@rget r_mean

(
    with_rcopy = r_mean_with_rcopy,
    with_rget = r_mean,
)
(with_rcopy = 2.5, with_rget = 2.5)

Difference-in-Differences

  • The R package did estimates group-time average treatment effects.
  • The example sends the Julia panel to R and performs the entire event-study analysis with did.
did_data = select(
    panel,
    :county_id,
    :year,
    :first_treat,
    :log_population,
    :employment_rate,
)

did_available = rcopy(R"requireNamespace('did', quietly = TRUE)")
did_available || error("R package 'did' is required.")

did_figure_path = joinpath(
    "assets",
    "reduced_form_analysis",
    "did-event-study.png",
)

@rput did_data did_figure_path
"assets/reduced_form_analysis/did-event-study.png"
Run did serially through RCall
  • RCall.jl embeds R inside the Julia process.
  • In the tested macOS setup, multithreaded data.table caused an OpenMP conflict and terminated the process after estimation.
  • This example therefore uses data.table::setDTthreads(1L) and cores = 1.
  • If bootstrap inference is enabled for a larger job, use independent Rscript processes rather than forking from inside RCall.
R"""
library(did)
library(ggplot2)

data.table::setDTthreads(1L)
panel_data <- as.data.frame(did_data)

att_group_time <- did::att_gt(
    yname = "employment_rate",
    tname = "year",
    idname = "county_id",
    gname = "first_treat",
    xformla = ~ log_population,
    data = panel_data,
    panel = TRUE,
    control_group = "nevertreated",
    anticipation = 0,
    est_method = "dr",
    base_period = "universal",
    bstrap = FALSE,
    cband = FALSE,
    cores = 1,
    print_details = FALSE
)

event_study <- did::aggte(
    att_group_time,
    type = "dynamic",
    min_e = -3,
    max_e = 4,
    na.rm = TRUE
)

event_study_plot <- did::ggdid(
    event_study,
    xlab = "Event time",
    ylab = "Effect on the employment rate",
    title = ""
)

invisible(
    ggplot2::ggsave(
        filename = did_figure_path,
        plot = event_study_plot,
        width = 7,
        height = 4.5,
        dpi = 160,
        bg = "white"
    )
)
""";
┌ Warning: RCall.jl: Warning: package ‘ggplot2’ was built under R version 4.3.3
└ @ RCall ~/.julia/packages/RCall/fTLHT/src/io.jl:166
Code
R"""
critical_value <- stats::qnorm(0.975)

did_results <- data.frame(
    "Event time" = event_study$egt,
    "Estimate" = round(event_study$att.egt, 3),
    "Standard error" = round(event_study$se.egt, 3),
    "CI lower" = round(
        event_study$att.egt - critical_value * event_study$se.egt,
        3
    ),
    "CI upper" = round(
        event_study$att.egt + critical_value * event_study$se.egt,
        3
    ),
    check.names = FALSE
)
""";

@rget did_results
did_results
8×5 DataFrame
Row Event time Estimate Standard error CI lower CI upper
Int64 Float64 Float64? Float64? Float64?
1 -3 -0.011 0.082 -0.171 0.149
2 -2 0.032 0.1 -0.165 0.229
3 -1 0.0 missing missing missing
4 0 -1.149 0.101 -1.346 -0.951
5 1 -1.52 0.08 -1.677 -1.362
6 2 -1.971 0.101 -2.168 -1.773
7 3 -2.662 0.121 -2.9 -2.424
8 4 -2.427 0.126 -2.674 -2.179

Event-study estimates produced by R’s did package.
  • att_gt estimates group-time average treatment effects.
  • aggte(..., type = "dynamic") aggregates them by event time.
  • ggdid creates the event-study figure in R.
  • This example disables the bootstrap and simultaneous confidence bands to keep the render fast.

Regression Discontinuity

  • The example uses a sharp RD design with a cutoff at zero.
  • The running variable is \(X_i\sim U[-2,2]\).
  • Treatment is \(D_i=\mathbf{1}\{X_i\geq0\}\).
  • The outcome is:

\[ Y_i=2+0.5X_i+1.5D_i+\varepsilon_i, \qquad \varepsilon_i\sim N(0,1). \]

  • The true discontinuity at the cutoff is \(1.5\).
rdd_rng = MersenneTwister(2_026_072_502)

rdd_data = DataFrame(
    running_variable = 4 .* rand(rdd_rng, 2_000) .- 2,
)

rdd_data.error = randn(rdd_rng, nrow(rdd_data))
rdd_data.treated = rdd_data.running_variable .>= 0

rdd_data.outcome = (
    2 .+
    0.5 .* rdd_data.running_variable .+
    1.5 .* rdd_data.treated .+
    rdd_data.error
)

select!(rdd_data, :running_variable, :outcome)

rdd_available = rcopy(
    R"requireNamespace('rdrobust', quietly = TRUE)"
)
rdd_available || error("R package 'rdrobust' is required.")

rdd_figure_path = joinpath(
    "assets",
    "reduced_form_analysis",
    "rdd-plot.png",
)

@rput rdd_data rdd_figure_path
first(rdd_data, 5)
5×2 DataFrame
Row running_variable outcome
Float64 Float64
1 -0.361527 1.60127
2 1.54547 4.16002
3 0.888075 3.32952
4 -1.44712 0.970556
5 0.603016 5.22834
R"""
library(rdrobust)
library(ggplot2)

rd_data <- as.data.frame(rdd_data)

rd_result <- rdrobust::rdrobust(
    y = rd_data$outcome,
    x = rd_data$running_variable,
    c = 0,
    p = 1
)

rd_plot_result <- rdrobust::rdplot(
    y = rd_data$outcome,
    x = rd_data$running_variable,
    c = 0,
    p = 1,
    hide = TRUE,
    x.label = "Running variable",
    y.label = "Outcome",
    title = ""
)

invisible(
    ggplot2::ggsave(
        filename = rdd_figure_path,
        plot = rd_plot_result$rdplot,
        width = 7,
        height = 4.5,
        dpi = 160,
        bg = "white"
    )
)
""";
┌ Warning: RCall.jl: Warning: package ‘rdrobust’ was built under R version 4.3.1
└ @ RCall ~/.julia/packages/RCall/fTLHT/src/io.jl:166
Code
R"""
rdd_results <- data.frame(
    "Method" = rownames(rd_result$coef),
    "Estimate" = round(as.numeric(rd_result$coef), 3),
    "Standard error" = round(as.numeric(rd_result$se), 3),
    "p-value" = format.pval(
        as.numeric(rd_result$pv),
        digits = 3,
        eps = 0.001
    ),
    "CI lower" = round(rd_result$ci[, 1], 3),
    "CI upper" = round(rd_result$ci[, 2], 3),
    check.names = FALSE,
    row.names = NULL
)
""";

@rget rdd_results
rdd_results
3×6 DataFrame
Row Method Estimate Standard error p-value CI lower CI upper
String Float64 Float64 String Float64 Float64
1 Conventional 1.577 0.151 <0.001 1.282 1.872
2 Bias-Corrected 1.598 0.151 <0.001 1.303 1.893
3 Robust 1.598 0.177 <0.001 1.25 1.946
Code
R"""
rdd_diagnostics <- data.frame(
    "Side" = c("Left of cutoff", "Right of cutoff"),
    "Estimation bandwidth (h)" = round(rd_result$bws["h", ], 3),
    "Bias bandwidth (b)" = round(rd_result$bws["b", ], 3),
    "Effective observations" = as.integer(rd_result$N_h),
    check.names = FALSE,
    row.names = NULL
)
""";

@rget rdd_diagnostics
rdd_diagnostics
2×4 DataFrame
Row Side Estimation bandwidth (h) Bias bandwidth (b) Effective observations
String Float64 Float64 Int64
1 Left of cutoff 0.751 1.164 370
2 Right of cutoff 0.751 1.164 401

RD plot produced by R’s rdrobust package.
  • rdrobust estimates the discontinuity with local-polynomial methods and robust inference.
  • The conventional row reports the unadjusted local-polynomial estimate and standard error.
  • The bias-corrected row adjusts the point estimate for estimated bias.
  • The robust row reports the bias-corrected estimate with a robust standard error and confidence interval.
  • The diagnostics table reports the bandwidths and effective observations used on each side of the cutoff.
  • rdplot provides an exploratory binned plot of the conditional mean around the cutoff.