-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
limit the range of numeric predictions (#142)
* add a function to constrain numeric predictions * test cases * argument name change * test against tune() value * Apply suggestions from code review Co-authored-by: Simon P. Couch <[email protected]> * cleanup * rlang type checkers * re-doc * Apply suggestions from code review Co-authored-by: Simon P. Couch <[email protected]> * update tests --------- Co-authored-by: ‘topepo’ <‘[email protected]’> Co-authored-by: Simon P. Couch <[email protected]>
- Loading branch information
1 parent
f36f4b2
commit 5aa7ecd
Showing
11 changed files
with
1,098 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,42 @@ | ||
#' Truncate a numeric prediction column | ||
#' | ||
#' For user-defined `lower_limit` and/or `upper_limit` bound, ensure that the values in the | ||
#' `.pred` column are coerced to these bounds. | ||
#' | ||
#' @param x A data frame that contains a numeric column named `.pred`. | ||
#' @param lower_limit,upper_limit Single numerics (or `NA`) that define | ||
#' constraints on `.pred`. | ||
#' @param call The call to be displayed in warnings or errors. | ||
#' @return `x` with potentially adjusted values. | ||
#' @examples | ||
#' data(solubility_test, package = "yardstick") | ||
#' | ||
#' names(solubility_test) <- c("solubility", ".pred") | ||
#' | ||
#' bound_prediction(solubility_test, lower_limit = -1) | ||
#' @export | ||
bound_prediction <- function(x, lower_limit = -Inf, upper_limit = Inf, | ||
call = rlang::current_env()) { | ||
check_data_frame(x, call = call) | ||
|
||
if (!any(names(x) == ".pred")) { | ||
cli::cli_abort("The argument {.arg x} should have a column named {.code .pred}.", | ||
call = call) | ||
} | ||
if (!is.numeric(x$.pred)) { | ||
cli::cli_abort("Column {.code .pred} should be numeric.", call = call) | ||
} | ||
|
||
check_number_decimal(lower_limit, allow_na = TRUE, call = call) | ||
check_number_decimal(upper_limit, allow_na = TRUE, call = call) | ||
|
||
if (!is.na(lower_limit)) { | ||
x$.pred <- ifelse(x$.pred < lower_limit, lower_limit, x$.pred) | ||
} | ||
|
||
if (!is.na(upper_limit)) { | ||
x$.pred <- ifelse(x$.pred > upper_limit, upper_limit, x$.pred) | ||
} | ||
x | ||
} | ||
|
Oops, something went wrong.