Skip to content

Commit

Permalink
rename adjust_*_calibration(type) to adjust_*_calibration(method)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed May 2, 2024
1 parent 9f9423c commit 33ab426
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 45 deletions.
18 changes: 9 additions & 9 deletions R/adjust-numeric-calibration.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Re-calibrate numeric predictions
#'
#' @param x A [container()].
#' @param type Character. One of `"linear"`, `"isotonic"`, or
#' @param method Character. One of `"linear"`, `"isotonic"`, or
#' `"isotonic_boot"`, corresponding to the function from the \pkg{probably}
#' package [probably::cal_estimate_linear()],
#' [probably::cal_estimate_isotonic()], or
Expand All @@ -20,20 +20,20 @@
#' # specify calibration
#' reg_ctr <-
#' container() %>%
#' adjust_numeric_calibration(type = "linear")
#' adjust_numeric_calibration(method = "linear")
#'
#' # train container
#' reg_ctr_trained <- fit(reg_ctr, dat, outcome = y, estimate = y_pred)
#'
#' predict(reg_ctr_trained, dat)
#' @export
adjust_numeric_calibration <- function(x, type = NULL) {
adjust_numeric_calibration <- function(x, method = NULL) {
# to-do: add argument specifying `prop` in initial_split
check_container(x, calibration_type = "numeric")
# wait to `check_type()` until `fit()` time
if (!is.null(type)) {
# wait to `check_method()` until `fit()` time
if (!is.null(method)) {
arg_match0(
type,
method,
c("linear", "isotonic", "isotonic_boot")
)
}
Expand All @@ -43,7 +43,7 @@ adjust_numeric_calibration <- function(x, type = NULL) {
"numeric_calibration",
inputs = "numeric",
outputs = "numeric",
arguments = list(type = type),
arguments = list(method = method),
results = list(),
trained = FALSE
)
Expand All @@ -66,13 +66,13 @@ print.numeric_calibration <- function(x, ...) {

#' @export
fit.numeric_calibration <- function(object, data, container = NULL, ...) {
type <- check_type(object$type, container$type)
method <- check_method(object$method, container$type)
# todo: adjust_numeric_calibration() should take arguments to pass to
# cal_estimate_* via dots
fit <-
eval_bare(
call2(
paste0("cal_estimate_", type),
paste0("cal_estimate_", method),
.data = data,
truth = container$columns$outcome,
estimate = container$columns$estimate,
Expand Down
16 changes: 8 additions & 8 deletions R/adjust-probability-calibration.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#' Re-calibrate classification probability predictions
#'
#' @param x A [container()].
#' @param type Character. One of `"logistic"`, `"multinomial"`,
#' @param method Character. One of `"logistic"`, `"multinomial"`,
#' `"beta"`, `"isotonic"`, or `"isotonic_boot"`, corresponding to the
#' function from the \pkg{probably} package [probably::cal_estimate_logistic()],
#' [probably::cal_estimate_multinomial()], etc., respectively.
#' @export
adjust_probability_calibration <- function(x, type = NULL) {
adjust_probability_calibration <- function(x, method = NULL) {
# to-do: add argument specifying `prop` in initial_split
check_container(x, calibration_type = "probability")
# wait to `check_type()` until `fit()` time
if (!is.null(type)) {
# wait to `check_method()` until `fit()` time
if (!is.null(method)) {
arg_match(
type,
method,
c("logistic", "multinomial", "beta", "isotonic", "isotonic_boot")
)
}
Expand All @@ -22,7 +22,7 @@ adjust_probability_calibration <- function(x, type = NULL) {
"probability_calibration",
inputs = "probability",
outputs = "probability_class",
arguments = list(type = type),
arguments = list(method = method),
results = list(),
trained = FALSE
)
Expand All @@ -45,14 +45,14 @@ print.probability_calibration <- function(x, ...) {

#' @export
fit.probability_calibration <- function(object, data, container = NULL, ...) {
type <- check_type(object$type, container$type)
method <- check_method(object$method, container$type)
# todo: adjust_probability_calibration() should take arguments to pass to
# cal_estimate_* via dots
# to-do: add argument specifying `prop` in initial_split
fit <-
eval_bare(
call2(
paste0("cal_estimate_", type),
paste0("cal_estimate_", method),
.data = data,
# todo: make getters for the entries in `columns`
truth = container$columns$outcome,
Expand Down
37 changes: 18 additions & 19 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ check_container <- function(x, calibration_type = NULL, call = caller_env(), arg
# check that the type of calibration ("numeric" or "probability") is
# compatible with the container type
if (!is.null(calibration_type)) {
container_type <- x$type
type <- x$type
switch(
container_type,
type,
regression =
check_calibration_type(calibration_type, "numeric", container_type, call = call),
binary = , multinomial =
check_calibration_type(calibration_type, "probability", container_type, call = call)
check_calibration_type(calibration_type, "numeric", type, call = call),
binary = , multiclass =
check_calibration_type(calibration_type, "probability", type, call = call)
)
}

Expand All @@ -90,54 +90,53 @@ types_binary <- c("logistic", "beta", "isotonic", "isotonic_boot")
types_multiclass <- c("multinomial", "beta", "isotonic", "isotonic_boot")
# a check function to be called when a container is being `fit()`ted.
# by the time a container is fitted, we have:
# * `adjust_type`, the `type` argument passed to an `adjust_*` function
# * `method`, the `method` argument passed to an `adjust_*` function
# * this argument has already been checked to agree with the kind of
# `adjust_*()` function via `arg_match0()`.
# * `container_type`, the `type` argument either specified in `container()`
# or inferred in `fit.container()`.
check_type <- function(adjust_type,
container_type,
arg = caller_arg(adjust_type),
check_method <- function(method,
type,
arg = caller_arg(method),
call = caller_env()) {
# if no `adjust_type` was supplied, infer a reasonable one based on the
# `container_type`
if (is.null(adjust_type)) {
# if no `method` was supplied, infer a reasonable one based on the `type`
if (is.null(method)) {
switch(
container_type,
type,
regression = return("linear"),
binary = return("logistic"),
multiclass = return("multinomial")
)
}

switch(
container_type,
type,
regression = arg_match0(
adjust_type,
method,
types_regression,
arg_nm = arg,
error_call = call
),
binary = arg_match0(
adjust_type,
method,
types_binary,
arg_nm = arg,
error_call = call
),
multiclass = arg_match0(
adjust_type,
method,
types_multiclass,
arg_nm = arg,
error_call = call
),
arg_match0(
adjust_type,
method,
unique(c(types_regression, types_binary, types_multiclass)),
arg_nm = arg,
error_call = call
)
)

adjust_type
method
}

6 changes: 3 additions & 3 deletions man/adjust_numeric_calibration.Rd

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

4 changes: 2 additions & 2 deletions man/adjust_probability_calibration.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/adjust-numeric-calibration.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
adjust_numeric_calibration(container(), "boop")
Condition
Error in `adjust_numeric_calibration()`:
! `type` must be one of "linear", "isotonic", or "isotonic_boot", not "boop".
! `method` must be one of "linear", "isotonic", or "isotonic_boot", not "boop".

---

Expand All @@ -31,6 +31,6 @@
container("regression") %>% adjust_numeric_calibration("binary")
Condition
Error in `adjust_numeric_calibration()`:
! `type` must be one of "linear", "isotonic", or "isotonic_boot", not "binary".
! `method` must be one of "linear", "isotonic", or "isotonic_boot", not "binary".
i Did you mean "linear"?

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/adjust-probability-calibration.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
adjust_probability_calibration(container(), "boop")
Condition
Error in `adjust_probability_calibration()`:
! `type` must be one of "logistic", "multinomial", "beta", "isotonic", or "isotonic_boot", not "boop".
! `method` must be one of "logistic", "multinomial", "beta", "isotonic", or "isotonic_boot", not "boop".

---

Expand All @@ -31,5 +31,5 @@
container("binary") %>% adjust_probability_calibration("linear")
Condition
Error in `adjust_probability_calibration()`:
! `type` must be one of "logistic", "multinomial", "beta", "isotonic", or "isotonic_boot", not "linear".
! `method` must be one of "logistic", "multinomial", "beta", "isotonic", or "isotonic_boot", not "linear".

0 comments on commit 33ab426

Please sign in to comment.