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

correct slot for calibration method #50

Merged
merged 5 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 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 @@ -87,7 +87,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
43 changes: 43 additions & 0 deletions tests/testthat/test-adjust-probability-calibration.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,49 @@ 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")
)

# TODO: cannot be `expect_no_condition()` due to tidymodels/probably#157
expect_no_error(expect_no_warning(
tlr_fit <- fit(
tlr,
d_calibration,
outcome = c(truth),
estimate = c(predicted),
probabilities = c(Class1, Class2)
)
))

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_probability_calibration("logistic"))
Expand Down
Loading