Fix TWFE within-transformation bug and add methodology review by igerber · Pull Request #139 · igerber/diff-diff · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions METHODOLOGY_REVIEW.md
132 changes: 132 additions & 0 deletions benchmarks/R/benchmark_twfe.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/usr/bin/env Rscript
# Benchmark: Two-Way Fixed Effects (R `fixest` package with absorbed FE)
#
# This uses fixest::feols() with absorbed unit + post FE and unit-level clustering,
# matching the Python TwoWayFixedEffects estimator's approach.
#
# Usage:
# Rscript benchmark_twfe.R --data path/to/data.csv --output path/to/results.json

library(fixest)
library(jsonlite)
library(data.table)

# Parse command line arguments
args <- commandArgs(trailingOnly = TRUE)

parse_args <- function(args) {
result <- list(
data = NULL,
output = NULL
)

i <- 1
while (i <= length(args)) {
if (args[i] == "--data") {
result$data <- args[i + 1]
i <- i + 2
} else if (args[i] == "--output") {
result$output <- args[i + 1]
i <- i + 2
} else {
i <- i + 1
}
}

if (is.null(result$data) || is.null(result$output)) {
stop("Usage: Rscript benchmark_twfe.R --data <path> --output <path>")
}

return(result)
}

config <- parse_args(args)

# Load data
message(sprintf("Loading data from: %s", config$data))
data <- fread(config$data)

# Run benchmark
message("Running TWFE estimation with absorbed FE...")
start_time <- Sys.time()

# TWFE with absorbed unit + post fixed effects, clustered at unit level
# This matches Python's TwoWayFixedEffects:
# - Within-transformation removes unit and time (post) FE
# - Cluster-robust SE at unit level (automatic)
model <- feols(
outcome ~ treated:post | unit + post,
data = data,
cluster = ~unit
)

estimation_time <- as.numeric(difftime(Sys.time(), start_time, units = "secs"))

# Extract results
coef_name <- "treated:post"
coefs <- coef(model)
ses <- se(model)
pvals <- pvalue(model)
ci <- confint(model)

# Find the treatment effect coefficient
if (coef_name %in% names(coefs)) {
att <- coefs[coef_name]
att_se <- ses[coef_name]
att_pval <- pvals[coef_name]
att_ci <- ci[coef_name, ]
} else {
# Try alternative name formats
idx <- grep("treated.*post|post.*treated", names(coefs))
if (length(idx) > 0) {
att <- coefs[idx[1]]
att_se <- ses[idx[1]]
att_pval <- pvals[idx[1]]
att_ci <- ci[idx[1], ]
coef_name <- names(coefs)[idx[1]]
} else {
stop("Could not find treatment effect coefficient")
}
}

# Format output
results <- list(
estimator = "fixest::feols (absorbed FE)",
cluster = "unit",

# Treatment effect
att = unname(att),
se = unname(att_se),
pvalue = unname(att_pval),
ci_lower = unname(att_ci[1]),
ci_upper = unname(att_ci[2]),
coef_name = coef_name,

# Model statistics
model_stats = list(
r_squared = summary(model)$r2,
adj_r_squared = summary(model)$adj.r2,
n_obs = model$nobs
),

# Timing
timing = list(
estimation_seconds = estimation_time,
total_seconds = estimation_time
),

# Metadata
metadata = list(
r_version = R.version.string,
fixest_version = as.character(packageVersion("fixest")),
n_units = length(unique(data$unit)),
n_periods = length(unique(data$post)),
n_obs = nrow(data)
)
)

# Write output
message(sprintf("Writing results to: %s", config$output))
write_json(results, config$output, auto_unbox = TRUE, pretty = TRUE, digits = 15)

message(sprintf("Completed in %.3f seconds", estimation_time))
129 changes: 129 additions & 0 deletions benchmarks/python/benchmark_twfe.py
Loading