Skip to content

Commit

Permalink
Support more sandwich methods (#939)
Browse files Browse the repository at this point in the history
* Support more sandwich methods

* fix
  • Loading branch information
strengejacke authored Oct 14, 2024
1 parent c0b5fbf commit 7ad72d7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: insight
Title: Easy Access to Model Information for Various Model Objects
Version: 0.20.5
Version: 0.20.5.1
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# insight 0.20.x

## General

* Updates `get_varcov()` (and related documentation) to support new covariance
matrix estimation methods from the **sandwich** package.

# insight 0.20.5

## General
Expand Down
12 changes: 6 additions & 6 deletions R/get_varcov.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
#' * A covariance matrix
#' * A function which returns a covariance matrix (e.g., `stats::vcov()`)
#' * A string which indicates the kind of uncertainty estimates to return.
#' - Heteroskedasticity-consistent: `"vcovHC"`, `"HC"`, `"HC0"`, `"HC1"`,
#' `"HC2"`, `"HC3"`, `"HC4"`, `"HC4m"`, `"HC5"`. See `?sandwich::vcovHC`
#' - Heteroskedasticity-consistent: `"HC"`, `"HC0"`, `"HC1"`, `"HC2"`,
#' `"HC3"`, `"HC4"`, `"HC4m"`, `"HC5"`. See `?sandwich::vcovHC`
#' - Cluster-robust: `"vcovCR"`, `"CR0"`, `"CR1"`, `"CR1p"`, `"CR1S"`,
#' `"CR2"`, `"CR3"`. See `?clubSandwich::vcovCR()`
#' `"CR2"`, `"CR3"`. See `?clubSandwich::vcovCR`
#' - Bootstrap: `"vcovBS"`, `"xy"`, `"residual"`, `"wild"`, `"mammen"`,
#' `"webb"`. See `?sandwich::vcovBS`
#' - Other `sandwich` package functions: `"vcovHAC"`, `"vcovPC"`, `"vcovCL"`,
#' `"vcovPL"`.
#' `"fractional"`, `"jackknife"`, `"norm"`, `"webb"`.
#' See `?sandwich::vcovBS`
#' - Other `sandwich` package functions: `"HAC"`, `"PC"`, `"vCL"`, `"PL"`.
#' @param vcov_args List of arguments to be passed to the function identified by
#' the `vcov` argument. This function is typically supplied by the **sandwich**
#' or **clubSandwich** packages. Please refer to their documentation (e.g.,
Expand Down
15 changes: 10 additions & 5 deletions R/get_varcov_sandwich.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
return(.vcov)
}

vcov_type_shortcuts <- c(
"HC0", "HC1", "HC2", "HC3", "HC4", "HC4m", "HC5", "CR0", "CR1",
"CR1p", "CR1S", "CR2", "CR3", "xy", "residual", "wild", "mammen",
"webb", "fractional", "jackknife", "norm"
)

# type shortcuts: overwrite only if not supplied explicitly by the user
if (!"type" %in% names(vcov_args)) {
if (isTRUE(vcov_fun %in% c(
"HC0", "HC1", "HC2", "HC3", "HC4", "HC4m", "HC5",
"CR0", "CR1", "CR1p", "CR1S", "CR2", "CR3", "xy",
"residual", "wild", "mammen", "webb"
))) {
if (isTRUE(vcov_fun %in% vcov_type_shortcuts)) {
vcov_args[["type"]] <- vcov_fun
} else if (is.null(vcov_fun)) {
# set defaults
Expand Down Expand Up @@ -73,6 +75,9 @@
CR = "vcovCR",
xy = ,
residual = ,
norm = ,
jackknife = ,
fractional = ,
wild = ,
mammen = ,
webb = ,
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-get_varcov.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ test_that("lm: sandwich", {
sandwich::vcovOPG(mod),
tolerance = 1e-5
)

# examples from ?vcovBS
skip_on_cran()
data("PetersenCL", package = "sandwich")
m <- lm(y ~ x, data = PetersenCL)

set.seed(1234)
expect_equal(
get_varcov(m, vcov = "jackknife", vcov_args = list(cluster = PetersenCL$firm)),
sandwich::vcovBS(m, cluster = ~firm, type = "jackknife"),
tolerance = 1e-5
)
})

test_that("lm: clubSandwich", {
Expand Down

0 comments on commit 7ad72d7

Please sign in to comment.