A Python package for regression and machine learning adjustments of treatment effects in randomized experiments.
You can install the package using pip:
pip install pyregadj- Implements difference-in-means estimator for randomized experiments
- Supports covariate-adjusted linear regression methods: pooled and groupwise
- Machine learning adjustment with support for Random Forest, Gradient Boosting, Lasso, Ridge, and Elastic Net.
- Optional cross-fitting with
$K$ -folds - Clean API for pandas DataFrame input
import pandas as pd
import numpy as np
from pyregadj import RegAdjustRCT
# Generate dummy data
np.random.seed(1988)
n = 200
# Create covariates
age = np.random.normal(35, 10, n)
income = np.random.normal(50000, 20000, n)
education = np.random.normal(14, 3, n)
# Generate treatment assignment (50% treated)
treatment = np.random.binomial(1, 0.5, n)
# Generate outcome with treatment effect
baseline = 100 + 0.5 * age + 0.001 * income + 2 * education + np.random.normal(0, 10, n)
outcome = baseline + 15 * treatment # True treatment effect = 15
# Create DataFrame
data = pd.DataFrame({
'y': outcome,
'd': treatment,
'age': age,
'income': income,
'education': education
})
# Initialize calculator
te = RegAdjustRCT()
# 1. Difference-in-means
result1 = te.calculate(data, outcome='y', treatment='d', adjustment='none')
# 2. Pooled regression
result2 = te.calculate(data, outcome='y', treatment='d', adjustment='pooled',
covariates=['age', 'income', 'education'])You can find detailed usage examples in the examples/ directory.
This package estimates average treatment effects (ATE) in randomized experiments using unadjusted, regression-adjusted, and machine learning-based estimators. All estimators assume a binary treatment variable, and they provide valid inference under standard assumptions of randomization.
Let
where
The estimand of interest is the average treatment effect (ATE):
This package assumes that treatment assignment is randomized:
When properly implemented, randomization ensures this assumption is satisfied. Under this assumption, the estimators described below are consistent for the ATE.
This estimator compares average outcomes in the treatment and control groups:
A two-sample
where
This estimator is equivalent to the simple linear model
although the regression framework allows for more flexible variance estimation.
This estimator fits a linear regression of the outcome on treatment and covariates:
The coefficient
This estimator can improve precision over the unadjusted estimator, particularly when covariates are predictive of the outcome.
This estimator fits separate linear models for treatment and control groups:
The ATE is estimated as:
where
Standard errors are computed using residual variances from the two regressions.
where
We now use the variance of the estimated residuals
These estimators use flexible models (e.g., random forests, gradient boosting, lasso, etc.) to estimate the conditional expectation
The ATE is estimated via:
where
When cross_fit=True, predictions are obtained via sample-splitting and
Variance is estimated from the influence functions (IFs):
where
and
This estimator offers a flexible and efficient approach, particularly in high dimensions when the methods above do not have attractive properties.
- All estimators are valid under random assignment, but adjusted estimators (linear or ML) may yield narrower confidence intervals.
- Mean-centering covariates may improve interpretability but does not affect consistency.
- The ML estimators rely on user-specified models (
rf,gbm,lasso,ridge,elastic-net) and can be cross-fit for robustness. - For small samples, linear adjustment may be preferable due to reduced variance.
- Covariates should not be post-treatment or affected by treatment.
- Lin, W. (2013). "Agnostic Notes on Regression Adjustments to Experimental Data: Reexamining Freedman’s Critique". Annals of Applied Statistics.
- List, J. A., Muir, I., & Sun, G. (2024). Using machine learning for efficient flexible regression adjustment in economic experiments. Econometric Reviews, 44(1), 2-40.
- Negi, A., & Wooldridge, J. M. (2021). Revisiting regression adjustment in experiments with heterogeneous treatment effects. Econometric Reviews, 40(5), 504-534.
- Wu, E., & Gagnon-Bartsch, J. A. (2018). The LOOP estimator: Adjusting for covariates in randomized experiments. Evaluation review, 42(4), 458-488.
This project is licensed under the MIT License - see the LICENSE file for details.
To cite this package in publications, please use the following BibTeX entry:
@misc{yasenov2025pytreateffects,
author = {Vasco Yasenov},
title = {pytreateffects: Treatment Effect Estimation for Randomized Experiments in Python},
year = {2025},
howpublished = {\url{https://github.com/vyasenov/pytreateffects}},
note = {Version 0.1.0}
}