brycewang-stanford/StatsPAI

StatsPAI is the first Agent-native Python library for causal inference and applied econometrics — unified API, broad cross-method coverage, structured result objects, machine-readable schemas, Skills, an MCP server, and R/Stata parity validation.

StatsPAI – A Python‑native replacement for Stata & R in empirical research

What it does – StatsPAI bundles a huge collection of econometric and causal‑inference tools that traditionally live in Stata or R (e.g., regress, ivregress, reghdfe, csdid, rdrobust, synth, psmatch2, esttab). It re‑exposes these commands through a clean, Python‑native API while preserving the familiar syntax and options (robust/cluster standard errors, vce= handling, post‑estimation commands like test, lincom, margins). The package also ships a registry of ~1,600 functions, built‑in datasets, and utilities for exporting tables to Excel, Word, LaTeX, or directly to LLM‑agents.

Who it’s for – Empirical economists, social scientists, or policy analysts who currently switch between Stata, R, and Python. If you write a lot of regression, DiD, RD, synthetic‑control, or matching code and want to stay in Python without losing the “Stata‑style” workflow, this library is aimed at you.

Key features

  • Stata‑style API – functions such as sp.regress, sp.ivreg, sp.feols, sp.rdrobust, sp.synth, sp.psmatch2, etc. Options follow Stata conventions (vce="robust", vce="cluster firm").
  • R‑style equivalents – many functions map 1‑to‑1 to R packages (lm, fixest, did, DoubleML). The same call works for both worlds.
  • Agent‑ready metadata – every public function registers a machine‑readable schema (sp.describe_function, sp.function_schema). A bundled statspai‑mcp service can expose the estimators to Claude Code, Cursor, etc.
  • Cross‑language bridges – sp.stata("…", data=df) runs raw Stata code; sp.from_stata / sp.from_r translate commands into the Python API.
  • Extensible extras – optional dependencies add plotting (statspai[plotting]), high‑dimensional fixed effects (statspai[fixest]), Bayesian models (statspai[bayes]), neural‑network causal models (statspai[neural]), JAX acceleration (statspai[performance]), and spatial analysis (statspai[spatial]).
  • Built‑in datasets – 14 offline datasets (Card 1995, LaLonde/NSW, RD Senate data, etc.) for quick demos and validation.
  • Result export – sp.regtable builds a single table object that can be printed, saved as .xlsx, .docx, LaTeX, Markdown, or HTML.
  • Interactive graph editor – sp.interactive(fig) gives a Stata‑like GUI inside Jupyter notebooks for fine‑tuning Matplotlib/Seaborn/Plotly figures.

How it works – The core library (≈ 634 k lines) implements the statistical estimators, a registry system, and a thin wrapper around existing Python packages (e.g., pyfixest, linearmodels, DoubleML, EconML). Validation status (certified, validated, api_stable) is attached to each function so users know whether the numerical output has been cross‑checked against Stata/R.

Typical workflow

import statspai as sp

# Load a built‑in dataset
card = sp.datasets.card_1995()

# Ordinary least squares with Stata‑style robust SEs
ols = sp.regress("lwage ~ educ + exper + expersq + black + south + smsa",
                data=card, robust="hc1")
print(ols.summary())

# Post‑estimation test and marginal effects
print(ols.test("educ = 0"))
print(sp.margins(ols, variables=["educ"]))

# Export a regression table with multiple specifications
tbl = sp.regtable(ols, sp.ivreg(...), model_labels=["OLS", "2SLS"])
tbl.to_excel("table1.xlsx")

The same code can be called from an LLM‑agent, and the agent can query the function schema to discover available arguments.

Why it matters – It lowers the friction of moving empirical workflows into Python, keeps the statistical conventions that researchers trust (Stata‑style small‑sample corrections, clustering, post‑estimation commands), and makes those workflows discoverable by AI assistants. The project is open‑source, peer‑reviewed (JOSS 2026), and maintained by the Stanford REAP team.


All details above are taken directly from the repository’s README; no additional features have been inferred.

Related

  • Project
  • Project
  • Project
  • Project
  • Project