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

cli updates for bootci.R #516

Merged
merged 14 commits into from
Sep 11, 2024
46 changes: 20 additions & 26 deletions R/bootci.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

check_rset <- function(x, app = TRUE) {
if (!inherits(x, "bootstraps")) {
rlang::abort("`.data` should be an `rset` object generated from `bootstraps()`")
cli::cli_abort("`.data` should be an `rset` object generated from `bootstraps()`")
}

if (app) {
if (x %>% dplyr::filter(id == "Apparent") %>% nrow() != 1) {
rlang::abort("Please set `apparent = TRUE` in `bootstraps()` function")
cli::cli_abort("Please set `apparent = TRUE` in `bootstraps()` function")
}
}
invisible(NULL)
Expand All @@ -28,15 +28,15 @@ std_exp <- c("std.error", "robust.se")
check_tidy_names <- function(x, std_col) {
# check for proper columns
if (sum(colnames(x) == "estimate") != 1) {
rlang::abort(stat_nm_err)
cli::cli_abort(stat_nm_err)
}
if (sum(colnames(x) == "term") != 1) {
rlang::abort(stat_nm_err)
cli::cli_abort(stat_nm_err)
}
if (std_col) {
std_candidates <- colnames(x) %in% std_exp
if (sum(std_candidates) != 1) {
rlang::abort(
cli::cli_abort(
"`statistics` should select a single column for the standard error."
)
}
Expand All @@ -59,7 +59,7 @@ check_tidy <- function(x, std_col = FALSE) {
}

if (inherits(x, "try-error")) {
rlang::abort(stat_fmt_err)
cli::cli_abort(stat_fmt_err)
}

check_tidy_names(x, std_col)
Expand Down Expand Up @@ -117,7 +117,7 @@ new_stats <- function(x, lo, hi) {
has_dots <- function(x) {
nms <- names(formals(x))
if (!any(nms == "...")) {
rlang::abort("`.fn` must have an argument `...`.")
cli::cli_abort("`.fn` must have an argument `...`.")
}
invisible(NULL)
}
Expand All @@ -131,14 +131,8 @@ check_num_resamples <- function(x, B = 1000) {

if (nrow(x) > 0) {
terms <- paste0("`", x$term, "`")
msg <-
paste0(
"Recommend at least ", B, " non-missing bootstrap resamples for ",
ifelse(length(terms) > 1, "terms: ", "term "),
paste0(terms, collapse = ", "),
"."
)
rlang::warn(msg)
cli::cli_warn(paste0("Recommend at least {B} non-missing bootstrap ",
"resamples for {cli::qty(terms)} term{?s} {terms}."))
}
invisible(NULL)
}
Expand All @@ -149,11 +143,11 @@ check_num_resamples <- function(x, B = 1000) {

pctl_single <- function(stats, alpha = 0.05) {
if (all(is.na(stats))) {
rlang::abort("All statistics have missing values..")
cli::cli_abort("All statistics have missing values.")
}

if (!is.numeric(stats)) {
rlang::abort("`stats` must be a numeric vector.")
cli::cli_abort("`stats` must be a numeric vector.")
}

# stats is a numeric vector of values
Expand Down Expand Up @@ -289,19 +283,19 @@ 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))) {
rlang::abort("All statistics have missing values.")
cli::cli_abort("All statistics have missing values.")
}

if (!is.logical(is_orig) || any(is.na(is_orig))) {
rlang::abort(
cli::cli_abort(
"`is_orig` should be a logical column the same length as `stats` with no missing values."
)
}
if (length(stats) != length(std_err) && length(stats) != length(is_orig)) {
rlang::abort("`stats`, `std_err`, and `is_orig` should have the same length.")
cli::cli_abort("`stats`, `std_err`, and `is_orig` should have the same length.")
}
if (sum(is_orig) != 1) {
rlang::abort("The original statistic must be in a single row.")
cli::cli_abort("The original statistic must be in a single row.")
}

theta_obs <- stats[is_orig]
Expand Down Expand Up @@ -339,12 +333,12 @@ int_t.bootstraps <- function(.data, statistics, alpha = 0.05, ...) {
check_dots_empty()
check_rset(.data)
if (length(alpha) != 1 || !is.numeric(alpha)) {
abort("`alpha` must be a single numeric value.")
cli::cli_abort("`alpha` must be a single numeric value.")
}

column_name <- tidyselect::vars_select(names(.data), !!enquo(statistics))
if (length(column_name) != 1) {
rlang::abort(stat_fmt_err)
cli::cli_abort(stat_fmt_err)
}
stats <- .data %>% dplyr::select(!!column_name, id)
stats <- check_tidy(stats, std_col = TRUE)
Expand All @@ -366,7 +360,7 @@ bca_calc <- function(stats, orig_data, alpha = 0.05, .fn, ...) {

# TODO check per term
if (all(is.na(stats$estimate))) {
rlang::abort("All statistics have missing values.")
cli::cli_abort("All statistics have missing values.")
}

### Estimating Z0 bias-correction
Expand Down Expand Up @@ -440,14 +434,14 @@ int_bca <- function(.data, ...) {
int_bca.bootstraps <- 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.")
cli::cli_abort("`alpha` must be a single numeric value.")
}

has_dots(.fn)

column_name <- tidyselect::vars_select(names(.data), !!enquo(statistics))
if (length(column_name) != 1) {
rlang::abort(stat_fmt_err)
cli::cli_abort(stat_fmt_err)
}
stats <- .data %>% dplyr::select(!!column_name, id)
stats <- check_tidy(stats)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/bootci.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Warning:
Recommend at least 1000 non-missing bootstrap resamples for term `mean`.
Error in `pctl_single()`:
! All statistics have missing values..
! All statistics have missing values.

---

Expand Down
Loading