Skip to main content

RegHDFE

Note: This package continues to be maintained. Additionally, reghdfe functionality is also integrated into StatsPAI for users who prefer the unified ecosystem.


Python Version PyPI Version License: MIT Downloads

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

Installation

pip install reghdfe

📖 Quick Start

Basic Example

import pandas as pd
import numpy as np
from reghdfe 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())

Advanced 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. No Fixed Effects (OLS)

# 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
t_stats = result.tstat
print("T-statistics:", t_stats)

# Get p-values
p_values = result.pvalue
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
    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 (reghdfe):

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

📋 Key Features

  • ✅ High-dimensional fixed effects - Efficiently absorb multiple fixed effect dimensions
  • ✅ Cluster-robust standard errors - Support for one-way and two-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

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 - Stats + Econometrics + ML + AI + LLMs
  • StatsPAI - StatsPAI = Stats + Econometrics + ML + AI + LLMs
  • PyStataR - Unified Stata-equivalent commands and R functions

Documentation

For detailed API reference and additional examples, visit our GitHub repository.

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

📄 License

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


This package is actively maintained. For questions, bug reports, or feature requests, please open an issue on GitHub.

Metadata

Release files for reghdfe 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for reghdfe 0.1.1
File Size Uploaded
reghdfe-0.1.1.tar.gz 22.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for reghdfe 0.1.1
File Interpreter ABI Platform
reghdfe-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 42.6 kB

Release files / reghdfe-0.1.1.tar.gz

Download URL reghdfe-0.1.1.tar.gz
Size 22.9 kB
Tags Source
SHA-256 checksum
How to use checksums
73a0cb1b34e6313215e8183922e20c04779796191249316a5f5dece47a7bac4e
BLAKE2b-256 checksum
How to use checksums
766187a1d4ca25a9bb88188a6b57f75b7082f0aad8cdb7f2643534756bb03a19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / reghdfe-0.1.1-py3-none-any.whl

Download URL reghdfe-0.1.1-py3-none-any.whl
Size 19.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f8febc73df71bdf6ee2340b8e2cb7bac37cdf70f9f314dcdb6a443310165284
BLAKE2b-256 checksum
How to use checksums
35cdf119e96947bc37a7ab110e31e3a96b16429cda910d116cad04d2f749ab3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page