Debian Codegraph @

vignettes/panel-data-CRVE.Rmd

raw
1 ---
2 title: "Cluster-robust standard errors and hypothesis tests in panel data models"
3 author: "James E. Pustejovsky"
4 date: "`r Sys.Date()`"
5 output: rmarkdown::html_vignette
6 vignette: >
7 %\VignetteIndexEntry{Cluster-robust standard errors and hypothesis tests in panel data models}
8 %\VignetteEngine{knitr::rmarkdown}
9 %\VignetteEncoding{UTF-8}
10 ---
11
12 The importance of using cluster-robust variance estimators (i.e., "clustered standard errors") in panel models is now widely recognized.
13 Less widely recognized is the fact that standard methods for constructing hypothesis tests and confidence intervals based on CRVE can perform quite poorly in when based on a limited number of independent clusters.
14 Furthermore, it can be difficult to determine what counts as a large-enough sample to trust standard CRVE methods, because the finite-sample behavior of the variance estimators and test statistics depends on the configuration of the covariates, not just the total number of clusters.
15
16 One solution to this problem is to use bias-reduced linearization (BRL), which was proposed by Bell and McCaffrey (2002) and has recently begun to receive attention in the econometrics literature (e.g., Cameron & Miller, 2015; Imbens & Kolesar, 2015).
17 The idea of BRL is to correct the bias of standard CRVE based on a working model, and then to use a degrees-of-freedom correction for Wald tests based on the bias-reduced CRVE.
18 That may seem silly (after all, the whole point of CRVE is to avoid making distributional assumptions about the errors in your model), but it turns out that the correction can help quite a bit, even when the working model is wrong.
19 The degrees-of-freedom correction is based on a standard Satterthwaite-type approximation, and also relies on the working model.
20
21 A problem with Bell and McCaffrey's original formulation of BRL is that it does not work in some very common models for panel data, such as state-by-year panels that include fixed effects for each state and each year (Angrist and Pischke, 2009, point out this issue in their chapter on "non-standard standard error issues"; see also Young, 2016).
22 However, Pustejovsky and Tipton (2016) proposed a generalization of BRL that works even in models with arbitrary sets of fixed effects, and this generalization is implemented in `clubSandwich` as CRVE type `CR2`.
23 The package also implements small-sample corrections for multiple-constraint hypothesis tests based on an approximation proposed by Pustejovsky and Tipton (2016). For one-parameter constraints, the test reduces to a t-test with Satterthwaite degrees of freedom, and so it is a natural extension of BRL.
24
25 The following example demonstrates how to use `clubSandwich` to do cluster-robust inference for a state-by-year panel model with fixed effects in both dimensions, clustering by states.
26
27 ## Effects of changing the minimum legal drinking age
28
29 Carpenter and Dobkin (2011) analyzed the effects of changes in the minimum legal drinking age on rates of motor vehicle fatalities among 18-20 year olds, using state-level panel data from the National Highway Traffic Administration's Fatal Accident Reporting System.
30 In their new textbook, Angrist and Pischke (2014) developed a stylized example based on Carpenter and Dobkin's work.
31 The following example uses Angrist and Pischke's data and follows their analysis because their data are [easily available](https://www.masteringmetrics.com/resources/).
32
33 The outcome is the incidence of deaths in motor vehicle crashes among 18-20 year-olds (per 100,000 residents), for each state plus the District of Columbia, over the period 1970 to 1983.
34 There were several changes in the minimum legal drinking age during this time period, with variability in the timing of changes across states.
35 Angrist and Pischke (following Carpenter and Dobkin) use a difference-in-differences strategy to estimate the effects of lowering the minimum legal drinking age from 21 to 18.
36 Their specification is
37
38 $$y_{it} = \alpha_i + \beta_t + \gamma b_{it} + \delta d_{it} + \epsilon_{it},$$
39
40 for $i$ = 1,...,51 and $t$ = 1970,...,1983.
41 In this model, $\alpha_i$ is a state-specific fixed effect, $\beta_t$ is a year-specific fixed effect, $b_{it}$ is the current rate of beer taxation in state $i$ in year $t$, $d_{it}$ is the proportion of 18-20 year-olds in state $i$ in year $t$ who are legally allowed to drink, and $\delta$ captures the effect of shifting the minimum legal drinking age from 21 to 18.
42 Following Angrist and Pischke's analysis, we estimate this model both by (unweighted) OLS and by weighted least squares with weights corresponding to population size in a given state and year.
43 We also demonstrate random effects estimation and implement a cluster-robust Hausman specification test.
44
45 ## Unweighted OLS
46
47 The following code does some simple data-munging and the estimates the model by OLS:
48
49 ```{r, message = FALSE, warning = FALSE}
50 library(clubSandwich)
51 data(MortalityRates)
52
53 # subset for deaths in motor vehicle accidents, 1970-1983
54 MV_deaths <- subset(MortalityRates, cause=="Motor Vehicle" &
55 year <= 1983 & !is.na(beertaxa))
56
57 # fit by OLS
58 lm_unweighted <- lm(mrate ~ 0 + legal + beertaxa +
59 factor(state) + factor(year), data = MV_deaths)
60 ```
61
62 The `coef_test` function from `clubSandwich` can then be used to test the hypothesis that changing the minimum legal drinking age has no effect on motor vehicle deaths in this cohort (i.e., $H_0: \delta = 0$).
63 The usual way to test this is to cluster the standard errors by state, calculate the robust Wald statistic, and compare that to a standard normal reference distribution.
64 The code and results are as follows:
65
66 ```{r}
67 coef_test(lm_unweighted, vcov = "CR1",
68 cluster = MV_deaths$state, test = "naive-t")[1:2,]
69 ```
70
71 A better approach would be to use the generalized, bias-reduced linearization CRVE, together with Satterthwaite degrees of freedom.
72 In the `clubSandwich` package, the BRL adjustment is called "CR2" because it is directly analogous to the HC2 correction used in heteroskedasticity-robust variance estimation.
73 When applied to an OLS model estimated by `lm`, the default working model is an identity matrix, which amounts to the "working" assumption that the errors are all uncorrelated and homoskedastic.
74 Here's how to apply this approach in the example:
75
76 ```{r}
77 coef_test(lm_unweighted, vcov = "CR2",
78 cluster = MV_deaths$state, test = "Satterthwaite")[1:2,]
79 ```
80
81 The Satterthwaite degrees of freedom are different for each coefficient in the model, and so the `coef_test` function reports them right alongside the standard error.
82 For the effect of legal drinking age, the degrees of freedom are about half of what might be expected, given that there are 51 clusters.
83 The p-value for the CR2+Satterthwaite test is about twice as large as the p-value based on the standard Wald test, although the coefficient is still statistically significant at conventional levels.
84 Note, however, that the degrees of freedom on the beer taxation rate are considerably smaller because there are only a few states with substantial variability in taxation rates over time.
85
86 ```{r, echo = FALSE, results = "asis"}
87 plm_available <- requireNamespace("plm", quietly = TRUE)
88 if (!plm_available) cat("## Building the remainder of the vignette requires the plm package. Please install it. {-}")
89 ```
90
91 ## Unweighted "within" estimation
92
93 The `plm` package in R provides another way to estimate the same model.
94 It is convenient because it absorbs the state and year fixed effects before estimating the effect of `legal`.
95 The `clubSandwich` package works with fitted `plm` models too:
96
97 ```{r, message = FALSE, eval = plm_available}
98 library(plm)
99 plm_unweighted <- plm(mrate ~ legal + beertaxa, data = MV_deaths,
100 effect = "twoways", index = c("state","year"))
101 coef_test(plm_unweighted, vcov = "CR1", cluster = "individual", test = "naive-t")
102 coef_test(plm_unweighted, vcov = "CR2", cluster = "individual", test = "Satterthwaite")
103 ```
104
105 ## Population-weighted estimation
106
107 The difference between the standard method and the new method are not terribly exciting in the above example.
108 However, things change quite a bit if the model is estimated using population weights.
109 We go back to fitting in `lm` with dummies for all the fixed effects because `plm` does not handle weighted least squares.
110
111 ```{r}
112 lm_weighted <- lm(mrate ~ 0 + legal + beertaxa + factor(state) + factor(year),
113 weights = pop, data = MV_deaths)
114 coef_test(lm_weighted, vcov = "CR1",
115 cluster = MV_deaths$state, test = "naive-t")[1:2,]
116 coef_test(lm_weighted, vcov = "CR2",
117 cluster = MV_deaths$state, test = "Satterthwaite")[1:2,]
118 ```
119
120 Using population weights slightly reduces the point estimate of the effect, while also slightly increasing its precision.
121 If you were following the standard approach, you would probably be happy with the weighted estimates and wouldn't think about it any further.
122 However, using the CR2 variance estimator and Satterthwaite correction produces a p-value that is an order of magnitude larger (though still significant at the conventional 5% level).
123 The degrees of freedom are just `r round(coef_test(lm_weighted, vcov = "CR2", cluster = MV_deaths$state, test = "Satterthwaite")["legal","df_Satt"], 1)`---drastically smaller than would be expected based on the number of clusters.
124
125 Even with weights, the `coef_test` function uses an "independent, homoskedastic" working model as a default for `lm` objects.
126 In the present example, the outcome is a standardized rate and so a better assumption might be that the error variances are inversely proportional to population size.
127 The following code uses this alternate working model:
128
129 ```{r}
130 coef_test(lm_weighted, vcov = "CR2",
131 cluster = MV_deaths$state, target = 1 / MV_deaths$pop,
132 test = "Satterthwaite")[1:2,]
133 ```
134
135 The new working model leads to slightly smaller standard errors and a couple of additional degrees of freedom, though they remain in small-sample territory.
136
137 ## Random effects estimation
138
139 If the unobserved effects $\alpha_1,...,\alpha_{51}$ are uncorrelated with the regressors, then a more efficient way to estimate $\gamma,\delta$ is by weighted least squares, with weights based on a random effects model. We still treat the year effects as fixed.
140
141 ```{r, eval = plm_available}
142 plm_random <- plm(mrate ~ 0 + legal + beertaxa + year, data = MV_deaths,
143 effect = "individual", index = c("state","year"),
144 model = "random")
145 coef_test(plm_random, vcov = "CR1", test = "naive-t")[1:2,]
146 coef_test(plm_random, vcov = "CR2", test = "Satterthwaite")[1:2,]
147 ```
148
149 With random effects estimation, the effect of legal drinking age is smaller by about 1 death per 100,000. As a procedural aside, note that `coef_test` infers that `state` is the clustering variable because the call to plm includes only one type of effects (random state effects).
150
151 ## Robust Hausman test
152
153 CRVE is also used in specification tests, as in the artificial Hausman-type test for endogeneity of unobserved effects (Arellano, 1993).
154 As noted above, random effects estimation is more efficient than fixed effects estimation, but requires the assumption that the unobserved effects are uncorrelated with the regressors.
155 However, if the unobserved effects covary with $\mathbf{b}_i, \mathbf{d}_i$, then the random-effects estimator will be biased.
156
157 We can test for whether endogeneity is a problem by including group-centered covariates as additional regressors. Let $\tilde{d}_{it} = d_{it} - \frac{1}{T}\sum_t d_{it}$, with $\tilde{b}_{it}$ defined analogously. Now estimate the regression
158
159 $$y_{it} = \beta_t + \gamma_1 b_{it} + \gamma_2 \tilde{b}_{it} + \delta_1 d_{it} + \delta_2 \tilde{d}_{it} + \epsilon_{it},$$
160
161 which does not include state fixed effects. The parameters $\gamma_2,\delta_2$ represent the differences between the within-groups and between-groups estimands of $\gamma_1, \delta_1$. If these are both zero, then the random effects estimator is unbiased. Thus, the joint test for $H_0: \gamma_2 = \delta_2 = 0$ amounts to a test for exogeneity of the unobserved effects.
162
163 For efficiency, we estimate this specification using weighted least squares (although OLS would be valid too):
164
165 ```{r, eval = plm_available}
166 MV_deaths <- within(MV_deaths, {
167 legal_cent <- legal - tapply(legal, state, mean)[factor(state)]
168 beer_cent <- beertaxa - tapply(beertaxa, state, mean)[factor(state)]
169 })
170
171 plm_Hausman <- plm(mrate ~ 0 + legal + beertaxa + legal_cent + beer_cent + factor(year),
172 data = MV_deaths,
173 effect = "individual", index = c("state","year"),
174 model = "random")
175 coef_test(plm_Hausman, vcov = "CR2", test = "Satterthwaite")[1:4,]
176 ```
177
178 To conduct a joint test on the centered covariates, we can use the `Wald_test` function. The usual way to test this hypothesis would be to use the `CR1` variance estimator to calculate the robust Wald statistic, then use a $\chi^2_2$ reference distribution (or equivalently, compare a re-scaled Wald statistic to an $F(2,\infty)$ distribution). The `Wald_test` function reports the latter version:
179
180 ```{r, eval = plm_available}
181 Wald_test(plm_Hausman,
182 constraints = constrain_zero(c("legal_cent","beer_cent")),
183 vcov = "CR1", test = "chi-sq")
184 ```
185
186 The test is just shy of significance at the 5% level. If we instead use the `CR2` variance estimator and our newly proposed approximate F-test (which is the default in `Wald_test`), then we get:
187
188 ```{r, eval = plm_available}
189 Wald_test(plm_Hausman,
190 constraints = constrain_zero(c("legal_cent","beer_cent")),
191 vcov = "CR2")
192 ```
193
194 The low degrees of freedom of the test indicate that we're definitely in small-sample territory and should not trust the asymptotic $\chi^2$ approximation.
195
196
197 ## References
198
199 Angrist, J. D., & Pischke, J. S. (2009). _Mostly Harmless Econometrics: An Empiricist’s Companion_. Princeton, NJ: Princeton University Press.
200
201 Angrist, J. D., & Pischke, J. S. (2014). _Mastering 'Metrics: The Path from Cause to Effect_. Princeton, NJ: Princeton University Press.
202
203 Arellano, M. (1993). On the testing of correlated effects with panel data. Journal of Econometrics, 59(1-2), 87-97. doi: [10.1016/0304-4076(93)90040-C](https://doi.org/10.1016/0304-4076(93)90040-C)
204
205 Bell, R. M., & McCaffrey, D. F. (2002). Bias reduction in standard errors for linear regression with multi-stage samples. _Survey Methodology, 28_(2), 169-181.
206
207 Cameron, A. C., & Miller, D. L. (2015). A practitioner’s guide to cluster-robust inference. _Journal of Human Resources, 50_(2), 317-372. doi: [10.3368/jhr.50.2.317](https://doi.org/10.3368/jhr.50.2.317)
208
209 Carpenter, C., & Dobkin, C. (2011). The minimum legal drinking age and public health. _Journal of Economic Perspectives, 25_(2), 133-156. doi: [10.1257/jep.25.2.133](https://doi.org/10.1257/jep.25.2.133)
210
211 Imbens, G. W., & Kolesar, M. (2015). Robust standard errors in small samples: Some practical advice. doi: [10.1162/REST_a_00552](https://doi.org/10.1162/REST_a_00552)
212
213 Pustejovsky, J. E. & Tipton, E. (2016). Small sample methods for cluster-robust variance estimation and hypothesis testing in fixed effects models. arXiv: [1601.01981](https://arxiv.org/abs/1601.01981) [stat.ME]
214
215 Young, A. (2016). Improved, nearly exact, statistical inference with robust and clustered covariance matrices using effective degrees of freedom corrections.