Skip to content

Commit 29140e7

Browse files
committed
add examples for all functions (#35)
1 parent 5b82fe7 commit 29140e7

14 files changed

+199
-59
lines changed

R/adjust-equivocal-zone.R

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,34 @@
55
#' value is the size of the buffer around the threshold.
66
#' @param threshold A numeric value (between zero and one) or [hardhat::tune()].
77
#' @examplesIf rlang::is_installed("modeldata")
8-
#' library(dplyr)
98
#' library(modeldata)
109
#'
11-
#' post_obj <-
10+
#' head(two_class_example)
11+
#'
12+
#' # `predicted` gives hard class predictions based on probabilities
13+
#' two_class_example %>% count(predicted)
14+
#'
15+
#' # when probabilities are within (.25, .75), consider them equivocal
16+
#' tlr <-
1217
#' tailor() %>%
1318
#' adjust_equivocal_zone(value = 1 / 4)
1419
#'
20+
#' tlr
1521
#'
16-
#' post_res <- fit(
17-
#' post_obj,
22+
#' # fit by supplying column names. situate in a modeling workflow
23+
#' # with `workflows::add_tailor()` to avoid having to do so manually
24+
#' tlr_fit <- fit(
25+
#' tlr,
1826
#' two_class_example,
1927
#' outcome = c(truth),
2028
#' estimate = c(predicted),
2129
#' probabilities = c(Class1, Class2)
2230
#' )
2331
#'
24-
#' predict(post_res, two_class_example)
32+
#' tlr_fit
33+
#'
34+
#' # adjust hard class predictions
35+
#' predict(tlr_fit, two_class_example) %>% count(predicted)
2536
#' @export
2637
adjust_equivocal_zone <- function(x, value = 0.1, threshold = 1 / 2) {
2738
check_tailor(x)

R/adjust-numeric-calibration.R

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@
66
#' package [probably::cal_estimate_linear()],
77
#' [probably::cal_estimate_isotonic()], or
88
#' [probably::cal_estimate_isotonic_boot()], respectively.
9-
#' @examplesIf rlang::is_installed("modeldata")
10-
#' library(modeldata)
11-
#' library(probably)
9+
#' @examples
1210
#' library(tibble)
1311
#'
1412
#' # create example data
1513
#' set.seed(1)
16-
#' dat <- tibble(y = rnorm(100), y_pred = y/2 + rnorm(100))
14+
#' d_potato <- tibble(y = rnorm(100), y_pred = y/2 + rnorm(100))
15+
#' d_test <- tibble(y = rnorm(100), y_pred = y/2 + rnorm(100))
1716
#'
18-
#' dat
17+
#' d_potato
1918
#'
2019
#' # specify calibration
21-
#' reg_tailor <-
20+
#' tlr <-
2221
#' tailor() %>%
2322
#' adjust_numeric_calibration(method = "linear")
2423
#'
25-
#' # train tailor
26-
#' reg_tailor_trained <- fit(reg_tailor, dat, outcome = y, estimate = y_pred)
24+
#' # train tailor on a subset of data. situate in a modeling workflow with
25+
#' # `workflows::add_tailor()` to avoid having to specify column names manually
26+
#' tlr_fit <- fit(tlr, d_potato, outcome = y, estimate = y_pred)
2727
#'
28-
#' predict(reg_tailor_trained, dat)
28+
#' # apply to predictions on another subset of data
29+
#' d_test
30+
#'
31+
#' predict(tlr_fit, d_test)
2932
#' @export
3033
adjust_numeric_calibration <- function(x, method = NULL) {
3134
check_tailor(x, calibration_type = "numeric")

R/adjust-numeric-range.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@
33
#' @param x A [tailor()].
44
#' @param upper_limit,lower_limit A numeric value, NA (for no truncation) or
55
#' [hardhat::tune()].
6+
#'
7+
#' @examplesIf FALSE
8+
# # TODO: unskip -- fn currently requires estimate to be called `.pred` (#22)
9+
#' library(tibble)
10+
#'
11+
#' # create example data
12+
#' set.seed(1)
13+
#' d <- tibble(y = rnorm(100), y_pred = y/2 + rnorm(100))
14+
#' d
15+
#'
16+
#' # specify calibration
17+
#' tlr <-
18+
#' tailor() %>%
19+
#' adjust_numeric_range(lower_limit = 1)
20+
#'
21+
#' # train tailor by passing column names. situate in a modeling workflow with
22+
#' # `workflows::add_tailor()` to avoid having to specify column names manually
23+
#' tlr_fit <- fit(tlr, d, outcome = y, estimate = y_pred)
24+
#'
25+
#' predict(tlr_fit, d)
626
#' @export
727
adjust_numeric_range <- function(x, lower_limit = -Inf, upper_limit = Inf) {
828
# remaining input checks are done via probably::bound_prediction

R/adjust-predictions-custom.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
#' the commands.
66
#' @param ... Name-value pairs of expressions. See [dplyr::mutate()].
77
#' @examplesIf rlang::is_installed("modeldata")
8-
#' library(dplyr)
98
#' library(modeldata)
109
#'
11-
#' post_obj <-
10+
#' head(two_class_example)
11+
#'
12+
#' tlr <-
1213
#' tailor() %>%
1314
#' adjust_equivocal_zone() %>%
1415
#' adjust_predictions_custom(linear_predictor = binomial()$linkfun(Class2))
1516
#'
16-
#'
17-
#' post_res <- fit(
18-
#' post_obj,
17+
#' tlr_fit <- fit(
18+
#' tlr,
1919
#' two_class_example,
2020
#' outcome = c(truth),
2121
#' estimate = c(predicted),
2222
#' probabilities = c(Class1, Class2)
2323
#' )
2424
#'
25-
#' predict(post_res, two_class_example)
25+
#' predict(tlr_fit, two_class_example) %>% head()
2626
#' @export
2727
adjust_predictions_custom <- function(x, ..., .pkgs = character(0)) {
2828
check_tailor(x)

R/adjust-probability-calibration.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,40 @@
55
#' `"beta"`, `"isotonic"`, or `"isotonic_boot"`, corresponding to the
66
#' function from the \pkg{probably} package [probably::cal_estimate_logistic()],
77
#' [probably::cal_estimate_multinomial()], etc., respectively.
8+
#'
9+
# TODO: see #36
10+
#' @examplesIf FALSE
11+
# @examplesIf rlang::is_installed("modeldata")
12+
#' library(modeldata)
13+
#'
14+
#' # split example data
15+
#' set.seed(1)
16+
#' in_rows <- sample(c(TRUE, FALSE), nrow(two_class_example), replace = TRUE)
17+
#' d_potato <- two_class_example[in_rows, ]
18+
#' d_test <- two_class_example[!in_rows, ]
19+
#'
20+
#' head(d_potato)
21+
#'
22+
#' # specify calibration
23+
#' tlr <-
24+
#' tailor() %>%
25+
#' adjust_probability_calibration(method = "logistic")
26+
#'
27+
#' # train tailor on a subset of data. situate in a modeling workflow with
28+
#' # `workflows::add_tailor()` to avoid having to specify column names manually
29+
#' tlr_fit <- fit(
30+
#' tlr,
31+
#' d_potato,
32+
#' outcome = c(truth),
33+
#' estimate = c(predicted),
34+
#' probabilities = c(Class1, Class2)
35+
#' )
36+
#'
37+
#' # apply to predictions on another subset of data
38+
#' head(d_test)
39+
#'
40+
#' predict(tlr_fit, d_test)
41+
#'
842
#' @export
943
adjust_probability_calibration <- function(x, method = NULL) {
1044
check_tailor(x, calibration_type = "probability")

R/adjust-probability-threshold.R

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@
33
#' @param x A [tailor()].
44
#' @param threshold A numeric value (between zero and one) or [hardhat::tune()].
55
#' @examplesIf rlang::is_installed("modeldata")
6-
#' library(dplyr)
76
#' library(modeldata)
87
#'
9-
#' post_obj <-
10-
#' tailor() %>%
11-
#' adjust_probability_threshold(threshold = .1)
8+
#' # `predicted` gives hard class predictions based on probability threshold .5
9+
#' head(two_class_example)
1210
#'
13-
#' two_class_example %>% count(predicted)
11+
#' # use a threshold of .1 instead:
12+
#' tlr <-
13+
#' tailor() %>%
14+
#' adjust_probability_threshold(.1)
1415
#'
15-
#' post_res <- fit(
16-
#' post_obj,
16+
#' # fit by supplying column names. situate in a modeling workflow
17+
#' # with `workflows::add_tailor()` to avoid having to do so manually
18+
#' tlr_fit <- fit(
19+
#' tlr,
1720
#' two_class_example,
1821
#' outcome = c(truth),
1922
#' estimate = c(predicted),
2023
#' probabilities = c(Class1, Class2)
2124
#' )
2225
#'
23-
#' predict(post_res, two_class_example) %>% count(predicted)
26+
#' # adjust hard class predictions
27+
#' predict(tlr_fit, two_class_example) %>% head()
2428
#' @export
2529
adjust_probability_threshold <- function(x, threshold = 0.5) {
2630
check_tailor(x)

R/tailor.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#' classification, these should be given in the order of the factor levels of
2929
#' the `estimate`.
3030
#' @examplesIf rlang::is_installed("modeldata")
31-
#' library(dplyr)
3231
#' library(modeldata)
3332
#'
3433
#' # `predicted` gives hard class predictions based on probabilities

man/adjust_equivocal_zone.Rd

Lines changed: 16 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/adjust_numeric_calibration.Rd

Lines changed: 11 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/adjust_numeric_range.Rd

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)