Skip to content

Commit

Permalink
Merge branch 'main' into strengejacke/issue937
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke authored Oct 14, 2024
2 parents 5a34f32 + 7ad72d7 commit 9092c6b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

* `get_dispersion()` is now an exported function.

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

## Bug fix

* `clean_parameters()` now uses the correct labels for the random effects
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 9092c6b

Please sign in to comment.