Skip to content
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

limit the range of numeric predictions #142

Merged
merged 10 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export(as.factor)
export(as.ordered)
export(as_class_pred)
export(augment)
export(bound_prediction)
export(cal_apply)
export(cal_estimate_beta)
export(cal_estimate_isotonic)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# probably (development version)

* A new function `bound_prediction()` is available to constrain the values of a numeric prediction.

# probably 1.0.3

* Fixed a bug where the grouping for calibration methods was sensitive to the type of the grouping variables (#127).
Expand Down
38 changes: 38 additions & 0 deletions R/bound_prediction.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#' 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
#' constrains 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::caller_env()) {
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)
}

if (is.numeric(lower_limit) && !is.na(lower_limit)) {
x$.pred <- ifelse(x$.pred < lower_limit, lower_limit, x$.pred)
}

if (is.numeric(upper_limit) && !is.na(upper_limit)) {
x$.pred <- ifelse(x$.pred > upper_limit, upper_limit, x$.pred)
}
x
}

1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ reference:
- starts_with("int_")
- starts_with("control_conformal_")
- starts_with("predict.int")
- bound_prediction

- title: Data
contents:
Expand Down
35 changes: 35 additions & 0 deletions man/bound_prediction.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/probably-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added tests/testthat/Rplots.pdf
Binary file not shown.
34 changes: 34 additions & 0 deletions tests/testthat/_snaps/bound-prediction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# lower_limit bounds for numeric predictions

Code
bound_prediction(solubility_test, lower_limit = 2)
Condition
Error:
! The argument `x` should have a column named `.pred`

---

Code
solubility_test %>% mutate(.pred = format(prediction)) %>% bound_prediction(
lower_limit = 2)
Condition
Error:
! Column `.pred` should be numeric.

# upper_limit bounds for numeric predictions

Code
bound_prediction(solubility_test, lower_limit = 2)
Condition
Error:
! The argument `x` should have a column named `.pred`

---

Code
solubility_test %>% mutate(.pred = format(prediction)) %>% bound_prediction(
lower_limit = 2)
Condition
Error:
! Column `.pred` should be numeric.

55 changes: 55 additions & 0 deletions tests/testthat/test-bound-prediction.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
test_that("lower_limit bounds for numeric predictions", {
skip_if_not_installed("modeldata")
library(dplyr)
library(rlang)
data("solubility_test", package = "modeldata")
tune2 <- function() call("tune", "test")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function ever going to have to deal with tune() values. I guess I'm using my mental model from parsnip here, where upstream packages take care of tune() calls before we send specs off to fit().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might erroneously receive that from users. I'd err on the side of over-checking

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an error for these values (instead of just passing)


# ------------------------------------------------------------------------------

expect_snapshot(bound_prediction(solubility_test, lower_limit = 2), error = TRUE)
expect_snapshot(
solubility_test %>%
mutate(.pred = format(prediction)) %>%
bound_prediction(lower_limit = 2),
error = TRUE)

sol <- solubility_test %>% set_names(c("solubility", ".pred"))

expect_equal(bound_prediction(sol), sol)
expect_equal(bound_prediction(sol, lower_limit = NA), sol)

res_1 <- bound_prediction(sol, lower_limit = -1)
expect_true(all(res_1$.pred[res_1$.pred < -1] == -1))
expect_true(all(res_1$.pred[res_1$.pred >= -1] == res_1$.pred[res_1$.pred >= -1]))

expect_equal(bound_prediction(sol, lower_limit = tune2()), sol)
})

test_that("upper_limit bounds for numeric predictions", {
skip_if_not_installed("modeldata")
library(dplyr)
library(rlang)
data("solubility_test", package = "modeldata")
tune2 <- function() call("tune", "test")

# ------------------------------------------------------------------------------

expect_snapshot(bound_prediction(solubility_test, lower_limit = 2), error = TRUE)
expect_snapshot(
solubility_test %>%
mutate(.pred = format(prediction)) %>%
bound_prediction(lower_limit = 2),
error = TRUE)

sol <- solubility_test %>% set_names(c("solubility", ".pred"))

expect_equal(bound_prediction(sol), sol)
expect_equal(bound_prediction(sol, upper_limit = NA), sol)

res_1 <- bound_prediction(sol, upper_limit = -1)
expect_true(all(res_1$.pred[res_1$.pred > -1] == -1))
expect_true(all(res_1$.pred[res_1$.pred <= -1] == res_1$.pred[res_1$.pred <= -1]))

expect_equal(bound_prediction(sol, upper_limit = tune2()), sol)
})
Loading