swh:1:snp:81eadaa089e8253d8469bcef66aa332632c6c669
Tip revision: f0ba8a5d88ce30d08c8312fd9b882f2ff051d91b authored by Hans W. Borchers on 25 August 2018, 21:00:11 UTC
version 2.1.5
version 2.1.5
Tip revision: f0ba8a5
cutpoints.R
cutpoints <- function(x, nmax = 8, quant = 0.95) {
stopifnot(is.numeric(x), is.numeric(nmax), is.numeric(quant))
if (length(nmax) != 1 || floor(nmax) != ceiling(nmax) || nmax <= 0)
stop("Argument 'nmax' must be a positive integer.")
if (length(quant) != 1 || quant < 0 || quant > 1)
stop("Argument 'quant' must be a numeric value in [0,1].")
if (length(x) == 1) return(list(cutp = c(), cutd = c()))
if (!is.vector(x)) x <- c(x)
if (is.unsorted(x)) x <- sort(x)
d <- diff(x)
if (quant == 0.0) n <- nmax
else {
q <- quantile(d, probs = quant)
n <- min(nmax, sum(d >= q))
}
o <- order(d)
inds <- o[length(d):(length(d)-(n-1))]
dc <- d[inds]
xc <- x[inds] + dc/2
o <- order(xc)
return(list(cutp = xc[o], cutd = dc[o]))
}
## Example