Set of functions to compute the VCOVs robust to different forms correlation in panel or time series settings.
vcov_DK(x, time = NULL, lag = NULL, ssc = NULL)
vcov_NW(x, unit = NULL, time = NULL, lag = NULL, ssc = NULL)
NW(lag = NULL)
newey_west(lag = NULL)
DK(lag = NULL)
driscoll_kraay(lag = NULL)
A fixest
object.
A character scalar or a one sided formula giving the name of the variable representing the time.
An integer scalar, default is NULL
. If NULL
, then the default lag is equal to n_t^0.25
with n_t
the number of time periods (as of Newey and West 1987) for panel Newey-West and Driscoll-Kraay. The default for the time series Newey-West is computed via bwNeweyWest
which implements the Newey and West 1994 method.
An object returned by the function ssc
. It specifies how to perform the small sample correction.
A character scalar or a one sided formula giving the name of the variable representing the units of the panel.
If the first argument is a fixest
object, then a VCOV is returned (i.e. a symmetric matrix).
If the first argument is not a fixest
object, then a) implicitly the arguments are shifted to the left (i.e. vcov_DK(~year)
is equivalent to vcov_DK(time = ~year)
) and b) a VCOV-request is returned and NOT a VCOV. That VCOV-request can then be used in the argument vcov
of various fixest
functions (e.g. vcov.fixest
or even in the estimation calls).
There are currently three VCOV types: Newey-West applied to time series, Newey-West applied to a panel setting (when the argument 'unit' is not missing), and Driscoll-Kraay.
The functions on this page without the prefix "vcov_" do not compute VCOVs directly but are meant to be used in the argument vcov
of fixest
functions (e.g. in vcov.fixest
or even in the estimation calls).
Note that for Driscoll-Kraay VCOVs, to ensure its properties the number of periods should be long enough (a minimum of 20 periods or so).
The default lag selection depends on whether the VCOV applies to a panel or a time series.
For panels, i.e. panel Newey-West or Driscoll-Kraay VCOV, the default lag is n_t^0.25
with n_t
the number of time periods. This is based on Newey and West 1987.
For time series Newey-West, the default lag is found thanks to the bwNeweyWest
function from the sandwich
package. It is based on Newey and West 1994.
Newey WK, West KD (1987). "A Simple, Positive Semi-Definite, Heteroskedasticity and Autocorrelation Consistent Covariance Matrix." Econometrica, 55(3), 703-708. doi:10.2307/1913610.
Driscoll JC, Kraay AC (1998). "Consistent Covariance Matrix Estimation with Spatially Dependent Panel Data." The Review of Economics and Statistics, 80(4), 549-560. doi:10.1162/003465398557825.
Millo G (2017). "Robust Standard Error Estimators for Panel Models: A Unifying Approach" Journal of Statistical Software, 82(3). doi:10.18637/jss.v082.i03.
data(base_did)
#
# During the estimation
#
# Panel Newey-West, lag = 2
feols(y ~ x1, base_did, NW(2) ~ id + period)
#> OLS estimation, Dep. Var.: y
#> Observations: 1,080
#> Standard-errors: Newey-West (L=2)
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 1.988753 0.186061 10.6887 2.0500e-06 ***
#> x1 0.983110 0.051618 19.0457 1.3968e-08 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 4.89686 Adj. R2: 0.262357
# Driscoll-Kraay
feols(y ~ x1, base_did, DK ~ period)
#> OLS estimation, Dep. Var.: y
#> Observations: 1,080
#> Standard-errors: Driscoll-Kraay (L=1)
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 1.988753 0.789538 2.51888 3.2829e-02 *
#> x1 0.983110 0.036115 27.22141 5.9051e-10 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 4.89686 Adj. R2: 0.262357
# If the estimation is made with a panel.id, the dimensions are
# automatically deduced:
est = feols(y ~ x1, base_did, "NW", panel.id = ~id + period)
est
#> OLS estimation, Dep. Var.: y
#> Observations: 1,080
#> Standard-errors: Newey-West (L=1)
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 1.988753 0.174111 11.4223 1.1709e-06 ***
#> x1 0.983110 0.052699 18.6551 1.6762e-08 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 4.89686 Adj. R2: 0.262357
#
# Post estimation
#
# If missing, the unit and time are automatically deduced from
# the panel.id used in the estimation
vcov_NW(est, lag = 2)
#> (Intercept) x1
#> (Intercept) 0.034618659 0.000265638
#> x1 0.000265638 0.002664456