Learn R Programming

fastglm (version 0.1.0)

vcovHC.fastglm: Heteroskedasticity-consistent (HC) variance estimators for `fastglm` objects

Description

Methods 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.

Usage

vcovHC.fastglm(object, type = c("HC3", "HC2", "HC1", "HC0"), ...)

vcovHC.fastglmFit(object, type = c("HC3", "HC2", "HC1", "HC0"), ...)

Value

A `p x p` heteroskedasticity-consistent variance-covariance matrix.

Arguments

object

a fitted object of class `"fastglm"` or `"fastglmFit"`.

type

one of `"HC0"`, `"HC1"`, `"HC2"`, `"HC3"`. Default `"HC3"` matches `sandwich::vcovHC.glm`.

...

not used.

Details

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:

`HC0`

`omega_i = s_i^2`

`HC1`

`HC0` rescaled by `n / (n - p)`

`HC2`

`omega_i = s_i^2 / (1 - h_i)`

`HC3`

`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).

Examples

Run this code
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")
}

Run the code above in your browser using DataLab