Skip to content

Commit

Permalink
Merge pull request #552 from tidymodels/make_strata-warnings
Browse files Browse the repository at this point in the history
Switch to cli for `make_strata()`
  • Loading branch information
hfrick authored Sep 27, 2024
2 parents 580a12f + 7592d3e commit f799dba
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
35 changes: 15 additions & 20 deletions R/make_strata.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,18 @@ make_strata <- function(x, breaks = 4, nunique = 5, pool = .1, depth = 20) {

## This should really be based on some combo of rate and number.
if (all(pcts < pool)) {
rlang::warn(c(
cli_warn(c(
"Too little data to stratify.",
"Resampling will be unstratified."
"*" = "Resampling will be unstratified."
))
return(factor(rep("strata1", n)))
}

if (pool < default_pool & any(pcts < default_pool)) {
rlang::warn(c(
paste0(
"Stratifying groups that make up ",
round(100 * pool), "% of the data may be ",
"statistically risky."
),
"Consider increasing `pool` to at least 0.1"
cli_warn(c(
"Stratifying groups that make up {round(100 * pool)}% of the data may
be statistically risky.",
i = "Consider increasing {.arg pool} to at least 0.1."
))
}

Expand All @@ -104,25 +101,23 @@ make_strata <- function(x, breaks = 4, nunique = 5, pool = .1, depth = 20) {
out <- factor(as.character(x))
} else {
if (breaks < 2) {
rlang::warn(c(
"The bins specified by `breaks` must be >=2.",
"Resampling will be unstratified."
cli_warn(c(
"The bins specified by {.arg breaks} must be >=2.",
"*" = "Resampling will be unstratified."
))
return(factor(rep("strata1", n)))
} else if (floor(n / breaks) < depth) {
rlang::warn(c(
paste0(
"The number of observations in each quantile is ",
"below the recommended threshold of ", depth, "."
),
paste0("Stratification will use ", floor(n / depth), " breaks instead.")
cli_warn(c(
"The number of observations in each quantile is below the recommended
threshold of {depth}.",
"*" = "Stratification will use {floor(n / depth)} breaks instead."
))
}
breaks <- min(breaks, floor(n / depth))
if (breaks < 2) {
rlang::warn(c(
cli_warn(c(
"Too little data to stratify.",
"Resampling will be unstratified."
"*" = "Resampling will be unstratified."
))
return(factor(rep("strata1", n)))
}
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/make_strata.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
Condition
Warning:
Stratifying groups that make up 5% of the data may be statistically risky.
* Consider increasing `pool` to at least 0.1
i Consider increasing `pool` to at least 0.1.

---

Expand All @@ -23,7 +23,7 @@
Condition
Warning:
Stratifying groups that make up 5% of the data may be statistically risky.
* Consider increasing `pool` to at least 0.1
i Consider increasing `pool` to at least 0.1.

# bad data

Expand All @@ -41,7 +41,7 @@
Condition
Warning:
Stratifying groups that make up 6% of the data may be statistically risky.
* Consider increasing `pool` to at least 0.1
i Consider increasing `pool` to at least 0.1.

---

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/vfold.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Condition
Warning:
Stratifying groups that make up 1% of the data may be statistically risky.
* Consider increasing `pool` to at least 0.1
i Consider increasing `pool` to at least 0.1.

# strata arg is checked

Expand Down

0 comments on commit f799dba

Please sign in to comment.