Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not error when bootstrapping CIs if sample is too sparse #550

Merged
merged 6 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 sample was too sparse to compute
etiennebacher marked this conversation as resolved.
Show resolved Hide resolved
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)
})
Loading