Skip to main content

Gravity Portal: FastHDFE

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 four million fixed effects)
  • Many useful input and output options
  • Output reproduces similar Stata packages

 

Commands reghdfe and ppmlhdfe.

The commands use the method of alternating projections and, for Poisson Pseudo-Maximum Likelihood (PPML), iteratively reweighted least squares, and provide homoskedastic, heteroskedasticity-robust, and multi-way cluster-robust standard errors, detection and removal of separated observations via the iterative rectifier, and singleton handling. The commands closely replicate similar Stata packages, reproducing their point estimates to at least six decimal places and the standard errors to at least five decimal places. 

Learn more

Technical documentation for the package

Getting started

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 pandas as pd
import ppmlhdfe as ph

df = pd.read_csv('https://www.usitc.gov/data/gravity/example_trade_and_grav_data_small.csv')
result = ph.ppmlhdfe(y=['trade'],
                     x=['LN_DIST','agree_pta','contiguity','common_language'],
                     fixedeffects=['exp_ind#industry_id#year','imp_ind#industry_id#year','exp_ind#imp_ind#industry_id'],
                     data=df,
                     eps_beta=1e-5,
                     setype='cluster',
                     clustvars=['exp_ind', 'imp_ind','year','industry_id'])