Skip to content

Commit

Permalink
Do not error when bootstrapping CIs if sample is too sparse
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Oct 7, 2024
1 parent efa7df0 commit 6a0eccb
Show file tree
Hide file tree
Showing 4 changed files with 37 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 sample was too sparse to compute
CIs. Instead, it warns the user and returns `NA` (#550).

# datawizard 0.13.0

BREAKING CHANGES
Expand Down
22 changes: 17 additions & 5 deletions R/describe_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,23 @@ 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."
)
return(list(t = c(NA_real_, NA_real_)))

Check warning on line 203 in R/describe_distribution.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/describe_distribution.R,line=203,col=11,[return_linter] Use implicit return behavior; explicit return() is not needed.

Check warning on line 203 in R/describe_distribution.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/describe_distribution.R,line=203,col=11,[return_linter] Use implicit return behavior; explicit return() is not needed.
}
}
)
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
12 changes: 12 additions & 0 deletions tests/testthat/test-describe_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,15 @@ 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", {
set.seed(123456)
expect_warning(
res <- describe_distribution(mtcars[mtcars$cyl=="6",], wt, centrality = "map", ci = 0.95),

Check warning on line 295 in tests/testthat/test-describe_distribution.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-describe_distribution.R,line=295,col=5,[implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.

Check warning on line 295 in tests/testthat/test-describe_distribution.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-describe_distribution.R,line=295,col=51,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 295 in tests/testthat/test-describe_distribution.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-describe_distribution.R,line=295,col=57,[commas_linter] Put a space after a comma.
"When bootstrapping CIs, sample was too sparse to find TD"
)
expect_identical(res$CI_low, NA)
expect_identical(res$CI_high, NA)

Check warning on line 299 in tests/testthat/test-describe_distribution.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-describe_distribution.R,line=299,col=36,[trailing_whitespace_linter] Remove trailing whitespace.
})

0 comments on commit 6a0eccb

Please sign in to comment.