Skip to content

Commit

Permalink
correct slot for calibration method
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Sep 24, 2024
1 parent e81dffd commit 4b6fd1a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
2 changes: 1 addition & 1 deletion R/adjust-numeric-calibration.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ print.numeric_calibration <- function(x, ...) {

#' @export
fit.numeric_calibration <- function(object, data, tailor = NULL, ...) {
method <- check_method(object$method, tailor$type)
method <- check_method(object$arguments$method, tailor$type)
# todo: adjust_numeric_calibration() should take arguments to pass to
# cal_estimate_* via dots
fit <-
Expand Down
2 changes: 1 addition & 1 deletion R/adjust-probability-calibration.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ print.probability_calibration <- function(x, ...) {

#' @export
fit.probability_calibration <- function(object, data, tailor = NULL, ...) {
method <- check_method(object$method, tailor$type)
method <- check_method(object$arguments$method, tailor$type)
# todo: adjust_probability_calibration() should take arguments to pass to
# cal_estimate_* via dots
fit <-
Expand Down
34 changes: 33 additions & 1 deletion tests/testthat/test-adjust-numeric-calibration.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,39 @@ test_that("basic adjust_numeric_calibration usage works", {
# TODO: write out the probably code manually here
})

# TODO: test sensitivity to function arguments
test_that("adjust_numeric_calibration() respects `method` argument", {
library(tibble)

set.seed(1)
d_calibration <- tibble(y = rnorm(100), y_pred = y/2 + rnorm(100))
d_test <- tibble(y = rnorm(100), y_pred = y/2 + rnorm(100))

expect_no_condition(
tlr <-
tailor() %>%
adjust_numeric_calibration(method = "isotonic")
)

# TODO: cannot be `expect_no_condition()` due to tidymodels/probably#157
expect_no_error(expect_no_warning(
tlr_fit <- fit(tlr, d_calibration, outcome = y, estimate = y_pred)
))

expect_no_error(expect_no_warning(
tlr_pred <- predict(tlr_fit, d_test)
))

# classes are as expected
expect_s3_class(tlr, "tailor")
expect_s3_class(tlr_fit, "tailor")
expect_s3_class(tlr_pred, "tbl_df")

# column names are as expected
expect_equal(colnames(d_test), colnames(tlr_pred))

# probably actually used an isotonic calibrator
expect_equal(tlr_fit$adjustments[[1]]$results$fit$method, "Isotonic regression")
})

test_that("adjustment printing", {
expect_snapshot(tailor() %>% adjust_numeric_calibration())
Expand Down
44 changes: 44 additions & 0 deletions tests/testthat/test-adjust-probability-calibration.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,50 @@ test_that("basic adjust_probability_calibration() usage works", {
# TODO: write out the manual code with probably
})

test_that("basic adjust_probability_calibration() usage works", {
skip_if_not_installed("modeldata")
library(modeldata)

# split example data
set.seed(1)
in_rows <- sample(c(TRUE, FALSE), nrow(two_class_example), replace = TRUE)
d_calibration <- two_class_example[in_rows, ]
d_test <- two_class_example[!in_rows, ]

# fitting and predicting happens without raising conditions
expect_no_condition(
tlr <-
tailor() %>%
adjust_probability_calibration(method = "isotonic")
)

skip("TODO: cannot run until #49 is merged")

expect_no_condition(
tlr_fit <- fit(
tlr,
d_calibration,
outcome = c(truth),
estimate = c(predicted),
probabilities = c(Class1, Class2)
)
)

expect_no_condition(
tlr_pred <- predict(tlr_fit, d_test)
)

# classes are as expected
expect_s3_class(tlr, "tailor")
expect_s3_class(tlr_fit, "tailor")
expect_s3_class(tlr_pred, "tbl_df")

# column names are as expected
expect_equal(colnames(d_test), colnames(tlr_pred))

# probably actually used an isotonic calibrator
expect_equal(tlr_fit$adjustments[[1]]$results$fit$method, "Generalized additive model")
})

test_that("adjustment printing", {
expect_snapshot(tailor() %>% adjust_probability_calibration("logistic"))
Expand Down

0 comments on commit 4b6fd1a

Please sign in to comment.