Skip to content

Commit 39734a4

Browse files
committed
add extra sanity checks for given quantile probs
1 parent 936157b commit 39734a4

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

R/classInt.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ classIntervals <- function(var, n, style="quantile", rtimes=3, ..., intervalClos
192192
} else if (style =="quantile") {
193193
# stats
194194
dots <- list(...)
195-
probs <- seq(0,1,1/n)
195+
probs <- seq(0, 1, 1/n)
196196
if (!is.null(dots$probs)) {
197197
if (n_missing) {
198198
probs <- dots$probs
@@ -201,6 +201,16 @@ classIntervals <- function(var, n, style="quantile", rtimes=3, ..., intervalClos
201201
stop("both n and probs given, but length(probs)-1 != ", n)
202202
} else probs <- dots$probs
203203
}
204+
r_probs <- range(probs)
205+
if (r_probs[1] < 0 || r_probs[2] > 1)
206+
stop("given probs range exceeds the unit interval: [",
207+
paste(r_probs, collapse=", "), "]")
208+
if (r_probs[1] != 0 || r_probs[2] != 1) {
209+
warning("given probs range does not span the unit interval: [",
210+
paste(r_probs, collapse=", "), "]")
211+
}
212+
if (length(unique(round(diff(probs), digits=14))) != 1L)
213+
warning("given probs do not have equal steps")
204214
}
205215
na.rm <- FALSE
206216
if (!is.null(dots$na.rm)) na.rm <- dots$na.rm

inst/tinytest/test_quantile_probs.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@ expect_error(classInt::classIntervals(data_censored, style = "quantile",
88
n = 5, probs = seq(0, 1, 0.25)))
99
expect_silent(classInt::classIntervals(data_censored, style = "quantile",
1010
n = 4, probs = seq(0, 1, 0.25)))
11-
11+
expect_error(classInt::classIntervals(data_censored, style = "quantile",
12+
probs = seq(-0.25, 1.25, 0.25)))
13+
expect_warning(classInt::classIntervals(data_censored, style = "quantile",
14+
probs = seq(0.25, 0.75, 0.25)))
15+
expect_error(classInt::classIntervals(data_censored, style = "quantile",
16+
probs = seq(0, 1, 0.25)-0.25))
17+
expect_warning(classInt::classIntervals(data_censored, style = "quantile",
18+
probs = c(0, 0.15804, 0.36603, 0.63975, 1))) # log sequence (bigsnpr::seq_log(1, 3, 5)-1)/2

man/classIntervals.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ classIntervals2shingle(x)
5353

5454
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}.
5555

56-
The "quantile" style provides quantile breaks; arguments to \code{quantile} may be passed through \code{\dots}.
56+
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.
5757

5858
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}.
5959

0 commit comments

Comments
 (0)