Skip to contents

Heteroskedasticity- or cluster-robust covariance of the coefficients of a glm_fit() model, valid when the variance function or dispersion is wrong as long as the mean is right. The dispersion cancels. Results match statsmodels' cov_type = "HC0" and "cluster" (validation/scripts/statsmodels_glm_robust.py). For a non-canonical link (a log-link gamma, say) the bread is the observed information, as in statsmodels; sandwich::vcovHC() uses the expected information, so the two differ slightly there and agree for canonical links.

Usage

robust_vcov(object, type = c("HC0", "HC1"), cluster = NULL)

Arguments

object

A glm_model.

type

"HC0" (White's estimator) or "HC1" (scaled by n / (n - p)). Ignored when cluster is given.

cluster

Optional cluster labels, one per row the model was fitted on (a policy or an event, say). The scores are summed within each cluster and the result scaled by G / (G - 1) * (n - 1) / (n - p) for G clusters.

Value

A named covariance matrix; sqrt(diag(.)) gives the robust standard errors.

Examples

d <- data.frame(y = c(1, 2, 6, 1, 4, 2), x = c(0, 0, 0, 1, 1, 1),
                policy = c(1, 1, 2, 2, 3, 3))
m <- glm_fit(y ~ x, d, family = "poisson")
robust_vcov(m)
#>             (Intercept)          x
#> (Intercept)   0.1728395 -0.1728395
#> x            -0.1728395  0.2680776
sqrt(diag(robust_vcov(m, cluster = d$policy)))
#> (Intercept)           x 
#>   0.6454972   0.8892785