R/fastglm-methods.R
vcovHC.fastglm.RdMethods for `sandwich::vcovHC()` on objects of class `"fastglm"` and `"fastglmFit"`. Load `sandwich` (`library(sandwich)`) before calling `vcovHC(fit)`; otherwise no `vcovHC` generic is in scope.
A `p x p` heteroskedasticity-consistent variance-covariance matrix.
Computes the Eicker-Huber-White sandwich estimator `bread `cov.unscaled`) and `meat = X' diag(omega_i) X`. With `s_i = w_i^2 * r_i` the score contribution from observation `i`, the omegas are:
`omega_i = s_i^2`
`HC0` rescaled by `n / (n - p)`
`omega_i = s_i^2 / (1 - h_i)`
`omega_i = s_i^2 / (1 - h_i)^2`
where `r_i` is the working residual `(y - mu) / mu.eta(eta)`, `w_i^2 = prior.weight * mu.eta(eta)^2 / variance(mu)` is the IRLS working weight, and `h_i = w_i^2 * x_i' (X' W X)^(-1) x_i` is the IRLS leverage. Equivalent to `sandwich::vcovHC.glm`.
Requires the model matrix `x` stored on the fitted object (set automatically by `fastglm()`, `fastglmPure()`, and `fastglm_fit()` since version 0.0.6).
if (requireNamespace("sandwich", quietly = TRUE)) {
x <- cbind(1, matrix(rnorm(500 * 4), ncol = 4))
y <- rbinom(500, 1, plogis(x %*% c(0.2, 0.3, -0.4, 0.1, 0.2)))
fit <- fastglm(x, y, family = binomial())
sandwich::vcovHC(fit)
sandwich::vcovHC(fit, type = "HC0")
}
#> (Intercept) X2 X3 X4
#> (Intercept) 0.0091471782 -0.0006155770 -0.0001452969 2.762778e-04
#> X2 -0.0006155770 0.0089333291 -0.0004291057 3.364747e-04
#> X3 -0.0001452969 -0.0004291057 0.0094949173 3.196950e-04
#> X4 0.0002762778 0.0003364747 0.0003196950 1.007547e-02
#> X5 -0.0004690929 0.0003366282 -0.0002316126 9.898358e-05
#> X5
#> (Intercept) -4.690929e-04
#> X2 3.366282e-04
#> X3 -2.316126e-04
#> X4 9.898358e-05
#> X5 8.198523e-03