Learn R Programming

lfe

The goal of lfe is to speed up the estimation of linear models with large fixed effects. It includes support for instrumental variables, conditional F statistics for weak instruments, robust and multi-way clustered standard errors, as well as limited mobility bias correction. See Gaure (2013) doi:10.1016/j.csda.2013.03.024 and Gaure 2014 doi:10.1002/sta4.68.

Installation

You can install the development version of lfe like so:

remotes::install_github("MatthieuStigler/lfe")

Example

This is a basic example which shows you the speed improvement over base R for fixed effects estimation.

library(lfe) # fixed effects estimation
library(tradepolicy) # intl trade data
library(dplyr) # data cleaning/transforming

training_data <- agtpa_applications %>% 
  mutate(
    log_trade = log(trade),
    log_dist = log(dist),
    exp_year = paste(exporter, year, sep = "_"),
    imp_year = paste(importer, year, sep = "_")
  ) %>% 
  filter(trade > 0, exporter != importer, year %in% seq(1986, 2006, 4)) %>% 
  select(year, log_trade, log_dist, cntg, lang, clny, rta, exp_year, imp_year)

# note the difference with the | operator to indicate the FEs
# this is just an example, here I am not estimating a PPML model or anything
# in the state of the art
fml1 <- 0 + log_trade ~ 
  log_dist + cntg + lang + clny + rta + exp_year + imp_year # base

fml2 <- log_trade ~ 
  log_dist + cntg + lang + clny + rta | exp_year + imp_year # lfe

lm(fml1, data = training_data)

felm(fml2, data = training_data)

Testing

For a complete test with devtools::check(), you need to run sudo apt-get devtools or similar before. The package is written in C, in the future I shall try to rewrite it in C++ to ease long term maintenance.

The package also needs additional testing. At the present time,the tests it cover around 30% of the written lines.

Copy Link

Version

Install

install.packages('lfe')

Monthly Downloads

9,744

Version

2.9-0

License

Artistic-2.0

Issues

Pull Requests

Stars

Forks

Last Published

February 13th, 2023

Functions in lfe (2.9-0)

felm

Fit a linear model with multiple group fixed effects
diammatrix

Find diameters of mobility graphs
btrap

Bootstrap standard errors for the group fixed effects
condfstat

Compute conditional F statistic for weak instruments in an IV-estimation with multiple endogenous variables
chainsubset

Chain subset conditions
efactory

Create estimable function
bccorr

Compute limited mobility bias corrected correlation between fixed effects
cgsolve

Solve a symmetric linear system with the conjugate gradient method
compfactor

Find the connected components
demeanlist

Centre vectors on multiple groups
kaczmarz

Solve a linear system defined by factors
fixedse

Compute standard errors for fixed effects
fevcov

Compute limited mobility bias corrected covariance matrix between fixed effects
nlexpect

Compute expectation of a function of the coefficients.
mctrace

Compute trace of a large matrix by sample means
is.estimable

Verify estimability of function
sargan

Compute Sargan's S
makeDmatrix

Make sparse matrix of dummies from factor list
getfe

Retrieve the group fixed effects
lfe-package

Overview. Linear Group Fixed Effects
varvars

Compute the variance of the fixed effect variance estimate
summary.felm

Summarize felm model fits
waldtest

Compute Wald test for joint restrictions on coefficients