Skip to content

Commit 317a4db

Browse files
authored
correct slot for calibration method (#50)
1 parent 6aad3fa commit 317a4db

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

R/adjust-numeric-calibration.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ print.numeric_calibration <- function(x, ...) {
8484

8585
#' @export
8686
fit.numeric_calibration <- function(object, data, tailor = NULL, ...) {
87-
method <- check_method(object$method, tailor$type)
87+
method <- check_method(object$arguments$method, tailor$type)
8888
# todo: adjust_numeric_calibration() should take arguments to pass to
8989
# cal_estimate_* via dots
9090
fit <-

R/adjust-probability-calibration.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ print.probability_calibration <- function(x, ...) {
8787

8888
#' @export
8989
fit.probability_calibration <- function(object, data, tailor = NULL, ...) {
90-
method <- check_method(object$method, tailor$type)
90+
method <- check_method(object$arguments$method, tailor$type)
9191
# todo: adjust_probability_calibration() should take arguments to pass to
9292
# cal_estimate_* via dots
9393
fit <-

tests/testthat/test-adjust-numeric-calibration.R

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,39 @@ test_that("basic adjust_numeric_calibration usage works", {
3232
# TODO: write out the probably code manually here
3333
})
3434

35-
# TODO: test sensitivity to function arguments
35+
test_that("adjust_numeric_calibration() respects `method` argument", {
36+
library(tibble)
37+
38+
set.seed(1)
39+
d_calibration <- tibble(y = rnorm(100), y_pred = y/2 + rnorm(100))
40+
d_test <- tibble(y = rnorm(100), y_pred = y/2 + rnorm(100))
41+
42+
expect_no_condition(
43+
tlr <-
44+
tailor() %>%
45+
adjust_numeric_calibration(method = "isotonic")
46+
)
47+
48+
# TODO: cannot be `expect_no_condition()` due to tidymodels/probably#157
49+
expect_no_error(expect_no_warning(
50+
tlr_fit <- fit(tlr, d_calibration, outcome = y, estimate = y_pred)
51+
))
52+
53+
expect_no_error(expect_no_warning(
54+
tlr_pred <- predict(tlr_fit, d_test)
55+
))
56+
57+
# classes are as expected
58+
expect_s3_class(tlr, "tailor")
59+
expect_s3_class(tlr_fit, "tailor")
60+
expect_s3_class(tlr_pred, "tbl_df")
61+
62+
# column names are as expected
63+
expect_equal(colnames(d_test), colnames(tlr_pred))
64+
65+
# probably actually used an isotonic calibrator
66+
expect_equal(tlr_fit$adjustments[[1]]$results$fit$method, "Isotonic regression")
67+
})
3668

3769
test_that("adjustment printing", {
3870
expect_snapshot(tailor() %>% adjust_numeric_calibration())

tests/testthat/test-adjust-probability-calibration.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,49 @@ test_that("basic adjust_probability_calibration() usage works", {
4141
# TODO: write out the manual code with probably
4242
})
4343

44+
test_that("basic adjust_probability_calibration() usage works", {
45+
skip_if_not_installed("modeldata")
46+
library(modeldata)
47+
48+
# split example data
49+
set.seed(1)
50+
in_rows <- sample(c(TRUE, FALSE), nrow(two_class_example), replace = TRUE)
51+
d_calibration <- two_class_example[in_rows, ]
52+
d_test <- two_class_example[!in_rows, ]
53+
54+
# fitting and predicting happens without raising conditions
55+
expect_no_condition(
56+
tlr <-
57+
tailor() %>%
58+
adjust_probability_calibration(method = "isotonic")
59+
)
60+
61+
# TODO: cannot be `expect_no_condition()` due to tidymodels/probably#157
62+
expect_no_error(expect_no_warning(
63+
tlr_fit <- fit(
64+
tlr,
65+
d_calibration,
66+
outcome = c(truth),
67+
estimate = c(predicted),
68+
probabilities = c(Class1, Class2)
69+
)
70+
))
71+
72+
expect_no_error(expect_no_warning(
73+
tlr_pred <- predict(tlr_fit, d_test)
74+
))
75+
76+
# classes are as expected
77+
expect_s3_class(tlr, "tailor")
78+
expect_s3_class(tlr_fit, "tailor")
79+
expect_s3_class(tlr_pred, "tbl_df")
80+
81+
# column names are as expected
82+
expect_equal(colnames(d_test), colnames(tlr_pred))
83+
84+
# probably actually used an isotonic calibrator
85+
expect_equal(tlr_fit$adjustments[[1]]$results$fit$method, "Isotonic regression")
86+
})
4487

4588
test_that("adjustment printing", {
4689
expect_snapshot(tailor() %>% adjust_probability_calibration("logistic"))

0 commit comments

Comments
 (0)