-
Notifications
You must be signed in to change notification settings - Fork 20
add quantile_pred #260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add quantile_pred #260
Changes from 16 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
0b4cf5d
code from parsnip
1739032
version bump
82a89c4
news update
8add7cb
add pkgdown entry
7192170
Apply suggestions from code review
topepo fa85a3b
add snapshot file
hfrick cae0633
#259 already claimed `.9001`
hfrick b18861e
tidy style
hfrick a8c9355
change styling to class and test
hfrick 754e385
add test for `as.matrix()` to illustrate current behavior
hfrick 100ddcb
move constructor
hfrick 89d98d1
group vctrs methods
hfrick 9ab5809
group checking functions together
hfrick 26bc327
move up functions which are placed on the main help page
hfrick 0979985
group remaining methods
hfrick c8c4230
move unused check function with others for visibility
hfrick 9f14926
Merge branch 'main' into quantile-pred
hfrick db0e1a5
fix typo
9a659ef
remove function for parsnip's set_mode()
30de46f
remove test helpers
9dc4b12
remove functions for restructuring
dfe8288
remove range code; add digits argument
7d6c4e2
fix as.matrix method
d0444bd
remove @importFrom
c597214
don't re-export vctrs generics
hfrick 9006d02
add pluralization to `obj_print_footer()` method
hfrick fbed1c1
refactor inout checks
f9808c6
fix typo
hfrick 04525e8
only show unique values in error
hfrick 480c2ff
add pluralization
hfrick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: hardhat | ||
Title: Construct Modeling Packages | ||
Version: 1.4.0.9000 | ||
Version: 1.4.0.9002 | ||
Authors@R: c( | ||
person("Hannah", "Frick", , "[email protected]", role = c("aut", "cre"), | ||
comment = c(ORCID = "0000-0002-6049-5258")), | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
#' Create a vector containing sets of quantiles | ||
#' | ||
#' [quantile_pred()] is a special vector class used to efficiently store | ||
#' predictions from a quantile regression model. It requires the same quantile | ||
#' levels for each row being predicted. | ||
#' | ||
#' @param values A matrix of values. Each column should correspond to one of | ||
#' the quantile levels. | ||
#' @param quantile_levels A vector of probabilities corresponding to `values`. | ||
#' @param x An object produced by [quantile_pred()]. | ||
#' @param .rows,.name_repair,rownames Arguments not used but required by the | ||
#' original S3 method. | ||
#' @param ... Not currently used. | ||
#' | ||
#' @export | ||
#' @return | ||
#' * [quantile_pred()] returns a vector of values associated with the | ||
#' quantile levels. | ||
#' * [extract_quantile_levels()] returns a numeric vector of levels. | ||
#' * [as_tibble()] returns a tibble with rows `".pred_quantile"`, | ||
#' `".quantile_levels"`, and `".row"`. | ||
#' * [as.matrix()] returns an unnamed matrix with rows as sames, columns as | ||
topepo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
#' quantile levels, and entries are predictions. | ||
#' @examples | ||
#' .pred_quantile <- quantile_pred(matrix(rnorm(20), 5), c(.2, .4, .6, .8)) | ||
#' | ||
#' unclass(.pred_quantile) | ||
#' | ||
#' # Access the underlying information | ||
#' extract_quantile_levels(.pred_quantile) | ||
#' | ||
#' # Matrix format | ||
#' as.matrix(.pred_quantile) | ||
#' | ||
#' # Tidy format | ||
#' library(tibble) | ||
#' as_tibble(.pred_quantile) | ||
quantile_pred <- function(values, quantile_levels = double()) { | ||
check_quantile_pred_inputs(values, quantile_levels) | ||
|
||
quantile_levels <- vctrs::vec_cast(quantile_levels, double()) | ||
num_lvls <- length(quantile_levels) | ||
|
||
if (ncol(values) != num_lvls) { | ||
cli::cli_abort( | ||
"The number of columns in {.arg values} must be equal to the length of | ||
{.arg quantile_levels}." | ||
) | ||
} | ||
rownames(values) <- NULL | ||
colnames(values) <- NULL | ||
values <- lapply(vctrs::vec_chop(values), drop) | ||
new_quantile_pred(values, quantile_levels) | ||
} | ||
|
||
new_quantile_pred <- function(values = list(), quantile_levels = double()) { | ||
quantile_levels <- vctrs::vec_cast(quantile_levels, double()) | ||
vctrs::new_vctr( | ||
values, quantile_levels = quantile_levels, class = "quantile_pred" | ||
) | ||
} | ||
|
||
check_quantile_pred_inputs <- function(values, levels, call = caller_env()) { | ||
if (any(is.na(levels))) { | ||
cli::cli_abort("Missing values are not allowed in {.arg quantile_levels}.", | ||
call = call) | ||
} | ||
|
||
if (!is.matrix(values)) { | ||
cli::cli_abort( | ||
"{.arg values} must be a {.cls matrix}, not {.obj_type_friendly {values}}.", | ||
call = call | ||
) | ||
} | ||
hfrick marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
check_vector_probability(levels, arg = "quantile_levels", call = call) | ||
|
||
if (is.unsorted(levels)) { | ||
cli::cli_abort( | ||
"{.arg quantile_levels} must be sorted in increasing order.", | ||
call = call | ||
) | ||
} | ||
invisible(NULL) | ||
} | ||
|
||
check_vector_probability <- function(x, ..., | ||
allow_na = FALSE, | ||
allow_null = FALSE, | ||
arg = caller_arg(x), | ||
call = caller_env()) { | ||
for (d in x) { | ||
check_number_decimal( | ||
d, | ||
min = 0, | ||
max = 1, | ||
arg = arg, | ||
call = call, | ||
allow_na = allow_na, | ||
allow_null = allow_null, | ||
allow_infinite = FALSE | ||
) | ||
} | ||
} | ||
|
||
check_quantile_level <- function(x, object, call) { | ||
topepo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (object$mode != "quantile regression") { | ||
return(invisible(TRUE)) | ||
} else { | ||
if (is.null(x)) { | ||
cli::cli_abort("In {.fn check_mode}, at least one value of | ||
{.arg quantile_level} must be specified for quantile regression models.") | ||
} | ||
} | ||
if (any(is.na(x))) { | ||
cli::cli_abort("Missing values are not allowed in {.arg quantile_levels}.", | ||
call = call) | ||
} | ||
x <- sort(unique(x)) | ||
check_vector_probability(x, arg = "quantile_level", call = call) | ||
x | ||
} | ||
|
||
#' @export | ||
#' @rdname quantile_pred | ||
extract_quantile_levels <- function(x) { | ||
if (!inherits(x, "quantile_pred")) { | ||
cli::cli_abort("{.arg x} should have class {.cls quantile_pred}.") | ||
} | ||
attr(x, "quantile_levels") | ||
} | ||
|
||
#' @export | ||
#' @rdname quantile_pred | ||
as_tibble.quantile_pred <- | ||
function (x, ..., .rows = NULL, .name_repair = "minimal", rownames = NULL) { | ||
lvls <- attr(x, "quantile_levels") | ||
n_samp <- length(x) | ||
n_quant <- length(lvls) | ||
tibble::new_tibble(list( | ||
.pred_quantile = unlist(x), | ||
.quantile_levels = rep(lvls, n_samp), | ||
.row = rep(1:n_samp, each = n_quant) | ||
)) | ||
} | ||
topepo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#' @export | ||
#' @rdname quantile_pred | ||
as.matrix.quantile_pred <- function(x, ...) { | ||
num_samp <- length(x) | ||
matrix(unlist(x), nrow = num_samp) | ||
topepo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
#' @export | ||
format.quantile_pred <- function(x, ...) { | ||
topepo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
quantile_levels <- attr(x, "quantile_levels") | ||
if (length(quantile_levels) == 1L) { | ||
x <- unlist(x) | ||
out <- round(x, 3L) | ||
out[is.na(x)] <- NA_real_ | ||
} else { | ||
rng <- sapply(x, range, na.rm = TRUE) | ||
out <- paste0("[", round(rng[1, ], 3L), ", ", round(rng[2, ], 3L), "]") | ||
out[is.na(rng[1, ]) & is.na(rng[2, ])] <- NA_character_ | ||
m <- median(x) | ||
out <- paste0("[", round(m, 3L), "]") | ||
} | ||
out | ||
} | ||
|
||
#' @export | ||
median.quantile_pred <- function(x, ...) { | ||
lvls <- attr(x, "quantile_levels") | ||
loc_median <- (abs(lvls - 0.5) < sqrt(.Machine$double.eps)) | ||
if (any(loc_median)) { | ||
return(map_dbl(x, ~ .x[min(which(loc_median))])) | ||
} | ||
if (length(lvls) < 2 || min(lvls) > 0.5 || max(lvls) < 0.5) { | ||
return(rep(NA, vctrs::vec_size(x))) | ||
} | ||
map_dbl(x, ~ stats::approx(lvls, .x, xout = 0.5)$y) | ||
} | ||
|
||
#' @importFrom vctrs vec_ptype_abbr | ||
#' @export | ||
hfrick marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
vctrs::vec_ptype_abbr | ||
|
||
#' @importFrom vctrs vec_ptype_full | ||
#' @export | ||
vctrs::vec_ptype_full | ||
|
||
#' @export | ||
vec_ptype_abbr.quantile_pred <- function(x, ...) { | ||
n_lvls <- length(attr(x, "quantile_levels")) | ||
cli::format_inline("qtl{?s}({n_lvls})") | ||
} | ||
|
||
#' @export | ||
vec_ptype_full.quantile_pred <- function(x, ...) "quantiles" | ||
|
||
#' @importFrom vctrs obj_print_footer | ||
#' @export | ||
vctrs::obj_print_footer | ||
|
||
#' @export | ||
obj_print_footer.quantile_pred <- function(x, digits = 3, ...) { | ||
lvls <- attr(x, "quantile_levels") | ||
cat("# Quantile levels: ", format(lvls, digits = digits), "\n", sep = " ") | ||
} | ||
|
||
restructure_rq_pred <- function(x, object) { | ||
topepo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (!is.matrix(x)) { | ||
x <- as.matrix(x) | ||
} | ||
rownames(x) <- NULL | ||
n_pred_quantiles <- ncol(x) | ||
quantile_level <- object$spec$quantile_level | ||
|
||
tibble::new_tibble(x = list(.pred_quantile = quantile_pred(x, quantile_level))) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.