Skip to main content

DEPRECATED — use StatsPAI (pip install statspai). Final release of the Python reghdfe port.

Project description

pyreghdfe

⚠️ DEPRECATED — use StatsPAI instead

pyreghdfe 0.3.0 is the final release. The package is no longer maintained. Its entire feature surface — multi-way FE OLS, robust / multi-way cluster SEs, singleton dropping, weights, LSMR/LSQR Krylov solvers — is now a strict subset of StatsPAI (v1.6.6+).

pip install statspai
# pyreghdfe (legacy)
from pyreghdfe import reghdfe
res = reghdfe(data=df, y="wage", x=["experience"],
              fe=["firm_id", "year"], cluster=["firm_id"])

# StatsPAI equivalent
import statspai as sp
res = sp.absorb_ols(
    y=df["wage"].values,
    X=df[["experience"]].values,
    fe=df[["firm_id", "year"]],
    cluster=df["firm_id"].values,
    solver="lsmr",   # or "map" (faster default) / "lsqr"
)

StatsPAI ships additional capabilities pyreghdfe never provided: PPML-HDFE (sp.ppmlhdfe), a formula interface (sp.hdfe_ols("y ~ x | firm + year", data=df)), a Rust-accelerated mean-sweep kernel, and a unified result object with .summary() / .to_latex() / .to_excel().

Migration guide: https://github.com/brycewang-stanford/StatsPAI/blob/main/MIGRATION.md#migrating-from-pyreghdfe

What the 0.3.0 release does: it still installs and works. Importing pyreghdfe now emits a DeprecationWarning with a migration link; no API was broken. Future bugs and feature requests should be filed against StatsPAI — this repository is archived.


Python Version PyPI Version License: MIT Downloads

Python implementation of Stata's reghdfe for high-dimensional fixed effects regression

pyreghdfe is a fast and efficient Python package that replicates the functionality of Stata's popular reghdfe command. It provides high-dimensional fixed effects estimation, cluster-robust standard errors, and seamless integration with pandas DataFrames.

Quick Installation

pip install pyreghdfe

Quick Start

import pandas as pd
import numpy as np
from pyreghdfe import reghdfe

# Create sample data
np.random.seed(42)
n = 1000
data = pd.DataFrame({
    'wage': np.random.normal(10, 2, n),
    'experience': np.random.normal(5, 2, n),
    'education': np.random.normal(12, 3, n),
    'firm_id': np.random.choice(range(100), n),
    'year': np.random.choice(range(2010, 2020), n)
})

# Run regression with firm fixed effects
result = reghdfe(
    data=data,
    y='wage',
    x=['experience', 'education'],
    fe=['firm_id']
)

# Display results
print(result.summary())

📋 Key Features

  • High-dimensional fixed effects - Efficiently absorb multiple fixed effect dimensions
  • Cluster-robust standard errors - Support for one-way and multi-way clustering
  • Weighted regression - Handle sampling weights and frequency weights
  • Singleton dropping - Automatically handle singleton groups
  • Fast computation - Optimized algorithms for large datasets
  • Stata compatibility - Results match Stata's reghdfe command
  • Pandas integration - Seamless DataFrame compatibility
  • Flexible output - Rich statistical results and summary tables

Usage Examples

1. Multiple Fixed Effects

# Regression with firm and year fixed effects
result = reghdfe(
    data=data,
    y='wage',
    x=['experience', 'education'],
    fe=['firm_id', 'year']  # Multiple dimensions
)
print(result.summary())

2. Cluster-Robust Standard Errors

# One-way clustering
result = reghdfe(
    data=data,
    y='wage',
    x=['experience', 'education'],
    fe=['firm_id'],
    cluster=['firm_id']  # Cluster by firm
)

# Two-way clustering
result = reghdfe(
    data=data,
    y='wage',
    x=['experience', 'education'],
    fe=['firm_id'],
    cluster=['firm_id', 'year']  # Cluster by firm and year
)

3. Weighted Regression

# Add weights to your data
data['weight'] = np.random.uniform(0.5, 2.0, len(data))

# Run weighted regression
result = reghdfe(
    data=data,
    y='wage',
    x=['experience', 'education'],
    fe=['firm_id'],
    weights='weight'
)

4. OLS Regression (No Fixed Effects)

# Simple OLS regression
result = reghdfe(
    data=data,
    y='wage',
    x=['experience', 'education'],
    fe=None  # No fixed effects
)

Working with Results

Accessing Coefficients and Statistics

result = reghdfe(data=data, y='wage', x=['experience', 'education'], fe=['firm_id'])

# Get coefficients
coefficients = result.coef
print("Coefficients:", coefficients)

# Get standard errors
std_errors = result.se
print("Standard Errors:", std_errors)

# Get t-statistics and p-values
t_stats = result.tstat
p_values = result.pvalue
print("T-statistics:", t_stats)
print("P-values:", p_values)

# Get confidence intervals
conf_int = result.conf_int()
print("95% Confidence Intervals:", conf_int)

# Get R-squared
print(f"R-squared: {result.rsquared:.4f}")
print(f"Adjusted R-squared: {result.rsquared_adj:.4f}")

Summary Statistics

# Full regression summary
print(result.summary())

# Detailed summary with additional statistics
print(result.summary(show_dof=True))

Advanced Configuration

Custom Absorption Options

result = reghdfe(
    data=data,
    y='wage',
    x=['experience', 'education'],
    fe=['firm_id'],
    absorb_tolerance=1e-10,  # Higher precision for absorption
    drop_singletons=True,    # Drop singleton groups
    absorb_method='lsmr'     # Alternative solver
)

Different Covariance Types

# Robust standard errors (default)
result = reghdfe(
    data=data, 
    y='wage', 
    x=['experience'], 
    fe=['firm_id'], 
    cov_type='robust'
)

# Clustered standard errors
result = reghdfe(
    data=data, 
    y='wage', 
    x=['experience'], 
    fe=['firm_id'], 
    cov_type='cluster', 
    cluster=['firm_id']
)

Comparison with Stata

This package aims to replicate Stata's reghdfe command. Here's how the syntax translates:

Stata:

reghdfe wage experience education, absorb(firm_id year) cluster(firm_id)

Python (PyRegHDFE):

result = reghdfe(
    data=data,
    y='wage',
    x=['experience', 'education'],
    fe=['firm_id', 'year'],
    cluster=['firm_id']
)

Integration Options

This package is actively maintained as a standalone library. For users who prefer a unified ecosystem with additional econometric and statistical tools, reghdfe functionality is also available through:

  • StatsPAI - Comprehensive Stats + Econometrics + ML + AI + LLMs toolkit

Related Projects

  • PyStataR - Unified Stata-equivalent commands and R functions in Python

API Reference

Main Function: reghdfe()

reghdfe(data, y, x, fe=None, cluster=None, weights=None, 
        cov_type='robust', absorb_tolerance=1e-8, 
        drop_singletons=True, absorb_method='lsmr')

Parameters:

  • data (DataFrame): Input data
  • y (str): Dependent variable name
  • x (list): List of independent variable names
  • fe (list, optional): List of fixed effect variable names
  • cluster (list, optional): List of clustering variable names
  • weights (str, optional): Weight variable name
  • cov_type (str): Covariance type ('robust', 'cluster')
  • absorb_tolerance (float): Tolerance for fixed effect absorption
  • drop_singletons (bool): Whether to drop singleton groups
  • absorb_method (str): Absorption method ('lsmr', 'lsqr')

Returns:

  • RegressionResults: Object containing regression results

Results Object

The RegressionResults object provides:

  • .coef: Coefficients
  • .se: Standard errors
  • .tstat: T-statistics
  • .pvalue: P-values
  • .rsquared: R-squared
  • .rsquared_adj: Adjusted R-squared
  • .conf_int(): Confidence intervals
  • .summary(): Formatted summary table

Requirements

  • Python ≥ 3.9
  • NumPy ≥ 1.20.0
  • SciPy ≥ 1.7.0
  • Pandas ≥ 1.3.0
  • PyHDFE ≥ 0.1.0
  • Tabulate ≥ 0.8.0

Contributing

We welcome contributions! Please feel free to:

  • Report bugs or request features via GitHub Issues
  • Submit pull requests for improvements
  • Share your use cases and examples
  • Improve documentation and add examples

Development Setup

git clone https://github.com/brycewang-stanford/pyreghdfe.git
cd pyreghdfe
pip install -e ".[dev]"
pytest tests/

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

Support


This package is actively maintained. If you find it useful, please consider giving it a star on GitHub!

Questions, bug reports, or feature requests? Please open an issue on GitHub.

Project details


Download files

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

Source Distribution

pyreghdfe-0.3.0.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

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

pyreghdfe-0.3.0-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file pyreghdfe-0.3.0.tar.gz.

File metadata

  • Download URL: pyreghdfe-0.3.0.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for pyreghdfe-0.3.0.tar.gz
Algorithm Hash digest
SHA256 6600cb5685d512c266a92eb6d11f175a1f7ca6bd9416cf993cf3f40cf9199f4b
MD5 ca1cd2b05c164f499faa03f0c25ce229
BLAKE2b-256 e955633ea19883312d90de3c117484bd75ed5a259a7abf9255ba30523bb92553

See more details on using hashes here.

File details

Details for the file pyreghdfe-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pyreghdfe-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for pyreghdfe-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7eed45b7256bb09ee844a7035c90ea3578facd01d187a4a85faca2f0832cf16e
MD5 3a4dab0cb9c58ecfc78fa5c9fa39be6b
BLAKE2b-256 07f6fea73a28afa7b21ec035c72b6bfacb22744c72769a24563e5dd5786a8f93

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 Pingdom Monitoring Sentry Error logging StatusPage Status page