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) X1 X2 X3
#> (Intercept) 0.0091471737 -0.0006155774 -0.0001452957 2.762773e-04
#> X1 -0.0006155774 0.0089333226 -0.0004291016 3.364751e-04
#> X2 -0.0001452957 -0.0004291016 0.0094949073 3.196938e-04
#> X3 0.0002762773 0.0003364751 0.0003196938 1.007547e-02
#> X4 -0.0004690929 0.0003366271 -0.0002316114 9.898379e-05
#> X4
#> (Intercept) -4.690929e-04
#> X1 3.366271e-04
#> X2 -2.316114e-04
#> X3 9.898379e-05
#> X4 8.198518e-03