Skip to content

DAAF-Contribution-Community/daaf / .claude/skills/r-python-translation

r-python-translation

Verified

R-to-Python translation for data analysis. Maps R packages (tidyverse, ggplot2, fixest, survey, sf, plm) to Python equivalents (polars, plotnine, pyfixest, svy, geopandas). Use when user has R background or requests R-equivalent code comments.

references/causal-inference.md

View on GitHub
# Causal Inference: R to Python Translation

In DAAF's assessment, R has been the primary language for causal inference in
quantitative social science for over a decade. Many influential causal methods papers (DiD, RD,
synthetic control, matching) ship reference implementations as R packages first,
with Python ports following months to years later. This gap has narrowed
considerably: pyfixest now mirrors R fixest's DiD capabilities almost exactly,
and several R package authors (Cattaneo for rdrobust, Arel-Bundock for
marginaleffects) have released official Python ports of their own tools.

DAAF's Python stack provides strong coverage for the most common causal designs:
- **Difference-in-differences:** pyfixest (TWFE, did2s, lpdid, Sun-Abraham)
- **Event studies:** pyfixest (i() operator, iplot)
- **Regression discontinuity:** rdrobust (pre-installed; same rdpackages project as the R version)
- **Instrumental variables:** pyfixest (with FE) and linearmodels (LIML, GMM)
- **Marginal effects:** marginaleffects (same author as R version)

Some causal methods remain gaps in Python: matching/weighting (no MatchIt/WeightIt
equivalent in DAAF), generalized random forests (no grf port), and multiple
imputation (no mice equivalent). These are documented honestly in this reference.

> **Versions referenced:**
> Python: pyfixest 0.60.0, marginaleffects 0.5.0, rdrobust 1.3.0 (all pre-installed)
> R: fixest 0.14.0, marginaleffects 0.32.0, rdrobust 3.0.0, did 2.3.0, did2s 1.2.1
> See SKILL.md § Library Versions for the complete version table.

> **Sources:** Cunningham, *Causal Inference: The Mixtape* (Yale, 2021);
> Huntington-Klein, *The Effect* (Chapman & Hall/CRC, 2022);
> Bergé, *fixest* (CRAN, v0.14.0, the version referenced above;
> https://cran.r-project.org/package=fixest);
> Fischer et al., *pyfixest* (pyfixest.org, v0.60.0, accessed 2026-07-23);
> Calonico, Cattaneo, Farrell, & Titiunik, *rdrobust*
> (https://github.com/rdpackages/rdrobust; software paper: *Stata Journal* 17(2),
> 372-404, 2017, https://doi.org/10.1177/1536867x1701700208);
> Arel-Bundock, Greifer, & Heiss, "How to Interpret Statistical Models Using
> marginaleffects for R and Python" (JSS, 2024)

> **Package availability:** The `pip install X` lines throughout this reference —
> in code comments and in the mapping-table cells — are package-identity
> references, not instructions to run. The Python causal stack that IS
> pre-installed comprises pyfixest, marginaleffects, rdrobust, and linearmodels.
> The other packages named here — csdid, rddensity, rdmulti, rdpower,
> SyntheticControlMethods, CausalPy, synthdid, lifelines, econml, miceforest, and
> pymatchit-causal — are **not** pre-installed in the DAAF container, and runtime
> installs are blocked (`pip install`/`uv add` are refused both at the command
> line and inside executed scripts — see CLAUDE.md § Runtime Package
> Installation). Before planning an analysis around any of them, verify
> availability with `pip show <pkg>` and, if it is absent, escalate to the user to
> add it to the Dockerfile (user additions block near the end) and rebuild before
> use.

---

## 1. Difference-in-Differences

### Traditional TWFE DiD

The simplest DiD design: all treated units adopt at the same time. In the
canonical two-period setting, TWFE recovers the ATT under parallel trends and no
anticipation, without assuming homogeneous effects (Roth et al., 2023, Sec. 2).

**R (fixest):**
```r
library(fixest)

# Binary treatment indicator × post-treatment interaction
fit <- feols(y ~ treat:post | unit + time, data = df, vcov = ~unit)
summary(fit)

# Or with an explicit treatment variable
fit <- feols(y ~ treated | unit + time, data = df, vcov = ~unit)
```

**Python (pyfixest):**
```python
import pyfixest as pf

# Binary treatment indicator x post-treatment interaction
fit = pf.feols("y ~ treat:post | unit + time", data=df, vcov={"CRV1": "unit"})
fit.summary()

# Or with an explicit treatment variable
fit = pf.feols("y ~ treated | unit + time", data=df, vcov={"CRV1": "unit"})
```

The formula syntax is identical. The only difference is the clustering syntax
(`~unit` vs `{"CRV1": "unit"}`).

**When TWFE fails:** With staggered treatment timing and heterogeneous effects,
the TWFE coefficient is a weighted sum of group-period average effects whose
weights may be negative, so it can be negative even when every effect is positive
(de Chaisemartin & D'Haultfoeuille, 2020; see also Goodman-Bacon, 2021). Use one
of the modern estimators below for staggered designs.

### Two-Stage DiD (Gardner, 2022)

did2s imputes the counterfactual using only untreated observations, then estimates
treatment effects in a second stage.

**R (did2s):**
```r
library(did2s)

fit <- did2s(
  data = df,
  yname = "y",
  first_stage = ~ 0 | unit + time,        # FE from untreated obs
  second_stage = ~ i(rel_time, ref = -1),  # Treatment effect spec
  treatment = "treated",
  cluster_var = "unit"
)
summary(fit)
iplot(fit)
```

**Python (pyfixest):**
```python
fit = pf.did2s(
    data=df,
    yname="y",
    first_stage="~ 0 | unit + time",         # FE from untreated obs
    second_stage="~ i(rel_time, ref=-1)",     # Treatment effect spec
    treatment="treated",
    cluster="unit",
)
fit.summary()
fit.iplot()
```

**Key differences:**
- R argument is `cluster_var`; Python is `cluster`
- R returns a fixest object; Python returns a Feols object (both support etable/iplot)
- Formula syntax within `first_stage` and `second_stage` is identical

**Pooled ATT** (single treatment effect):
```r
# R
fit <- did2s(df, "y", ~ 0 | unit + time, ~ treated, "treated", "unit")
```
```python
# Python
fit = pf.did2s(df, "y", "~ 0 | unit + time", "~ treated", "treated", "unit")
```

### Callaway-Sant'Anna (2021)

Group-time ATTs that properly handle staggered adoption.

**R (did):**
```r
library(did)

out <- att_gt(
  yname = "y",
  tname = "year",
  idname = "unit_id",
  gname = "first_treat",      # Year unit first treated (0 = never)
  data = df,
  control_group = "nevertreated",
  est_method = "dr"           # Doubly robust
)
summary(out)
ggdid(out)                    # Event study plot

# Aggregate to overall ATT
agg <- aggte(out, type = "simple")
summary(agg)

# Dynamic (event study) aggregation
agg_dyn <- aggte(out, type = "dynamic")
summary(agg_dyn)
ggdid(agg_dyn)
```

**Python (csdid):**
```python
# Requires: pip install csdid
from csdid import att_gt

out = att_gt(
    yname="y",
    tname="year",
    idname="unit_id",
    gname="first_treat",
    data=df,
    control_group="nevertreated",
    est_method="dr",
)
# API mirrors R did package; check csdid documentation for
# current aggregation and plotting methods
```

**Coverage note:** The Python `csdid` package is a community port (d2cml-ai), not
an official release by the original authors. It aims to replicate the R `did`
package API, but may lag behind on features and bug fixes. Verify results against
R when using for published research.

### Sun-Abraham Saturated Estimator

Fully saturates the model with cohort × relative-period indicators, using
never-treated (or last-to-be-treated) units as the comparison, then aggregates
(Sun & Abraham, 2021, as summarized by Roth et al. 2023).

**R (fixest):**
```r
# sunab() is a special function within feols
fit <- feols(y ~ sunab(cohort, period) | unit + period, data = df,
             vcov = ~unit)
summary(fit)
iplot(fit)

# Aggregate to ATT
summary(fit, agg = "ATT")

# Aggregate by cohort
summary(fit, agg = "cohort")
```

**Python (pyfixest):**
```python
# Via event_study() with estimator="saturated"
fit = pf.event_study(
    data=df,
    yname="y",
    idname="unit",
    tname="period",
    gname="cohort",           # Year of treatment adoption
    estimator="saturated",
    att=False,                # False = dynamic event study
    cluster="unit",
)
fit.summary()
fit.iplot()

# Aggregate to overall ATT
agg = fit.aggregate(weighting="shares")
```

**Key difference:** R uses `sunab()` as a formula function inside `feols()`.
Python uses a separate `event_study()` function with `estimator="saturated"`.
The underlying estimator is identical.

### Local Projections DiD (Dube et al., 2023)

Local projections estimated separately at each horizon (Jordà, 2005), combined with a "clean control" condition; can accommodate non-absorbing treatment (Dube et al., 2023).

**R:** No CRAN package (DAAF check 2026-10-13: no `lpdid` in CRAN's package
list). The method's authors provide R example scripts for a manual
implementation (github.com/danielegirardi/lpdid), and a GitHub-only R package,
`lpdid` (Cardazzi, v0.6.1; github.com/alexCardazzi/lpdid), exists but is not
installed in DAAF's image. Typically implemented manually using local
projections (Jordà, 2005) on a clean-control sample.

**Python (pyfixest):**
```python
result = pf.lpdid(
    data=df,
    yname="y",
    idname="unit",
    tname="year",
    gname="treatment_year",
    att=True,                 # True = pooled ATT, False = period-specific
    pre_window=5,
    post_window=10,
    never_treated=0,          # Value of gname for never-treated units
)
# Note: lpdid() returns a DataFrame, NOT a Feols object
# result.summary() and result.iplot() will NOT work
```

This is a rare case where Python (pyfixest) has a more convenient implementation
than R.

**Important:** `lpdid()` returns a pandas DataFrame with columns for period,
estimate, std_error, ci_lower, ci_upper, etc. It cannot be passed to
`pf.etable()` or use `.iplot()`. Plot results manually with matplotlib or
plotnine.

---

## 2. Event Studies

### Manual Event Study with i()

**R (fixest):**
```r
# Create relative-time variable
df$rel_year <- df$year - df$treatment_year

# Event study with i() — omit t=-1 as reference
fit <- feols(y ~ i(rel_year, ref = -1) | unit + year, data = df,
             vcov = ~unit)

# Plot
iplot(fit)

# With joint confidence bands
iplot(fit, joint = TRUE)  # Bonferroni bands
```

**Python (pyfixest):**
```python
# Create relative-time variable
df["rel_year"] = df["year"] - df["treatment_year"]

# Event study with i() — omit t=-1 as reference
fit = pf.feols("y ~ i(rel_year, ref=-1) | unit + year", data=df,
               vcov={"CRV1": "unit"})

# Plot
fit.iplot()

# With joint confidence bands
fit.iplot(joint="both")   # Bonferroni + Scheffe bands
```

**Key differences:**

| Feature | R fixest | pyfixest |
|---------|----------|----------|
| Reference level | `i(var, ref = -1)` (space after `=`) | `i(var, ref=-1)` (no space required) |
| iplot call | `iplot(fit)` (standalone function) | `fit.iplot()` (method on Feols) |
| Joint bands | `iplot(fit, joint = TRUE)` | `fit.iplot(joint="both")` |
| Band types | Bonferroni (default TRUE) | `"bonferroni"`, `"scheffe"`, or `"both"` |

### Unified Event Study Interface

**R (fixest):** Does not have a single unified function; uses `sunab()` or manual
`i()` specification within `feols()`.

**Python (pyfixest):**
```python
# pf.event_study() provides a clean unified interface
fit = pf.event_study(
    data=df,
    yname="y",
    idname="unit",
    tname="year",
    gname="treatment_year",
    estimator="twfe",         # "twfe", "did2s", or "saturated"
    att=False,                # False = dynamic event study
    cluster="unit",
)
```

This unified interface is a Python advantage over R, where you must choose between
different function calls (`feols` + `i()`, `did2s`, `sunab`) for different
estimators.

### Comparing Estimators Visually

**R (fixest):**
```r
fit_twfe <- feols(y ~ i(rel_year, ref = -1) | unit + year, data = df)
# did2s returns fixest object, can overlay
fit_d2s <- did2s(df, "y", ~ 0 | unit + year,
                 ~ i(rel_year, ref = -1), "treated", "unit")

coefplot(list(fit_twfe, fit_d2s))
```

**Python (pyfixest):**
```python
fit_twfe = pf.event_study(data=df, yname="y", idname="unit",
                          tname="year", gname="g", estimator="twfe", att=False)
fit_did2s = pf.event_study(data=df, yname="y", idname="unit",
                           tname="year", gname="g", estimator="did2s", att=False)

pf.coefplot([fit_twfe, fit_did2s])
```

### Treatment Pattern Visualization

**R:** No built-in equivalent in fixest. Typically uses custom ggplot2 heatmaps.

**Python (pyfixest):**
```python
pf.panelview(data=df, unit="unit", time="year", treat="treated")
```

`panelview()` produces a heatmap showing treatment assignment across units and
time. This is a pyfixest feature without a direct fixest equivalent.

---

## 3. Regression Discontinuity

Both R and Python implementations come from the rdpackages project. The rdrobust
software papers and CRAN package are by Calonico, Cattaneo, Farrell, and Titiunik
(2017); the Python package's metadata (rdrobust 1.3.0) lists Ricardo Masini as
author; the current PyPI release (2.1.1) lists Calonico, Cattaneo, Farrell,
Masini, and Titiunik.

### Sharp RD

**R (rdrobust):**
```r
library(rdrobust)

# Point estimate with robust bias-corrected CI
rd <- rdrobust(Y, X, c = cutoff)
summary(rd)

# RD plot
rdplot(Y, X, c = cutoff)

# Bandwidth selection
bw <- rdbwselect(Y, X, c = cutoff)
summary(bw)
```

**Python (rdrobust):**
```python
# rdrobust is pre-installed in DAAF (pinned 1.3.0)
from rdrobust import rdrobust, rdplot, rdbwselect

# Point estimate with robust bias-corrected CI
rd = rdrobust(Y, X, c=cutoff)
print(rd)

# RD plot
rdplot(Y, X, c=cutoff)

# Bandwidth selection
bw = rdbwselect(Y, X, c=cutoff)
print(bw)
```

### Fuzzy RD

**R:**
```r
rd <- rdrobust(Y, X, c = cutoff, fuzzy = T)  # T = treatment indicator
```

**Python:**
```python
rd = rdrobust(Y, X, c=cutoff, fuzzy=T)
```

### Key Parameters

| Parameter | R | Python | Notes |
|-----------|---|--------|-------|
| Outcome | `y` (1st positional) | `y` (1st positional) | Numpy array or Series |
| Running variable | `x` (2nd positional) | `x` (2nd positional) | Numpy array or Series |
| Cutoff | `c = 0` | `c=0` | Default is 0 |
| Kernel | `kernel = "tri"` | `kernel="tri"` | `"tri"`, `"uni"`, `"epa"` |
| Bandwidth | `h = c(left, right)` | `h=[left, right]` | R uses `c()`, Python uses list |
| Polynomial order | `p = 1` | `p=1` | Local linear is default |
| Fuzzy | `fuzzy = T` | `fuzzy=T` | Treatment indicator for fuzzy RD |
| Covariates | `covs = cbind(c1, c2)` | `covs=covs_array` | Matrix/array of covariates |
| Clustering | `cluster = cl` | `cluster=cl` | Cluster variable |

The API is virtually identical. The main syntactic differences are R's `c()` vs
Python's `[]` for paired values, and R's `cbind()` vs numpy arrays for
covariates.

### Additional RD Packages

| Tool | R Package | Python Package | Install |
|------|-----------|----------------|---------|
| Local polynomial RD | `rdrobust` | `rdrobust` | Pre-installed (1.3.0) |
| RD plots | `rdrobust::rdplot()` | `rdrobust.rdplot()` | Included |
| Bandwidth selection | `rdrobust::rdbwselect()` | `rdrobust.rdbwselect()` | Included |
| Manipulation testing | `rddensity` | `rddensity` | `pip install rddensity` |
| Multi-cutoff/score | `rdmulti` | `rdmulti` | `pip install rdmulti` |
| Power calculations | `rdpower` | `rdpower` | `pip install rdpower` |

The `rdpackages` suite (https://rdpackages.github.io/) collects these packages;
author lists differ by package, so check each package's own author list and
software paper.

---

## 4. Instrumental Variables

See also Section 3 of the companion `regression-modeling.md` reference for
formula syntax details. This section focuses on the causal inference aspects.

### IV with Fixed Effects

**R (fixest):**
```r
# Classic IV: education instrumented by college proximity
fit <- feols(log_wage ~ experience | state + year | education ~ college_prox,
             data = df, vcov = ~state)

# First-stage results
summary(fit, stage = 1)

# Diagnostics
fitstat(fit, type = "ivf")   # First-stage F
```

**Python (pyfixest):**
```python
fit = pf.feols("log_wage ~ experience | state + year | education ~ college_prox",
               data=df, vcov={"CRV1": "state"})

# First-stage results
fit._model_1st_stage.summary()

# First-stage F and effective F (Montiel Olea-Pflueger 2013)
fit.IV_Diag()
```

### IV without Fixed Effects

**R (ivreg):** `ivreg` is not installed in DAAF. In a DAAF R pipeline, fit plain
2SLS with fixest, omitting the fixed-effects part:
`feols(log_wage ~ experience | education ~ college_prox, data = df)`.
```r
library(ivreg)
fit <- ivreg(log_wage ~ experience + education | experience + college_prox,
             data = df)
summary(fit, diagnostics = TRUE)  # Includes weak instrument tests
```

**Python (linearmodels):**
```python
from linearmodels.iv import IV2SLS

fit = IV2SLS.from_formula(
    "log_wage ~ 1 + experience + [education ~ college_prox]", data=df
).fit(cov_type="robust")
print(fit.summary)

# First-stage diagnostics
print(fit.first_stage)
```

### IV Diagnostic Comparison

| Diagnostic | R (fixest) | R (ivreg) | pyfixest | linearmodels |
|-----------|-----------|-----------|----------|--------------|
| First-stage F | `fitstat(, "ivf")` | `summary(, diag=T)` | `fit.IV_Diag()` | `fit.first_stage` |
| Weak instrument | `fitstat(, "ivwald")` | Per-endogenous "Weak instruments" F in `summary(, diagnostics = TRUE)` | Effective F (Montiel Olea-Pflueger 2013) | Partial F per endogenous regressor in `fit.first_stage` (no Cragg-Donald F in linearmodels 7.0; 7.1 adds `fit.first_stage.cragg_donald_f`) |
| Sargan/Hansen | `fitstat(, "sargan")` | `summary(, diag=T)` | Not built-in | `fit.sargan` |
| Wu-Hausman | `fitstat(, "wh")` | `summary(, diag=T)` | Not built-in | `fit.wu_hausman()` |

### LIML and GMM (Beyond 2SLS)

**R:** fixest fits 2SLS but not LIML or GMM-IV, and the `ivreg` package has no
LIML either: its `method` options are `"OLS"`, `"M"` and `"MM"` (verified against
the CRAN ivreg reference manual on 2026-11-02). No installed R package fits LIML
or non-panel GMM-IV, so a DAAF R pipeline escalates them to the orchestrator; in
Python, use linearmodels as below.

**Python (linearmodels):**
```python
from linearmodels.iv import IVLIML, IVGMM

# LIML — often recommended over 2SLS with weak instruments (standard result; not checked against a source here)
fit_liml = IVLIML.from_formula("y ~ 1 + x + [endo ~ z1 + z2]", data=df).fit()

# GMM — efficiency gains under heteroskedasticity with overidentification (standard result; not checked here)
fit_gmm = IVGMM.from_formula("y ~ 1 + x + [endo ~ z1 + z2]", data=df).fit()
```

This is a case where Python (linearmodels) has broader coverage than the standard
R toolkit for IV estimation methods.

---

## 5. Marginal Effects and Post-Estimation Interpretation

Both R and Python implementations are by the same author (Vincent Arel-Bundock),
ensuring consistent methodology and API design.

### Average Marginal Effects (AME)

**R (marginaleffects):**
```r
library(marginaleffects)

fit <- glm(y ~ x1 * x2 + x3, data = df, family = binomial)

# Average marginal effect of x1 (accounts for interaction with x2)
avg_slopes(fit, variables = "x1")

# All variables
avg_slopes(fit)
```

**Python (marginaleffects):**
```python
# marginaleffects is pre-installed in DAAF (pinned 0.5.0)
from marginaleffects import avg_slopes

fit = smf.logit("y ~ x1 * x2 + x3", data=df).fit()

# Average marginal effect of x1
avg_slopes(fit, variables="x1")

# All variables
avg_slopes(fit)
```

### Predictions at Specific Values

**R:**
```r
predictions(fit, newdata = datagrid(x1 = c(0, 1), x2 = mean))
```

**Python:**
```python
from marginaleffects import predictions, datagrid

predictions(fit, newdata=datagrid(x1=[0, 1], x2="mean", model=fit))
```

### Comparisons (Contrasts)

**R:**
```r
# Average effect of a one-unit change in x1
avg_comparisons(fit, variables = "x1")

# Specific contrast
avg_comparisons(fit, variables = list(x1 = c(0, 1)))
```

**Python:**
```python
from marginaleffects import avg_comparisons

avg_comparisons(fit, variables="x1")
avg_comparisons(fit, variables={"x1": [0, 1]})
```

### Hypothesis Testing (Delta Method)

**R:**
```r
hypotheses(fit, "x1 = x2")
hypotheses(fit, "(x1 / x2 - 1) * 100 = 0")  # Nonlinear
```

**Python:**
```python
from marginaleffects import hypotheses

hypotheses(fit, "x1 = x2")
hypotheses(fit, "(x1 / x2 - 1) * 100 = 0")
```

### Compatibility

| Model Source | R marginaleffects | Python marginaleffects |
|-------------|------------------|----------------------|
| Base R lm/glm | Yes | N/A |
| fixest feols/fepois | Yes | N/A |
| statsmodels OLS/GLM | N/A | Yes |
| pyfixest Feols/Fepois | N/A | Yes |
| lme4 mixed models | Yes | N/A |
| brms Bayesian | Yes | N/A |
| scikit-learn | N/A | Yes |

The R version supports 100+ model classes. The Python version supports
statsmodels, pyfixest, scikit-learn, and a growing list of other packages.

**Caveat:** When the Python port was announced in July 2023, its author called it
an "alpha" release with results that still needed checking; it has matured since
(pinned here at 0.5.0). For published research, verify critical marginal-effects
calculations against the R implementation.

---

## 6. Matching and Weighting

This is a **significant gap** in the Python ecosystem. R has mature,
well-documented packages; Python alternatives are fragmented.

### R Packages (Mature)

```r
library(MatchIt)

# Propensity score matching
m.out <- matchit(treat ~ x1 + x2 + x3, data = df, method = "nearest")
summary(m.out)
matched_df <- match.data(m.out)

# Optimal full matching
m.out <- matchit(treat ~ x1 + x2 + x3, data = df, method = "full")

# Coarsened exact matching
m.out <- matchit(treat ~ x1 + x2 + x3, data = df, method = "cem")
```

```r
library(WeightIt)

# Inverse probability weighting
w.out <- weightit(treat ~ x1 + x2 + x3, data = df, method = "ps")
summary(w.out)
# Use weights in outcome model
fit <- lm(y ~ treat, data = df, weights = w.out$weights)
```

### Python Alternatives (Fragmented)

| R Package | Python Equivalent | Fidelity | Notes |
|-----------|------------------|----------|-------|
| `MatchIt` | `pymatchit-causal` | Low-Medium | Community port; limited method coverage |
| `MatchIt` | Manual with scikit-learn | Low | Build propensity model + manual matching |
| `WeightIt` | No direct equivalent | N/A | Manual IPW with statsmodels/sklearn |
| `cobalt` (balance) | No direct equivalent | N/A | Manual balance tables |

**Recommended workaround for DAAF projects:** Implement propensity score matching
manually using scikit-learn for the propensity model and pandas/polars for the
matching algorithm. For published work requiring formal matching diagnostics,
consider running the matching step in R and importing the matched dataset.

```python
# Manual PSM sketch (not a full replacement for MatchIt)
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import NearestNeighbors

# Estimate propensity scores
ps_model = LogisticRegression(C=1e10, max_iter=1000).fit(X, treatment)  # default C=1.0 is L2-penalized
ps = ps_model.predict_proba(X)[:, 1]

# Nearest-neighbor matching on propensity score
nn = NearestNeighbors(n_neighbors=1)
nn.fit(ps[treatment == 0].reshape(-1, 1))
distances, indices = nn.kneighbors(ps[treatment == 1].reshape(-1, 1))
```

This lacks MatchIt's balance diagnostics, caliper options, exact matching
constraints, and variance ratio checks. The gap is real and consequential.

---

## 7. Synthetic Control

### R Packages

```r
# Classic synthetic control (Abadie, Diamond, Hainmueller)
library(Synth)
synth_out <- synth(dataprep.out)
path.plot(synth_out, dataprep.out)

# Generalized synthetic control (Xu, 2017)
library(gsynth)
out <- gsynth(y ~ treat + x1, data = df, index = c("unit", "year"),
              force = "two-way", r = c(0, 5))

# Augmented synthetic control (Ben-Michael, Feller, Rothstein)
library(augsynth)
aug <- augsynth(y ~ treat, unit = unit, time = year, data = df)
summary(aug)
plot(aug)
```

### Python Alternatives

| R Package | Python Equivalent | Install | Fidelity |
|-----------|------------------|---------|----------|
| `Synth` | `SyntheticControlMethods` | `pip install SyntheticControlMethods` | Medium |
| `gsynth` | No direct equivalent | N/A | Gap |
| `augsynth` | No direct equivalent | N/A | Gap |
| N/A | `CausalPy` (Bayesian SC) | `pip install CausalPy` | Different methodology |
| N/A | `synthdid` | `pip install synthdid` | Medium (synthetic DiD) |

**CausalPy** (by PyMC Labs) provides a Bayesian approach to synthetic control
with full uncertainty quantification via PyMC. It is methodologically different
from the frequentist `Synth` package but achieves similar goals:

```python
# Requires: pip install CausalPy
import causalpy as cp

result = cp.SyntheticControl(
    df,
    treatment_time=treatment_time,
    formula="y ~ 0 + x1 + x2",
    model=cp.pymc_models.WeightedSumFitter(),
)
result.plot()
```

The Python synthetic control ecosystem is less mature than R's. For research
requiring the exact Abadie-Diamond-Hainmueller estimator, consider running the
analysis in R.

---

## 8. Other Causal Tools

### Survival Analysis / Duration Models

**R:**
```r
library(survival)
fit <- coxph(Surv(time, event) ~ x1 + x2 + strata(group), data = df)
summary(fit)
```

**Python:**
```python
# Requires: pip install lifelines
# DAAF note: lifelines is NOT installable here — its latest release (0.30.3)
# requires pandas<3.0, incompatible with the pinned pandas 3.0.0 (see Dockerfile).
# Use R's pre-installed survival::coxph() for survival analysis instead.
from lifelines import CoxPHFitter

cph = CoxPHFitter()
cph.fit(df, duration_col="time", event_col="event", formula="x1 + x2 + strata(group)")
cph.print_summary()
```

`lifelines` is mature and well-maintained. It covers Cox PH, Kaplan-Meier,
Nelson-Aalen, and parametric survival models. However, in DAAF it cannot be
installed: its latest release (0.30.3) requires `pandas<3.0`, which conflicts
with the pinned pandas 3.0.0. The working route for survival analysis is R's
pre-installed `survival` package.

### Bayesian Causal Impact (Time Series Intervention)

**R:**
```r
library(CausalImpact)
impact <- CausalImpact(data, pre.period, post.period)
summary(impact)
plot(impact)
```

**Python:**
```python
# Requires: pip install CausalPy
import causalpy as cp

result = cp.InterruptedTimeSeries(
    df,
    treatment_time=treatment_time,
    formula="y ~ 1 + t",
    model=cp.pymc_models.LinearRegression(),
)
result.plot()
```

`CausalPy`'s interrupted time series is the closest Python analog to R's
`CausalImpact`, though the underlying methodology differs (Bayesian structural
time series in R vs. Bayesian regression in CausalPy).

### Generalized Random Forests (GRF)

**R:**
```r
library(grf)
cf <- causal_forest(X, Y, W)
ate <- average_treatment_effect(cf)
cate <- predict(cf)
```

**Python:** No direct equivalent in the DAAF stack. The `econml` package
(Microsoft) provides causal forest implementations:

```python
# Requires: pip install econml
from econml.dml import CausalForestDML

cf = CausalForestDML(model_y="auto", model_t="auto")
cf.fit(Y, T, X=X, W=W)
cate = cf.effect(X)
```

`econml` is a different implementation from `grf` and may produce different
results. The `grf` R package (Athey, Tibshirani, and Wager 2019) remains the
reference implementation for generalized random forests, including causal forests
(Wager and Athey 2018).

### Multiple Imputation

**R:**
```r
library(mice)
imp <- mice(df, m = 5, method = "pmm")
fit <- with(imp, lm(y ~ x1 + x2))
pooled <- pool(fit)
summary(pooled)
```

**Python:** **No equivalent in the DAAF stack.** This is a significant gap.
Partial alternatives exist:

| Approach | Package | Limitation |
|----------|---------|-----------|
| Single imputation | `sklearn.impute.IterativeImputer` | No Rubin's rules; single imputation only |
| Manual MI | statsmodels + sklearn | Must implement Rubin's pooling rules manually |
| `miceforest` | `pip install miceforest` | Community package; less validated than R mice |

For research requiring proper multiple imputation with Rubin's pooling rules,
R's `mice` remains the standard choice (DAAF opinion). Consider running imputation in R and
exporting the completed datasets.

---

## 9. Ecosystem Mapping Table

| Method | R Package | Python Equivalent | Fidelity | Install | Key Difference |
|--------|-----------|------------------|----------|---------|----------------|
| **OLS + FE** | `fixest::feols()` | `pf.feols()` | Very High | Pre-installed | Cluster SE syntax differs |
| **Poisson + FE** | `fixest::fepois()` | `pf.fepois()` | Very High | Pre-installed | Identical formula syntax |
| **GLM + FE** | `fixest::feglm()` | `pf.feglm()` (since 0.50) | High | Pre-installed | logit/probit/gaussian; Gamma and negbin remain R-only |
| **TWFE DiD** | `fixest::feols()` | `pf.feols()` | Very High | Pre-installed | Cluster SE syntax only |
| **did2s** | `did2s::did2s()` | `pf.did2s()` | Very High | Pre-installed | `cluster_var` vs `cluster` |
| **Sun-Abraham** | `fixest::sunab()` | `pf.event_study(est="saturated")` | High | Pre-installed | Different API, same estimator |
| **Callaway-Sant'Anna** | `did::att_gt()` | `csdid.att_gt()` | Medium | `pip install csdid` | Community port |
| **LP-DiD** | Manual (GitHub-only package; none on CRAN) | `pf.lpdid()` | N/A | Pre-installed | Python has better wrapper |
| **Event study (iplot)** | `fixest::iplot()` | `fit.iplot()` | Very High | Pre-installed | Method vs function syntax |
| **RD (sharp/fuzzy)** | `rdrobust::rdrobust()` | `rdrobust.rdrobust()` | Very High | Pre-installed | Same rdpackages project |
| **RD plots** | `rdrobust::rdplot()` | `rdrobust.rdplot()` | Very High | Pre-installed | Same rdpackages project |
| **RD density test** | `rddensity::rddensity()` | `rddensity.rddensity()` | Very High | `pip install rddensity` | Same core authors (Cattaneo, Jansson, Ma) |
| **IV + FE** | `fixest::feols()` | `pf.feols()` | Very High | Pre-installed | Identical 3-part formula |
| **IV (2SLS, no FE)** | `ivreg::ivreg()` | `linearmodels.IV2SLS()` | High | Pre-installed (Python); R `ivreg` not pre-installed | Formula syntax differs |
| **LIML** | No installed package (escalate; `ivreg` has no LIML) | `linearmodels.IVLIML()` | N/A | Pre-installed (Python) | Python only in DAAF |
| **GMM-IV** | No installed package for non-panel GMM-IV (escalate); dynamic-panel GMM: `plm::pgmm()` | `linearmodels.IVGMM()` | N/A | Pre-installed (Python) | Python has broader coverage |
| **Panel FE** | `plm::plm(model="within")` | `linearmodels.PanelOLS()` | High | Pre-installed | MultiIndex vs pdata.frame |
| **Panel RE** | `plm::plm(model="random")` | `linearmodels.RandomEffects()` | High | Pre-installed | MultiIndex vs pdata.frame |
| **Marginal effects** | `marginaleffects` | `marginaleffects` | High | Pre-installed | Same author; Python version is younger |
| **Matching (PSM)** | `MatchIt::matchit()` | `pymatchit-causal` | Low | `pip install pymatchit-causal` | **Significant gap** |
| **Weighting (IPW)** | `WeightIt::weightit()` | No equivalent | N/A | N/A | **Significant gap** |
| **Synthetic control** | `Synth`, `augsynth` | `CausalPy` (Bayesian) | Low | `pip install CausalPy` | Different methodology |
| **Causal impact** | `CausalImpact` | `CausalPy` (ITS) | Medium | `pip install CausalPy` | Different methodology |
| **Causal forests** | `grf` | `econml` | Medium | `pip install econml` | Different implementation |
| **Survival/Cox** | `survival::coxph()` | `lifelines.CoxPHFitter()` | High | Not installable (`lifelines` needs pandas<3.0; DAAF pins 3.0.0) | Use pre-installed R `survival` |
| **Multiple imputation** | `mice::mice()` | No equivalent | N/A | N/A | **Significant gap** |

### Fidelity Legend

- **Very High:** Same or near-identical API, same authors, or results match to high precision
- **High:** Reliable port with minor syntax differences; results match
- **Medium:** Community port or different implementation; verify results for published work
- **Low:** Partial coverage; significant feature gaps
- **N/A:** No equivalent available; gap in the ecosystem

> **Sources:** Cunningham, *Causal Inference: The Mixtape* (Yale, 2021), ch. 5-10 (1st edition);
> Huntington-Klein, *The Effect* (Chapman & Hall/CRC, 2022; online 2nd ed., Chs. 14, 16-21);
> Gardner, "Two-Stage Differences in Differences" (arXiv:2207.05943, 2022);
> Callaway & Sant'Anna, "Difference-in-Differences with Multiple Time Periods"
> (*J. Econometrics*, 2021);
> Sun & Abraham, "Estimating Dynamic Treatment Effects in Event Studies with
> Heterogeneous Treatment Effects" (*J. Econometrics*, 2021);
> Dube, Girardi, Jordà, & Taylor, "A Local Projections Approach to
> Difference-in-Differences" (NBER WP 31184, 2023);
> Jordà, "Estimation and Inference of Impulse Responses by Local Projections"
> (*AER*, 2005), https://doi.org/10.1257/0002828053828518;
> Roth, J., Sant'Anna, P.H.C., Bilinski, A., and Poe, J. (2023). "What's Trending in Difference-in-Differences?" *Journal of Econometrics*, 235(2), 2218-2244. https://doi.org/10.1016/j.jeconom.2023.03.008;
> Cattaneo, Idrobo, & Titiunik, *A Practical Introduction to Regression
> Discontinuity Designs: Foundations* (Cambridge, 2020);
> Montiel Olea & Pflueger, "A Robust Test for Weak Instruments"
> (*Journal of Business & Economic Statistics*, 31(3), 358-369, 2013),
> https://doi.org/10.1080/00401706.2013.806694;
> Xu, "Generalized Synthetic Control Method: Causal Inference with Interactive
> Fixed Effects Models" (*Political Analysis*, 2017),
> https://doi.org/10.1017/pan.2016.2;
> Wager & Athey, "Estimation and Inference of Heterogeneous Treatment Effects
> using Random Forests" (*J. American Statistical Association*, 2018),
> https://doi.org/10.1080/01621459.2017.1319839;
> Athey, Tibshirani, & Wager, "Generalized Random Forests" (*Annals of
> Statistics*, 2019), https://doi.org/10.1214/18-aos1709;
> Arel-Bundock, "marginaleffects" (marginaleffects.com, accessed 2026-03-28);
> Goodman-Bacon, "Difference-in-Differences with Variation in Treatment Timing"
> (*J. Econometrics*, 2021);
> de Chaisemartin & D'Haultfoeuille, "Two-Way Fixed Effects Estimators with
> Heterogeneous Treatment Effects" (*AER*, 2020)
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall