Skip to main content

Gravity Portal: FastHDFE

is a Python package for estimating linear and multiplicative models with high-dimensional fixed effects.

Features:

  • Fast estimation: FastHDFE commands run about six to forty-four times faster than Stata with big data such as ITPD-E (~83 million observations and over 4 million fixed effects)
  • Many useful input and output options
  • Output reproduces similar Stata packages
  • Command reghdfe estimates linear models
  • Command ppmlhdfe estimates multiplicative Poisson Pseudo-Maximum Likelihood (PPML) models
  • The commands use the method of alternating projections and, for PPML, iteratively reweighted least squares
  • The commands provide homoskedastic, heteroskedasticity-robust, and multi-way cluster-robust standard errors
  • The commands provide detection and removal of separated observations via the iterative rectifier, and singleton handling 

Learn more

Technical documentation for the package [PDF, 70 pages]

Installation

The package requires NumPy, SciPy, pandas, and tabulate packages. They will be automatically installed during FastHDFE installation unless already present.

pip install fasthdfe

Example using a small dataset

import fasthdfe as fh
import pandas as pd

# Load small testing dataset
df = pd.read_csv("https://www.usitc.gov/data/gravity/example_trade_and_grav_data_small.csv")

# Estimate with the PPML model, assuming robust standard errors
results = fh.ppmlhdfe(y=['trade_value'],
                      x=['log_distance','agree_pta','common_language','contiguity'],
                      fixedeffects=['importer#year', 'exporter#year', 'exporter#importer'],
                      data=df,
                      setype='r')

# Estimate with the PPML model and cluster standard errors by importer, exporter, and year
results = fh.ppmlhdfe(y=['trade_value'],
                      x=['log_distance','agree_pta','common_language','contiguity'],
                      fixedeffects=['importer#year', 'exporter#year', 'exporter#importer'],
                      data=df,
                      setype='cluster',
                      clustvars=['importer', 'exporter', 'year'])