Causal Inference with AI Agents
Empirical Analysis in Python with StatsPAI
Preface


This book teaches causal inference and empirical analysis in Python with StatsPAI, and shows how to do that work with a coding agent such as Claude Code or Codex. You are the principal investigator; the agent is your research assistant; StatsPAI is the toolkit you share. Each design chapter follows the same steps: the question, the assumption that identifies the effect, the estimator built by hand, the same analysis asked of an agent, a reading of what the agent did, the ways agents go wrong with that design, and a checklist.
import statspai as spThat one import covers the econometrics in the book: regression, panel data, causal graphs, instrumental variables, regression discontinuity, difference-in-differences, synthetic control, double machine learning, causal forests and g-methods. Almost all data used in the book ship with the package or are fetched by it; the chapter that uses any other data links to it.
StatsPAI is also built to be used by AI agents. Its functions describe themselves, so an agent can find the right estimator, check the design before fitting, and chain robustness checks onto a result. Through the StatsPAI MCP server, an agent can take a question such as “did the policy work?” from raw data to an audited estimate. An agent is fast, but it is not always right. This book teaches the econometrics you need to direct the agent and to check what it did. Chapters 2 and 3 set up Claude Code or Codex with these tools.
This is the outline of the book. Chapters are being written. Each chapter page lists its planned sections, data and functions.
How the book is organised
- Part I, The Agentic Research Desk, sets up Claude Code or Codex with the StatsPAI MCP server, skills and plugins, and walks through a first agent session.
- Part II, Directing and Checking an Agent, covers writing a research brief, reading what the agent did, cross-checking against Stata and R, and how fast an agent can p-hack.
- Part III, Regression with an Agent, covers OLS and inference, panel data, limited dependent variables and time series.
- Part IV, Causal Designs, Agent-Driven, starts from potential outcomes and causal graphs (DAGs, confounders, colliders, bad controls and selection bias), then gives one chapter per design: matching and weighting, IV, RD, DiD, staggered DiD and synthetic control.
- Part V, Machine Learning, Heterogeneity and Time-Varying Treatments, covers double machine learning, heterogeneous effects and g-methods.
- Part VI, From Estimate to Paper, with Agents, covers sensitivity analysis, tables and replication packages, writing with skills, multi-agent research crews and how to evaluate research agents.
Software
The book is built with StatsPAI 1.47.0 and Python 3.12.
License
The text of this book is licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0). The code in this book is licensed under the MIT License. You may reuse both, with attribution.
Claude and Claude Code are trademarks of Anthropic. Codex, OpenAI and the OpenAI logo are trademarks of OpenAI. Their logos appear on the cover only to identify the tools this book uses. This book is not affiliated with, or endorsed by, Anthropic or OpenAI. The cover logos are not covered by the CC BY 4.0 or MIT licenses above.