diff --git a/R/adjust-numeric-calibration.R b/R/adjust-numeric-calibration.R index 402ddbc..8170961 100644 --- a/R/adjust-numeric-calibration.R +++ b/R/adjust-numeric-calibration.R @@ -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 <- diff --git a/R/adjust-probability-calibration.R b/R/adjust-probability-calibration.R index 089399b..38fc04e 100644 --- a/R/adjust-probability-calibration.R +++ b/R/adjust-probability-calibration.R @@ -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 <- diff --git a/tests/testthat/test-adjust-numeric-calibration.R b/tests/testthat/test-adjust-numeric-calibration.R index 379907d..2e4c37d 100644 --- a/tests/testthat/test-adjust-numeric-calibration.R +++ b/tests/testthat/test-adjust-numeric-calibration.R @@ -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()) diff --git a/tests/testthat/test-adjust-probability-calibration.R b/tests/testthat/test-adjust-probability-calibration.R index 5704466..0ca01b7 100644 --- a/tests/testthat/test-adjust-probability-calibration.R +++ b/tests/testthat/test-adjust-probability-calibration.R @@ -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"))