% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/fastglm-methods.R
\name{vcovHC.fastglm}
\alias{vcovHC.fastglm}
\alias{vcovHC.fastglmFit}
\title{Heteroskedasticity-consistent (HC) variance estimators for `fastglm` objects}
\usage{
vcovHC.fastglm(object, type = c("HC3", "HC2", "HC1", "HC0"), ...)

vcovHC.fastglmFit(object, type = c("HC3", "HC2", "HC1", "HC0"), ...)
}
\arguments{
\item{object}{a fitted object of class `"fastglm"` or `"fastglmFit"`.}

\item{type}{one of `"HC0"`, `"HC1"`, `"HC2"`, `"HC3"`. Default `"HC3"` matches
`sandwich::vcovHC.glm`.}

\item{...}{not used.}
}
\value{
A `p x p` heteroskedasticity-consistent variance-covariance matrix.
}
\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.
}
\details{
Computes the Eicker-Huber-White sandwich estimator
`bread %*% meat %*% bread`, where `bread = (X' W X)^{-1}` (already stored as
`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:
\describe{
  \item{`HC0`}{`omega_i = s_i^2`}
  \item{`HC1`}{`HC0` rescaled by `n / (n - p)`}
  \item{`HC2`}{`omega_i = s_i^2 / (1 - h_i)`}
  \item{`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{
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")
}

}
