forked from tidymodels/parsnip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_quantile.R
50 lines (41 loc) · 1.46 KB
/
predict_quantile.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#' @keywords internal
#' @rdname other_predict
#' @param quantile A vector of numbers between 0 and 1 for the quantile being
#' predicted.
#' @inheritParams predict.model_fit
#' @method predict_quantile model_fit
#' @export predict_quantile.model_fit
#' @export
predict_quantile.model_fit <- function(object,
new_data,
quantile = (1:9)/10,
interval = "none",
level = 0.95,
...) {
check_spec_pred_type(object, "quantile")
if (inherits(object$fit, "try-error")) {
cli::cli_warn("Model fit failed; cannot make predictions.")
return(NULL)
}
new_data <- prepare_data(object, new_data)
# preprocess data
if (!is.null(object$spec$method$pred$quantile$pre)) {
new_data <- object$spec$method$pred$quantile$pre(new_data, object)
}
# Pass some extra arguments to be used in post-processor
object$spec$method$pred$quantile$args$p <- quantile
pred_call <- make_pred_call(object$spec$method$pred$quantile)
res <- eval_tidy(pred_call)
# post-process the predictions
if(!is.null(object$spec$method$pred$quantile$post)) {
res <- object$spec$method$pred$quantile$post(res, object)
}
res
}
# @export
# @keywords internal
# @rdname other_predict
# @inheritParams predict.model_fit
predict_quantile <- function (object, ...) {
UseMethod("predict_quantile")
}