DAAF-Contribution-Community/daaf / .claude/skills/python-r-translation
python-r-translation
VerifiedPython-to-R translation for data analysis. Maps Python (polars, plotnine, pyfixest, statsmodels, svy, geopandas) to R (tidyverse, ggplot2, fixest, survey, sf). Use when user has Python background or requests Python-equivalent code comments in R pipelines.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 22.6 KB |
| references/causal-inference.md | markdown | 6.2 KB |
| references/dplyr-polars.md | markdown | 11.4 KB |
| references/external-resources.md | markdown | 7.2 KB |
| references/gotchas.md | markdown | 9.8 KB |
| references/paradigm-differences.md | markdown | 17.0 KB |
| references/regression-modeling.md | markdown | 9.4 KB |
| references/strings-dates-factors.md | markdown | 8.2 KB |
| references/survey-spatial-ml.md | markdown | 8.0 KB |
| references/visualization.md | markdown | 5.2 KB |
| references/workflow-environment.md | markdown | 6.0 KB |
references/causal-inference.md
View on GitHub# Causal Inference: R Explained for Python Users
In DAAF's assessment, R has been the primary language for causal inference in
quantitative social science. Many influential causal methods papers ship R
implementations first.
This reference explains R causal inference code in terms Python users know.
DAAF's R stack provides strong coverage:
- **fixest:** TWFE DiD, Sun-Abraham (`sunab()`), event studies (`i()`, `iplot`)
- **did2s:** Two-stage DiD (Gardner, 2022)
- **rdrobust:** Regression discontinuity (same rdpackages project as the Python version)
- **marginaleffects:** Post-estimation interpretation (same author as Python)
- **MatchIt/WeightIt:** Matching and weighting (R has much stronger coverage)
> **Versions referenced:**
> R: fixest 0.14.0, marginaleffects 0.32.0, rdrobust 3.0.0
> Python: pyfixest 0.60.0, marginaleffects 0.5.0, rdrobust 1.3.0
> See SKILL.md § Library Versions for the complete version table.
> **Availability:** Of the R packages below, `fixest`, `marginaleffects`,
> `rdrobust` (3.0.0), and `survival` (ships with the R distribution) are
> pre-installed in DAAF. The specialized causal packages referenced here —
> `did2s`, `did`, `MatchIt`, `WeightIt`, `cobalt`, `grf`, `Synth`,
> `augsynth`, `gsynth` — are NOT pre-installed, and runtime installs are blocked
> (see CLAUDE.md § Runtime Package Installation) — escalate to the user to add the
> needed package to the Dockerfile (user additions block) and rebuild before use.
---
## 1. Difference-in-Differences
### Traditional TWFE DiD
```r
# R (fixest) -- you know this as pf.feols("y ~ treated | unit + time", ...)
fit <- feols(y ~ treated | unit + time, data = df, vcov = ~unit)
```
Formula is identical. Only SE syntax differs (`~unit` vs `{"CRV1": "unit"}`).
### Two-Stage DiD (did2s)
```r
# R -- you know this as pf.did2s(...)
library(did2s)
fit <- did2s(
data = df, yname = "y",
first_stage = ~ 0 | unit + time,
second_stage = ~ i(rel_time, ref = -1),
treatment = "treated", cluster_var = "unit"
)
iplot(fit)
```
Key difference: R uses `cluster_var`; Python uses `cluster`.
### Sun-Abraham Saturated Estimator
```r
# R -- sunab() is a formula function inside feols
fit <- feols(y ~ sunab(cohort, period) | unit + period, data = df, vcov = ~unit)
summary(fit, agg = "ATT")
iplot(fit)
```
```python
# Python -- uses a separate function
fit = pf.event_study(data=df, yname="y", idname="unit", tname="period",
gname="cohort", estimator="saturated", att=False)
```
R uses `sunab()` inline in the formula. Python uses `pf.event_study()` with
`estimator="saturated"`.
---
## 2. Event Studies
```r
# R -- you know this as pf.feols("y ~ i(rel_year, ref=-1) | unit + year", ...)
df$rel_year <- df$year - df$treatment_year
fit <- feols(y ~ i(rel_year, ref = -1) | unit + year, data = df, vcov = ~unit)
iplot(fit)
iplot(fit, joint = TRUE) # Bonferroni bands
```
| R | Python |
|---|--------|
| `iplot(fit)` (standalone) | `fit.iplot()` (method) |
| `iplot(fit, joint = TRUE)` | `fit.iplot(joint="both")` |
---
## 3. Regression Discontinuity
Both R and Python rdrobust come from the rdpackages project (author lists
differ: see the Sources entry and the Python package metadata). The API is
virtually identical.
```r
# R -- you know this as rdrobust.rdrobust(Y, X, c=cutoff)
library(rdrobust)
rd <- rdrobust(Y, X, c = cutoff)
summary(rd)
rdplot(Y, X, c = cutoff)
```
| R | Python |
|---|--------|
| `rdrobust(Y, X, c = cutoff)` | `rdrobust(Y, X, c=cutoff)` |
| `rdplot(Y, X, c = cutoff)` | `rdplot(Y, X, c=cutoff)` |
| `h = c(left, right)` | `h=[left, right]` |
| `covs = cbind(c1, c2)` | `covs=covs_array` |
The API is nearly identical. Main differences: R's `c()` vs Python's `[]`,
R's `cbind()` vs numpy arrays.
---
## 4. Matching and Weighting
This is where **R significantly exceeds Python coverage.** R has mature, validated
packages; Python alternatives are fragmented.
```r
# R (MatchIt) -- no strong Python equivalent
library(MatchIt)
m.out <- matchit(treat ~ x1 + x2 + x3, data = df, method = "nearest")
summary(m.out)
matched_df <- match.data(m.out)
# R (WeightIt) -- no Python equivalent
library(WeightIt)
w.out <- weightit(treat ~ x1 + x2 + x3, data = df, method = "ps")
```
| R Package | Python Equivalent | Fidelity |
|-----------|------------------|----------|
| `MatchIt` | Manual with scikit-learn | Low |
| `WeightIt` | No equivalent | N/A |
| `cobalt` (balance) | No equivalent | N/A |
---
## 5. Marginal Effects
Both implementations are by Vincent Arel-Bundock. The API is intentionally
parallel.
```r
# R -- you know this as marginaleffects.avg_slopes(fit, variables="x1")
library(marginaleffects)
avg_slopes(fit, variables = "x1")
predictions(fit, newdata = datagrid(x1 = c(0, 1), x2 = mean))
hypotheses(fit, "x1 = x2")
```
The R version supports 100+ model classes. The Python version supports
statsmodels, pyfixest, and scikit-learn.
---
## 6. Ecosystem Comparison
| Method | R | Python | Fidelity |
|--------|---|--------|----------|
| TWFE DiD | `fixest::feols()` | `pf.feols()` | Very High |
| did2s | `did2s::did2s()` | `pf.did2s()` | Very High |
| Callaway-Sant'Anna | `did::att_gt()` (not pre-installed) | `csdid` (`pip install csdid`) | Medium |
| Sun-Abraham | `fixest::sunab()` | `pf.event_study(est="saturated")` | High |
| LP-DiD | Manual (GitHub-only package; none on CRAN) | `pf.lpdid()` | Python has better wrapper |
| RD | `rdrobust` | `rdrobust` (Python) | Very High (same rdpackages project) |
| IV + FE | `fixest::feols()` | `pf.feols()` | Very High |
| **Matching** | `MatchIt` | Manual | **Significant gap** |
| **Weighting** | `WeightIt` | No equivalent | **Significant gap** |
| Synthetic control | `Synth`, `augsynth`, `gsynth` | `CausalPy` (Bayesian) | Low |
| Causal forests | `grf` | `econml` | Medium |
| GLM + FE | `fixest::feglm()` | `pf.feglm()` (logit/probit/gaussian, since 0.50) | High for those families; Gamma/negbin still R-only |
| Survival/Cox | `survival::coxph()` | `lifelines` | High |
> **Sources:** Cunningham, *Causal Inference: The Mixtape* (2021);
> Huntington-Klein, *The Effect* (Chapman & Hall/CRC, 2022);
> Gardner, "Two-Stage Differences in Differences" (arXiv:2207.05943, 2022);
> Calonico, Cattaneo, Farrell, & Titiunik, *rdrobust* (https://github.com/rdpackages/rdrobust; *Stata Journal* 17(2), 372-404, 2017)
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deployment | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |