Empirical influence function of glm/gam object
glm_IF(glm_obj)
The object returned by glm().
matrix A n
by p
matrix.
x1 <- rnorm(1e3)
x2 <- rnorm(1e3)
y <- rbinom(n = 1e3, size = 1, plogis(-1 + x1 + 2 * x2))
glm_obj <- glm(y ~ x1 + x2, family = binomial(link = "logit"))
all.equal(crossprod(glm_IF(glm_obj)) / 1e6, sandwich::vcovHC(glm_obj, type = "HC"))
#> [1] TRUE
# Also works with mgcv::gam object for nonparametric first-step estimation
library(mgcv)
#> Loading required package: nlme
#> This is mgcv 1.8-41. For overview type 'help("mgcv-package")'.
gam_obj <- gam(y ~ s(x1) + s(x2), family = binomial(link = "logit"), method = "REML")
all.equal(crossprod(glm_IF(gam_obj)) / 1e6, sandwich::vcovHC(gam_obj, type = "HC"))
#> [1] TRUE