From 7ad72d794f5a9a9cd272fab5de08288cc443aac6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 14 Oct 2024 13:55:02 +0200 Subject: [PATCH] Support more sandwich methods (#939) * Support more sandwich methods * fix --- DESCRIPTION | 2 +- NEWS.md | 7 +++++++ R/get_varcov.R | 12 ++++++------ R/get_varcov_sandwich.R | 15 ++++++++++----- tests/testthat/test-get_varcov.R | 12 ++++++++++++ 5 files changed, 36 insertions(+), 12 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 42299e609..ca64a28ab 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", diff --git a/NEWS.md b/NEWS.md index e534109bd..d354a1f93 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/get_varcov.R b/R/get_varcov.R index 4c129c485..9ceab154c 100644 --- a/R/get_varcov.R +++ b/R/get_varcov.R @@ -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., diff --git a/R/get_varcov_sandwich.R b/R/get_varcov_sandwich.R index 4f2e8e4bd..bca0acc5f 100644 --- a/R/get_varcov_sandwich.R +++ b/R/get_varcov_sandwich.R @@ -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 @@ -73,6 +75,9 @@ CR = "vcovCR", xy = , residual = , + norm = , + jackknife = , + fractional = , wild = , mammen = , webb = , diff --git a/tests/testthat/test-get_varcov.R b/tests/testthat/test-get_varcov.R index 9ec689cf5..161c3192a 100644 --- a/tests/testthat/test-get_varcov.R +++ b/tests/testthat/test-get_varcov.R @@ -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", {