Skip to content
Licensed Unlicensed Requires Authentication Published by De Gruyter March 12, 2015

An Algorithm to Estimate the Two-Way Fixed Effects Model

Paulo Somaini and Frank A. Wolak


We present an algorithm to estimate the two-way fixed effect linear model. The algorithm relies on the Frisch-Waugh-Lovell theorem and applies to ordinary least squares (OLS), two-stage least squares (TSLS) and generalized method of moments (GMM) estimators. The coefficients of interest are computed using the residuals from the projection of all variables on the two sets of fixed effects. Our algorithm has three desirable features. First, it manages memory and computational resources efficiently which speeds up the computation of the estimates. Second, it allows the researcher to estimate multiple specifications using the same set of fixed effects at a very low computational cost. Third, the asymptotic variance of the parameters of interest can be consistently estimated using standard routines on the residualized data.

Corresponding author: Paulo Somaini, Department of Economics, MIT, Cambridge, MA, USA, e-mail:


Abowd, J. M., F. Kramarz, and D. N. Margolis (1999): “High Wage Workers and High Wage Firms,” Econometrica 67 (2): 251–333.10.1111/1468-0262.00020Search in Google Scholar

Arellano, M. 1987. “Computing Robust Standard Errors for Within-Groups Estimators.” Oxford Bulletin of Economics and Statistics 49 (4): 431–434.10.1111/j.1468-0084.1987.mp49004006.xSearch in Google Scholar

Baum, C. F., M. E. Schaffer, and S. Stillman. 2007. “Enhanced Routines for Instrumental Variables/GMM Estimation and Testing.” Stata Journal 7 (4): 465–506.10.1177/1536867X0800700402Search in Google Scholar

Carneiro, A., P. Guimares, and P. Portugal (2012): “Real Wages and the Business Cycle: Accounting for Worker, Firm, and Job Title Heterogeneity,” American Economic Journal: Macroeconomics 4(2): 133–152.10.1257/mac.4.2.133Search in Google Scholar

Cornelissen, T. 2008. “The Stata Command Felsdvreg to Fit a Linear Model with Two High-Dimensional Fixed Effects.” Stata Journal 8 (2): 170–189.10.1177/1536867X0800800202Search in Google Scholar

Correia, S. 2014. “REGHDFE: Stata Module to Perform Linear or Instrumental-Variable Regression Absorbing Any Number of High-Dimensional Fixed Effects,” Statistical Software Components S457874, Boston College Department of Economics.Search in Google Scholar

Davis, P. 2002. “Estimating Multi-Way Error Components Models with Unbalanced Data Structures.” Journal of Econometrics 106 (1): 67–95.10.1016/S0304-4076(01)00087-2Search in Google Scholar

Frisch, R., and F. V. Waugh. 1933. “Partial Time Regressions as Compared with Individual Trends.” Econometrica 1 (4): 387–401.10.2307/1907330Search in Google Scholar

Gaure, S. 2013a. “Lfe: Fitting Linear Models with Multiple Factors with Many Levels.” The R Journal 5(2): 104–116.10.32614/RJ-2013-031Search in Google Scholar

Gaure, S. 2013b. “OLS with Multiple High Dimensional Category Variables.” Computational Statistics & Data Analysis 66: 8–18.10.1016/j.csda.2013.03.024Search in Google Scholar

Giles, D. E. A. 1984. “Instrumental Variables Regressions Involving Seasonal Data.” Economics Letters 14 (4): 339–343.10.1016/0165-1765(84)90008-9Search in Google Scholar

Guimaraes, P. 2009. “REG2HDFE: Stata Module to Estimate a Linear Regression Model with two High Dimensional Fixed Effects,” Statistical Software Components S457101, Boston College Department of Economics.Search in Google Scholar

Guimaraes, P., and P. Portugal. 2010. “A Simple Feasible Procedure to Fit Models with High-Dimensional Fixed Effects.” Stata Journal 10 (4): 628–649.10.1177/1536867X1101000406Search in Google Scholar

Hayashi, F. 2000. Econometrics. Princeton: Princeton University Press.Search in Google Scholar

Lovell, M. C. 1963. “Seasonal Adjustment of Economic Time Series and Multiple Regression Analysis.” Journal of the American Statistical Association 58 (304): 993–1010.10.1080/01621459.1963.10480682Search in Google Scholar

Lovell, M. C. 2008. “A Simple Proof of the FWL Theorem.” The Journal of Economic Education 39 (1): 88–91.10.3200/JECE.39.1.88-91Search in Google Scholar

Ouazad, A. 2008. “A2REG: Stata Module to Estimate Models with Two Fixed Effects,” Statistical Software Components S456942, Boston College, Department of Economics.Search in Google Scholar

Stock, J. H., and M. W. Watson. 2008. “Heteroskedasticity-Robust Standard Errors for Fixed Effects Panel Data Regression.” Econometrica 76 (1): 155–174.10.1111/j.0012-9682.2008.00821.xSearch in Google Scholar

Wansbeek, T., and A. Kapteyn. 1989. “Estimation of the Error-Components Model with Incomplete Panels.” Journal of Econometrics 41 (3): 341–361.10.1016/0304-4076(89)90066-3Search in Google Scholar

Wooldridge, J. M. 2010. Econometric Analysis of Cross Section and Panel Data. 2nd ed. Cambridge, Mass: The MIT Press.Search in Google Scholar

Supplemental Material

The online version of this article (DOI: 10.1515/jem-2014-0008) offers supplementary material, available to authorized users.

Published Online: 2015-3-12
Published in Print: 2016-1-1

©2016 by De Gruyter