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

throw warnings when na_rm = FALSE and missing is found in curve metrics #491

Merged
merged 3 commits into from
Jan 19, 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: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ calculated with `roc_auc_survival()`.

* Metrics now throw more informative error if `estimate` argument is wrongly used. (#443)

* Curve metrics now throw an informative error instead of returning `NA` when missing values are found and `na_rm = FALSE`. (#344)

# yardstick 1.2.0

## New Metrics
Expand Down
5 changes: 4 additions & 1 deletion R/prob-gain_curve.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ gain_curve_vec <- function(truth,
estimate <- result$estimate
case_weights <- result$case_weights
} else if (yardstick_any_missing(truth, estimate, case_weights)) {
return(NA_real_)
cli::cli_abort(c(
x = "Missing values were detected and {.code na_ra = FALSE}.",
i = "Not able to perform calculations."
))
}

gain_curve_estimator_impl(
Expand Down
5 changes: 4 additions & 1 deletion R/prob-pr_curve.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ pr_curve_vec <- function(truth,
estimate <- result$estimate
case_weights <- result$case_weights
} else if (yardstick_any_missing(truth, estimate, case_weights)) {
return(NA_real_)
cli::cli_abort(c(
x = "Missing values were detected and {.code na_ra = FALSE}.",
i = "Not able to perform calculations."
))
}

pr_curve_estimator_impl(
Expand Down
5 changes: 4 additions & 1 deletion R/prob-roc_curve.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ roc_curve_vec <- function(truth,
estimate <- result$estimate
case_weights <- result$case_weights
} else if (yardstick_any_missing(truth, estimate, case_weights)) {
return(NA_real_)
cli::cli_abort(c(
x = "Missing values were detected and {.code na_ra = FALSE}.",
i = "Not able to perform calculations."
))
}

# estimate here is a matrix of class prob columns
Expand Down
12 changes: 5 additions & 7 deletions R/surv-roc_curve_survival.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,11 @@ roc_curve_survival_vec <- function(truth,
truth <- result$truth
estimate <- estimate[result$estimate]
case_weights <- result$case_weights
} else {
any_missing <- yardstick_any_missing(
truth, estimate, case_weights
)
if (any_missing) {
return(NA_real_)
}
} else if (yardstick_any_missing(truth, estimate, case_weights)) {
cli::cli_abort(c(
x = "Missing values were detected and {.code na_ra = FALSE}.",
i = "Not able to perform calculations."
))
}

roc_curve_survival_impl(
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/_snaps/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@
x This metric doesn't use the `estimate` argument.
i Specify the columns without `estimate = `.

# curve_metric_summarizer()'s na_rm argument work

Code
curve_metric_summarizer(name = "roc_curve", fn = roc_curve_vec, data = hpc_f1_na,
truth = obs, VF:L, na_rm = FALSE, case_weights = NULL)
Condition
Error:
x Missing values were detected and `na_ra = FALSE`.
i Not able to perform calculations.

# curve_metric_summarizer()'s errors when wrong things are passes

Code
Expand Down Expand Up @@ -207,6 +217,16 @@
x Problematic argument:
* obviouslywrong = TRUE

# curve_survival_metric_summarizer()'s na_rm argument works

Code
curve_survival_metric_summarizer(name = "roc_curve_survival", fn = roc_curve_survival_vec,
data = lung_surv, truth = surv_obj, .pred, na_rm = FALSE, case_weights = NULL)
Condition
Error:
x Missing values were detected and `na_ra = FALSE`.
i Not able to perform calculations.

# curve_survival_metric_summarizer()'s errors with bad input

Code
Expand Down
54 changes: 22 additions & 32 deletions tests/testthat/test-template.R
Original file line number Diff line number Diff line change
Expand Up @@ -899,23 +899,18 @@ test_that("curve_metric_summarizer()'s na_rm argument work", {

expect_identical(roc_curve_res, roc_curve_exp)

roc_curve_res <- curve_metric_summarizer(
name = "roc_curve",
fn = roc_curve_vec,
data = hpc_f1_na,
truth = obs,
VF:L,
na_rm = FALSE,
case_weights = NULL
)

roc_curve_exp <- dplyr::tibble(
.metric = "roc_curve",
.estimator = "multiclass",
.estimate = na_dbl
expect_snapshot(
error = TRUE,
curve_metric_summarizer(
name = "roc_curve",
fn = roc_curve_vec,
data = hpc_f1_na,
truth = obs,
VF:L,
na_rm = FALSE,
case_weights = NULL
)
)

expect_identical(roc_curve_res, roc_curve_exp)
})

test_that("curve_metric_summarizer()'s case_weights argument work", {
Expand Down Expand Up @@ -1613,23 +1608,18 @@ test_that("curve_survival_metric_summarizer()'s na_rm argument works", {

expect_identical(roc_curve_survival_res, roc_curve_survival_exp)

roc_curve_survival_res <- curve_survival_metric_summarizer(
name = "roc_curve_survival",
fn = roc_curve_survival_vec,
data = lung_surv,
truth = surv_obj,
.pred,
na_rm = FALSE,
case_weights = NULL
)

roc_curve_survival_exp <- dplyr::tibble(
.metric = "roc_curve_survival",
.estimator = "standard",
.estimate = na_dbl
expect_snapshot(
error = TRUE,
curve_survival_metric_summarizer(
name = "roc_curve_survival",
fn = roc_curve_survival_vec,
data = lung_surv,
truth = surv_obj,
.pred,
na_rm = FALSE,
case_weights = NULL
)
)

expect_identical(roc_curve_survival_res, roc_curve_survival_exp)
})

test_that("curve_survival_metric_summarizer()'s case_weights argument works", {
Expand Down
Loading