Skip to content

Commit

Permalink
add extra sanity checks for given quantile probs
Browse files Browse the repository at this point in the history
  • Loading branch information
rsbivand committed Nov 28, 2024
1 parent 936157b commit 39734a4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
12 changes: 11 additions & 1 deletion R/classInt.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ classIntervals <- function(var, n, style="quantile", rtimes=3, ..., intervalClos
} else if (style =="quantile") {
# stats
dots <- list(...)
probs <- seq(0,1,1/n)
probs <- seq(0, 1, 1/n)
if (!is.null(dots$probs)) {
if (n_missing) {
probs <- dots$probs
Expand All @@ -201,6 +201,16 @@ classIntervals <- function(var, n, style="quantile", rtimes=3, ..., intervalClos
stop("both n and probs given, but length(probs)-1 != ", n)
} else probs <- dots$probs
}
r_probs <- range(probs)
if (r_probs[1] < 0 || r_probs[2] > 1)
stop("given probs range exceeds the unit interval: [",
paste(r_probs, collapse=", "), "]")
if (r_probs[1] != 0 || r_probs[2] != 1) {
warning("given probs range does not span the unit interval: [",
paste(r_probs, collapse=", "), "]")
}
if (length(unique(round(diff(probs), digits=14))) != 1L)
warning("given probs do not have equal steps")
}
na.rm <- FALSE
if (!is.null(dots$na.rm)) na.rm <- dots$na.rm
Expand Down
9 changes: 8 additions & 1 deletion inst/tinytest/test_quantile_probs.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ expect_error(classInt::classIntervals(data_censored, style = "quantile",
n = 5, probs = seq(0, 1, 0.25)))
expect_silent(classInt::classIntervals(data_censored, style = "quantile",
n = 4, probs = seq(0, 1, 0.25)))

expect_error(classInt::classIntervals(data_censored, style = "quantile",
probs = seq(-0.25, 1.25, 0.25)))
expect_warning(classInt::classIntervals(data_censored, style = "quantile",
probs = seq(0.25, 0.75, 0.25)))
expect_error(classInt::classIntervals(data_censored, style = "quantile",
probs = seq(0, 1, 0.25)-0.25))
expect_warning(classInt::classIntervals(data_censored, style = "quantile",
probs = c(0, 0.15804, 0.36603, 0.63975, 1))) # log sequence (bigsnpr::seq_log(1, 3, 5)-1)/2
2 changes: 1 addition & 1 deletion man/classIntervals.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ classIntervals2shingle(x)

The "pretty" style chooses a number of breaks not necessarily equal to n using \code{pretty}, but likely to be legible; arguments to \code{pretty} may be passed through \code{\dots}.

The "quantile" style provides quantile breaks; arguments to \code{quantile} may be passed through \code{\dots}.
The "quantile" style provides quantile breaks; arguments to \code{quantile} may be passed through \code{\dots}. Rather than giving \code{probs}, use the \code{n} argument as by default \code{probs = seq(0, 1, 1/n)}. If \code{probs} is given, it should span the unit interval [0, 1]. If given \code{probs} values do not have equal steps, consider using \code{style = "fixed"} and giving \code{"fixedBreaks"} directly.

The "kmeans" style uses \code{kmeans} to generate the breaks; it may be anchored using \code{set.seed}; the \code{pars} attribute returns the kmeans object generated; if \code{kmeans} fails, a jittered input vector containing \code{rtimes} replications of \code{var} is tried --- with few unique values in \code{var}, this can prove necessary; arguments to \code{kmeans} may be passed through \code{\dots}.

Expand Down

0 comments on commit 39734a4

Please sign in to comment.