Skip to main content

StataFlow

A Python econometrics toolkit designed to reproduce Stata 17 estimation results with field-level validation.

简体中文

PyPI version Python 3.10+


from stataflow.compat.stata import reghdfe

result = reghdfe(
    df,
    y="lwage",
    x=["exper", "edu"],
    absorb="firm_id year_id",
    vce="cluster",
    cluster="firm_id",
)
result.display()

Why StataFlow

StataFlow is for researchers who want Python workflows without giving up the empirical conventions they rely on in Stata. The project is not a generic statistics library: public capabilities are backed by synthetic cases, public real-data cases, and field-level Stata 17 comparisons.

The current release is 1.2.0, covering 14 Stata-style commands.

Features

  • 14 estimation commands in Python: regress, xtreg_fe, areg, reghdfe, ivregress_2sls, ivreghdfe, logit, probit, poisson, ppmlhdfe, did_imputation, eventstudyinteract, csdid, and rdrobust. The exported rdplot companion is a helper and is not counted as an estimation command.
  • Two API layers: a Stata-compatible command layer (stataflow.compat.stata) and a Python-native estimator layer (stataflow.estimators).
  • Stata-style output: result.display() prints compact regression tables with coefficients, standard errors, test statistics, p-values, and fit statistics.
  • High-dimensional fixed effects: MAP absorption for large FE designs, multi-FE workflows, singleton handling, individual slopes, and cluster-aware VCE paths.
  • Instrumental variables: 2SLS, GMM2S, LIML, Fuller/k-class, first-stage diagnostics, weak-instrument tests, and overidentification tests.
  • Binary, count, and PPML models: Logit, Probit, Poisson, and PPML-HDFE with robust and clustered covariance estimators.
  • Causal inference: BJS DID imputation, Sun-Abraham event-study interactions, Callaway-Sant'Anna DID, and sharp/fuzzy regression discontinuity.
  • Stata-compatible syntax subsets: factor variables, command-specific analytic-weight support, multiple fixed effects, common VCE choices, and hard rejection of unsupported parameters.
  • Validation-first development: public commands are backed by field-level Stata 17 comparison evidence.

Installation

pip install StataFlow

Python 3.10, 3.11, or 3.12 is required. Core dependencies are NumPy, pandas, SciPy, scikit-learn, and PyYAML.

Quick Start

Stata-Compatible API

from stataflow.compat.stata import regress, reghdfe, logit, ivregress_2sls, ppmlhdfe

# OLS with robust standard errors
result = regress(df, y="wage", x=["edu", "exper"], vce="robust")
result.display()

# High-dimensional fixed effects
result = reghdfe(
    df,
    y="wage",
    x=["edu", "exper"],
    absorb="firm_id year_id",
    vce="cluster",
    cluster="industry",
)

# Logit
result = logit(df, y="inlf", x=["nwifeinc", "educ", "exper"])
result.display()

# 2SLS with robust VCE
result = ivregress_2sls(
    df,
    y="lwage",
    x_exog=["educ"],
    x_endog=["exper"],
    instruments=["age", "kidslt6"],
    vce="robust",
)

# PPML with high-dimensional fixed effects
result = ppmlhdfe(
    df,
    y="trade",
    x=["lndist", "contig", "fta"],
    absorb=["exporter", "importer", "year"],
    vce="cluster",
    cluster="exporter",
)

Native Python API

from stataflow import OLS, AbsorbingOLS, Logit

model = OLS(data=df, y="wage", x=["edu", "exper"])
result = model.fit(vce="robust")
result.display()

Programmatic Results

result.display(show_ci=True)

for coef in result.coefficients:
    print(f"{coef.name}: b={coef.beta:.6f}, se={coef.std_err:.6f}, t={coef.t_stat:.2f}")

print(f"R2 = {result.fit.r2:.4f}, N = {result.sample.nobs}")

Supported Models

Family Available via Estimators and VCE
Linear regress, areg, xtreg_fe, reghdfe OLS with ols, robust (HC1), and command-specific clustering; reghdfe also supports dkraay panel HAC
IV ivregress_2sls, ivreghdfe 2SLS, GMM2S, LIML, Fuller/k-class, first-stage diagnostics, weak-IV tests
Binary / Count logit, probit, poisson MLE with ols, robust, and cluster VCE
PPML + HDFE ppmlhdfe IRLS with fixed effects, offset/exposure, separation checks, eform, and common prediction types
DID did_imputation, csdid, eventstudyinteract BJS imputation, Callaway-Sant'Anna, and Sun-Abraham IW estimators
RD rdrobust Sharp/fuzzy RD, MSE/CER bandwidth selectors, covariates, weights, mass points, and cluster/nncluster VCE

See the Command Support Matrix and Known Issues for exact support boundaries.

Validation

The July 2026 release scope is frozen to the cases summarized below. Relative deviation is |Python - Stata| / max(|Stata|, 1e-15).

Family Covered commands Stata 17 comparisons Max coefficient deviation Max SE deviation
Linear / FE regress, areg, xtreg_fe, reghdfe 18/18 2.48e-7 2.25e-7
IV ivregress_2sls, ivreghdfe 5/5 1.16e-8 3.74e-8
Binary / count logit, probit, poisson, ppmlhdfe 12/12 1.33e-7 8.42e-8
DID did_imputation, csdid, eventstudyinteract 2/2 + 1 functional check 8.13e-8 5.13e-8
RD rdrobust 3/3 9.23e-8 2.96e-8
Total 14 public estimation commands 40/40 2.48e-7 2.25e-7

Full local Stata validation checks: 856 passed, 12 skipped. The public, self-contained suite passes 10/10 reproducible validation cases with Stata 17. The values above are stored in evidence-summary.json.

Documentation

Running Tests

# Unit and integration tests
pytest tests/ -v

# Reproducible Stata validation cases (require local Stata 17)
pytest tests/stata_validation/ -v -s

Community

License

This project is licensed under the MIT License. See LICENSE for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stataflow-1.2.0.tar.gz (178.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stataflow-1.2.0-py3-none-any.whl (150.8 kB view details)

Uploaded Python 3

File details

Details for the file stataflow-1.2.0.tar.gz.

File metadata

  • Download URL: stataflow-1.2.0.tar.gz
  • Upload date:
  • Size: 178.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for stataflow-1.2.0.tar.gz
Algorithm Hash digest
SHA256 a273acf9fd00f091c30304b850bdeba1757a9fa5758b1e3eebb10f8dad752bdd
MD5 757621254756863ad43ed5aa961f4851
BLAKE2b-256 3b507d9bf7a15706c1e912ad42e8e8528ee3e55964624726db72486f3cdc7c8b

See more details on using hashes here.

File details

Details for the file stataflow-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: stataflow-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 150.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for stataflow-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c301684618f9a382610df5ec71843afd0dad334d8eb3071c9fa4e0ad1fb958de
MD5 32dba23e4b4f70dd1a10b6f10198701f
BLAKE2b-256 5cc1a959897f6cc16c8862e30ec78dbc5c976ba89671c0eadb324b451f253567

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page