Skip to contents

Given an already-fitted ordinary least squares model, computes the HC2 heteroskedasticity-consistent sandwich covariance matrix (MacKinnon-White) for the coefficients: \(\widehat{\mathrm{Var}}(\hat\beta) = B\,M\,B\), with "bread" \(B = (X^\top X)^{-1}\) and "meat" \(M = X^\top \mathrm{diag}(\omega_i) X\), where \(\omega_i = r_i^2 / (1 - h_{ii})\) — the squared OLS residual \(r_i\) leverage-corrected by dividing by \(1 - h_{ii}\) (\(h_{ii}\) the \(i\)-th diagonal of the OLS hat matrix \(X(X^\top X)^{-1}X^\top\)), unlike the plain HC0 sandwich (gcomp_logistic_post_fit_cpp's logistic analogue, or this function's own uncorrected \(r_i^2\) meat) which does not correct for leverage. HC2 is unbiased under homoskedasticity for balanced designs and generally has better small-sample properties than HC0/HC1 when leverage is uneven. Internally, this function first computes the (design-only) "setup" quantities bread/hat via ols_hc2_setup_cpp, then calls ols_hc2_post_fit_precomputed_cpp; callers who already have those precomputed (e.g. across repeated resampling on the same fixed design) can call the precomputed variant directly instead to skip recomputing the \((X^\top X)^{-1}\) bread and leverage each time.

Usage

ols_hc2_post_fit_cpp(X_fit, y, coef_hat, j_treat)

Arguments

X_fit

A numeric matrix of predictors, as used to fit the model.

y

A numeric vector of responses.

coef_hat

A numeric vector of fitted OLS coefficients \(\hat\beta\), same length and column order as X_fit.

j_treat

1-based column index of the treatment indicator in X_fit.

Value

A list with components beta_hat (\(\hat\beta_{j_{\mathrm{treat}}}\)), ssq_hat (its HC2 variance), se (its HC2 standard error), vcov (the full \(p \times p\) HC2 covariance matrix), std_err (per-coefficient HC2 standard errors), and z_vals (per-coefficient Wald z-statistics, \(\hat\beta_j / \widehat{\mathrm{SE}}(\hat\beta_j)\)).

References

MacKinnon, J. G., and White, H. (1985). "Some Heteroskedasticity-Consistent Covariance Matrix Estimators with Improved Finite Sample Properties." Journal of Econometrics, 29(3), 305-325, doi:10.1016/0304-4076(85)90158-7 , for the HC2 estimator used here.