*! version 1.0 6 Sep 2020

Title

twfem -- Algorithm to efficiently estimate a two-way fixed effects model based on Somaini and Wolak (2015).

Syntax

twfem command varlist
[if]
[in]
[using]
[,
options]

Command is an estimation command, e.g., regress, ivregress, sureg. The varlist contains the dependent variable followed by the independent variables. The syntaxis of the varlist replicates the syntaxis of the original estimation command.

using followed by a path will save a set of matrices in that path. If using option is omitted, the matrices will be stored in eresults

Required in the first regression
absorb(absvars weight) two categorical variables that indentify the fixed effects to be absorbed. Analytic weights can be added as an optional third argument

generate(newvars) creates new group identifiers

newvars(name) creates residualized variables with
the prefix in "newvars"

or

replace replace the variables for their residualized
version

Running different specifications with residualized variables
noproj Run the regression without creating any arrays or projecting new variables. It assumes that all the variables have been residualized.

SE/Robust
+ vce(vcetype) vcetype
may be unadjusted (default), robust or cluster

Description

twfem is 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 GMM estimators.

Options

Full Description

using adding using followed by a path will save a set of matrices in that path. Otherwise, the matrices will be saved in eresults. STATA may fail if it tries to store a matrix that exceeds matsize in ereturn. Adding using avoids that limitation.

absorb(absvars weight) the two levels of fixed effects are specified in this option, it takes at least two variables with group identifiers. The optional third input can be a variable of analytic weights.

generate(newvars) if the group identifiers are not consecutive after dropping redundants and missing observations, then you have this option to create new group identifiers. This option will create new variables in the database.

newvars(name) creates new variables, the residualized variables will be stored as new variables that will be named with a prefix specified in `newvars'. This option will create new variables in the database. See option replace for replacing existing variables.

replace replace the variables for their residualized version. See option newvars for creating new variables without changing existent ones.

noproj run the regression without creating any arrays or projecting new variables. This option will take the matrices created and stored in eresults or in the path selected by using option.

Examples

twfem reg y x1 x2 x3 x4, absorb(hhid tid w) newv(w_) vce(robust)

twfem reg w_y w_x1, noproj vce(cluster hhid)

twfem ivregress 2sls w_y w_x1 (w_x2= w_x3), noproj vce(robust)

Shortcomings

1. Factor-variable and time-series operators not allowed.

2. The program may throw an error if the dataset contains a variable with the name of an estimation command such as reg (commonly used for variables of region). If the dataset contains a variable called reg, then do not abbreviate the estimation command.

Common Errors

1. The algorithm will not work if the fixed effects after the cleaning of the redundants and missing observations are not consecutives and the generate option is not used.

2. The algorithm will not work if the user tries to create with "generate" or with "newvars" new variables with a name that already exits.

3. The vce option is not allowed when the estimation command is "sureg".

Stored results

Note: All the results of the original command selected and the matrices and scalars needed for the algorithm will be stored in the eresults. Use ereturn to list them.

Scalars
e(dof_adj) Degree of freedom adjustment

e(dimN) number of first fixed effect without the redundants observations

e(dimT) number of second fixed effect without the redundants observations

e(nested_adj) Adjustment in the degree of freedom if one of the fixed effect or both of them are nested within the cluster var

e(rank_adj) Adjustment based on the rank of e(A) if e(dimN)<e(dimT), otherwise is based on the rank of e(C)

Macros
e(absorbed) name of the fixed effects absorbed and the weight variable

Matrices
e(b) coefficient vector

e(V) variance-covariance matrix with the degree of freedom correction

If "using" is omitted

e(invDD) is a vector of (dimN)x1 taking the diagonal elements of the inverse of the matrix D'D

e(invHH) is a vector of (dimT-1)x1 taking the diagonal elements of the inverse of the matrix H'H

e(B) Matrix B is needed for the first and second step of the algorithm

If e(dimN)<e(dimT)

e(A) Matrix A is needed for the first and second step of the algorithm

e(CinvHHDH) Matrix CinvHHDH is needed for the first and second step of the algorithm

If e(dimN)>=e(dimT)

e(C) Matrix C is needed for the first and second step of the algorithm

e(AinvDDDH) Matrix AinvDDDH is needed for the first step of the algorithm

Functions
e(sample) marks estimation sample

More information

For more information of twfem Github

Also see

For advanced options:
twset
twres
twest
twsave
twload

References

Somaini, P. and F.A. Wolak, (2015), An Algorithm to Estimate the Two-Way Fixed Effects Model, Journal of Econometric Methods, 5, issue 1, p. 143-152.

Aditional References

Arellano, M. (1987), Computing Robust Standard Errors for Within-Groups Estimators, Oxford Bulletin of Economics and Statistics, 49, issue 4, p. 431–434.

Cameron, A. C., & Miller, D. L. (2015). A practitioner’s guide to cluster-robust inference. Journal of human resources, 50(2), 317-372.