Skip to content

Commit

Permalink
Do not error when bootstrapping CIs if sample is too sparse (#550)
Browse files Browse the repository at this point in the history
* Do not error when bootstrapping CIs if sample is too sparse

* lint, style

* conditional test

* typo

* style again

* Update NEWS.md
  • Loading branch information
etiennebacher authored Oct 7, 2024
1 parent efa7df0 commit db85541
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 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: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.13.0
Version: 0.13.0.1
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531")),
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# datawizard (development)

BUG FIXES

* `describe_distribution()` no longer errors if the sample was too sparse to compute
CIs. Instead, it warns the user and returns `NA` (#550).

# datawizard 0.13.0

BREAKING CHANGES
Expand Down
23 changes: 18 additions & 5 deletions R/describe_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,24 @@ describe_distribution.numeric <- function(x,
# Confidence Intervals
if (!is.null(ci)) {
insight::check_if_installed("boot")
results <- boot::boot(
data = x,
statistic = .boot_distribution,
R = iterations,
centrality = centrality
results <- tryCatch(
{
boot::boot(
data = x,
statistic = .boot_distribution,
R = iterations,
centrality = centrality
)
},
error = function(e) {
msg <- conditionMessage(e)
if (!is.null(msg) && msg == "sample is too sparse to find TD") {
insight::format_warning(
"When bootstrapping CIs, sample was too sparse to find TD. Returning NA for CIs."
)
list(t = c(NA_real_, NA_real_))
}
}
)
out_ci <- bayestestR::ci(results$t, ci = ci, verbose = FALSE)
out <- cbind(out, data.frame(CI_low = out_ci$CI_low[1], CI_high = out_ci$CI_high[1]))
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-describe_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,16 @@ test_that("describe_distribution formatting", {
x <- describe_distribution(iris$Sepal.Width, quartiles = TRUE)
expect_snapshot(format(x))
})

# other -----------------------------------

test_that("return NA in CI if sample is too sparse", {
skip_if_not_installed("bayestestR")
set.seed(123456)
expect_warning(
res <- describe_distribution(mtcars[mtcars$cyl == "6", ], wt, centrality = "map", ci = 0.95), # nolint
"When bootstrapping CIs, sample was too sparse to find TD"
)
expect_identical(res$CI_low, NA)
expect_identical(res$CI_high, NA)
})

0 comments on commit db85541

Please sign in to comment.