Python implementation of the Interactive Fixed Effects Estimator for panel data presented in Bai (2009).
pip install pyInteractiveFixedEffects
First, we need to import the module into our script.
import bai2009
Then we need the initiate the estimator by specifying the number of factors in the model.
# Load the Interactive Fixed Effects estimator with r=3 factors
ife = bai2009.InteractiveFixedEffects(3)
Finally, there are two ways to estimate a model.
The easiest way to get an estimate is using a Patsy formula to specify the model. The regression is specified as normal and we add an additional term ~ife(I,T)
at the end to specify the columns of the data with the
# Estimate the model using a Patsy formula
betas, F, Lambda = ife.fit_from_formula('Y~0 X1 X2~ife(I,T)', df)
If you prefer to specify each term explicitly in your code, you can use the code below.
# Alternatively, estimate the model specifying every term explicitly
betas, F, Lambda = ife.fit(
df['Y'].values[:,np.newaxis], # Outcome
df[['X1', 'X2']].values, # Observable regressors
df['I'].values[:,np.newaxis], # First level of the factor model (I)
df['T'].values[:,np.newaxis] # Second level of the factor model (T)
)
The estimator returns:
-
$\beta$ a$p\times 1$ vector of coefficients associated with the observables. -
$F$ a$T\times r$ matrix of the factors on the$T$ dimension. -
$\Lambda$ a$N\times r$ matrix of the loadings on the$N$ dimension.