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 error for single eval time in brier_survival_integrated() #467

Merged
merged 2 commits into from
Dec 13, 2023
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 @@ -20,6 +20,8 @@ calculated with `roc_auc_survival()`.

* All warnings and errors have been updated to use the cli package for increased clarity and consistency. (#456, #457, #458)

* `brier_survival_integrated()` now throws an error if input data only includes 1 evalution time point. (#460)

# yardstick 1.2.0

## New Metrics
Expand Down
16 changes: 16 additions & 0 deletions R/surv-brier_survival_integrated.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ brier_survival_integrated_vec <- function(truth,
truth, estimate, case_weights
)

num_eval_times <- get_unique_eval_times(estimate)
if (num_eval_times < 2) {
cli::cli_abort(
"At least 2 evaluation time{?s} {?is/are} required. \\
Only {num_eval_times} unique time{?s} {?was/were} given."
Comment on lines +97 to +98
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the pluralization is overkill here :) The requirement is always "at least 2" and {num_eval_times} is not going to be 2 or more when we trigger that error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new and improved in #470

)
}

if (na_rm) {
result <- yardstick_remove_missing(
truth, seq_along(estimate), case_weights
Expand All @@ -111,6 +119,14 @@ brier_survival_integrated_vec <- function(truth,
brier_survival_integrated_impl(truth, estimate, case_weights)
}

get_unique_eval_times <- function(x) {
res <- lapply(x, function(x) x$.eval_time)
res <- unlist(res)
res <- unique(res)
res <- length(res)
res
}

brier_survival_integrated_impl <- function(truth,
estimate,
case_weights) {
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/surv-brier_survival_integrated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# brier_survival_integrated calculations

Code
brier_survival_integrated(data = lung_surv, truth = surv_obj, .pred)
Condition
Error in `brier_survival_integrated()`:
! At least 2 evaluation time is required. Only 1 unique time was given.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this slightly off message is what caught my eye :)


15 changes: 15 additions & 0 deletions tests/testthat/test-surv-brier_survival_integrated.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ test_that("brier_survival_integrated calculations", {
)
})

test_that("brier_survival_integrated calculations", {
lung_surv <- data_lung_surv()

lung_surv$.pred <- lapply(lung_surv$.pred, function(x) x[1, ])

expect_snapshot(
error = TRUE,
brier_survival_integrated(
data = lung_surv,
truth = surv_obj,
.pred
)
)
})

test_that("case weights", {
lung_surv <- data_lung_surv()
lung_surv$case_wts <- seq_len(nrow(lung_surv))
Expand Down
Loading