Awesome-Agent-Skills-for-Empirical-Research stata-python-translation
git clone https://github.com/brycewang-stanford/Awesome-Agent-Skills-for-Empirical-Research
T=$(mktemp -d) && git clone --depth=1 https://github.com/brycewang-stanford/Awesome-Agent-Skills-for-Empirical-Research "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/17-DAAF-Contribution-Community-daaf/dot-claude/skills/stata-python-translation" ~/.claude/skills/brycewang-stanford-awesome-agent-skills-for-empirical-research-stata-python-tran && rm -rf "$T"
skills/17-DAAF-Contribution-Community-daaf/dot-claude/skills/stata-python-translation/SKILL.mdStata-to-Python Translation Skill
Stata-to-Python translation reference for quantitative social science data analysis. Maps Stata commands and packages (reghdfe, xtreg, ivregress, margins, esttab, svy:, graph twoway) to DAAF Python equivalents (polars, pyfixest, statsmodels, linearmodels, marginaleffects, svy, plotnine). Use when user mentions Stata background, requests Stata-equivalent code comments, needs to understand Python analysis code from a Stata perspective, or wants to translate Stata data analysis concepts to Python. Covers paradigm differences, command-by-command operation translations, regression modeling, causal inference, visualization, and workflow adaptation.
Cross-language translation reference for researchers moving between the Stata and Python data analysis ecosystems. This skill maps Stata commands, idioms, and workflows to their DAAF Python equivalents so that Stata-background users can audit, understand, and learn from DAAF-produced code, and so that code-producing agents can annotate their output with Stata equivalents when directed.
This skill is a routing hub -- it provides overview tables, decision trees, and directs readers to the detailed reference files listed below. The reference files contain the exhaustive command-by-command mappings, code examples, and edge-case documentation.
What This Skill Does
- Maps the Stata command universe to DAAF's Python stack across data management, regression modeling, causal inference, surveys, visualization, and workflow tooling
- Provides a structured annotation protocol for agents to add inline Stata-equivalent comments to Python code
- Identifies paradigm gaps where Stata and Python diverge fundamentally, so users know where to expect friction
Use cases:
- Stata user auditing DAAF Python code and needing to understand what operations are being performed
- Agent annotating code with Stata-equivalent comments for a Stata-background researcher
- Stata user learning Python for data analysis and needing a conceptual bridge
- Translating a specific Stata command or do-file idiom to its Python equivalent
- Understanding where Stata commands have no direct Python equivalent (and what the workaround is)
How to Use This Skill
Reference File Structure
Each topic in
./references/ contains focused documentation:
| File | Purpose | When to Read |
|---|---|---|
| Core language and paradigm differences (single-dataset model, missing values, value labels, macros, by:/_n/_N) | Encountering fundamental Stata-vs-Python confusion |
| gen/replace/keep/drop/sort/merge/append/reshape/collapse/egen to polars | Reading or writing data manipulation code |
| String functions, date epoch, value labels, encode/decode | Working with string, date, or categorical columns |
| regress/areg/reghdfe/xtreg/ivregress/logit/probit/margins/test/esttab to pyfixest/statsmodels/linearmodels | Reading or writing regression code |
| DiD/RDD/IV/event studies/synthetic control/matching | Working with causal inference methods |
| graph twoway/bar/box/histogram to plotnine/plotly | Reading or writing visualization code |
| svy: commands, spatial data, machine learning | Working with surveys, spatial data, or ML |
| Do-files/log/macros/ado/ssc to Python/DAAF execution model | Adapting to DAAF's execution model |
| Curated guides and tutorials with provenance | Seeking additional learning materials |
| Common Stata-user mistakes in Python | Debugging or reviewing code from Stata perspective |
Reading Order
- Stata user auditing DAAF code:
then the relevant domain file (e.g.,paradigm-differences.md
for wrangling,data-management.md
for models) thenregression-modeling.mdgotchas.md - Agent annotating code with Stata equivalents: Agent Code Annotation Protocol section below, then the relevant domain file for the code being annotated
- Learning Python from Stata background:
thenparadigm-differences.md
thendata-management.md
thenworkflow-environment.mdexternal-resources.md - Looking up a specific Stata command translation: Quick Decision Trees below, then the relevant reference file
Quick Decision Trees
"How do I do X from Stata in Python?"
What kind of Stata command? +- Data management (gen, replace, keep, drop, merge, reshape, collapse) | +-- ./references/data-management.md +- Group operations (by:, bysort, egen) | +-- ./references/data-management.md +- Regression / estimation (regress, areg, reghdfe, xtreg, logit, probit) | +-- ./references/regression-modeling.md +- Post-estimation (margins, test, lincom, nlcom, predict, esttab) | +-- ./references/regression-modeling.md +- Causal inference (diff, did_multiplegt, rdrobust, teffects, synth) | +-- ./references/causal-inference.md +- Surveys (svyset, svy:) | +-- ./references/survey-spatial-ml.md +- Plotting (graph twoway, histogram, graph bar) | +-- ./references/visualization.md +- String/date manipulation (substr, strpos, date, mdy) | +-- ./references/strings-dates-labels.md +- Value labels (label define, encode, decode) | +-- ./references/strings-dates-labels.md +-- Programming (local, global, foreach, forvalues, tempvar, preserve) +-- ./references/workflow-environment.md
"Why does this Python code look different from Stata?"
What looks unfamiliar? +- Expression syntax (pl.col().method().alias()) | +-- ./references/paradigm-differences.md +- Missing values (None vs NaN vs null vs .) | +-- ./references/paradigm-differences.md +- No single "dataset" -- multiple DataFrames everywhere | +-- ./references/paradigm-differences.md +- Value labels missing from output | +-- ./references/paradigm-differences.md +- Regression output structure (model objects vs e()/r()) | +-- ./references/regression-modeling.md +- No `by:` prefix -- .over() and .group_by() instead | +-- ./references/paradigm-differences.md +-- Import statements and namespacing +-- ./references/gotchas.md
"I want to translate a Stata do-file to Python"
What does the do-file do? +- Loads and wrangles data (use, gen, replace, keep, merge, collapse) | +-- ./references/data-management.md +- Runs regressions (regress, xtreg, reghdfe, ivregress) | +-- ./references/regression-modeling.md +- Creates tables (esttab, outreg2, margins) | +-- ./references/regression-modeling.md +- Creates plots (graph twoway, histogram) | +-- ./references/visualization.md +- Uses survey weights (svyset, svy:) | +-- ./references/survey-spatial-ml.md +- Multiple of the above | +-- Start with ./references/paradigm-differences.md, then each relevant file +-- Uses macros, loops, or programs +-- ./references/workflow-environment.md
"Something isn't working and I think it's a Stata habit"
What went wrong? +- Missing values behaving differently than expected | +-- ./references/paradigm-differences.md +- gen/replace pattern not translating | +-- ./references/gotchas.md +- Merge producing wrong results (no _merge diagnostic) | +-- ./references/gotchas.md +- Model output looks different from Stata | +-- ./references/regression-modeling.md +- Off-by-one error (0-indexed vs 1-indexed) | +-- ./references/gotchas.md +- `by:` / `_n` / `_N` not available | +-- ./references/paradigm-differences.md +-- Macro syntax not working +-- ./references/gotchas.md
"Which Python package replaces my Stata command?"
Which Stata command? +- regress / areg / reghdfe -> pyfixest | +-- ./references/regression-modeling.md +- xtreg (fe/re) -> pyfixest (FE) / linearmodels (RE) | +-- ./references/regression-modeling.md +- ivregress / ivreg2 / ivreghdfe -> pyfixest / linearmodels | +-- ./references/regression-modeling.md +- logit / probit / ologit / mlogit -> statsmodels | +-- ./references/regression-modeling.md +- poisson / nbreg / ppmlhdfe -> statsmodels / pyfixest fepois | +-- ./references/regression-modeling.md +- margins / marginsplot -> marginaleffects | +-- ./references/regression-modeling.md +- esttab / outreg2 -> pf.etable() | +-- ./references/regression-modeling.md +- test / lincom / nlcom -> pyfixest .wald_test() / marginaleffects hypotheses() | +-- ./references/regression-modeling.md +- gen / replace / drop / keep / sort -> polars | +-- ./references/data-management.md +- merge / append -> polars .join() / pl.concat() | +-- ./references/data-management.md +- collapse / egen -> polars .group_by().agg() / .over() | +-- ./references/data-management.md +- reshape long/wide -> polars .unpivot() / .pivot() | +-- ./references/data-management.md +- graph twoway / histogram / graph bar -> plotnine / plotly | +-- ./references/visualization.md +- svyset / svy: -> svy package | +-- ./references/survey-spatial-ml.md +- rdrobust -> rdrobust (Python, same authors) | +-- ./references/causal-inference.md +- binscatter -> binsreg (Python, same authors) | +-- ./references/causal-inference.md +- synth -> scpi (Python, same authors) | +-- ./references/causal-inference.md +-- ado-file / ssc install -> pip install +-- ./references/workflow-environment.md
Command Mapping Overview
| Stata Command(s) | Python Package | Fidelity | Key Difference |
|---|---|---|---|
, , | pyfixest | High | Near-identical formula syntax; for FE absorption |
| pyfixest | High | No needed; FE specified in formula |
| linearmodels | Medium | Requires pandas MultiIndex for panel structure |
, , | pyfixest / linearmodels | High | Three-part formula for IV in pyfixest |
, , , | statsmodels | Medium | Requires ; for categoricals in formulas |
, | statsmodels / pyfixest | High | for Poisson with multi-way FE |
, | marginaleffects | Medium | Separate package; different function names |
, | pyfixest | High | produces publication tables |
, , , , | polars | Low | Expression system vs imperative commands; immutable DataFrames |
, | polars , | Medium | No automatic diagnostic |
, | polars , | Medium | Must choose aggregation vs window explicitly |
| polars , | Medium | No in-place reshape; column naming differs |
, , | plotnine / plotly | Low | Declarative grammar of graphics vs imperative graph syntax |
, | svy | Medium | Explicit / objects instead of persistent |
, | rdrobust (Python) | Very High | Same authors; nearly identical API |
, | binsreg (Python) | Very High | Same authors; nearly identical API |
| scpi | High | Same authors; includes prediction intervals |
, , , | Python variables, f-strings, for loops | Low | Fundamentally different paradigm (text substitution vs value binding) |
Fidelity key: Very High = same authors, near-identical API. High = same capability, similar syntax. Medium = same capability, different API patterns. Low = fundamentally different paradigm requiring conceptual remapping.
Library Versions
Translations in this skill reference specific library versions. Python versions are pinned in DAAF's Docker environment (Python 3.12). Stata versions reference the current release as of March 2026. When syntax or behavior has changed between versions, the reference files note the change.
| Python Package | DAAF Version | Stata Equivalent | Stata Version |
|---|---|---|---|
| polars | 1.38.1 | Data management commands (gen, replace, merge, etc.) | Stata 18 |
| pyfixest | 0.40.0 | regress, areg, reghdfe, ivreghdfe, ppmlhdfe, esttab | Stata 18 + reghdfe 6.x |
| statsmodels | 0.14.6 | regress, logit, probit, poisson, nbreg, glm | Stata 18 |
| linearmodels | unpinned | xtreg, sureg, ivregress | Stata 18 |
| plotnine | 0.15.3 | graph twoway, graph bar, graph box, histogram | Stata 18 |
| plotly | 6.5.2 | (no direct Stata equivalent; interactive charts) | N/A |
| svy | 0.13.0 | svyset, svy: prefix commands | Stata 18 |
| marginaleffects | unpinned | margins, marginsplot, lincom, nlcom | Stata 18 |
| rdrobust | unpinned | rdrobust, rdplot, rdbwselect | rdrobust (SSC) |
| binsreg | unpinned | binsreg, binscatter | binsreg (SSC) |
| scpi | unpinned | synth, synth_runner | synth (SSC) |
| scikit-learn | 1.8.0 | (limited; teffects, psmatch2 partially) | Stata 18 |
| marimo | 0.19.11 | (no equivalent; replaces do-file + log workflow) | N/A |
Unpinned packages: linearmodels, marginaleffects, rdrobust, binsreg, and scpi install the latest version at Docker build time. Translations reference their documented API as of March 2026.
Stata version note: Stata 18 is the current release as of March 2026. Most command mappings apply to Stata 15+; version-specific features (frames,
hdidregress) are noted
in the reference files.
Top 10 Paradigm Differences
These are the friction points Stata users encounter most frequently when reading or writing DAAF Python code. Each is covered in depth in the referenced file.
| # | Friction Point | Stata Way | Python Way | Reference |
|---|---|---|---|---|
| 1 | Single-dataset model | One dataset in memory; commands implicit | Multiple DataFrames as variables; must specify which | |
| 2 | Missing values | = +infinity; 27 types (-) | excluded from comparisons; one null type; NaN distinct | |
| 3 | Value labels | Three-layer system (data, variable, value labels) | No built-in equivalent; dictionaries or Enum types | |
| 4 | // system | | (window) vs (collapse) | |
| 5 | In-place modification | modifies data directly | returns new DataFrame | |
| 6 | Macro system | and text substitution | Python variables + f-strings | |
| 7 | required | auto-fits and prints | -- two steps | |
| 8 | Expression system | (bare column names) | | |
| 9 | Merge diagnostics | variable (1/2/3) automatic | No automatic merge indicator; must add explicitly | |
| 10 | 1-based vs 0-based indexing | starts at 1; = first obs | Python indices start at 0 | |
Agent Code Annotation Protocol
This section defines when and how code-producing agents add inline Stata-equivalent comments to DAAF Python scripts.
When to Annotate
Annotations are added only when the orchestrator explicitly passes a Stata-background directive to the agent. This is not a default behavior.
Trigger conditions (orchestrator activates this when any apply):
- User states they have a Stata background
- User requests Stata-equivalent comments in code
- User asks to understand Python code from a Stata perspective
How the orchestrator passes the directive: The orchestrator adds the following to the agent prompt:
"User has Stata background. Load stata-python-translation skill. Add inline Stata-equivalent comments for non-trivial data operations."
Comment Format
# Stata: gen log_income = log(income) df = df.with_columns(pl.col("income").log().alias("log_income")) # Stata: bysort state: egen mean_score = mean(test_score) df = df.with_columns( pl.col("test_score").mean().over("state").alias("mean_score") ) # Stata: reghdfe wage education experience, absorb(industry year) cluster(state) fit = pf.feols("wage ~ education + experience | industry + year", data=pdf, vcov={"CRV1": "state"}) # Stata: drop if missing(income) df = df.filter(pl.col("income").is_not_null()) # Stata: merge 1:1 school_id using "districts.dta" df = df.join(districts, on="school_id", how="inner")
What to Annotate
- Annotate: Data wrangling (polars operations), modeling calls (pyfixest, statsmodels, linearmodels), visualization layer construction (plotnine, plotly), causal inference method calls, survey estimation calls
- Do NOT annotate: Import statements,
/print()
validation lines, file I/O boilerplate (assert
,pl.read_parquet
), config sections, section separator commentsdf.write_parquet
Rules
- One
comment per logical operation, placed on the line immediately above the Python code# Stata: - Keep annotations to a single line; abbreviate complex Stata command sequences if needed
- Stata annotations are in addition to standard IAT comments (
,# INTENT:
,# REASONING:
), not a replacement# ASSUMES: - Consumer agents: research-executor, code-reviewer, debugger, data-ingest
Related Skills
| Skill | Relationship |
|---|---|
| Python-side data wrangling -- detailed API reference for the gen/replace/merge/collapse equivalent |
| Python-side fixed effects regression -- detailed API for the regress/reghdfe/ivregress equivalent |
| Python-side static visualization -- detailed API for the graph twoway equivalent |
| Python-side interactive visualization -- no direct Stata equivalent |
| Python-side general modeling -- covers logit, probit, poisson, nbreg, glm equivalents |
| Python-side panel/IV models -- covers xtreg, sureg, ivregress equivalents |
| Python-side ML -- covers limited teffects/matching equivalents |
| Python-side survey analysis -- covers svyset and svy: prefix command equivalents |
| Python-side spatial data -- covers Stata spmap/spregress equivalents |
| Python-side notebooks -- replaces do-file + log workflow |
| Parallel skill for R-background users -- shares the same Python target stack |
Note: Individual tool skills contain library-specific usage guidance (syntax, gotchas, performance). This skill provides the Stata-to-Python conceptual bridge -- use both together when a Stata-background user is working with a specific library.
Topic Index
| Topic | Reference File |
|---|---|
| Single-dataset model (one dataset in memory) | |
| Missing values (. vs None/NaN/null) | |
| Extended missing values (.a-.z) | |
| Value labels (label define, label values) | |
| Variable labels (label variable) | |
| by: prefix and _n/_N system variables | |
| In-place modification vs immutable DataFrames | |
| Macro system (local, global) | |
| Estimation and e()/r() stored results | |
| Type system (destring/tostring vs .cast()) | |
| Panel data operators (L./F./D. vs .shift()/.over()) | |
| Package model (ssc install vs pip install) | |
| gen / replace / rename | |
| drop / keep (variables and observations) | |
| sort / gsort | |
| merge 1:1 / m:1 / 1:m | |
| append | |
| reshape long / reshape wide | |
| collapse (aggregation) | |
| egen functions (mean, sum, count, rowtotal, group, tag) | |
| encode / decode | |
| String functions (substr, strpos, subinstr, regexm) | |
| Date system (epoch, %td, mdy(), date()) | |
| destring / tostring | |
| regress (OLS) | |
| areg / reghdfe (fixed effects) | |
| xtreg fe / xtreg re (panel models) | |
| ivregress / ivreg2 / ivreghdfe (IV) | |
| logit / probit / ologit / mlogit | |
| poisson / nbreg / ppmlhdfe | |
| sureg (seemingly unrelated regression) | |
| margins / marginsplot (marginal effects) | |
| test / lincom / nlcom (hypothesis testing) | |
| esttab / outreg2 (regression tables) | |
| predict (fitted values, residuals) | |
| Robust and clustered standard errors | |
| Factor variable notation (i., c., #, ##) | |
| diff / did_multiplegt / csdid (DiD) | |
| eventstudyinteract / event studies | |
| rdrobust / rdplot (regression discontinuity) | |
| teffects / psmatch2 / cem (matching) | |
| synth / synth_runner (synthetic control) | |
| binscatter / binsreg | |
| graph twoway scatter / line / area / connected | |
| graph bar / graph box / histogram / kdensity | |
| graph export | |
| coefplot / iplot | |
| svyset / svy: prefix | |
| svy: mean / total / proportion / ratio | |
| svy: regress / logit | |
| Spatial data analysis | |
| Machine learning (teffects, matching workarounds) | |
| Do-file execution model | |
| Log files (log using) | |
| Ado-files and ssc install | |
| Macros in loops (foreach, forvalues) | |
| tempvar / tempfile / preserve / restore | |
| quietly / capture / noisily | |
| Curated Stata-to-Python migration guides | |
| Textbooks with trilingual code (Stata/R/Python) | |
| Package documentation links | |
| Tutorial recommendations with provenance | |
| gen/replace pattern not translating | |
| Missing value comparison traps | |
| drop if -> filter(NOT) negation trap | |
| Merge without _merge diagnostics | |
| egen rowtotal missing-value behavior | |
| .fit() forgotten in statsmodels | |
| Robust SE syntax differences | |
| 0-based vs 1-based indexing | |
| Macro syntax in Python context | |
| Error message translation table | |