From e1f149d13b23567c796d1ca49718a6fa677efd77 Mon Sep 17 00:00:00 2001 From: Julia Silge Date: Mon, 14 Sep 2020 17:39:15 -0600 Subject: [PATCH] Validate single numeric value for alpha argument (#184) * Validate single numeric value for alpha argument * Update NEWS for more bootstrap CI alpha fixing --- NEWS.md | 2 +- R/bootci.R | 9 +++++++++ tests/testthat/test_bootci.R | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 5885cd58..0395832e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,7 +7,7 @@ `sliding_index()`, and `sliding_period()`, which have more flexibility than the pre-existing `rolling_origin()`. -* Correct passing `alpha` parameter for `int_bca()` (#179). +* Correct `alpha` parameter handling for bootstrap CI functions (#179, #184). # rsample 0.0.7 diff --git a/R/bootci.R b/R/bootci.R index 23e0816f..bea46097 100644 --- a/R/bootci.R +++ b/R/bootci.R @@ -239,6 +239,9 @@ pctl_single <- function(stats, alpha = 0.05) { int_pctl <- function(.data, statistics, alpha = 0.05) { check_rset(.data, app = FALSE) + if (length(alpha) != 1 || !is.numeric(alpha)) { + abort("`alpha` must be a single numeric value.") + } .data <- .data %>% dplyr::filter(id != "Apparent") @@ -312,6 +315,9 @@ t_single <- function(stats, std_err, is_orig, alpha = 0.05) { int_t <- function(.data, statistics, alpha = 0.05) { check_rset(.data) + if (length(alpha) != 1 || !is.numeric(alpha)) { + abort("`alpha` must be a single numeric value.") + } column_name <- tidyselect::vars_select(names(.data), !!enquo(statistics)) if (length(column_name) != 1) { @@ -410,6 +416,9 @@ bca_calc <- function(stats, orig_data, alpha = 0.05, .fn, ...) { int_bca <- function(.data, statistics, alpha = 0.05, .fn, ...) { check_rset(.data) + if (length(alpha) != 1 || !is.numeric(alpha)) { + abort("`alpha` must be a single numeric value.") + } has_dots(.fn) diff --git a/tests/testthat/test_bootci.R b/tests/testthat/test_bootci.R index 66d45018..794f12bf 100644 --- a/tests/testthat/test_bootci.R +++ b/tests/testthat/test_bootci.R @@ -206,6 +206,10 @@ context("boot_ci() Input Validation") test_that("bad input", { expect_error(int_pctl(bt_small, id)) expect_error(int_pctl(bt_small, junk)) + expect_error(int_pctl(bt_small, stats, alpha = c(0.05, 0.2))) + expect_error(int_t(bt_small, stats, alpha = "potato")) + expect_error(int_bca(bt_small, stats, alpha = 1:2, .fn = get_stats)) + bad_bt_norm <- bt_norm %>%