From ea75e1465af7944cfe292a8cbb750331023f5858 Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 11:18:15 +0100 Subject: [PATCH 01/16] reduce `check_rset()` to `check_includes_apparent()` don't need to check for `bootstraps` now that the functions are S3 methods --- R/bootci.R | 22 ++++++++++------------ tests/testthat/_snaps/bootci.md | 31 ++++++++++++++++++------------- tests/testthat/test-bootci.R | 9 +++++++++ 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/R/bootci.R b/R/bootci.R index aa72f318..3420fe63 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -4,15 +4,14 @@ # helpers -check_rset <- function(x, app = TRUE) { - if (!inherits(x, "bootstraps")) { - cli_abort("{.arg .data} should be an {.cls rset} object generated from {.fn bootstraps}.") - } - - if (app) { - if (x %>% dplyr::filter(id == "Apparent") %>% nrow() != 1) { - cli_abort("Please set {.code apparent = TRUE} in {.fn bootstraps} function.") - } +check_includes_apparent <- function(x, call = caller_env()) { + if (x %>% dplyr::filter(id == "Apparent") %>% nrow() != 1) { + cli_abort(c( + "The bootstrap resamples must include an apparent sample.", + i = "Please set {.code apparent = TRUE} in the {.fn bootstraps} function." + ), + call = call + ) } invisible(NULL) } @@ -283,7 +282,6 @@ int_pctl <- function(.data, ...) { #' @rdname int_pctl int_pctl.bootstraps <- function(.data, statistics, alpha = 0.05, ...) { check_dots_empty() - check_rset(.data, app = FALSE) if (length(alpha) != 1 || !is.numeric(alpha)) { cli_abort("{.arg alpha} must be a single numeric value.") } @@ -368,7 +366,7 @@ int_t <- function(.data, ...) { #' @export int_t.bootstraps <- function(.data, statistics, alpha = 0.05, ...) { check_dots_empty() - check_rset(.data) + check_includes_apparent(.data) if (length(alpha) != 1 || !is.numeric(alpha)) { cli_abort("{.arg alpha} must be a single numeric value.") } @@ -477,7 +475,7 @@ int_bca <- function(.data, ...) { #' @rdname int_pctl #' @export int_bca.bootstraps <- function(.data, statistics, alpha = 0.05, .fn, ...) { - check_rset(.data) + check_includes_apparent(.data) if (length(alpha) != 1 || !is.numeric(alpha)) { cli_abort("{.arg alpha} must be a single numeric value.") } diff --git a/tests/testthat/_snaps/bootci.md b/tests/testthat/_snaps/bootci.md index 26fa9aad..6f3900df 100644 --- a/tests/testthat/_snaps/bootci.md +++ b/tests/testthat/_snaps/bootci.md @@ -54,19 +54,6 @@ 1 mean 9.96 10.0 10.1 0.05 student-t ---- - - Code - int_bca(bt_small, stats, .fn = get_stats) - Condition - Warning: - Recommend at least 1000 non-missing bootstrap resamples for term `mean`. - Output - # A tibble: 1 x 6 - term .lower .estimate .upper .alpha .method - - 1 mean 9.96 10.0 10.1 0.05 BCa - # bad input Code @@ -219,3 +206,21 @@ Error in `pctl_single()`: ! `stats` must be a numeric vector. +# checks for apparent bootstrap + + Code + int_t(rs_boot) + Condition + Error in `int_t()`: + ! The bootstrap resamples must include an apparent sample. + i Please set `apparent = TRUE` in the `bootstraps()` function. + +--- + + Code + int_bca(rs_boot) + Condition + Error in `int_bca()`: + ! The bootstrap resamples must include an apparent sample. + i Please set `apparent = TRUE` in the `bootstraps()` function. + diff --git a/tests/testthat/test-bootci.R b/tests/testthat/test-bootci.R index 57afea07..220bd405 100644 --- a/tests/testthat/test-bootci.R +++ b/tests/testthat/test-bootci.R @@ -299,6 +299,15 @@ test_that("bad input", { }) }) +test_that("checks for apparent bootstrap", { + rs_boot <- bootstraps(mtcars, times = 10, apparent = FALSE) + expect_snapshot(error = TRUE, { + int_t(rs_boot) + }) + expect_snapshot(error = TRUE, { + int_bca(rs_boot) + }) +}) # ------------------------------------------------------------------------------ From f715d50fc7770c4b73d310764e012511c08ada92 Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 11:24:42 +0100 Subject: [PATCH 02/16] fix formatting and give speaking name --- R/bootci.R | 14 ++++++++------ tests/testthat/_snaps/bootci.md | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/R/bootci.R b/R/bootci.R index 3420fe63..cbf26ba8 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -17,7 +17,9 @@ check_includes_apparent <- function(x, call = caller_env()) { } -stat_fmt_err <- "{.arg statistics} should select a list column of tidy results." +statistics_format_error <- cli::format_inline( + "{.arg statistics} should select a list column of tidy results." +) stat_nm_err <- paste( "The tibble in {.arg statistics} should have columns for", "'estimate' and 'term'." @@ -43,7 +45,7 @@ check_tidy_names <- function(x, std_col) { check_tidy <- function(x, std_col = FALSE) { if (!is.list(x)) { - rlang::abort(stat_fmt_err) + rlang::abort(statistics_format_error) } # convert to data frame from list @@ -56,7 +58,7 @@ check_tidy <- function(x, std_col = FALSE) { } if (inherits(x, "try-error")) { - cli_abort(stat_fmt_err) + cli_abort(statistics_format_error) } check_tidy_names(x, std_col) @@ -290,7 +292,7 @@ int_pctl.bootstraps <- function(.data, statistics, alpha = 0.05, ...) { column_name <- tidyselect::vars_select(names(.data), !!rlang::enquo(statistics)) if (length(column_name) != 1) { - rlang::abort(stat_fmt_err) + rlang::abort(statistics_format_error) } stats <- .data[[column_name]] stats <- check_tidy(stats, std_col = FALSE) @@ -373,7 +375,7 @@ int_t.bootstraps <- function(.data, statistics, alpha = 0.05, ...) { column_name <- tidyselect::vars_select(names(.data), !!enquo(statistics)) if (length(column_name) != 1) { - cli_abort(stat_fmt_err) + cli_abort(statistics_format_error) } stats <- .data %>% dplyr::select(!!column_name, id) stats <- check_tidy(stats, std_col = TRUE) @@ -484,7 +486,7 @@ int_bca.bootstraps <- function(.data, statistics, alpha = 0.05, .fn, ...) { column_name <- tidyselect::vars_select(names(.data), !!enquo(statistics)) if (length(column_name) != 1) { - cli_abort(stat_fmt_err) + cli_abort(statistics_format_error) } stats <- .data %>% dplyr::select(!!column_name, id, dplyr::starts_with(".")) stats <- check_tidy(stats) diff --git a/tests/testthat/_snaps/bootci.md b/tests/testthat/_snaps/bootci.md index 6f3900df..1cde79d3 100644 --- a/tests/testthat/_snaps/bootci.md +++ b/tests/testthat/_snaps/bootci.md @@ -60,7 +60,7 @@ int_pctl(bt_small, id) Condition Error in `check_tidy()`: - ! {.arg statistics} should select a list column of tidy results. + ! `statistics` should select a list column of tidy results. --- @@ -68,7 +68,7 @@ int_pctl(bt_small, junk) Condition Error in `check_tidy()`: - ! {.arg statistics} should select a list column of tidy results. + ! `statistics` should select a list column of tidy results. --- From 977a1270a315c97c3b1373a1fb641ff800b38025 Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 11:47:50 +0100 Subject: [PATCH 03/16] fix styling and improve name --- R/bootci.R | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/R/bootci.R b/R/bootci.R index cbf26ba8..686c38e7 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -20,19 +20,18 @@ check_includes_apparent <- function(x, call = caller_env()) { statistics_format_error <- cli::format_inline( "{.arg statistics} should select a list column of tidy results." ) -stat_nm_err <- paste( - "The tibble in {.arg statistics} should have columns for", - "'estimate' and 'term'." +statistics_name_error <- cli::format_inline( + "The tibble in {.arg statistics} should have columns for 'estimate' and 'term'." ) std_exp <- c("std.error", "robust.se") check_tidy_names <- function(x, std_col) { # check for proper columns if (sum(colnames(x) == "estimate") != 1) { - cli_abort(stat_nm_err) + cli_abort(statistics_name_error) } if (sum(colnames(x) == "term") != 1) { - cli_abort(stat_nm_err) + cli_abort(statistics_name_error) } if (std_col) { std_candidates <- colnames(x) %in% std_exp From 5f7323716c373ad81028d13180ab1e3c8b1c4915 Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 11:48:10 +0100 Subject: [PATCH 04/16] add tests --- tests/testthat/_snaps/bootci.md | 24 ++++++++++++++++++++++++ tests/testthat/test-bootci.R | 29 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/tests/testthat/_snaps/bootci.md b/tests/testthat/_snaps/bootci.md index 1cde79d3..25417165 100644 --- a/tests/testthat/_snaps/bootci.md +++ b/tests/testthat/_snaps/bootci.md @@ -224,3 +224,27 @@ ! The bootstrap resamples must include an apparent sample. i Please set `apparent = TRUE` in the `bootstraps()` function. +# checks input for statistics + + Code + int_t(rs_boot_missing_term, stats) + Condition + Error in `check_tidy_names()`: + ! The tibble in `statistics` should have columns for 'estimate' and 'term'. + +--- + + Code + int_t(rs_boot_missing_estimate, stats) + Condition + Error in `check_tidy_names()`: + ! The tibble in `statistics` should have columns for 'estimate' and 'term'. + +--- + + Code + int_t(rs_boot_missing_std_err, stats) + Condition + Error in `check_tidy_names()`: + ! `statistics` should select a single column for the standard error. + diff --git a/tests/testthat/test-bootci.R b/tests/testthat/test-bootci.R index 220bd405..5305bf6b 100644 --- a/tests/testthat/test-bootci.R +++ b/tests/testthat/test-bootci.R @@ -309,6 +309,35 @@ test_that("checks for apparent bootstrap", { }) }) +test_that("checks input for statistics", { + dat <- data.frame(x = rnorm(n = 1000, mean = 10, sd = 1)) + rs_boot <- bootstraps(dat, times = 10, apparent = TRUE) + + rs_boot_missing_term <- rs_boot %>% + dplyr::mutate( + stats = purrr::map(1:11, ~ tibble(estimate = 1)) + ) + expect_snapshot(error = TRUE, { + int_t(rs_boot_missing_term, stats) + }) + + rs_boot_missing_estimate <- rs_boot %>% + dplyr::mutate( + stats = purrr::map(1:11, ~ tibble(term = 1)) + ) + expect_snapshot(error = TRUE, { + int_t(rs_boot_missing_estimate, stats) + }) + + rs_boot_missing_std_err <- rs_boot %>% + dplyr::mutate( + stats = purrr::map(1:11, ~ tibble(term = 1, estimate = 2)) + ) + expect_snapshot(error = TRUE, { + int_t(rs_boot_missing_std_err, stats) + }) +}) + # ------------------------------------------------------------------------------ test_that("compute intervals with additional grouping terms", { From f4881d7081c4aa3464e1be17bd7537a69e4aa1ee Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 11:51:00 +0100 Subject: [PATCH 05/16] report separate since we're checking separately --- R/bootci.R | 11 ++++++----- tests/testthat/_snaps/bootci.md | 8 ++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/R/bootci.R b/R/bootci.R index 686c38e7..00b1711f 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -20,18 +20,19 @@ check_includes_apparent <- function(x, call = caller_env()) { statistics_format_error <- cli::format_inline( "{.arg statistics} should select a list column of tidy results." ) -statistics_name_error <- cli::format_inline( - "The tibble in {.arg statistics} should have columns for 'estimate' and 'term'." -) std_exp <- c("std.error", "robust.se") check_tidy_names <- function(x, std_col) { # check for proper columns if (sum(colnames(x) == "estimate") != 1) { - cli_abort(statistics_name_error) + cli_abort( + "The tibble in {.arg statistics} must have a column for 'estimate'." + ) } if (sum(colnames(x) == "term") != 1) { - cli_abort(statistics_name_error) + cli_abort( + "The tibble in {.arg statistics} must have a column for 'term'." + ) } if (std_col) { std_candidates <- colnames(x) %in% std_exp diff --git a/tests/testthat/_snaps/bootci.md b/tests/testthat/_snaps/bootci.md index 25417165..d42f9f40 100644 --- a/tests/testthat/_snaps/bootci.md +++ b/tests/testthat/_snaps/bootci.md @@ -180,7 +180,7 @@ int_pctl(badder_bt_norm, bad_term) Condition Error in `check_tidy_names()`: - ! The tibble in `statistics` should have columns for 'estimate' and 'term'. + ! The tibble in `statistics` must have a column for 'term'. --- @@ -196,7 +196,7 @@ int_bca(badder_bt_norm, bad_est, .fn = get_stats) Condition Error in `check_tidy_names()`: - ! The tibble in `statistics` should have columns for 'estimate' and 'term'. + ! The tibble in `statistics` must have a column for 'estimate'. --- @@ -230,7 +230,7 @@ int_t(rs_boot_missing_term, stats) Condition Error in `check_tidy_names()`: - ! The tibble in `statistics` should have columns for 'estimate' and 'term'. + ! The tibble in `statistics` must have a column for 'term'. --- @@ -238,7 +238,7 @@ int_t(rs_boot_missing_estimate, stats) Condition Error in `check_tidy_names()`: - ! The tibble in `statistics` should have columns for 'estimate' and 'term'. + ! The tibble in `statistics` must have a column for 'estimate'. --- From c23f5e03c97c881055cdd58837394e81d3ef695a Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 11:53:12 +0100 Subject: [PATCH 06/16] rename to be more specific --- R/bootci.R | 4 ++-- tests/testthat/_snaps/bootci.md | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/R/bootci.R b/R/bootci.R index 00b1711f..995c70b9 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -22,7 +22,7 @@ statistics_format_error <- cli::format_inline( ) std_exp <- c("std.error", "robust.se") -check_tidy_names <- function(x, std_col) { +check_statistics_names <- function(x, std_col) { # check for proper columns if (sum(colnames(x) == "estimate") != 1) { cli_abort( @@ -61,7 +61,7 @@ check_tidy <- function(x, std_col = FALSE) { cli_abort(statistics_format_error) } - check_tidy_names(x, std_col) + check_statistics_names(x, std_col) if (std_col) { std_candidates <- colnames(x) %in% std_exp diff --git a/tests/testthat/_snaps/bootci.md b/tests/testthat/_snaps/bootci.md index d42f9f40..0f91d33b 100644 --- a/tests/testthat/_snaps/bootci.md +++ b/tests/testthat/_snaps/bootci.md @@ -123,7 +123,7 @@ Code int_t(bad_bt_norm, stats) Condition - Error in `check_tidy_names()`: + Error in `check_statistics_names()`: ! `statistics` should select a single column for the standard error. --- @@ -179,7 +179,7 @@ Code int_pctl(badder_bt_norm, bad_term) Condition - Error in `check_tidy_names()`: + Error in `check_statistics_names()`: ! The tibble in `statistics` must have a column for 'term'. --- @@ -187,7 +187,7 @@ Code int_t(badder_bt_norm, bad_err) Condition - Error in `check_tidy_names()`: + Error in `check_statistics_names()`: ! `statistics` should select a single column for the standard error. --- @@ -195,7 +195,7 @@ Code int_bca(badder_bt_norm, bad_est, .fn = get_stats) Condition - Error in `check_tidy_names()`: + Error in `check_statistics_names()`: ! The tibble in `statistics` must have a column for 'estimate'. --- @@ -229,7 +229,7 @@ Code int_t(rs_boot_missing_term, stats) Condition - Error in `check_tidy_names()`: + Error in `check_statistics_names()`: ! The tibble in `statistics` must have a column for 'term'. --- @@ -237,7 +237,7 @@ Code int_t(rs_boot_missing_estimate, stats) Condition - Error in `check_tidy_names()`: + Error in `check_statistics_names()`: ! The tibble in `statistics` must have a column for 'estimate'. --- @@ -245,6 +245,6 @@ Code int_t(rs_boot_missing_std_err, stats) Condition - Error in `check_tidy_names()`: + Error in `check_statistics_names()`: ! `statistics` should select a single column for the standard error. From a5e2e13db4a9f2c9f592499e9729f24583de87d0 Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 11:55:29 +0100 Subject: [PATCH 07/16] thread call through to `check_statistics_names()` --- R/bootci.R | 13 +++++++++---- tests/testthat/_snaps/bootci.md | 14 +++++++------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/R/bootci.R b/R/bootci.R index 995c70b9..e368dad7 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -22,22 +22,27 @@ statistics_format_error <- cli::format_inline( ) std_exp <- c("std.error", "robust.se") -check_statistics_names <- function(x, std_col) { +check_statistics_names <- function(x, std_col, call = caller_env()) { # check for proper columns if (sum(colnames(x) == "estimate") != 1) { cli_abort( - "The tibble in {.arg statistics} must have a column for 'estimate'." + "The tibble in {.arg statistics} must have a column for 'estimate'.", + call = call ) } if (sum(colnames(x) == "term") != 1) { cli_abort( - "The tibble in {.arg statistics} must have a column for 'term'." + "The tibble in {.arg statistics} must have a column for 'term'.", + call = call ) } if (std_col) { std_candidates <- colnames(x) %in% std_exp if (sum(std_candidates) != 1) { - cli_abort("{.arg statistics} should select a single column for the standard error.") + cli_abort( + "{.arg statistics} should select a single column for the standard error.", + call = call + ) } } invisible(TRUE) diff --git a/tests/testthat/_snaps/bootci.md b/tests/testthat/_snaps/bootci.md index 0f91d33b..8e46e599 100644 --- a/tests/testthat/_snaps/bootci.md +++ b/tests/testthat/_snaps/bootci.md @@ -123,7 +123,7 @@ Code int_t(bad_bt_norm, stats) Condition - Error in `check_statistics_names()`: + Error in `check_tidy()`: ! `statistics` should select a single column for the standard error. --- @@ -179,7 +179,7 @@ Code int_pctl(badder_bt_norm, bad_term) Condition - Error in `check_statistics_names()`: + Error in `check_tidy()`: ! The tibble in `statistics` must have a column for 'term'. --- @@ -187,7 +187,7 @@ Code int_t(badder_bt_norm, bad_err) Condition - Error in `check_statistics_names()`: + Error in `check_tidy()`: ! `statistics` should select a single column for the standard error. --- @@ -195,7 +195,7 @@ Code int_bca(badder_bt_norm, bad_est, .fn = get_stats) Condition - Error in `check_statistics_names()`: + Error in `check_tidy()`: ! The tibble in `statistics` must have a column for 'estimate'. --- @@ -229,7 +229,7 @@ Code int_t(rs_boot_missing_term, stats) Condition - Error in `check_statistics_names()`: + Error in `check_tidy()`: ! The tibble in `statistics` must have a column for 'term'. --- @@ -237,7 +237,7 @@ Code int_t(rs_boot_missing_estimate, stats) Condition - Error in `check_statistics_names()`: + Error in `check_tidy()`: ! The tibble in `statistics` must have a column for 'estimate'. --- @@ -245,6 +245,6 @@ Code int_t(rs_boot_missing_std_err, stats) Condition - Error in `check_statistics_names()`: + Error in `check_tidy()`: ! `statistics` should select a single column for the standard error. From 69d15df8011788f191992647ca3a81c9860e13d1 Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 11:59:27 +0100 Subject: [PATCH 08/16] rename to be more specific --- R/bootci.R | 8 ++++---- tests/testthat/_snaps/bootci.md | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/R/bootci.R b/R/bootci.R index e368dad7..1848d7f0 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -48,7 +48,7 @@ check_statistics_names <- function(x, std_col, call = caller_env()) { invisible(TRUE) } -check_tidy <- function(x, std_col = FALSE) { +check_statistics <- function(x, std_col = FALSE) { if (!is.list(x)) { rlang::abort(statistics_format_error) } @@ -300,7 +300,7 @@ int_pctl.bootstraps <- function(.data, statistics, alpha = 0.05, ...) { rlang::abort(statistics_format_error) } stats <- .data[[column_name]] - stats <- check_tidy(stats, std_col = FALSE) + stats <- check_statistics(stats, std_col = FALSE) check_num_resamples(stats, B = 1000) @@ -383,7 +383,7 @@ int_t.bootstraps <- function(.data, statistics, alpha = 0.05, ...) { cli_abort(statistics_format_error) } stats <- .data %>% dplyr::select(!!column_name, id) - stats <- check_tidy(stats, std_col = TRUE) + stats <- check_statistics(stats, std_col = TRUE) check_num_resamples(stats, B = 500) @@ -494,7 +494,7 @@ int_bca.bootstraps <- function(.data, statistics, alpha = 0.05, .fn, ...) { cli_abort(statistics_format_error) } stats <- .data %>% dplyr::select(!!column_name, id, dplyr::starts_with(".")) - stats <- check_tidy(stats) + stats <- check_statistics(stats) check_num_resamples(stats, B = 1000) diff --git a/tests/testthat/_snaps/bootci.md b/tests/testthat/_snaps/bootci.md index 8e46e599..b2b893b5 100644 --- a/tests/testthat/_snaps/bootci.md +++ b/tests/testthat/_snaps/bootci.md @@ -59,7 +59,7 @@ Code int_pctl(bt_small, id) Condition - Error in `check_tidy()`: + Error in `check_statistics()`: ! `statistics` should select a list column of tidy results. --- @@ -67,7 +67,7 @@ Code int_pctl(bt_small, junk) Condition - Error in `check_tidy()`: + Error in `check_statistics()`: ! `statistics` should select a list column of tidy results. --- @@ -123,7 +123,7 @@ Code int_t(bad_bt_norm, stats) Condition - Error in `check_tidy()`: + Error in `check_statistics()`: ! `statistics` should select a single column for the standard error. --- @@ -179,7 +179,7 @@ Code int_pctl(badder_bt_norm, bad_term) Condition - Error in `check_tidy()`: + Error in `check_statistics()`: ! The tibble in `statistics` must have a column for 'term'. --- @@ -187,7 +187,7 @@ Code int_t(badder_bt_norm, bad_err) Condition - Error in `check_tidy()`: + Error in `check_statistics()`: ! `statistics` should select a single column for the standard error. --- @@ -195,7 +195,7 @@ Code int_bca(badder_bt_norm, bad_est, .fn = get_stats) Condition - Error in `check_tidy()`: + Error in `check_statistics()`: ! The tibble in `statistics` must have a column for 'estimate'. --- @@ -229,7 +229,7 @@ Code int_t(rs_boot_missing_term, stats) Condition - Error in `check_tidy()`: + Error in `check_statistics()`: ! The tibble in `statistics` must have a column for 'term'. --- @@ -237,7 +237,7 @@ Code int_t(rs_boot_missing_estimate, stats) Condition - Error in `check_tidy()`: + Error in `check_statistics()`: ! The tibble in `statistics` must have a column for 'estimate'. --- @@ -245,6 +245,6 @@ Code int_t(rs_boot_missing_std_err, stats) Condition - Error in `check_tidy()`: + Error in `check_statistics()`: ! `statistics` should select a single column for the standard error. From 4c748b46d06e49d41cf2fb4b1e9dc05f887bc6eb Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 12:01:50 +0100 Subject: [PATCH 09/16] thread call through to `check_statistics()` --- R/bootci.R | 8 ++++---- tests/testthat/_snaps/bootci.md | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/R/bootci.R b/R/bootci.R index 1848d7f0..c2844775 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -48,9 +48,9 @@ check_statistics_names <- function(x, std_col, call = caller_env()) { invisible(TRUE) } -check_statistics <- function(x, std_col = FALSE) { +check_statistics <- function(x, std_col = FALSE, call = caller_env()) { if (!is.list(x)) { - rlang::abort(statistics_format_error) + cli_abort(statistics_format_error, call = call) } # convert to data frame from list @@ -63,10 +63,10 @@ check_statistics <- function(x, std_col = FALSE) { } if (inherits(x, "try-error")) { - cli_abort(statistics_format_error) + cli_abort(statistics_format_error, call = call) } - check_statistics_names(x, std_col) + check_statistics_names(x, std_col, call = call) if (std_col) { std_candidates <- colnames(x) %in% std_exp diff --git a/tests/testthat/_snaps/bootci.md b/tests/testthat/_snaps/bootci.md index b2b893b5..5351db7b 100644 --- a/tests/testthat/_snaps/bootci.md +++ b/tests/testthat/_snaps/bootci.md @@ -59,7 +59,7 @@ Code int_pctl(bt_small, id) Condition - Error in `check_statistics()`: + Error in `int_pctl()`: ! `statistics` should select a list column of tidy results. --- @@ -67,7 +67,7 @@ Code int_pctl(bt_small, junk) Condition - Error in `check_statistics()`: + Error in `int_pctl()`: ! `statistics` should select a list column of tidy results. --- @@ -123,7 +123,7 @@ Code int_t(bad_bt_norm, stats) Condition - Error in `check_statistics()`: + Error in `int_t()`: ! `statistics` should select a single column for the standard error. --- @@ -179,7 +179,7 @@ Code int_pctl(badder_bt_norm, bad_term) Condition - Error in `check_statistics()`: + Error in `int_pctl()`: ! The tibble in `statistics` must have a column for 'term'. --- @@ -187,7 +187,7 @@ Code int_t(badder_bt_norm, bad_err) Condition - Error in `check_statistics()`: + Error in `int_t()`: ! `statistics` should select a single column for the standard error. --- @@ -195,7 +195,7 @@ Code int_bca(badder_bt_norm, bad_est, .fn = get_stats) Condition - Error in `check_statistics()`: + Error in `int_bca()`: ! The tibble in `statistics` must have a column for 'estimate'. --- @@ -229,7 +229,7 @@ Code int_t(rs_boot_missing_term, stats) Condition - Error in `check_statistics()`: + Error in `int_t()`: ! The tibble in `statistics` must have a column for 'term'. --- @@ -237,7 +237,7 @@ Code int_t(rs_boot_missing_estimate, stats) Condition - Error in `check_statistics()`: + Error in `int_t()`: ! The tibble in `statistics` must have a column for 'estimate'. --- @@ -245,6 +245,6 @@ Code int_t(rs_boot_missing_std_err, stats) Condition - Error in `check_statistics()`: + Error in `int_t()`: ! `statistics` should select a single column for the standard error. From 1d4955769d11768d9f8af0080cc53058d77bce60 Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 12:04:34 +0100 Subject: [PATCH 10/16] improve check on ellipses arg for `.fn` arg --- R/bootci.R | 6 +++--- tests/testthat/_snaps/bootci.md | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/bootci.R b/R/bootci.R index c2844775..5a07df1f 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -123,10 +123,10 @@ new_stats <- function(x, lo, hi) { tibble(.lower = min(res), .estimate = mean(x, na.rm = TRUE), .upper = max(res)) } -has_dots <- function(x) { +check_has_dots <- function(x, call = caller_env()) { nms <- names(formals(x)) if (!any(nms == "...")) { - cli_abort("{.arg .fn} must have an argument {.arg ...}.") + cli_abort("{.arg .fn} must have an argument {.arg ...}.", call = call) } invisible(NULL) } @@ -487,7 +487,7 @@ int_bca.bootstraps <- function(.data, statistics, alpha = 0.05, .fn, ...) { cli_abort("{.arg alpha} must be a single numeric value.") } - has_dots(.fn) + check_has_dots(.fn) column_name <- tidyselect::vars_select(names(.data), !!enquo(statistics)) if (length(column_name) != 1) { diff --git a/tests/testthat/_snaps/bootci.md b/tests/testthat/_snaps/bootci.md index 5351db7b..9d870798 100644 --- a/tests/testthat/_snaps/bootci.md +++ b/tests/testthat/_snaps/bootci.md @@ -131,7 +131,7 @@ Code int_bca(bt_norm, stats, .fn = no_dots) Condition - Error in `has_dots()`: + Error in `int_bca()`: ! `.fn` must have an argument `...`. --- From 970b396133892f0e51ac508e7cc7960e0b396e3f Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 12:08:03 +0100 Subject: [PATCH 11/16] thread call through to `check_num_resamples()` --- R/bootci.R | 7 +++++-- tests/testthat/_snaps/bootci.md | 10 +++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/R/bootci.R b/R/bootci.R index 5a07df1f..e70f378c 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -131,7 +131,7 @@ check_has_dots <- function(x, call = caller_env()) { invisible(NULL) } -check_num_resamples <- function(x, B = 1000) { +check_num_resamples <- function(x, B = 1000, call = caller_env()) { x <- x %>% dplyr::group_by(term) %>% @@ -140,7 +140,10 @@ check_num_resamples <- function(x, B = 1000) { if (nrow(x) > 0) { terms <- x$term - cli_warn("Recommend at least {B} non-missing bootstrap resamples for {cli::qty(terms)} term{?s} {.code {terms}}.") + cli_warn( + "Recommend at least {B} non-missing bootstrap resamples for {cli::qty(terms)} term{?s} {.code {terms}}.", + call = call + ) } invisible(NULL) } diff --git a/tests/testthat/_snaps/bootci.md b/tests/testthat/_snaps/bootci.md index 9d870798..88146544 100644 --- a/tests/testthat/_snaps/bootci.md +++ b/tests/testthat/_snaps/bootci.md @@ -3,7 +3,7 @@ Code int_pctl(bt_resamples, res) Condition - Warning: + Warning in `int_pctl()`: Recommend at least 1000 non-missing bootstrap resamples for term `mean`. Error in `pctl_single()`: ! All statistics have missing values. @@ -13,7 +13,7 @@ Code int_t(bt_resamples, res) Condition - Warning: + Warning in `int_t()`: Recommend at least 500 non-missing bootstrap resamples for term `mean`. Error in `t_single()`: ! All statistics have missing values. @@ -23,7 +23,7 @@ Code int_bca(bt_resamples, res, .fn = bad_stats) Condition - Warning: + Warning in `int_bca()`: Recommend at least 1000 non-missing bootstrap resamples for term `mean`. Error in `bca_calc()`: ! All statistics have missing values. @@ -33,7 +33,7 @@ Code int_pctl(bt_small, stats) Condition - Warning: + Warning in `int_pctl()`: Recommend at least 1000 non-missing bootstrap resamples for term `mean`. Output # A tibble: 1 x 6 @@ -46,7 +46,7 @@ Code int_t(bt_small, stats) Condition - Warning: + Warning in `int_t()`: Recommend at least 500 non-missing bootstrap resamples for term `mean`. Output # A tibble: 1 x 6 From 1d368ab9d7e7b788eb2e38fb313a81790e133a2a Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 13:09:18 +0100 Subject: [PATCH 12/16] standard check for `alpha` --- R/bootci.R | 12 +++--------- tests/testthat/_snaps/bootci.md | 6 +++--- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/R/bootci.R b/R/bootci.R index e70f378c..c51def5a 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -292,9 +292,7 @@ int_pctl <- function(.data, ...) { #' @rdname int_pctl int_pctl.bootstraps <- function(.data, statistics, alpha = 0.05, ...) { check_dots_empty() - if (length(alpha) != 1 || !is.numeric(alpha)) { - cli_abort("{.arg alpha} must be a single numeric value.") - } + check_number_decimal(alpha, min = 0, max = 1) .data <- .data %>% dplyr::filter(id != "Apparent") @@ -377,9 +375,7 @@ int_t <- function(.data, ...) { int_t.bootstraps <- function(.data, statistics, alpha = 0.05, ...) { check_dots_empty() check_includes_apparent(.data) - if (length(alpha) != 1 || !is.numeric(alpha)) { - cli_abort("{.arg alpha} must be a single numeric value.") - } + check_number_decimal(alpha, min = 0, max = 1) column_name <- tidyselect::vars_select(names(.data), !!enquo(statistics)) if (length(column_name) != 1) { @@ -486,9 +482,7 @@ int_bca <- function(.data, ...) { #' @export int_bca.bootstraps <- function(.data, statistics, alpha = 0.05, .fn, ...) { check_includes_apparent(.data) - if (length(alpha) != 1 || !is.numeric(alpha)) { - cli_abort("{.arg alpha} must be a single numeric value.") - } + check_number_decimal(alpha, min = 0, max = 1) check_has_dots(.fn) diff --git a/tests/testthat/_snaps/bootci.md b/tests/testthat/_snaps/bootci.md index 88146544..da2a8821 100644 --- a/tests/testthat/_snaps/bootci.md +++ b/tests/testthat/_snaps/bootci.md @@ -76,7 +76,7 @@ int_pctl(bt_small, stats, alpha = c(0.05, 0.2)) Condition Error in `int_pctl()`: - ! `alpha` must be a single numeric value. + ! `alpha` must be a number, not a double vector. --- @@ -84,7 +84,7 @@ int_t(bt_small, stats, alpha = "potato") Condition Error in `int_t()`: - ! `alpha` must be a single numeric value. + ! `alpha` must be a number, not the string "potato". --- @@ -92,7 +92,7 @@ int_bca(bt_small, stats, alpha = 1:2, .fn = get_stats) Condition Error in `int_bca()`: - ! `alpha` must be a single numeric value. + ! `alpha` must be a number, not an integer vector. --- From d6b2868fb4071c2cab139f5e23f5b9037561f2fe Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 13:17:59 +0100 Subject: [PATCH 13/16] Improve error message `stats` is not a user-facing argument and if we say "vector" we should probably rather talk about a column in the tidy tibble and that's rather long. --- R/bootci.R | 2 +- tests/testthat/_snaps/bootci.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/bootci.R b/R/bootci.R index c51def5a..5d84bf9e 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -158,7 +158,7 @@ pctl_single <- function(stats, alpha = 0.05) { } if (!is.numeric(stats)) { - cli_abort("{.arg stats} must be a numeric vector.") + cli_abort("All statistics must be numeric.") } # stats is a numeric vector of values diff --git a/tests/testthat/_snaps/bootci.md b/tests/testthat/_snaps/bootci.md index da2a8821..93ab8f4e 100644 --- a/tests/testthat/_snaps/bootci.md +++ b/tests/testthat/_snaps/bootci.md @@ -204,7 +204,7 @@ int_pctl(badder_bt_norm, bad_num) Condition Error in `pctl_single()`: - ! `stats` must be a numeric vector. + ! All statistics must be numeric. # checks for apparent bootstrap From 3912355492b70ba132d6e018cc485cc986a3b8b0 Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 13:25:10 +0100 Subject: [PATCH 14/16] don't thread through `dplyr::do()` --- R/bootci.R | 8 ++++---- tests/testthat/_snaps/bootci.md | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/R/bootci.R b/R/bootci.R index 5d84bf9e..04b36fa6 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -154,11 +154,11 @@ check_num_resamples <- function(x, B = 1000, call = caller_env()) { pctl_single <- function(stats, alpha = 0.05) { if (all(is.na(stats))) { - cli_abort("All statistics have missing values.") + cli_abort("All statistics have missing values.", call = call2("int_pctl")) } if (!is.numeric(stats)) { - cli_abort("All statistics must be numeric.") + cli_abort("All statistics must be numeric.", call = call2("int_pctl")) } # stats is a numeric vector of values @@ -298,7 +298,7 @@ int_pctl.bootstraps <- function(.data, statistics, alpha = 0.05, ...) { column_name <- tidyselect::vars_select(names(.data), !!rlang::enquo(statistics)) if (length(column_name) != 1) { - rlang::abort(statistics_format_error) + cli_abort(statistics_format_error) } stats <- .data[[column_name]] stats <- check_statistics(stats, std_col = FALSE) @@ -325,7 +325,7 @@ t_single <- function(stats, std_err, is_orig, alpha = 0.05) { # which_orig is the index of stats and std_err that has the original result if (all(is.na(stats))) { - cli_abort("All statistics have missing values.") + cli_abort("All statistics have missing values.", call = call2("int_t")) } if (!is.logical(is_orig) || any(is.na(is_orig))) { diff --git a/tests/testthat/_snaps/bootci.md b/tests/testthat/_snaps/bootci.md index 93ab8f4e..5ba973aa 100644 --- a/tests/testthat/_snaps/bootci.md +++ b/tests/testthat/_snaps/bootci.md @@ -5,7 +5,7 @@ Condition Warning in `int_pctl()`: Recommend at least 1000 non-missing bootstrap resamples for term `mean`. - Error in `pctl_single()`: + Error in `int_pctl()`: ! All statistics have missing values. --- @@ -15,7 +15,7 @@ Condition Warning in `int_t()`: Recommend at least 500 non-missing bootstrap resamples for term `mean`. - Error in `t_single()`: + Error in `int_t()`: ! All statistics have missing values. --- @@ -203,7 +203,7 @@ Code int_pctl(badder_bt_norm, bad_num) Condition - Error in `pctl_single()`: + Error in `int_pctl()`: ! All statistics must be numeric. # checks for apparent bootstrap From 21943e564802bb505a1fd665901b4dc38b40be23 Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 13:47:02 +0100 Subject: [PATCH 15/16] thread call through to `bca_calc()` and switch from `cat()` to cli --- R/bootci.R | 6 +++--- tests/testthat/_snaps/bootci.md | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/bootci.R b/R/bootci.R index 04b36fa6..50b49d3f 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -398,11 +398,11 @@ int_t.bootstraps <- function(.data, statistics, alpha = 0.05, ...) { # ---------------------------------------------------------------- -bca_calc <- function(stats, orig_data, alpha = 0.05, .fn, ...) { +bca_calc <- function(stats, orig_data, alpha = 0.05, .fn, ..., call = caller_env()) { # TODO check per term if (all(is.na(stats$estimate))) { - cli_abort("All statistics have missing values.") + cli_abort("All statistics have missing values.", call = call) } stat_groups_chr <- c("term", grep("^\\.", names(stats), value = TRUE)) @@ -420,7 +420,7 @@ bca_calc <- function(stats, orig_data, alpha = 0.05, .fn, ...) { if (inherits(loo_test, "try-error")) { cat("Running `.fn` on the LOO resamples produced an error:\n") print(loo_test) - cli_abort("{.arg .fn} failed.") + cli_abort("{.arg .fn} failed.", call = call) } loo_res <- furrr::future_map(loo_rs$splits, .fn, ...) %>% list_rbind() diff --git a/tests/testthat/_snaps/bootci.md b/tests/testthat/_snaps/bootci.md index 5ba973aa..debba451 100644 --- a/tests/testthat/_snaps/bootci.md +++ b/tests/testthat/_snaps/bootci.md @@ -25,7 +25,7 @@ Condition Warning in `int_bca()`: Recommend at least 1000 non-missing bootstrap resamples for term `mean`. - Error in `bca_calc()`: + Error in `int_bca()`: ! All statistics have missing values. # Sufficient replications needed to sufficiently reduce Monte Carlo sampling Error for BCa method From 2ca6b5e77ea698a0ab37633505d4330f87a04271 Mon Sep 17 00:00:00 2001 From: Hannah Frick Date: Wed, 25 Sep 2024 13:47:17 +0100 Subject: [PATCH 16/16] switch to cli from `cat()` --- NAMESPACE | 1 + R/bootci.R | 2 +- R/rsample-package.R | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 524aa0bc..111918b6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -417,6 +417,7 @@ export(vfold_cv) import(rlang) import(vctrs) importFrom(cli,cli_abort) +importFrom(cli,cli_text) importFrom(cli,cli_warn) importFrom(dplyr,"%>%") importFrom(dplyr,arrange) diff --git a/R/bootci.R b/R/bootci.R index 50b49d3f..18490f23 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -418,7 +418,7 @@ bca_calc <- function(stats, orig_data, alpha = 0.05, .fn, ..., call = caller_env # To test, we run on the first LOO data set and see if it is a vector or df loo_test <- try(rlang::exec(.fn, loo_rs$splits[[1]], ...), silent = TRUE) if (inherits(loo_test, "try-error")) { - cat("Running `.fn` on the LOO resamples produced an error:\n") + cli_text("Running {.fn .fn} on the LOO resamples produced an error:") print(loo_test) cli_abort("{.arg .fn} failed.", call = call) } diff --git a/R/rsample-package.R b/R/rsample-package.R index a2e546af..68110391 100644 --- a/R/rsample-package.R +++ b/R/rsample-package.R @@ -4,7 +4,7 @@ ## usethis namespace: start #' @import rlang #' @importFrom lifecycle deprecated -#' @importFrom cli cli_abort cli_warn +#' @importFrom cli cli_abort cli_warn cli_text #' @importFrom utils globalVariables #' @importFrom purrr map map2 map_dbl pluck map_lgl list_rbind #' @importFrom tibble tibble is_tibble as_tibble obj_sum