
Export of C++ function ols_hc2_post_fit_cpp
Source:R/helper_glm_fit.R, R/RcppExports.R
ols_hc2_post_fit_cpp.RdGiven 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.
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.