Computes a heteroskedasticity-consistent covariance matrix estimator for an
ordinary least squares model fitted with stats::lm(). The function returns
a rich S3 object that stores the covariance matrix, HC weights, leverage
values, method parameters, and model metadata.
Arguments
- object
An ordinary least squares model fitted by
stats::lm().- type
A character string specifying the HC estimator. The default is
"hcbeta".- ...
Method-specific constants. Unknown names are rejected. See Details for the accepted names, defaults, and parameter domains.
Value
An object of class hcinfer_vcov, a list with components:
callThe matched call.
type,labelEstimator code and display label.
vcovThe HC covariance matrix, also returned by
vcov().weightsHC adjustment factors \(g_t\).
leverageLeverage values \(h_t\).
residualsResiduals entering \(\widehat{\Omega}\): the OLS residuals, or the LMS residuals for
"hc0r".coefficientsOLS estimates.
terms,observationCoefficient and observation names.
num_obs,p,residual_dfNumber of observations, number of coefficients, and residual degrees of freedom
num_obs - p.method_paramsMethod arguments and, for HCbeta, the estimated quantities.
model_call,model_formulaCall and formula of the fitted model.
Details
For a linear model with design matrix \(X\), residuals \(e_t\), and HC weights \(g_t\), the estimator is
$$\widehat{\Psi}_{HC} = (X'X)^{-1} X' \widehat{\Omega} X (X'X)^{-1},$$
where \(\widehat{\Omega} = diag(e_t^2 g_t)\). The residuals are the OLS
residuals \(\hat e_t\) for every estimator except "hc0r", described
below. The supported estimators are "hc0", "hc0r", "hc1", "hc2",
"hc3", "hc4", "hc4m", "hc5", "hc5m", and "hcbeta".
Additional arguments in ... are method-specific. The defaults are:
"hc0","hc1","hc2","hc3","hc4", and"hc4m": no method-specific arguments."hc0r":seed = 1."hc5":k = 0.7."hc5m":k = 0.7,k1 = 1,k2 = 0,k3 = 1,gamma1 = 1, andgamma2 = 1.5."hcbeta":c1 = 7,c2 = 0.75,lower = 0.01,upper = 0.99,a_max = 10000, andb_max = 10000.
The HC5 adjustment factor follows the corrected expression published in the erratum to Cribari-Neto, Souza and Vasconcellos (2007), that is, \(g_t = (1 - h_t)^{-\delta_t / 2}\) with \(\delta_t = \min\{h_t / \bar h, \max\{4, k h_{\max} / \bar h\}\}\), where \(\bar h = p / n\) and \(h_{\max} = \max_t h_t\). HC5m follows Li, Zhang, Zhang and Wang (2016) and applies its own exponent \(\delta_t\) without the factor \(1/2\).
HC0R keeps \(g_t = 1\) and replaces the OLS residuals by the residuals
\(\tilde e_t = y_t - x_t'\hat\beta_{LMS}\) of the least median of squares
(LMS) fit of Rousseeuw (1984), as proposed by Lima, Souza, Cribari-Neto and
Fernandes (2010); any model offset is subtracted from \(y_t\) first. The
label HC0R is the package's name for the estimator that Lima et al. (2010)
report as HC0 with LMS residuals; their symbol \(HC0_R\) denotes a
different, weighted least squares version of HC0. The LMS fit is computed by
MASS::lqs() with method = "lms" and its default settings. The stored
coefficients remain the OLS estimates, so only \(\widehat{\Omega}\)
changes. When choose(n, p) is smaller than 5000, MASS::lqs() examines
every elemental subset of p observations and the result does not depend on
seed; otherwise it examines min(500 * p, 3000) randomly drawn subsets.
The argument seed (default 1) is passed to set.seed() together with R's
default generator kinds, so it fixes these subsets whatever RNGkind() the
session uses and HC0R is a deterministic function of the data and seed.
The caller's random number generator state, including its kinds, is restored
afterwards. Refitting with another seed shows how sensitive the standard
errors are to the sampled subsets. Because \(g_t = 1\), HC0R is defined
when \(h_t = 1\), although the LMS fit still needs nonsingular elemental
subsets, which can be rare when the model contains several
observation-specific dummy regressors.
For "hc0r", seed must be one finite number between
-.Machine$integer.max and .Machine$integer.max. For "hc5" and
"hc5m", k, k1, k2, and k3 must be nonnegative, while gamma1 and
gamma2 must be positive. For "hcbeta", c1 must be nonnegative, c2
must be positive, and lower and upper must lie in (0, 1) with
lower < upper. The HCbeta leverage-complement truncation is
\(w_t = max(lower, min(1 - h_t, upper))\).
After shrinkage with \(\zeta = n / (n + 50)\), the Beta shape parameters
are clamped as
$$\tilde a =
\min\{\max\{(1 - \zeta) + \zeta\hat a, \epsilon\}, A_{\max}\},$$
$$\tilde b =
\min\{\max\{(1 - \zeta) + \zeta\hat b, \epsilon\}, B_{\max}\}.$$
The value \(\epsilon = 0.01\) is fixed, applied after shrinkage and before
the caps, and is not a method argument. It is distinct from lower, which
truncates \(w_t\); changing lower does not change \(\epsilon\).
Both a_max and b_max must be finite and lie in [50, 25000]; they
default to 10000 and can be set independently through ... for sensitivity
analysis. When the variance of the truncated complements is numerically
degenerate, the adjusted shapes are set directly to their respective caps.
HCbeta remains defined when \(h_t = 1\) because it truncates
\(1 - h_t\) before evaluating the Beta CDF. In contrast, HC2 through HC5m
require a strictly positive leverage complement.
References
White, H. (1980). A heteroskedasticity-consistent covariance matrix estimator and a direct test for heteroskedasticity. Econometrica, 48(4), 817-838. doi:10.2307/1912934
Hinkley, D. V. (1977). Jackknifing in unbalanced situations. Technometrics, 19(3), 285-292. doi:10.1080/00401706.1977.10489550
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
Davidson, R. and MacKinnon, J. G. (1993). Estimation and Inference in Econometrics. Oxford University Press.
Cribari-Neto, F. (2004). Asymptotic inference under heteroskedasticity of unknown form. Computational Statistics and Data Analysis, 45(2), 215-233. doi:10.1016/S0167-9473(02)00366-3
Cribari-Neto, F. and da Silva, W. B. (2011). A new heteroskedasticity consistent covariance matrix estimator for the linear regression model. AStA Advances in Statistical Analysis, 95(2), 129-146. doi:10.1007/s10182-010-0141-2
Cribari-Neto, F., Souza, T. C., and Vasconcellos, K. L. P. (2007). Inference under heteroskedasticity and leveraged data. Communications in Statistics - Theory and Methods, 36(10), 1877-1888. doi:10.1080/03610920601126589
Cribari-Neto, F., Souza, T. C., and Vasconcellos, K. L. P. (2008). Errata: Inference under heteroskedasticity and leveraged data, Communications in Statistics, Theory and Methods, 36, 1877-1888, 2007. Communications in Statistics - Theory and Methods, 37(20), 3329-3330. doi:10.1080/03610920802109210
Li, S., Zhang, N., Zhang, X., and Wang, G. (2016). A new heteroskedasticity-consistent covariance matrix estimator and inference under heteroskedasticity. Journal of Statistical Computation and Simulation, 87(1), 198-210. doi:10.1080/00949655.2016.1198906
Rousseeuw, P. J. (1984). Least median of squares regression. Journal of the American Statistical Association, 79(388), 871-880. doi:10.1080/01621459.1984.10477105
Lima, V. M. C., Souza, T. C., Cribari-Neto, F., and Fernandes, G. B. (2010). Heteroskedasticity-robust inference in linear regressions. Communications in Statistics - Simulation and Computation, 39(1), 194-206. doi:10.1080/03610910903402572
Cunha, M. O., Cribari-Neto, F., and Marinho, P. R. D. (2026). A beta-based heteroskedasticity-consistent covariance matrix estimator. Journal of Statistical Computation and Simulation. doi:10.1080/00949655.2026.2757763
Examples
schools <- PublicSchools |>
dplyr::mutate(
income_scaled = income / 10000,
income_scaled_sq = income_scaled^2
)
fit <- lm(expenditure ~ income_scaled + income_scaled_sq, data = schools)
cov <- vcov_hc(fit, type = "hcbeta")
cov
#>
#> ── HCbeta robust covariance ────────────────────────────────────────────────────
#> Model: `expenditure ~ income_scaled + income_scaled_sq`
#> Dimension: 3 x 3
#> Observations: 50
#> Parameters: 3
#> Maximum leverage: 0.6508
#> Maximum robust weight: 4.5807
#> Use `vcov()` to extract the stored covariance matrix.
vcov(cov)
#> (Intercept) income_scaled income_scaled_sq
#> (Intercept) 723617.6 -1962262 1312195
#> income_scaled -1962262.2 5329884 -3569755
#> income_scaled_sq 1312195.3 -3569755 2394627
plot(cov)
# Sensitivity analysis with nondefault HCbeta caps
vcov_hc(fit, type = "hcbeta", a_max = 20000, b_max = 20000)
#>
#> ── HCbeta robust covariance ────────────────────────────────────────────────────
#> Model: `expenditure ~ income_scaled + income_scaled_sq`
#> Dimension: 3 x 3
#> Observations: 50
#> Parameters: 3
#> Maximum leverage: 0.6508
#> Maximum robust weight: 4.5807
#> Use `vcov()` to extract the stored covariance matrix.
vcov_hc(fit, type = "hc5", k = 0.7)
#>
#> ── HC5 robust covariance ───────────────────────────────────────────────────────
#> Model: `expenditure ~ income_scaled + income_scaled_sq`
#> Dimension: 3 x 3
#> Observations: 50
#> Parameters: 3
#> Maximum leverage: 0.6508
#> Maximum robust weight: 54.2843
#> Use `vcov()` to extract the stored covariance matrix.
vcov_hc(fit, type = "hc5m", k = 0.7, k1 = 1, k2 = 0, k3 = 1)
#>
#> ── HC5m robust covariance ──────────────────────────────────────────────────────
#> Model: `expenditure ~ income_scaled + income_scaled_sq`
#> Dimension: 3 x 3
#> Observations: 50
#> Parameters: 3
#> Maximum leverage: 0.6508
#> Maximum robust weight: 8438.7828
#> Use `vcov()` to extract the stored covariance matrix.
# HC0R with another seed for the random LMS subsets
vcov_hc(fit, type = "hc0r", seed = 2)
#>
#> ── HC0R robust covariance ──────────────────────────────────────────────────────
#> Model: `expenditure ~ income_scaled + income_scaled_sq`
#> Dimension: 3 x 3
#> Observations: 50
#> Parameters: 3
#> Maximum leverage: 0.6508
#> Maximum robust weight: 1.0000
#> Use `vcov()` to extract the stored covariance matrix.
