Skip to content

Commit

Permalink
Rename use_ecdf to recalc_probs in chop_quantiles() and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
hughjonesd committed Jun 10, 2024
1 parent adf46d1 commit 3f2feb8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
5 changes: 3 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
* When multiple quantiles are the same, santoku warns and returns the leftmost
quantile interval. Before it would merge the intervals, creating labels that
might be different to what the user asked for.
* `chop_quantiles()` gains a `use_ecdf` argument. `use_ecdf = TRUE` recalculates
probabilities using `ecdf(x)`, which may give more accurate interval labels.
* `chop_quantiles()` gains a `recalc_probs` argument. `recalc_probs = TRUE`
recalculates probabilities using `ecdf(x)`, which may give more accurate
interval labels.
* `single = NULL` has been documented explicitly in `lbl_*` functions.


Expand Down
12 changes: 6 additions & 6 deletions R/breaks-by-group-size.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#'
#' @export
#' @order 2
brk_quantiles <- function (probs, ..., weights = NULL, use_ecdf = FALSE) {
brk_quantiles <- function (probs, ..., weights = NULL, recalc_probs = FALSE) {
assert_that(
is.numeric(probs),
noNA(probs),
all(probs >= 0),
all(probs <= 1),
is.null(weights) || is.numeric(weights),
is.flag(use_ecdf)
is.flag(recalc_probs)
)
probs <- sort(probs)

Expand All @@ -33,7 +33,7 @@ brk_quantiles <- function (probs, ..., weights = NULL, use_ecdf = FALSE) {
if (anyNA(qs)) return(empty_breaks()) # data was all NA

if (any(duplicated(qs))) {
if (! use_ecdf) {
if (! recalc_probs) {
warning("`x` has duplicate quantiles: break labels may be misleading")
}
# We use the left-most probabilities, so e.g. if 0%, 20% and 40% quantiles
Expand All @@ -54,7 +54,7 @@ brk_quantiles <- function (probs, ..., weights = NULL, use_ecdf = FALSE) {

class(breaks) <- c("quantileBreaks", class(breaks))

if (use_ecdf) {
if (recalc_probs) {
probs <- calculate_ecdf_probs(x, breaks, weights)
}

Expand All @@ -79,10 +79,10 @@ brk_quantiles <- function (probs, ..., weights = NULL, use_ecdf = FALSE) {
#' @noRd
calculate_ecdf_probs <- function (x, breaks, weights) {
if (! is.numeric(x)) {
stop("`use_ecdf = TRUE` can only be used with numeric `x`")
stop("`recalc_probs = TRUE` can only be used with numeric `x`")
}
if (! is.null(weights)) {
stop("`use_ecdf = TRUE` cannot be used with non-null `weights`")
stop("`recalc_probs = TRUE` cannot be used with non-null `weights`")
}

brk_vec <- unclass_breaks(breaks)
Expand Down
12 changes: 6 additions & 6 deletions R/chop-by-group-size.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' passed to [stats::quantile()] or [Hmisc::wtd.quantile()].
#' @param weights `NULL` or numeric vector of same length as `x`. If not
#' `NULL`, [Hmisc::wtd.quantile()] is used to calculate weighted quantiles.
#' @param use_ecdf Logical. Recalculate probabilities of quantiles using
#' @param recalc_probs Logical. Recalculate probabilities of quantiles using
#' [`ecdf(x)`][stats::ecdf()]? See below.
#'
#' @inheritParams chop
Expand All @@ -25,8 +25,8 @@
#' labels. To show the numeric quantiles themselves, set `raw = TRUE`.
#'
#' When `x` contains duplicates, consecutive quantiles may be the same number. If
#' so, interval labels may be misleading, and if `use_ecdf = FALSE` a warning is
#' emitted. Set `use_ecdf = TRUE` to recalculate the probabilities of the quantiles
#' so, interval labels may be misleading, and if `recalc_probs = FALSE` a warning is
#' emitted. Set `recalc_probs = TRUE` to recalculate the probabilities of the quantiles
#' using the [empirical cumulative distribution function][stats::ecdf()] of `x`.
#' Doing so may give you different labels from what you expect, and will
#' remove any names from `probs`. See the example below.
Expand All @@ -52,7 +52,7 @@
#' x <- c(1, 1, 1, 2, 3)
#' quantile(x, 1:5/5)
#' tab_quantiles(x, 1:5/5)
#' tab_quantiles(x, 1:5/5, use_ecdf = TRUE)
#' tab_quantiles(x, 1:5/5, recalc_probs = TRUE)
chop_quantiles <- function(
x,
probs,
Expand All @@ -62,9 +62,9 @@ chop_quantiles <- function(
left = is.numeric(x),
raw = FALSE,
weights = NULL,
use_ecdf = FALSE
recalc_probs = FALSE
) {
chop(x, brk_quantiles(probs, weights = weights, use_ecdf = use_ecdf),
chop(x, brk_quantiles(probs, weights = weights, recalc_probs = recalc_probs),
labels = labels, ..., left = left, raw = raw)
}

Expand Down
12 changes: 6 additions & 6 deletions man/chop_quantiles.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-breaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ test_that("brk_quantiles with duplicate quantiles", {
expect_equivalent(c(brks), c(1.0, 1.0, 1.4, 2.2, 3.0))

expect_silent(
brks <- brk_quantiles(0:5/5, use_ecdf = TRUE)(x, FALSE, TRUE, FALSE)
brks <- brk_quantiles(0:5/5, recalc_probs = TRUE)(x, FALSE, TRUE, FALSE)
)
expect_equivalent(c(brks), c(1.0, 1.0, 1.4, 2.2, 3.0))
})
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-chop.R
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ test_that("chop_quantiles", {

x <- c(1, 1, 1, 2, 3)
expect_equivalent(
chop_quantiles(x, 1:4/5, use_ecdf = TRUE),
chop_quantiles(x, 1:4/5, recalc_probs = TRUE),
factor(c("[0%, 60%]", "[0%, 60%]", "[0%, 60%]", "[60%, 80%)", "[80%, 100%]"))
)

Expand Down

0 comments on commit 3f2feb8

Please sign in to comment.