|
| 1 | +#!/usr/bin/env Rscript |
| 2 | +# Benchmark: MultiPeriodDiD event study (R `fixest` package) |
| 3 | +# |
| 4 | +# Usage: |
| 5 | +# Rscript benchmark_multiperiod.R --data path/to/data.csv --output path/to/results.json \ |
| 6 | +# --n-pre 4 --n-post 4 |
| 7 | + |
| 8 | +library(fixest) |
| 9 | +library(jsonlite) |
| 10 | +library(data.table) |
| 11 | + |
| 12 | +# Parse command line arguments |
| 13 | +args <- commandArgs(trailingOnly = TRUE) |
| 14 | + |
| 15 | +parse_args <- function(args) { |
| 16 | + result <- list( |
| 17 | + data = NULL, |
| 18 | + output = NULL, |
| 19 | + cluster = "unit", |
| 20 | + n_pre = NULL, |
| 21 | + n_post = NULL, |
| 22 | + reference_period = NULL |
| 23 | + ) |
| 24 | + |
| 25 | + i <- 1 |
| 26 | + while (i <= length(args)) { |
| 27 | + if (args[i] == "--data") { |
| 28 | + result$data <- args[i + 1] |
| 29 | + i <- i + 2 |
| 30 | + } else if (args[i] == "--output") { |
| 31 | + result$output <- args[i + 1] |
| 32 | + i <- i + 2 |
| 33 | + } else if (args[i] == "--cluster") { |
| 34 | + result$cluster <- args[i + 1] |
| 35 | + i <- i + 2 |
| 36 | + } else if (args[i] == "--n-pre") { |
| 37 | + result$n_pre <- as.integer(args[i + 1]) |
| 38 | + i <- i + 2 |
| 39 | + } else if (args[i] == "--n-post") { |
| 40 | + result$n_post <- as.integer(args[i + 1]) |
| 41 | + i <- i + 2 |
| 42 | + } else if (args[i] == "--reference-period") { |
| 43 | + result$reference_period <- as.integer(args[i + 1]) |
| 44 | + i <- i + 2 |
| 45 | + } else { |
| 46 | + i <- i + 1 |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if (is.null(result$data) || is.null(result$output)) { |
| 51 | + stop("Usage: Rscript benchmark_multiperiod.R --data <path> --output <path> --n-pre <int> --n-post <int>") |
| 52 | + } |
| 53 | + if (is.null(result$n_pre) || is.null(result$n_post)) { |
| 54 | + stop("--n-pre and --n-post are required") |
| 55 | + } |
| 56 | + |
| 57 | + # Default reference period: last pre-period |
| 58 | + if (is.null(result$reference_period)) { |
| 59 | + result$reference_period <- result$n_pre |
| 60 | + } |
| 61 | + |
| 62 | + return(result) |
| 63 | +} |
| 64 | + |
| 65 | +config <- parse_args(args) |
| 66 | + |
| 67 | +# Load data |
| 68 | +message(sprintf("Loading data from: %s", config$data)) |
| 69 | +data <- fread(config$data) |
| 70 | + |
| 71 | +ref_period <- config$reference_period |
| 72 | +message(sprintf("Reference period: %d", ref_period)) |
| 73 | +message(sprintf("n_pre: %d, n_post: %d", config$n_pre, config$n_post)) |
| 74 | + |
| 75 | +# Create factor for time with reference level |
| 76 | +data[, time_f := relevel(factor(time), ref = as.character(ref_period))] |
| 77 | + |
| 78 | +# Run benchmark |
| 79 | +message("Running MultiPeriodDiD estimation (fixest::feols)...") |
| 80 | +start_time <- Sys.time() |
| 81 | + |
| 82 | +# Regression: outcome ~ treated * time_f | unit, clustered SEs |
| 83 | +# With | unit, fixest absorbs unit fixed effects. The unit-invariant 'treated' |
| 84 | +# main effect is collinear with unit FE and is absorbed automatically. |
| 85 | +# Interaction coefficients treated:time_fK remain identified. |
| 86 | +cluster_formula <- as.formula(paste0("~", config$cluster)) |
| 87 | +model <- feols(outcome ~ treated * time_f | unit, data = data, cluster = cluster_formula) |
| 88 | + |
| 89 | +estimation_time <- as.numeric(difftime(Sys.time(), start_time, units = "secs")) |
| 90 | + |
| 91 | +# Extract all coefficients and SEs |
| 92 | +coefs <- coef(model) |
| 93 | +ses <- se(model) |
| 94 | +vcov_mat <- vcov(model) |
| 95 | + |
| 96 | +# Extract interaction coefficients (treated:time_fK for each non-reference K) |
| 97 | +interaction_mask <- grepl("^treated:time_f", names(coefs)) |
| 98 | +interaction_names <- names(coefs)[interaction_mask] |
| 99 | +interaction_coefs <- coefs[interaction_mask] |
| 100 | +interaction_ses <- ses[interaction_mask] |
| 101 | + |
| 102 | +message(sprintf("Found %d interaction coefficients", length(interaction_names))) |
| 103 | + |
| 104 | +# Build period effects list |
| 105 | +all_periods <- sort(unique(data$time)) |
| 106 | +period_effects <- list() |
| 107 | + |
| 108 | +for (i in seq_along(interaction_names)) { |
| 109 | + coef_name <- interaction_names[i] |
| 110 | + # Extract period value from coefficient name "treated:time_fK" |
| 111 | + period_val <- as.integer(sub("treated:time_f", "", coef_name)) |
| 112 | + event_time <- period_val - ref_period |
| 113 | + |
| 114 | + period_effects[[i]] <- list( |
| 115 | + period = period_val, |
| 116 | + event_time = event_time, |
| 117 | + att = unname(interaction_coefs[i]), |
| 118 | + se = unname(interaction_ses[i]) |
| 119 | + ) |
| 120 | +} |
| 121 | + |
| 122 | +# Compute average ATT across post-periods (covariance-aware SE) |
| 123 | +post_period_names <- c() |
| 124 | +for (coef_name in interaction_names) { |
| 125 | + period_val <- as.integer(sub("treated:time_f", "", coef_name)) |
| 126 | + if (period_val > config$n_pre) { |
| 127 | + post_period_names <- c(post_period_names, coef_name) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +n_post_periods <- length(post_period_names) |
| 132 | +message(sprintf("Post-period interaction coefficients: %d", n_post_periods)) |
| 133 | + |
| 134 | +if (n_post_periods > 0) { |
| 135 | + avg_att <- mean(coefs[post_period_names]) |
| 136 | + vcov_sub <- vcov_mat[post_period_names, post_period_names, drop = FALSE] |
| 137 | + avg_se <- sqrt(sum(vcov_sub) / n_post_periods^2) |
| 138 | + # NaN guard: match registry convention (REGISTRY.md lines 179-183) |
| 139 | + if (is.finite(avg_se) && avg_se > 0) { |
| 140 | + avg_t <- avg_att / avg_se |
| 141 | + avg_pval <- 2 * pt(abs(avg_t), df = model$nobs - length(coefs), lower.tail = FALSE) |
| 142 | + avg_ci_lower <- avg_att - qt(0.975, df = model$nobs - length(coefs)) * avg_se |
| 143 | + avg_ci_upper <- avg_att + qt(0.975, df = model$nobs - length(coefs)) * avg_se |
| 144 | + } else { |
| 145 | + avg_t <- NA |
| 146 | + avg_pval <- NA |
| 147 | + avg_ci_lower <- NA |
| 148 | + avg_ci_upper <- NA |
| 149 | + } |
| 150 | +} else { |
| 151 | + avg_att <- NA |
| 152 | + avg_se <- NA |
| 153 | + avg_pval <- NA |
| 154 | + avg_ci_lower <- NA |
| 155 | + avg_ci_upper <- NA |
| 156 | +} |
| 157 | + |
| 158 | +message(sprintf("Average ATT: %.6f", avg_att)) |
| 159 | +message(sprintf("Average SE: %.6f", avg_se)) |
| 160 | + |
| 161 | +# Format output |
| 162 | +results <- list( |
| 163 | + estimator = "fixest::feols (multiperiod)", |
| 164 | + cluster = config$cluster, |
| 165 | + |
| 166 | + # Average treatment effect |
| 167 | + att = avg_att, |
| 168 | + se = avg_se, |
| 169 | + pvalue = avg_pval, |
| 170 | + ci_lower = avg_ci_lower, |
| 171 | + ci_upper = avg_ci_upper, |
| 172 | + |
| 173 | + # Reference period |
| 174 | + reference_period = ref_period, |
| 175 | + |
| 176 | + # Period-level effects |
| 177 | + period_effects = period_effects, |
| 178 | + |
| 179 | + # Timing |
| 180 | + timing = list( |
| 181 | + estimation_seconds = estimation_time, |
| 182 | + total_seconds = estimation_time |
| 183 | + ), |
| 184 | + |
| 185 | + # Metadata |
| 186 | + metadata = list( |
| 187 | + r_version = R.version.string, |
| 188 | + fixest_version = as.character(packageVersion("fixest")), |
| 189 | + n_units = length(unique(data$unit)), |
| 190 | + n_periods = length(unique(data$time)), |
| 191 | + n_obs = nrow(data), |
| 192 | + n_pre = config$n_pre, |
| 193 | + n_post = config$n_post |
| 194 | + ) |
| 195 | +) |
| 196 | + |
| 197 | +# Write output |
| 198 | +message(sprintf("Writing results to: %s", config$output)) |
| 199 | +write_json(results, config$output, auto_unbox = TRUE, pretty = TRUE, digits = 10) |
| 200 | + |
| 201 | +message(sprintf("Completed in %.3f seconds", estimation_time)) |
0 commit comments