# How to use alpaca

## Generalized linear models with fixed effects

Our package offers a fast algorithm to estimate generalized linear models with high-dimensional fixed effects. The linear predictor of such models takes the following form: $\boldsymbol{\eta} = \mathbf{Z} \boldsymbol{\gamma} = \mathbf{D} \boldsymbol{\alpha} + \mathbf{X} \boldsymbol{\beta} = \sum_{j=1}^{k} \mathbf{D}_j \boldsymbol{\alpha}_{j} + \mathbf{X} \boldsymbol{\beta} \, ,$ where the matrix $$\mathbf{D}$$ arises from dummy encoding of $$k$$ high-dimensional categorical variables and $$\mathbf{X}$$ contains the variables of interest. We refer to $$\boldsymbol{\beta}$$ and $$\boldsymbol{\alpha}$$ as structural parameters and fixed effects. The latter are essentially nuisance parameters that are used to control for unobserved heterogeneity.

Brute force estimation of this kind of models is often restricted to computational limitations (either due to memory or time limitations). We tackle this problem by providing a fast and memory efficient algorithm suggested by Stammann (2018) based on the combination of the Frisch-Waugh-Lovell theorem and the method of alternating projections. We restrict ourselves to non-linear models because Gaure (2013) already offers a great package for linear models. Further, in the case of binary choice models with only one high-dimensional fixed effects we recommend using the package bife.

## Estimating a binary-choice model with individual and time fixed effects

In the following we utilize an example from labor economics to demonstrate the capabilities of feglm(). More precisely, we use a balanced micro panel data set from the Panel Study of Income Dynamics to analyze the intertemporal labor force participation of 1,461 married women observed for nine years. A similar empirical illustration is used in Fernández-Val (2009).

Before we start, we briefly inspect the data set to get an idea about its structure and potential covariates.

data(psid, package = "bife")
head(psid)
##    ID LFP KID1 KID2 KID3     INCH AGE TIME
## 1:  1   1    1    1    1 58807.81  26    1
## 2:  1   1    1    0    2 41741.87  27    2
## 3:  1   1    0    1    2 51320.73  28    3
## 4:  1   1    0    1    2 48958.58  29    4
## 5:  1   1    0    1    2 53634.62  30    5
## 6:  1   1    0    0    3 50983.13  31    6

ID and TIME are individual and time-specific identifiers, LFP is an indicator equal to one if a woman is in labor force, KID1 - KID3 are the number of children in a certain age group, INCH is the annual income of the husband, and AGE is the age of a woman.

First, we use a specification similar to Fernández-Val (2009) and estimate a static model of women’s labor supply where we control for individual and time-specific unobserved heterogeneity (so called individual and time fixed effects).

library(alpaca)
stat <- feglm(LFP ~ KID1 + KID2 + KID3 + log(INCH) | ID + TIME, psid, binomial("probit"))
summary(stat)
## binomial - probit link
##
## LFP ~ KID1 + KID2 + KID3 + log(INCH) | ID + TIME
##
## Estimates:
##            Estimate Std. error z value Pr(> |z|)
## KID1      -0.676905   0.056301 -12.023   < 2e-16 ***
## KID2      -0.344383   0.049896  -6.902  5.13e-12 ***
## KID3      -0.007039   0.035344  -0.199     0.842
## log(INCH) -0.234136   0.054403  -4.304  1.68e-05 ***
## ---
## Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
##
## residual deviance= 6069.65,
## null deviance= 8152.05,
## n= 5976, l= [664, 9]
##
## ( 7173 observation(s) deleted due to perfect classification )
##
## Number of Fisher Scoring Iterations: 6

As glm(), the summary statistic of the model provides detailed information about the coefficients and some information about the model fit (residual deviance and null deviance). Furthermore, we report statistics that are specific to fixed effects models. More precisely, we learn that only 5,976 observations out of 13,149 contribute to the idenfication of the structural parameters. This is indicated by the message that 7,173 observations are deleted due to perfect classification. With respect to binary choice models those are observations that are related to women who never change their labor force participation status during the nine years observed. Thus those women were either always employed or unemployed.1 Overall the estimation results are based on 664 women observed for nine years.

Because coefficients itself are not very meaningful, econometricians are usually interested in so called partial effects (also known as marginal or ceteris paribus effects). A commonly used statistic is the average partial effect. alpaca offers a post-estimation routine to estimate average partial effects and their corresponding standard errors.2

apes.stat <- getAPEs(stat)
summary(apes.stat)
## Estimates:
##             Estimate Std. error z value Pr(> |z|)
## KID1      -0.0880151  0.0179164  -4.913  8.99e-07 ***
## KID2      -0.0447764  0.0106707  -4.196  2.71e-05 ***
## KID3      -0.0009169  0.0050091  -0.183   0.85476
## log(INCH) -0.0304425  0.0095164  -3.199   0.00138 **
## ---
## Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

A widespread reason that prevents the use of non-linear fixed effects models in practice is the so-called incidental parameter bias problem (IPP) first mentioned by Neyman and Scott (1948). Fortunately, for classical panel data sets, like in this example, there already exist several asymptotic bias corrections tackling the IPP (see Fernández-Val and Weidner (2018) for an overview).3 Our package provides a post-estimation routine that applies the analytical bias correction derived by Fernández-Val and Weidner (2016).4 Technical details on how the bias correction is accelerated using the method of alternating projections can be found in Czarnowske and Stammann (2020).5

stat.bc <- biasCorr(stat)
summary(stat.bc)
## binomial - probit link
##
## LFP ~ KID1 + KID2 + KID3 + log(INCH) | ID + TIME
##
## Estimates:
##            Estimate Std. error z value Pr(> |z|)
## KID1      -0.596290   0.056301 -10.591   < 2e-16 ***
## KID2      -0.303340   0.049896  -6.079  1.21e-09 ***
## KID3      -0.006124   0.035344  -0.173  0.862431
## log(INCH) -0.207057   0.054403  -3.806  0.000141 ***
## ---
## Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
##
## residual deviance= 6069.65,
## null deviance= 8152.05,
## n= 5976, l= [664, 9]
##
## ( 7173 observation(s) deleted due to perfect classification )
##
## Number of Fisher Scoring Iterations: 6 
apes.stat.bc <- getAPEs(stat.bc)
summary(apes.stat.bc)
## Estimates:
##            Estimate Std. error z value Pr(> |z|)
## KID1      -0.086459   0.016344  -5.290  1.22e-07 ***
## KID2      -0.043983   0.010017  -4.391  1.13e-05 ***
## KID3      -0.000888   0.005044  -0.176   0.86026
## log(INCH) -0.030022   0.009222  -3.255   0.00113 **
## ---
## Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Whereas analytical bias corrections for static models get more and more attention in applied work, it is not well known that they can also be used for dynamic models with fixed effects.

Before we can adjust our static to a dynamic specification, we first have to generate a lagged dependent variable.

library(data.table)
setDT(psid)
psid[, LLFP := shift(LFP), by = ID]

Contrary to the bias correction for the static models, we need to additionally provide a bandwidth parameter (L) that is required for the estimation of spectral densities (see Hahn and Kuersteiner (2011)). Fernández-Val and Weidner (2016) suggest to do a sensitivity analysis and try different values for L but not larger than four. Note that in this case the order of factors to be concentrated out, specified in the second part of the formula, is important (cross-sectional identifier first and time identifier second).

dyn <- feglm(LFP ~ LLFP + KID1 + KID2 + KID3 + log(INCH) | ID + TIME, psid, binomial("probit"))
dyn.bc <- biasCorr(dyn, L = 1L)
summary(dyn.bc)
## binomial - probit link
##
## LFP ~ LLFP + KID1 + KID2 + KID3 + log(INCH) | ID + TIME
##
## Estimates:
##           Estimate Std. error z value Pr(> |z|)
## LLFP       1.01608    0.04695  21.643   < 2e-16 ***
## KID1      -0.45389    0.06787  -6.687  2.27e-11 ***
## KID2      -0.15737    0.06031  -2.610   0.00907 **
## KID3       0.01562    0.04331   0.361   0.71839
## log(INCH) -0.18834    0.06165  -3.055   0.00225 **
## ---
## Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
##
## residual deviance= 4777.58,
## null deviance= 6549.14,
## n= 4792, l= [599, 8]
##
## ( 1461 observation(s) deleted due to missingness )
## ( 6896 observation(s) deleted due to perfect classification )
##
## Number of Fisher Scoring Iterations: 6
apes.dyn.bc <- getAPEs(dyn.bc)
summary(apes.dyn.bc)
## Estimates:
##            Estimate Std. error z value Pr(> |z|)
## LLFP       0.156044   0.029129   5.357  8.46e-08 ***
## KID1      -0.059108   0.013078  -4.520  6.19e-06 ***
## KID2      -0.020493   0.007684  -2.667   0.00765 **
## KID3       0.002033   0.004875   0.417   0.67663
## log(INCH) -0.024526   0.008165  -3.004   0.00267 **
## ---
## Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

## Further Features

alpaca is also compatible with linearHypothesis() of the car package and offers the possibility to compute robust and multi-way clustered standard errors. Further it supports the two- and three-way bias corrections suggested by Hinz, Stammann, and Wanner (2020). For examples, see vignette “Estimating the intensive and extensive margin of trade”.

## References

Cruz-Gonzalez, Mario, Iván Fernández-Val, and Martin Weidner. 2017. “Bias Corrections for Probit and Logit Models with Two-Way Fixed Effects.” The Stata Journal 17 (3): 517–45.

Czarnowske, Daniel, and Amrei Stammann. 2020. “Fixed Effects Binary Choice Models: Estimation and Inference with Long Panels.” ArXiv E-Prints.

Fernández-Val, Iván. 2009. “Fixed Effects Estimation of Structural Parameters and Marginal Effects in Panel Probit Models.” Journal of Econometrics 150 (1): 71–85.

Fernández-Val, Iván, and Martin Weidner. 2016. “Individual and Time Effects in Nonlinear Panel Models with Large N, T.” Journal of Econometrics 192 (1): 291–312.

———. 2018. “Fixed Effects Estimation of Large-T Panel Data Models.” Annual Review of Economics 10 (1): 109–38.

Gaure, Simen. 2013. “lfe: Linear Group Fixed Effects.” The R Journal 5 (2): 104–17.

Hahn, Jinyong, and Guido Kuersteiner. 2011. “Bias Reduction for Dynamic Nonlinear Panel Models with Fixed Effects.” Econometric Theory 27 (6): 1152–91.

Hinz, Julian, Amrei Stammann, and Joschka Wanner. 2020. “State Dependence and Unobserved Heterogeneity in the Extensive Margin of Trade.” ArXiv E-Prints.

Neyman, Jerzy, and Elizabeth L. Scott. 1948. “Consistent Estimates Based on Partially Consistent Observations.” Econometrica 16 (1): 1–32.

Stammann, Amrei. 2018. “Fast and Feasible Estimation of Generalized Linear Models with High-Dimensional K-Way Fixed Effects.” ArXiv E-Prints.

1. Note that in this specification (with individual and time fixed effects) also observations related to a specific time period where all women are either in labor force or not can be dropped. However this is very unlikely in practice.↩︎

2. The routine is currently restricted to probit and logit models but will be extended in the future.↩︎

3. Cruz-Gonzalez, Fernández-Val, and Weidner (2017) apply the same bias correction to a pseudo panel of bilateral trade flows.↩︎

4. See footnote 2.↩︎

5. In our article, we also apply several asymptotic bias corrections to a very similar empirical example where we use German micro data from the German Socio-Economic Panel.↩︎