Classical end-to-end empirical analysis workflow in the modern tidyverse + econometrics R ecosystem — dplyr + tidyr + haven + fixest + sandwich + lmtest + clubSandwich + AER + ivreg + did + bacondecomp + HonestDiD + eventstudyr + rdrobust + rddensity + Synth + gsynth + synthdid + MatchIt + WeightIt + cobalt + ebal + grf + DoubleML + mediation + marginaleffects + modelsummary + kableExtra + gt + ggplot2 + ggpubr + cowplot + binsreg. **Defaults to economics empirical-paper style** (AER / QJE / AEJ) — every run produces a publication-ready output set with a multi-column regression table (M1→M6 progressive controls/FE) as the centerpiece, plus Table 1 (descriptives), mechanism / heterogeneity / robustness tables, and event-study + coefficient + trend figures. Covers the full 8-step R pipeline an applied economist runs on every paper — (1) data import & cleaning (read_dta/read_csv, naniar, janitor, validate-merges), (2) variable construction (mutate/across/winsorize/group_by + lag/lead with dplyr), (3) descriptive statistics & Table 1 (gtsummary, modelsummary::datasummary, tableone), (4) classical diagnostic tests (shapiro/jarque.bera.test/bptest/dwtest/bgtest/vif/adf.test/kpss.test/Hausman), (5) baseline modeling (fixest::feols, ivreg, did::att_gt, eventstudyr, sun_ab, did_imputation, synthdid, rdrobust, MatchIt, WeightIt, grf::causal_forest, DoubleML, mediation), (6) robustness battery (modelsummary stack, clubSandwich CRSE, fwildclusterboot, ri2, robomit Oster, bacondecomp, HonestDiD), (7) further analysis (interactions + marginaleffects, mediation::mediate, gsem via lavaan, dose-response splines, grf CATE), (8) publication-ready tables & figures (modelsummary, kableExtra, gt, stargazer, texreg, flextable to LaTeX/Word/HTML; ggplot2 + ggpubr + cowplot + binsreg + iplot for figures). **Also covers two parallel domain modes that share the same 8-step scaffolding** — **Mode A — Epidemiology / public health** (target-trial emulation, IPTW + g-formula + TMLE doubly-robust triplet via `WeightIt` / `gfoRmula` / `tmle` / `ltmle`, Mendelian randomization via `MendelianRandomization` / `TwoSampleMR` / `MRPRESSO`, KM / Cox / AFT / RMST survival via `survival` / `survminer` / `flexsurv`, E-value sensitivity via `EValue`, principal stratification — STROBE / TRIPOD reporting), and **Mode B — ML causal inference** (DML via `DoubleML`, S/T/X/R/DR meta-learners via `causalweight` / `grf`, causal forest via `grf::causal_forest`, BART/BCF via `bartCause` / `bcf`, matrix completion via `MCPanel`, CATE distribution + policy tree via `policytree`, off-policy evaluation, conformal causal via `conformalInference` / `cfcausal`, fairness audit via `fairmodels`, DAG learning via `pcalg` / `bnlearn` / LLM-assisted). Use when the user asks for a complete R empirical analysis, wants a tidyverse-style reproducible R script / Quarto workflow, prefers fixest over reghdfe, needs the R counterpart to StatsPAI / 00.1 / 00.2, or names a specific R step in isolation ("feols with cluster", "MatchIt nearest neighbor", "bacondecomp in R", "gtsummary table 1", "modelsummary to Word"). Mode A triggers on "target trial emulation R", "tmle ltmle", "MendelianRandomization", "TwoSampleMR", "MRPRESSO", "survival cox AFT", "STROBE R", "EValue R", "公共健康 R", "流行病学 R". Mode B triggers on "DoubleML R", "grf causal forest", "policytree", "bartCause bcf", "conformal causal R", "fairmodels", "pcalg NOTEARS", "因果机器学习 R".
triggers
["R empirical analysis","tidyverse econometrics workflow","reproducible R script","Quarto empirical pipeline","fixest feols feglm fepois","high-dimensional fixed effects R","clubSandwich cluster-robust","fwildclusterboot wild cluster bootstrap","ivreg AER 2SLS R","did att_gt Callaway SantAnna R","eventstudyr event study R","did_imputation Borusyak R","synthdid R package","bacondecomp R Goodman Bacon","HonestDiD R Rambachan Roth","rdrobust R","rddensity R","Synth gsynth R","MatchIt nearest neighbor R","WeightIt IPW propensity R","cobalt balance check R","ebal entropy balancing R","grf causal forest R","DoubleML R","mediation R Imai","marginaleffects R","gtsummary table 1","modelsummary publication table","kableExtra LaTeX","texreg stargazer","flextable Word","ggplot2 coefplot","iplot fixest","binsreg R","haven read_dta sav","janitor clean_names","naniar missing","epidemiology pipeline R","public health causal inference R","target trial emulation R","g-formula R gfoRmula","IPTW marginal structural model R","WeightIt PSweight","tmle ltmle doubly robust","HAL-TMLE R","Mendelian randomization R","MendelianRandomization package","TwoSampleMR","MRPRESSO","MR-Egger weighted median R","STROBE TRIPOD reporting R","EValue sensitivity R","Kaplan-Meier AFT survival R","survival survminer flexsurv","流行病学 R","公共健康 R","ML causal inference R","DoubleML R","grf causal forest R","meta-learner S T X R DR R","causalweight R","bartCause bcf","Bayesian causal forest BCF R","CATE distribution R","policytree R","off-policy evaluation R","conformalInference cfcausal","conformal causal prediction R","fairmodels fairness audit","causal discovery PC NOTEARS R","pcalg bnlearn","因果机器学习 R"]
Full Empirical Analysis — Classical R Workflow
This skill is the canonical 8-step pipeline an applied economist runs on every empirical paper, written in the modern tidyverse + econometrics R ecosystem — dplyr/tidyr/haven for data, fixest as the panel/IV/DID workhorse, did/bacondecomp/HonestDiD for modern DID, rdrobust/rddensity for RD, Synth/gsynth/synthdid for synthetic control, MatchIt/WeightIt/cobalt/ebal for matching, grf/DoubleML for ML causal, mediation for causal mediation, marginaleffects for post-estimation, modelsummary/kableExtra/gt for publication tables, ggplot2/iplot/binsreg for figures.
Companion skills: this is the R sibling of 00-StatsPAI_skill (Python DSL), 00.1-Full-empirical-analysis-skill (explicit Python), and 00.2-Full-empirical-analysis-skill_Stata (Stata .do). All four implement the same 8 steps, in their respective ecosystems.
Philosophy
Tidyverse + fixest, the modern R idioms.feols(... | unit + year, cluster = ~unit), not Frankenstein-y lm(y ~ x + factor(unit) + factor(year)).
Reproducible scripts / Quarto. Every example below is paste-runnable. renv for package locking; Quarto (.qmd) for combined narrative + code + tables/figures.
8 steps, first-class. R users historically over-invest in Step 5; this skill treats Steps 1–4 and 6–8 as core.
Rich outputs. Every step yields at least one table or figure — tex/docx/png/pdf.
Progressive disclosure.SKILL.md gives the canonical call per step; references/ holds variant-specific depth.
ملف SKILL.md هذا كبير جدا، لذلك يعرض SkillsMP القسم الاول فقط هنا.عرض على GitHub
SkillOpt-style execution gate
Use this long playbook as a seed skill, not as a script to exhaustively apply. SkillOpt discipline: treat each local R/Quarto change as a candidate patch that must beat a selection check and survive a held-out check before it becomes reusable boilerplate. Before writing or revising an R script/Quarto workflow, compress the user's request into a task-local best_skill card:
best_skill: <mode + design + artifact target>
train_signal: <current failure, user goal, or missing evidence>
selection_split: <focal dataset/spec/output used to judge the candidate>
heldout_gate: <checks the patch must pass beyond the focal example>
accepted_patterns: <rules to reuse after validation>
rejected_patterns: <failed shortcuts not to retry without new evidence>
patch_scope: <one estimator/sample/export/robustness change>
reject_if: <conditions that force rollback to the last passing spec>
Route card: record the mode (econ, epi, or ml-causal), estimand, identification design, focal outcome/treatment, R package family, and required artifacts.
Bounded edit: change one decision at a time (sample rule, estimator, clustering, export format, or robustness check). Prefer the smallest patch that can pass validation.
Selection split discipline: treat the user's immediate failure or requested artifact as the selection split. Reserve at least one alternate outcome, sample window, estimator family, or export target as the held-out gate.
Held-out gate: define checks before running code: row counts, distinct() key uniqueness, treatment support, missingness thresholds, expected table/figure files, and one non-focal robustness/specification that the change must not break.
Reject buffer: if a candidate spec fails the gate, log the failure, R/Quarto diff, and gate output in analysis_log.md; revert to the last passing spec and do not retry the same unchecked pattern.
Slow/meta update: at the end of the task, write down accepted_patterns and rejected_patterns from the trajectory. Do not widen the canonical project template from a single passing run.
Promote only after validation: only turn a one-off fix into reusable project boilerplate after it passes the current data and at least one alternate outcome/sample/specification.
The default playbook above is AER-style applied econometrics — the AEA convention: written-out estimating equation, identifying assumption, design horse-race, full robustness gauntlet. The skill also ships two parallel sub-pipelines for the other two big causal-inference traditions, each reusing the same Steps 1–4 (cleaning / construction / Table 1 / diagnostics) and Step 8 (tables/figures) — only Step 5 (estimator) and Step 6/7 swap packages:
"Mix" (e.g. "estimate DID + then ML CATE on the heterogeneity")
Default + Mode B in sequence — every estimator yields a coefficient + SE pair, drop them all into one modelsummary(...) for the horse-race column
The three modes share the same Step 1–4 cleaning / Table 1 / diagnostics scaffolding, the same Step 8 export stack, and the same DAG-first identification logic — switching modes only changes which Step-5 estimator family you reach for, not the surrounding paper structure. If you only want descriptive stats / Table 1 / a balance check, the AER gtsummary::tbl_summary / modelsummary::datasummary_balance calls in Step 3 work identically across all three modes.
Default Output Spec — Economics Empirical Paper
This skill defaults to the applied-economics paper convention. Unless the user explicitly asks for a single point estimate, every run produces the full publication-ready output set below. Treat it as the contract of Step 8 — mandatory, not opt-in.
Required tables (always produced)
#
Table
R source
Saves to
T1
Summary statistics & balance (treated vs control, with SMD / p-values)
Robustness battery — alt SE / cluster / sample / placebo, in one table
feols × variants → modelsummary (Step 6)
tables/table5_robustness.xlsx + .docx + .tex
★ Table 2 is the centerpiece of every economics paper. It is the multi-column regression table that walks the reader from raw correlation (M1) to the fully-specified design (M6: 2-way FE + interacted FE + cluster-robust SE). Do not collapse it into a single column. Do not report only the headline coefficient. The progression is the credibility argument: if M1→M6 is monotone and stable, the design is plausibly identifying; if it collapses on adding FE, that is the result.
Canonical 6 columns, in order:
M1 raw bivariate (feols(y ~ treat, data))
M2 + demographics (+ age + edu)
M3 + sector controls (+ tenure / firm_size)
M4 + unit FE (| worker_id)
M5 + 2-way FE (| worker_id + year)
M6 + interacted FE (| worker_id + year + industry^year) with cluster = ~ worker_id
Required figures (always produced)
#
Figure
R source
Saves to
F1
Trend / motivation — treated vs control over time, with policy line
dplyr group means → ggplot + geom_line (Step 3)
figures/fig1_trend.png (300 dpi, 必须导出 PNG) + .pdf
F2
Event-study coefficients with 95% CI, base period at –1
Single quick estimate — produce only the relevant cell, but warn that the standard deliverable is the full set above and offer to run it.
Design does not support a figure (cross-section → no event study) — skip with a printed message() explaining why; do not silently drop.
N=1 treated unit (Synth / synthdid) — replace F1/F2 with the SCM trajectory + placebo distribution; T1–T5 still apply.
Required packages
# Run once on a fresh R install:
install.packages(c(# Data"tidyverse","haven","readxl","data.table","janitor","naniar","VIM","mice","validate",# Description / tables"gtsummary","tableone","modelsummary","kableExtra","gt","stargazer","texreg","flextable","psych","summarytools",# Tests"lmtest","sandwich","car","tseries","urca","plm","clubSandwich","fwildclusterboot",# Modeling — workhorses"fixest",# panel/IV/DID with HD FE — primary"AER",# ivreg"ivreg",# alternative IV# Modern DID"did",# Callaway–Sant'Anna"didimputation",# Borusyak–Jaravel–Spiess"fixest",# sunab() for Sun–Abraham"synthdid",# Synthetic DID"bacondecomp","HonestDiD","DIDmultiplegtDYN",# de Chaisemartin–D'Haultfœuille# RD"rdrobust","rddensity","rdmulti",# Synthetic control"Synth","gsynth","tidysynth",# Matching / weighting"MatchIt","WeightIt","cobalt","ebal",# ML causal"grf","DoubleML",# Mediation / SEM"mediation","lavaan",# Robustness / inference"robomit",# Oster delta"ri2","ritools",# randomization inference"multcomp",# Margins / post-estimation"marginaleffects",# Plotting"ggplot2","ggpubr","cowplot","patchwork","binsreg","ggdist","ggrepel"))# fixest's iplot, esttex, etable are bundled.
The 8 Steps — Canonical Pipeline (mapped to AER paper sections)
The 8 steps mirror the canonical sections of an applied AER / QJE / AEJ paper. Each step is one paper section and emits a paper-ready artifact on disk:
Below is the canonical call at each step. All examples share one running narrative — labor-econ panel where training (treatment) affects log_wage (outcome), with covariates age, edu, tenure, panel keys worker_id/firm_id/year. Variable names and parameter values are illustrative.
When a step has many variants (5 staggered-DID estimators; 4 hetero tests), SKILL.md shows the one you reach for first; deeper variants live in references/NN-<topic>.md.
Paper-ready figure & table inventory (what to produce by section)
A modern AER paper has 5–7 figures and 3–5 main tables + an appendix robustness table. Every step below leaves at least one numbered artifact on disk. Default file names assume parallel .tex / .docx / .xlsx exports (the agent should produce all three so co-authors can edit in Word, the build system can use LaTeX, and editors can edit raw numbers in Excel). 所有图片必须同时保存 PNG(≥300 dpi)和 PDF 两种格式。
modelsummary(..., output="docx") · gt::gtsave() · Quarto / Rmd
replication/paper_tables.xlsx/.docx/.tex
Every R estimator above (fixest::feols / AER::ivreg / did::att_gt / grf::causal_forest / synthdid_estimate) returns a result object that can be passed straight into modelsummary(...) / modelplot(...) / etable(...). Don't hand-roll LaTeX from kable(), and don't render Word via flextable directly — modelsummary, etable, and gtsummary apply book-tab borders, AER stars, and the right SE label automatically. For deeper export recipes, see references/08-tables-plots.md.
Export cookbook — LaTeX / Word / Excel in one block
2. Multi-panel paper format (Tables 2 + 3 + A1 + A2 in one file)
Producing the paper-tables block — main + heterogeneity + robustness + placebo as a single document
modelsummary chained with gt::gt_group() for one document with section headers, OR Quarto .qmd rendering multiple modelsummary calls between prose
gt_group(modelsummary(...), modelsummary(...)) · quarto render paper.qmd
3. Full session bundle (the Stata collect / Python Stargazer + pylatex equivalent)
Replication appendix that mixes summary stats + balance + multiple regression tables + headings + prose in one file
Quarto is the modern R-native answer. master.qmd interleaves prose + chunks that emit modelsummary / gtsummary / ggplot2 outputs; one quarto render produces .pdf / .docx / .html
YAML front matter sets format: [pdf, docx, html] for triple-target output
Journal styling — pick the right stars and SE label. The AEA convention is c("*"=.1, "**"=.05, "***"=.01) and notes = "Cluster-robust standard errors in parentheses...". Define a wrapper once at the top of master.R:
For the multi-panel .docx / .xlsx and Quarto cookbook (single-file paper-tables bundle), see references/08-tables-plots.md.
Step −1 — Pre-Analysis Plan (pre-data; AEA RCT Registry style)
Before touching the data, write down (a) the population, (b) the design, (c) the minimum detectable effect (MDE) under the planned sample size and α=0.05, β=0.20. Persist the result as pap.json so a referee can verify the design was powered before, not after, the data were seen.
library(pwr)# classical power calculations
library(WebPower)# cluster RCT, longitudinal, mixed designs
library(jsonlite)# Two-sample MDE for a continuous outcome (Cohen's d framing)
pwr.t.test(d =0.20, power =0.80, sig.level =0.05,
type ="two.sample", alternative ="two.sided")# → required n per arm# Solve for MDE given fixed n
pwr.t.test(n =2000, power =0.80, sig.level =0.05,
type ="two.sample")$d
# → minimum detectable Cohen's d# Cluster-randomized RCT — design effect# Solve via WebPower::wp.crt2arm(...) for clusters / per-cluster size / power triangle
WebPower::wp.crt2arm(f =0.20, J =NULL, n =50, icc =0.05, power =0.80,
alpha =0.05, alternative ="two.sided")# → required clusters per arm# DID power (Frison-Pocock / Bloom 1995): use WebPower::wp.kanova() or simulate# RD power: simulate via DeclareDesign — see references/05-modeling.md §5.5# Persist the protocol — referee will ask whether design was powered ex ante
pap <-list(
population ="manufacturing workers, 2010–2020",
treatment ="training (binary, staggered adoption)",
outcome ="log_wage",
estimand ="ATT",
design ="staggered DID, Callaway-Sant'Anna",
alpha =0.05,
power_target =0.80,
mde_d =0.20,
n_planned =12000,
frozen_at ="2026-01-15T09:00:00Z",
git_sha ="<paste>")
write_json(pap,"artifacts/pap.json", pretty =TRUE, auto_unbox =TRUE)
For richer DAG-aware power analysis (write down the DAG, declare estimands, simulate the design), use DeclareDesign — it is the R-native equivalent of EGAP's pre-analysis flow.
Commit artifacts/pap.json in the repo before Step 1. AEA RCT Registry / OSF preregistration tools accept it as the analysis-plan exhibit.
Step 0 — Sample-construction log & 5-check data contract
An AER §1 Data section has three jobs: (a) describe sources, (b) document every sample restriction (the "footnote 4" sample log), (c) lock the panel structure.
Paste the printed tibble verbatim as footnote 4 of the paper.
0.2 Five-check data contract (go / no-go gate)
library(validate); library(assertr)
data_contract <-function(df, y, treatment, id =NULL, time =NULL, covariates =c()){
keys <-c(y, treatment, id, time, covariates)
contract <-list(
n_obs = nrow(df),# 1. shape
dtypes = sapply(df[keys],function(x)class(x)[1]),# 2. dtypes
n_missing = sapply(df[keys],function(x)sum(is.na(x))),# 3. missingness
n_dupes_on_keys =if(!is.null(id)&&!is.null(time))sum(duplicated(df[,c(id, time)]))else0,# 4. duplicates
panel_balanced =NULL,
cohort_sizes =NULL)if(!is.null(id)&&!is.null(time)){
bal <- df %>% count(.data[[id]])
contract$panel_balanced <-all(bal$n ==max(bal$n))# 5. balance
contract$n_dropped_by_balance <-sum(bal$n !=max(bal$n))if("first_treat"%in%names(df)){
contract$cohort_sizes <- df %>% distinct(.data[[id]], .keep_all =TRUE)%>%
count(first_treat)%>% deframe()}}
contract$y_range <-range(df[[y]], na.rm =TRUE)
contract$treatment_share <- mean(df[[treatment]], na.rm =TRUE)# MCAR sniff test (Rubin) — if missing(y) is associated with covariates,# listwise deletion biases the estimate. Use mice / IPW instead.
miss_y <-is.na(df[[y]])
contract$mcar_hint <-"likely MCAR (listwise OK)"if(any(miss_y)&&any(!miss_y)){for(cov in covariates){if(is.numeric(df[[cov]])){
p <- t.test(df[[cov]][miss_y], df[[cov]][!miss_y])$p.value
if(p <0.05){
contract$mcar_hint <- sprintf("NOT MCAR (y-miss differs on %s, p=%.3f) → use mice / IPW",
cov, p)break}}}}
contract
}
contract <- data_contract(df, y ="wage", treatment ="training",
id ="worker_id", time ="year",
covariates =c("age","edu","tenure"))
stopifnot(contract$n_dupes_on_keys ==0)
stopifnot(all(contract$n_missing ==0))
write_json(contract,"artifacts/data_contract.json",
pretty =TRUE, auto_unbox =TRUE)
If any stopifnot fires, stop and fix it in dplyr first. R estimators silently drop NA rows downstream — this contract is the cheapest insurance against "why did N drop from 12,000 to 9,800 between Table 1 and Table 2?" referee questions.
Step 1 — Data import & cleaning
Deeper patterns: references/01-data-cleaning.md — every format (haven/readxl/data.table::fread/arrow::read_parquet/DBI), janitor::clean_names, naniar missingness viz, MCAR/MAR/MNAR triage with mice, validation with validate/assertr, panel structure checks.
Deeper patterns: references/02-data-transformation.md — log/IHS/Box–Cox via MASS::boxcox, group winsorization with dplyr, scale() and bestNormalize, factor handling, lag/lead with dplyr::lag, panel timing.
This is the heart of an AER paper. Before any code, write down the equation explicitly and state the identifying assumption. Vague identification language is the single most common reason a referee rejects an applied paper. Persist the strategy as strategy.md so it is a dated, version-controlled artifact — not a post-hoc rationalization written after seeing the coefficient.
Equation × identifying assumption × R estimator (decision table)
┌─ running var + cutoff ───────────────── RDD (rdrobust)
│
├─ exogenous instrument Z ─────────────── IV/2SLS (feols / AER::ivreg)
data + question ─┤
├─ pre/post × treat/control ─┬ 2 periods ── 2×2 DID (feols + i())
│ └ staggered ── CS / SA / BJS (att_gt / sunab / did_imputation)
│
├─ 1 treated unit + donor pool + long pre ── SCM (Synth / gsynth / synthdid)
│
├─ high-dim X, selection-on-observables ── ML causal (DoubleML / grf — see §B)
│
└─ none of the above ──────────────────── matching + sensitivity (MatchIt + EValue)
Pre-registration strategy.md template
strategy <-"\\
# Empirical Strategy (pre-registration)
**Frozen**: 2026-01-15 (Git SHA: <paste>)
**Population**: manufacturing workers, 2010–2020, balanced panel
**Treatment**: training (binary, staggered adoption)
**Outcome**: log_wage (CPI-deflated 2010 USD)
**Estimand**: ATT on the treated, dynamic horizon -4..+4
## Estimating equation (paste from §2.5 row that matches the design)
log_wage_it = α_i + λ_t + Σ_{e≠-1} β_e · 1{t - G_i = e} + ε_it
## Identifying assumption
1. No anticipation: E[Y_it(0) | t < G_i] = E[Y_it(0) | never-treated]
2. Group-time PT: Δ E[Y_it(0)] is the same across treatment cohorts
## Auto-flagged threats (must defend in §2)
- Selection of G_i on Y_i(0) → bacondecomp + HonestDiD sensitivity
- Spillover within firm → cluster at firm_id, also try firm_id × year
- Anticipation in pre-period → include lead in event study
## Fallback estimators (Step 6 robustness)
- Sun–Abraham via `feols(y ~ sunab(G, t) | i + t, data)`
- Borusyak-Jaravel-Spiess via `didimputation::did_imputation`
- Synthetic DID via `synthdid::synthdid_estimate`
"
writeLines(strategy,"artifacts/strategy.md")
Commit artifacts/strategy.md in the repo before running Step 5 / Step 6. The git log of this file is the analysis plan.
Step 3 — Descriptive statistics & Table 1
Deeper patterns: references/03-descriptive-stats.md — gtsummary::tbl_summary (the modern Table 1 standard), modelsummary::datasummary_balance with SMDs, tableone::CreateTableOne, correlation matrices with significance via corrplot / psych::corr.test, distribution plots via ggplot2.
AER convention: the identification figure precedes the regression table. The reader should see graphical evidence that PT holds / first stage is strong / RD jumps cleanly before you ask them to trust your point estimate.
3.5.1 Event-study figure + numerical pre-trends test (DID identification)
Pre-period coefficients ≈ 0 (with the −1 reference period normalized to zero) is the visual evidence for parallel trends. Pair the figure with a numerical pre-trends test so reviewers don't have to eyeball it.
library(fixest); library(ggplot2)# (a) Sun-Abraham via fixest::sunab — the modern primary for staggered DID
es <- feols(log_wage ~ sunab(first_treat, year)| worker_id + year,
data = df, cluster =~ worker_id)# (b) Coefficient figure
iplot(es,
xlab ="Years relative to treatment",
ylab ="Coefficient (ATT, 95% CI)",
main ="Figure 2a. Event-study coefficients (95% CI; ref. e = -1)")
ggsave("figures/fig2a_event_study.pdf", width =7, height =4)
ggsave("figures/fig2a_event_study.png", width =7, height =4, dpi =300)# (c) Numerical pre-trends Wald test (joint zero on the leads)
pre_idx <- grep("year::-",names(coef(es)))[!grepl("ref",names(coef(es)))]
W <- wald(es,names(coef(es))[pre_idx])
cat(sprintf("Pre-trends Wald χ² = %.2f, p = %.3f\n", W$stat, W$p))# (d) Bacon decomposition (Goodman-Bacon 2021) — TWFE diagnostic
library(bacondecomp)
bd <- bacon(log_wage ~ training, data = df,
id_var ="worker_id", time_var ="year")
ggplot(bd, aes(weight, estimate, color = type, shape = type))+
geom_point(size =2)+
labs(title ="Figure 2a-bis. Goodman-Bacon decomposition",
x ="Weight", y ="Estimate")
ggsave("figures/fig2a_bacon.pdf", width =7, height =4)# (e) Callaway-Sant'Anna dynamic ATT (when att_gt is the main estimator)
library(did)
cs <- att_gt(yname ="log_wage", tname ="year", idname ="worker_id",
gname ="first_treat", data = df,
control_group ="nevertreated", est_method ="dr",
clustervars ="firm_id")
ggdid(aggte(cs, type ="dynamic"))+
labs(title ="Figure 2a-ter. Dynamic ATT (Callaway-Sant'Anna)")
ggsave("figures/fig2a_csdid.pdf", width =7, height =4)
Rule of thumb: first-stage F ≥ 10 for OLS-style inference; F ≥ 23 for AR-equivalent inference (Stock–Yogo / Lee 2022). fixest::feols reports F automatically; AER::ivreg requires summary(..., diagnostics = TRUE).
iv <- feols(log_wage ~ age + edu | training ~ Z1 + Z2,
data = df, cluster =~ firm_id)
summary(iv, stage =1)
fitstat(iv,~ ivf + ivwald + sargan + cd)# CD / KP / Sargan / first-stage F# Binscatter for the first-stage scatter (residualized on age + edu)
library(binsreg)
binsreg(y = df$training, x = df$Z1, w = df[,c("age","edu")],
nbins =20, polyreg =2, ci =c(3,3))
ggsave("figures/fig2b_first_stage.pdf", width =7, height =4)
3.5.3 RD: McCrary density + canonical RD plot
The signature RD figure is rdplot (CCT-style binned scatter with local-polynomial fit on each side), paired with the McCrary manipulation test.
library(rdrobust); library(rddensity)# (a) Canonical RD plot — binned means + local poly on each side
rdplot(y = df$outcome, x = df$running_var,c=0,
p =4, kernel ="triangular", binselect ="esmv",
title ="Figure 2c. RD plot")
ggsave("figures/fig2c_rdplot.pdf", width =7, height =4)# (b) McCrary density (Cattaneo-Jansson-Ma 2018)
rdd <- rddensity(X = df$running_var,c=0)
print(summary(rdd))
rdplotdensity(rdd, X = df$running_var,
title ="Figure 2c-bis. McCrary density (manipulation test)")
ggsave("figures/fig2c_mccrary.pdf", width =7, height =4)
3.5.4 Matching: love plot (standardized differences pre vs post)
library(MatchIt); library(cobalt)
m.out <- matchit(training ~ age + edu + tenure + firm_size,
data = df, method ="nearest", ratio =1)
love.plot(m.out, threshold =0.10,
var.order ="unadjusted",abs=TRUE,
title ="Figure 2d. Love plot — |SMD| pre vs post matching")
ggsave("figures/fig2d_loveplot.pdf", width =7, height =4)
3.5.5 SCM: synthetic-control trajectory + gap plot
For synthetic-control designs the canonical Figure 2 is the treated-vs-synthetic time series with treatment time annotated.
Identification-specific checks (PT for DID, weak-IV F, density for RD, common support for matching) are also auto-run inside the Step-5 estimators — don't duplicate the numerics here, but DO produce the figures: a referee scans the figures first.
library(lmtest)
library(sandwich)
library(car)
library(tseries)
library(urca)# Fit baseline OLS for diagnostics
ols <- lm(log_wage ~ training + age + edu + tenure, data = df)# 4a. Normality of residuals
shapiro.test(sample(residuals(ols),min(5000,length(residuals(ols)))))
tseries::jarque.bera.test(residuals(ols))# 4b. Heteroskedasticity
bptest(ols)# Breusch-Pagan
bptest(ols,~ I(fitted(ols)^2)+ ., data = df)# White-style# 4c. Autocorrelation (time series / panel)
dwtest(ols)# Durbin-Watson
bgtest(ols, order =4)# Breusch-Godfrey
Box.test(residuals(ols), lag =8, type ="Ljung-Box")# Panel-specific
library(plm)
pdata <- pdata.frame(df, index =c("worker_id","year"))
plm_fe <- plm(log_wage ~ training + age + edu, data = pdata, model ="within")
pbgtest(plm_fe)# Wooldridge serial correlation
pcdtest(plm_fe, test ="cd")# Pesaran cross-sectional dependence# 4d. Multicollinearity
vif(ols)# VIFs
kappa(model.matrix(ols), exact =TRUE)# condition number# 4e. Stationarity (time series — assumes a single y over time)
adf.test(df$log_wage, k =4)# ADF
kpss.test(df$log_wage, null ="Level")# KPSS# 4f. Hausman (FE vs RE)
plm_re <- plm(log_wage ~ training + age + edu, data = pdata, model ="random")
phtest(plm_fe, plm_re)# 4g. Specification — RESET
resettest(ols, power =2:3, type ="fitted")
Decision table:
Test
Null
Action if rejected
shapiro.test / jarque.bera.test
residuals Normal
bootstrap CIs if N small
bptest
homoskedastic
use HC3 via coeftest(ols, vcov = vcovHC(ols, "HC3")) or cluster
dwtest / bgtest
no autocorr
HAC SEs (vcovHAC) or cluster by unit
pbgtest (panel)
no panel autocorr
cluster by entity
pcdtest
no CSD
Driscoll–Kraay (vcovDC)
vif > 10
—
drop / combine
ADF rejects + KPSS doesn't
stationary
levels
ADF doesn't reject
unit root
first-difference
phtest
RE consistent
use FE
Step 5 — Baseline empirical modeling (Section 4: Main Results)
Canonical calls (the eight patterns A–H below are the AER table cookbook — modelsummary(...) and fixest::etable(...) are the two workhorses, equivalent to Stata outreg2/esttab and Python pf.etable/Stargazer).
5.A Pattern A — Progressive controls (the canonical Table 2)
Stable β̂ across columns ⇒ less concern that selection on observables is driving the estimate (Oster 2019 selection-stability logic; quantified in Step 6).
library(fixest); library(modelsummary)
m1 <- feols(log_wage ~ training, data = df, cluster =~ firm_id)
m2 <- feols(log_wage ~ training + age + edu, data = df, cluster =~ firm_id)
m3 <- feols(log_wage ~ training + age + edu + tenure + firm_size, data = df, cluster =~ firm_id)
m4 <- feols(log_wage ~ training + age + edu + tenure + firm_size | industry + year,
data = df, cluster =~ firm_id)
m5 <- feols(log_wage ~ training + age + edu + tenure + firm_size | worker_id + year,
data = df, cluster =~ firm_id)
m6 <- feols(log_wage ~ training + age + edu + tenure + firm_size | worker_id + year + industry^year,
data = df, cluster =~ firm_id)
modelsummary(list("(1) Baseline"= m1,"(2) +Demog"= m2,"(3) +Labor-mkt"= m3,"(4) Ind×Yr FE"= m4,"(5) Worker FE"= m5,"(6) Worker FE+Ind×Yr"= m6),
output ="tables/table2_main.tex",
stars =c("*"=0.1,"**"=0.05,"***"=0.01),
gof_omit ="BIC|AIC|F|Log|Adj",
coef_map =c("training"="Job training","age"="Age","edu"="Education","tenure"="Tenure","firm_size"="Firm size"),
notes =c("Cluster-robust SE in parentheses, clustered at firm_id.","* p<0.10, ** p<0.05, *** p<0.01."))
modelsummary(list("(1)"=m1,"(2)"=m2,"(3)"=m3,"(4)"=m4,"(5)"=m5,"(6)"=m6),
output ="tables/table2_main.docx")
AER convention: show ALL controls (and the intercept). Pass NEITHER keep = NOR coef_omit = so every parameter is visible. Use coef_map = c("training" = "Training") (single mapping) only when a focal-coefficient-only table is intentional (interaction-form heterogeneity, IV first-stage triplet); use coef_omit = "Intercept" only when you want to suppress the constant for paper aesthetics.
5.B Pattern B — Design horse race (Table 2-bis)
Show the same coefficient of interest under multiple identification strategies. This is the AER credibility move: convergent evidence across designs each making different identifying assumptions.
library(fixest); library(AER); library(did); library(MatchIt); library(WeightIt)
ols <- feols(log_wage ~ training + age + edu + tenure | industry + year,
data = df, cluster =~ firm_id)
iv <- feols(log_wage ~ age + edu + tenure | training ~ Z1 + Z2,
data = df, cluster =~ firm_id)
cs <- att_gt(yname ="log_wage", tname ="year", idname ="worker_id",
gname ="first_treat", data = df,
control_group ="nevertreated", est_method ="dr",
clustervars ="firm_id")
psm <- matchit(training ~ age + edu + tenure, data = df,
method ="nearest", ratio =1)
psm_lm <- lm(log_wage ~ training + age + edu + tenure,
data = match.data(psm), weights = weights)
ebal <- weightit(training ~ age + edu + tenure, data = df, method ="ebal")
ebal_lm <- lm(log_wage ~ training + age + edu + tenure,
data = df, weights = ebal$weights)
modelsummary(list("(1) OLS+FE"= ols,"(2) 2SLS"= iv,"(3) CS-DID"= aggte(cs, type ="simple"),"(4) PSM"= psm_lm,"(5) Entropy bal."= ebal_lm),
output ="tables/table2b_designs.tex",
stars =c("*"=0.1,"**"=0.05,"***"=0.01),
coef_map =c("training"="Job training (β̂)"),
gof_omit ="BIC|AIC|F|Log|Adj",
notes ="Convergent evidence: same β̂ under five identification strategies.")
5.C Pattern C — Multi-outcome table (same X, several Y's)
ys <-c("log_wage","weeks_employed","left_firm","promoted")
multi_y <- lapply(ys,function(y)
feols(as.formula(paste(y,"~ training + age + edu + tenure | industry + year")),
data = df, cluster =~ firm_id))names(multi_y)<- ys
modelsummary(multi_y,
output ="tables/table2c_multi_outcome.tex",
stars =c("*"=0.1,"**"=0.05,"***"=0.01),
coef_map =c("training"="Training"),
notes ="Each column is a separate regression on the labelled outcome.")
5.D Pattern D — Stacked Panel A / Panel B table
Same model family, two horizons (short-run / long-run) or two samples. Use gt::gt_group() to stack two modelsummary blocks with panel headers.
library(gt)
panelA <-list("(1) Industry FE"= feols(wage_t1 ~ training + X | industry + year, data = df, cluster =~ firm_id),"(2) Worker FE"= feols(wage_t1 ~ training + X | worker_id + year, data = df, cluster =~ firm_id))
panelB <-list("(1) Industry FE"= feols(wage_t5 ~ training + X | industry + year, data = df, cluster =~ firm_id),"(2) Worker FE"= feols(wage_t5 ~ training + X | worker_id + year, data = df, cluster =~ firm_id))
ms_A <- modelsummary(panelA, output ="gt")%>%
tab_header(title ="Panel A. Short-run (1 year)")
ms_B <- modelsummary(panelB, output ="gt")%>%
tab_header(title ="Panel B. Long-run (5 years)")
gt_group(ms_A, ms_B)%>%
gtsave("tables/table2d_horizons.tex")
gt_group(ms_A, ms_B)%>%
gtsave("tables/table2d_horizons.docx")
5.E Pattern E — IV reporting triplet (first-stage / reduced-form / 2SLS)
The textbook AER IV table presents the first stage, the reduced form, and the 2SLS in three columns so the reader can verify Wald-ratio = RF / FS.
fs <- feols(training ~ Z + age + edu | industry + year, data = df, cluster =~ firm_id)
rf <- feols(log_wage ~ Z + age + edu | industry + year, data = df, cluster =~ firm_id)
iv2 <- feols(log_wage ~ age + edu | training ~ Z, data = df, cluster =~ firm_id)
modelsummary(list("(1) First stage"= fs,"(2) Reduced form"= rf,"(3) 2SLS"= iv2),
output ="tables/table2e_iv_triplet.tex",
stars =c("*"=0.1,"**"=0.05,"***"=0.01),
coef_map =c("Z"="Instrument Z","training"="Training (endog.)"),
gof_map =list(list(raw ="ivf", clean ="First-stage F", fmt =2)),
notes ="Wald ratio: $\\hat\\beta_{2SLS} = \\hat\\beta_{RF} / \\hat\\pi_{FS}$.")
IV triplet is intentionally focal: show only Z + endogenous regressor so the reader can eyeball the Wald ratio. Drop coef_map= only if a referee asks for the full coefficient list.
5.F Pattern F — Causal-orchestrator main via did::att_gt / synthdid / grf::causal_forest
For DID / SCM / matching / forest mains, the modern R estimator returns a self-contained estimate + automatic placebos / pre-trends / overlap diagnostics. Pipe into modelsummary via the auto-tidiers.
# CS-DID with full diagnostics
cs <- att_gt(yname ="log_wage", tname ="year", idname ="worker_id",
gname ="first_treat", data = df,
control_group ="nevertreated", est_method ="dr",
clustervars ="firm_id")
print(aggte(cs, type ="group"))# ATT(g) summary
print(aggte(cs, type ="dynamic", min_e =-4, max_e =4))# event-study aggregation# Synthetic DID
library(synthdid)
sdid_setup <- panel.matrices(df, unit="worker_id", time="year",
outcome="log_wage", treatment="training")
sdid_fit <- synthdid_estimate(sdid_setup$Y, sdid_setup$N0, sdid_setup$T0)
print(summary(sdid_fit))# Causal forest with overlap + variable importance
library(grf)
cf <- causal_forest(X = as.matrix(df[,c("age","edu","tenure","firm_size")]),
Y = df$log_wage, W = df$training, num.trees =4000)
average_treatment_effect(cf, target.sample ="treated")
test_calibration(cf)
variable_importance(cf)
5.G Pattern G — Subgroup modelsummary (Table 3, see Step 7)
One column per subgroup. Detailed code in §Step 7 — Heterogeneity.
5.H Pattern H — Robustness master (Table A1, see Step 6)
Stack every robustness specification next to the baseline. Detailed code in §Step 6.
Canonical estimator commands (the underlying primitives)
library(fixest)# 5a. OLS with cluster-robust SEs — feols is the modern primary
ols <- feols(log_wage ~ training + age + edu + tenure,
data = df, cluster =~ firm_id)
summary(ols)# 5b. Two-way FE — single line
fe <- feols(log_wage ~ training + age + edu + tenure | worker_id + year,
data = df, cluster =~ worker_id)# Multi-way clustering
fe_mw <- feols(log_wage ~ training | worker_id + year,
data = df, cluster =~ worker_id + firm_id)# High-dim interaction FE
fe_hd <- feols(log_wage ~ training | worker_id + industry^year,
data = df, cluster =~ firm_id)# 5c. 2×2 DID
did22 <- feols(log_wage ~ i(treated, post, ref =0)+ age + edu,
data = df, cluster =~ worker_id)# Or with absorbed FE:
did22 <- feols(log_wage ~ i(treated, post, ref =0)| worker_id + year,
data = df, cluster =~ worker_id)# 5d. Event study — base period at -1
es <- feols(log_wage ~ i(rel_time, ref =-1)| worker_id + year,
data = df %>% filter(!is.na(first_treat)),
cluster =~ worker_id)
iplot(es,
xlab ="Years relative to treatment",
main ="Event study")# 5e. Staggered DID — modern estimators (see references/05-modeling.md §5.4)
library(did)
cs <- att_gt(yname ="log_wage", tname ="year", idname ="worker_id",
gname ="first_treat", data = df,
control_group ="nevertreated",
est_method ="dr",
clustervars ="firm_id")
ggdid(cs)# event-study plot# Sun & Abraham via fixest::sunab
sa <- feols(log_wage ~ sunab(first_treat, year)| worker_id + year,
data = df, cluster =~ worker_id)
iplot(sa, sub.title ="Sun-Abraham (2021)")# Borusyak–Jaravel–Spiess (didimputation)
library(didimputation)
bjs <- did_imputation(data = df, yname ="log_wage", gname ="first_treat",
tname ="year", idname ="worker_id",
horizon =0:5, pretrends =-5:-1,
cluster_var ="worker_id")# Synthetic DID
library(synthdid)
sdid_setup <- synthdid::panel.matrices(df, unit ="worker_id", time ="year",
outcome ="log_wage", treatment ="training")
sdid_fit <- synthdid_estimate(sdid_setup$Y, sdid_setup$N0, sdid_setup$T0)# 5f. IV / 2SLS
iv <- feols(log_wage ~ age + edu | training ~ draft_lottery + z2,
data = df, cluster =~ firm_id)
summary(iv, stage =1)
fitstat(iv,~ ivf + ivwald + sargan)# first-stage F + Wald + overid# Or via AER:
library(AER)
iv_aer <- ivreg(log_wage ~ training + age + edu |
draft_lottery + z2 + age + edu, data = df)
summary(iv_aer, vcov. = sandwich, diagnostics =TRUE)# 5g. Sharp RD
library(rdrobust); library(rddensity)
rd <- rdrobust(y = df$outcome, x = df$running_var,c=0,
kernel ="triangular", bwselect ="mserd")
summary(rd)
rdplot(y = df$outcome, x = df$running_var,c=0)
rddensity(X = df$running_var,c=0)# manipulation test# 5h. Binary outcome
logit <- feglm(employed ~ training + age + edu | firm_id + year,
data = df, family = binomial(link ="logit"),
cluster =~ firm_id)
library(marginaleffects)
avg_slopes(logit, variables ="training")# AME# 5i. Count w/ HD FE
pois <- fepois(citations ~ training + age | firm_id + year,
data = df, cluster =~ firm_id)
library(modelsummary)
library(fixest)# 6a. Progressive specs (M1 → M6)
m1 <- feols(log_wage ~ training, data = df, cluster =~ firm_id)
m2 <- feols(log_wage ~ training + age + edu, data = df, cluster =~ firm_id)
m3 <- feols(log_wage ~ training + age + edu + tenure | worker_id,
data = df, cluster =~ worker_id)
m4 <- feols(log_wage ~ training + age + edu + tenure | worker_id + year,
data = df, cluster =~ worker_id)
m5 <- feols(log_wage ~ training + age + edu + tenure | worker_id + year + region,
data = df, cluster =~ worker_id)
m6 <- feols(log_wage ~ training + age + edu + tenure | worker_id + year + industry^year,
data = df, cluster =~ worker_id)
modelsummary(list("(1)"= m1,"(2)"= m2,"(3)"= m3,"(4)"= m4,"(5)"= m5,"(6)"= m6),
stars =c('*'=.1,'**'=.05,'***'=.01),
gof_omit ="BIC|AIC|F|Log",
coef_map =c("training"="Training","age"="Age","edu"="Education","tenure"="Tenure"),
output ="tables/table_main.tex")# 6b. Alternative cluster levelsfor(cl inc("worker_id","firm_id","industry","state")){
fit <- feols(log_wage ~ training | worker_id + year, data = df,
cluster = as.formula(paste0("~", cl)))
cat(cl,": b=", coef(fit)["training"]," se=", se(fit)["training"],"\n")}# 6c. Wild cluster bootstrap (when few clusters)
library(fwildclusterboot)
boot <- boottest(m4, param ="training", clustid ="state",
B =9999, seed =42)
summary(boot)# 6d. Subsample splits
splits <-list("Female=0"= df %>% filter(female ==0),"Female=1"= df %>% filter(female ==1),"Young (<40)"= df %>% filter(age <40),"Old (>=40)"= df %>% filter(age >=40))
sub_fits <- imap(splits,~ feols(log_wage ~ training | worker_id + year,
data = .x, cluster =~ worker_id))
modelsummary(sub_fits, stars =TRUE)# 6e. Placebo — fake timing
df_placebo <- df %>%
mutate(fake_first = first_treat -3,
fake_post = year >= fake_first)%>%
filter(year < first_treat)
feols(log_wage ~ fake_post | worker_id + year,
data = df_placebo, cluster =~ worker_id)# 6f. Randomization inference
library(ri2)
ri_out <- conduct_ri(formula = log_wage ~ training + age + edu,
declaration = randomizr::declare_ra(N = nrow(df),
prob = mean(df$training)),
assignment ="training",
sharp_hypothesis =0,
data = df,
sims =1000)
summary(ri_out); plot(ri_out)# 6g. TWFE bias diagnosis
library(bacondecomp)
bacon_out <- bacon(log_wage ~ training,
data = df, id_var ="worker_id", time_var ="year")
ggplot(bacon_out, aes(weight, estimate, color = type))+ geom_point()
ggsave("figures/bacon.pdf")# 6h. Parallel-trends sensitivity
library(HonestDiD)
honest_out <- createSensitivityResults(betahat = es$coefficients,
sigma = vcov(es),
numPrePeriods =5, numPostPeriods =5,
Mbarvec = seq(0,0.5, by =0.05))
createSensitivityPlot(honest_out, originalResults = honest_out$mainResult)
ggsave("figures/honestdid.pdf")# 6i. Oster (2019) δ*
library(robomit)
o_test(y ="log_wage", x ="training",
con ="age + edu + tenure | worker_id + year",
id ="worker_id", time ="year",
data = df, R2max =1.3* fitstat(m6,"r2"), beta =0)# ============================================================# 6j. Pattern H — Robustness master table (Table A1, one column per check)# ============================================================
library(modelsummary); library(MatchIt); library(WeightIt)
base <- feols(log_wage ~ training + age + edu + tenure | industry + year,
data = df, cluster =~ firm_id)
no99 <- feols(log_wage ~ training + age + edu + tenure | industry + year,
data = df %>% filter(wage < quantile(wage,0.99, na.rm =TRUE)),
cluster =~ firm_id)
balpan <- feols(log_wage ~ training + age + edu + tenure | industry + year,
data = df %>% group_by(worker_id)%>%
filter(n_distinct(year)==max(n_distinct(year)))%>% ungroup(),
cluster =~ firm_id)
dropearly <- feols(log_wage ~ training + age + edu + tenure | industry + year,
data = df %>% filter(first_treat >2008), cluster =~ firm_id)
wfe <- feols(log_wage ~ training + age + edu + tenure | worker_id + year,
data = df, cluster =~ firm_id)
cl2way <- feols(log_wage ~ training + age + edu + tenure | industry + year,
data = df, cluster =~ firm_id + year)
logy <- feols(log(wage +1)~ training + age + edu + tenure | industry + year,
data = df, cluster =~ firm_id)
ihsy <- feols(asinh(wage)~ training + age + edu + tenure | industry + year,
data = df, cluster =~ firm_id)
m_psm <- matchit(training ~ age + edu + tenure + firm_size, data = df, method ="nearest")
psm_lm <- lm(log_wage ~ training + age + edu + tenure, data = match.data(m_psm), weights = weights)
ebal_w <- weightit(training ~ age + edu + tenure + firm_size, data = df, method ="ebal")
ebal_lm <- lm(log_wage ~ training + age + edu + tenure, data = df, weights = ebal_w$weights)
modelsummary(list("(1) Baseline"= base,"(2) Drop top 1%"= no99,"(3) Balanced"= balpan,"(4) Drop early"= dropearly,"(5) Worker FE"= wfe,"(6) 2-way cluster"= cl2way,"(7) log Y"= logy,"(8) IHS Y"= ihsy,"(9) PSM"= psm_lm,"(10) Entropy bal."= ebal_lm),
output ="tables/tableA1_robustness.tex",
stars =c("*"=0.1,"**"=0.05,"***"=0.01),
coef_map =c("training"="Training (β̂)"),
gof_omit ="BIC|AIC|F|Log|Adj",
notes ="Each column is one robustness check. β̂ on training is the focal coefficient.")# ============================================================# 6k. Specification curve (Simonsohn-Simmons-Nelson 2020) via `specr`# ============================================================
library(specr); library(ggplot2)
specs <- setup(data = df,
y =c("log_wage","ihs_wage"),
x ="training",
model =c("feols"),
controls =c("age","edu","tenure","firm_size"),
subsets =list(industry =c("manuf","construction","transport")))
results <- specr(specs)
plot(results, choices =c("x","y","controls","subsets"))
ggsave("figures/fig5_spec_curve.pdf", width =10, height =6)
ggsave("figures/fig5_spec_curve.png", width =10, height =6, dpi =300)# Hand-rolled alternative when `specr` doesn't fit (with custom FE / SE):# spec_grid <- expand.grid(controls = list(c("age"), c("age","edu"), c("age","edu","tenure")),# ytrans = c("log_wage", "ihs_wage"),# sample = c("all", "manuf", "no99"),# cluster = c("firm_id", "firm_id+year"))# Loop, run feols, collect b/se, ggplot::geom_pointrange.# ============================================================# 6l. Sensitivity dashboard — HonestDiD + Oster + E-value# ============================================================# (a) HonestDiD — Rambachan-Roth (2023): bound on β̂ under bounded PT violation
library(HonestDiD)
es_pre <- coef(es)[grep("year::-",names(coef(es)))]
es_post <- coef(es)[grep("year::[0-9]",names(coef(es)))]
honest_out <- createSensitivityResults(betahat =c(es_pre, es_post),
sigma = vcov(es)[c(names(es_pre),names(es_post)),c(names(es_pre),names(es_post))],
numPrePeriods =length(es_pre),
numPostPeriods =length(es_post),
Mbarvec = seq(0,0.5, by =0.05))
createSensitivityPlot(honest_out, originalResults = honest_out$mainResult)
ggsave("figures/fig6_honestdid.pdf", width =7, height =4)# (b) Oster δ — `robomit::o_test` (already shown in 6i)# (c) E-value (VanderWeele-Ding 2017) — for risk-ratio outcomes
library(EValue)
evalue(RR(1.45), lo =1.10, hi =1.91)# → reports the minimum strength of unmeasured confounding to nullify the result.
Step 7 — Further analysis
Deeper patterns: references/07-further-analysis.md — marginaleffects is the post-estimation workhorse; mediation::mediate for Imai mediation; lavaan for SEM; grf::causal_forest for CATE.
library(marginaleffects)
library(fixest)# 7a. Heterogeneity via interaction
het <- feols(log_wage ~ i(female, training, ref =0)+ age + edu | worker_id + year,
data = df, cluster =~ worker_id)
summary(het)
iplot(het)# visualize interaction# Continuous moderator + marginsplot
het_c <- feols(log_wage ~ training * tenure + age + edu | worker_id + year,
data = df, cluster =~ worker_id)
plot_slopes(het_c, variables ="training",
condition =list(tenure = seq(0,20, by =2)))+
geom_hline(yintercept =0, linetype ="dashed")+
labs(x ="Tenure", y ="Marginal effect of training")
ggsave("figures/het_tenure.pdf", width =6, height =4)# 7b. Triple difference
ddd <- feols(log_wage ~ treated * post * high_exposure | worker_id + year,
data = df, cluster =~ firm_id)# 7c. Outcome ladder
out_ladder <-list()for(y inc("hours_worked","productivity","log_wage")){
out_ladder[[y]]<- feols(as.formula(paste(y,"~ training | worker_id + year")),
data = df, cluster =~ worker_id)}
modelsummary(out_ladder, stars =TRUE,
coef_map =c("training"="Training"),
output ="tables/outcome_ladder.tex")# 7d. Mediation — Imai et al. (2010)
library(mediation)
med_M <- lm(hours_worked ~ training + age + edu, data = df)
med_Y <- lm(log_wage ~ training + hours_worked + age + edu, data = df)
med <- mediate(med_M, med_Y, treat ="training", mediator ="hours_worked",
boot =TRUE, sims =1000)
summary(med); plot(med)# Sensitivity to unobserved M-Y confounding
medsens <- medsens(med, rho.by =0.05, effect.type ="indirect")
plot(medsens)# 7e. CATE via causal forest
library(grf)
cf <- causal_forest(X = as.matrix(df %>% select(age, edu, tenure, firm_size)),
Y = df$log_wage, W = df$training,
num.trees =2000, min.node.size =5)
df$tau_hat <- predict(cf)$predictions
variable_importance(cf)
average_treatment_effect(cf, target.sample ="all")# Plot CATE by a moderator
ggplot(df, aes(tenure, tau_hat))+
geom_smooth(method ="loess", se =TRUE)+
labs(x ="Tenure", y ="Estimated CATE")
ggsave("figures/cate_tenure.pdf")# 7f. Dose-response — splines
library(splines)
dr <- feols(log_wage ~ ns(training_hours, df =4)+ age + edu | worker_id + year,
data = df, cluster =~ worker_id)
plot_predictions(dr, condition ="training_hours")
Step 8 — Publication tables & figures
This step is mandatory — every analysis run produces all 5 required tables (T1–T5) and all 4 required figures (F1–F4) defined in the Default Output Spec at the top of this skill. Do not skip Step 8 because "the regression already ran". A coefficient without a table and a figure is not how applied economics communicates a result.
Deeper patterns: references/08-tables-plots.md — modelsummary is the modern default (LaTeX/Word/HTML/Excel from one call); kableExtra for further LaTeX styling; gt for HTML/Word; ggplot2 + iplot + ggpubr + cowplot + binsreg for figures.
library(modelsummary)
library(kableExtra)
library(gt)
library(fixest)
library(ggplot2)# ============================================================# 8a. ★ TABLE 2 — Main results, multi-column regression M1→M6# (the centerpiece of every economics paper)# ============================================================
modelsummary(list("(1) Raw"= m1,"(2) +Demog"= m2,"(3) +Tenure"= m3,"(4) +Unit FE"= m4,"(5) +2-way FE"= m5,"(6) +Ind×Yr FE"= m6),
stars =c('*'=.1,'**'=.05,'***'=.01),
coef_map =c("training"="Training","age"="Age","edu"="Education","tenure"="Tenure"),
gof_map =list(list("raw"="nobs","clean"="N","fmt"=0),list("raw"="r.squared","clean"="R²","fmt"=3),list("raw"="adj.r.squared","clean"="Adj. R²","fmt"=3)),
notes ="Cluster-robust SE at worker_id in parentheses. * p<0.10, ** p<0.05, *** p<0.01.",
output ="tables/table2_main.tex")
modelsummary(list("(1)"=m1,"(2)"=m2,"(3)"=m3,"(4)"=m4,"(5)"=m5,"(6)"=m6),
stars =TRUE, output ="tables/table2_main.docx")# ============================================================# 8b. TABLE 1 — Summary statistics & balance# ============================================================
library(gtsummary)
tbl1 <- df %>%
select(log_wage, age, edu, tenure, female, training)%>%
tbl_summary(by = training,missing="ifany",
statistic = all_continuous()~"{mean} ({sd})")%>%
add_p()%>% add_difference()%>% add_n()%>% bold_labels()
tbl1 %>% as_kable_extra(format ="latex", booktabs =TRUE)%>%
kableExtra::save_kable("tables/table1_balance.tex")
tbl1 %>% as_flex_table()%>%
flextable::save_as_docx(path ="tables/table1_balance.docx")# ============================================================# 8c. TABLE 3 — Mechanism / outcome ladder (3+ outcomes)# ============================================================
ladder <-list()for(y inc("hours_worked","productivity","log_wage")){
ladder[[y]]<- feols(as.formula(paste(y,"~ training + age + edu + tenure | worker_id + year")),
data = df, cluster =~ worker_id)}
modelsummary(ladder,
stars =c('*'=.1,'**'=.05,'***'=.01),
coef_map =c("training"="Training"),
notes ="Each column is a separate regression on the labelled outcome. Cluster-robust SE at worker_id.",
output ="tables/table3_mechanism.tex")# ============================================================# 8d. TABLE 4 — Heterogeneity (subgroup × main coef)# ============================================================
het_specs <-list("All"= df,"Female=0"= df %>% filter(female ==0),"Female=1"= df %>% filter(female ==1),"Age<40"= df %>% filter(age <40),"Age≥40"= df %>% filter(age >=40),"Manuf."= df %>% filter(industry =="manufacturing"))
het_models <- imap(het_specs,~ feols(log_wage ~ training + age + edu + tenure | worker_id + year,
data = .x, cluster =~ worker_id))
modelsummary(het_models,
stars =c('*'=.1,'**'=.05,'***'=.01),
coef_map =c("training"="Training"),
notes ="Cluster-robust SE at worker_id. Wald p-values for cross-subgroup equality should accompany this table — see references/07.",
output ="tables/table4_heterogeneity.tex")# ============================================================# 8e. TABLE 5 — Robustness battery (alt SE / cluster / sample / placebo)# ============================================================
rob <-list("Baseline"= feols(log_wage ~ training | worker_id + year, data = df,
cluster =~ worker_id),"Cluster=Firm"= feols(log_wage ~ training | worker_id + year, data = df,
cluster =~ firm_id),"2-way Cluster"= feols(log_wage ~ training | worker_id + year, data = df,
cluster =~ worker_id + firm_id),"Winsor 1/99"= feols(log_wage ~ training | worker_id + year,
data = df %>% mutate(log_wage = DescTools::Winsorize(log_wage,
probs =c(.01,.99),
na.rm =TRUE)),
cluster =~ worker_id),"Drop Manuf."= feols(log_wage ~ training | worker_id + year,
data = df %>% filter(industry !="manufacturing"),
cluster =~ worker_id),"Placebo (-3)"= feols(log_wage ~ fake_post | worker_id + year,
data = df %>% filter(year < first_treat),
cluster =~ worker_id))
modelsummary(rob,
stars =c('*'=.1,'**'=.05,'***'=.01),
output ="tables/table5_robustness.tex")# ============================================================# 8f. ★ FIGURE 3 — Coefficient plot across M1→M6# ============================================================
modelplot(list("(1)"=m1,"(2)"=m2,"(3)"=m3,"(4)"=m4,"(5)"=m5,"(6)"=m6),
coef_map =c("training"="Training"),
conf_level =0.95)+
geom_vline(xintercept =0, linetype ="dashed", alpha =0.5)+
labs(x ="Coefficient on training (95% CI)", y ="Specification",
title ="Effect of training across specifications")+
theme_classic(base_size =11)
ggsave("figures/fig3_coefplot.pdf", width =6, height =4)
ggsave("figures/fig3_coefplot.png", width =6, height =4, dpi =300)# ============================================================# 8g. FIGURE 2 — Event-study plot (dynamic DID, base period = -1)# ============================================================
pdf("figures/fig2_event_study.pdf", width =7, height =4)
iplot(es,
xlab ="Years relative to treatment",
ylab ="Coefficient (ATT, 95% CI)",
main ="Event study: dynamic effect of training",
ref.line =-0.5)
dev.off()
png("figures/fig2_event_study.png", width =2100, height =1200, res =300)
iplot(es,
xlab ="Years relative to treatment",
ylab ="Coefficient (ATT, 95% CI)",