@@ -115,14 +115,58 @@ vec_proxy_equal.quantile_pred <- function(x, ...) {
115115# quantiles by treating quantile_pred like a distribution -----------------
116116
117117
118+ # ' Quantiles from a distribution
119+ # '
120+ # ' Given a [hardhat::quantile_pred] object, users may wish to compute additional
121+ # ' `quantile_levels` that are not part of the object. This function attempts
122+ # ' to estimate these quantities under some assumptions. Interior probabilities,
123+ # ' those contained within, existing probabilities are interpolated in a manner
124+ # ' controled by the `middle` argument. Those outside existing probabilities
125+ # ' are extrapolated under the assumption that the tails of the distribution
126+ # ' decays exponentially. Optionally, one may constrain _all_ quantiles to be
127+ # ' within some support (say, `[0, Inf)`).
128+ # '
129+ # ' @inheritParams stats::quantile
130+ # ' @param ... unused
131+ # ' @param lower Scalar. Optional lower bound.
132+ # ' @param upper Scalar. Optional upper bound.
133+ # ' @param middle Controls how extrapolation to "interior" probabilities is
134+ # ' performed. "cubic" attempts to use [stats::splinefun()] while "linear"
135+ # ' uses [stats::approx()]. The "linear" method is used as a fallback if
136+ # ' "cubic" should fail for some reason.
137+ # '
138+ # ' @returns a matrix with one row for each entry in `x` and one column for each
139+ # ' value in `probs`. If either has length 1, a vector.
140+ # ' @seealso [extrapolate_quantiles()]
118141# ' @export
119142# ' @importFrom stats quantile
120- quantile.quantile_pred <- function (x , p , na.rm = FALSE , ... ,
121- middle = c(" cubic" , " linear" )) {
122- arg_is_probabilities(p )
123- p <- sort(p )
143+ # '
144+ # ' @examples
145+ # ' qp <- quantile_pred(matrix(1:8, nrow = 2, byrow = TRUE), 1:4 / 5)
146+ # ' quantile(qp)
147+ # ' quantile(qp, lower = 0)
148+ # ' quantile(qp, probs = 0.5)
149+ # ' quantile(qp, probs = 1:9 / 10)
150+ quantile.quantile_pred <- function (x ,
151+ probs = seq(0 , 1 , 0.25 ),
152+ na.rm = FALSE ,
153+ lower = - Inf ,
154+ upper = Inf ,
155+ middle = c(" cubic" , " linear" ),
156+ ...
157+ ) {
158+ arg_is_probabilities(probs )
159+ arg_is_scalar(lower , upper , na.rm )
160+ arg_is_numeric(lower , upper )
161+ arg_is_lgl(na.rm )
162+
163+ if (lower > upper ) {
164+ cli_abort(" `lower` must be less than `upper`." )
165+ }
166+
167+ if (is.unsorted(probs )) probs <- sort(probs )
124168 middle <- rlang :: arg_match(middle )
125- quantile_internal(x , p , middle )
169+ drop(snap( quantile_internal(x , probs , middle ), lower , upper ) )
126170}
127171
128172
0 commit comments