Skip to content

Instantly share code, notes, and snippets.

@josherrickson
Created July 21, 2023 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josherrickson/28ac463f9a4c86c87c0f1ab66d150c6d to your computer and use it in GitHub Desktop.
Save josherrickson/28ac463f9a4c86c87c0f1ab66d150c6d to your computer and use it in GitHub Desktop.
summary.lm with vcov. argument
summary.lm <- function(object, correlation = FALSE, symbolic.cor = FALSE, ..., vcov. = NULL) {
ss <- stats::summary.lm(object, correlation, symbolic.cor, ...)
if (!is.null(vcov.)) {
if (is.function(vcov.)) {
vcov. <- vcov.(object, ...)
}
ss$cov.unscaled <- vcov./ss$sigma^2
ss$coefficients[,2] <- sqrt(diag(vcov.))
ss$coefficients[,3] <- ss$coefficients[,1]/ss$coefficients[,2]
ss$coefficients[,4] <- 2 * pt(abs(ss$coefficients[,3]), ss$df[2], lower.tail = FALSE)
}
ss
}
@josherrickson
Copy link
Author

This overloads stats::summary.lm to add the vcov. argument. It can be either a variance calculating function, or a variance/covariance matrix. E.g.

library(sandwich)
summary(mod, vcov. = vcovHC)
summary(mod, vcov. = vcovHC(mod, type = "HC1"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment