Skip to content

Commit 3fb23ca

Browse files
Merge pull request #1043 from tidymodels/fix-967
give .filter_eval_time() more informative warning
2 parents b305a8e + 353961f commit 3fb23ca

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

R/standalone-survival.R

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# ---
22
# repo: tidymodels/parsnip
33
# file: standalone-survival.R
4-
# last-updated: 2023-12-08
4+
# last-updated: 2024-01-10
55
# license: https://unlicense.org
66
# ---
77

88
# This file provides a portable set of helper functions for survival analysis.
99
#
1010

1111
# ## Changelog
12+
# 2024-01-10
13+
# * .filter_eval_time() gives more informative warning.
14+
#
1215
# 2023-12-08
13-
# * move .filter_eval_time to this file
16+
# * move .filter_eval_time() to this file
1417
#
1518
# 2023-11-09
1619
# * make sure survival vectors are unnamed.
@@ -132,10 +135,38 @@
132135
)
133136
}
134137
if (!identical(eval_time, eval_time_0)) {
135-
diffs <- setdiff(eval_time_0, eval_time)
136-
cli::cli_warn("There {?was/were} {length(diffs)} inappropriate evaluation
137-
time point{?s} that {?was/were} removed.", call = NULL)
138+
diffs <- length(eval_time_0) - length(eval_time)
139+
140+
offenders <- character()
141+
142+
n_na <- sum(is.na(eval_time_0))
143+
if (n_na > 0) {
144+
offenders <- c(offenders, "*" = "{n_na} missing value{?s}.")
145+
}
146+
147+
n_inf <- sum(is.infinite(eval_time_0))
148+
if (n_inf > 0) {
149+
offenders <- c(offenders, "*" = "{n_inf} infinite value{?s}.")
150+
}
138151

152+
n_neg <- sum(eval_time_0 < 0, na.rm = TRUE)
153+
if (n_neg > 0) {
154+
offenders <- c(offenders, "*" = "{n_neg} negative value{?s}.")
155+
}
156+
157+
n_dup <- diffs - n_na - n_inf - n_neg
158+
if (n_dup > 0) {
159+
offenders <- c(offenders, "*" = "{n_dup} duplicate value{?s}.")
160+
}
161+
162+
cli::cli_warn(
163+
c(
164+
"There {?was/were} {diffs} inappropriate evaluation time \\
165+
point{?s} that {?was/were} removed. {?It was/They were}:",
166+
offenders
167+
),
168+
call = NULL
169+
)
139170
}
140171
eval_time
141172
}

tests/testthat/_snaps/standalone-survival.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
parsnip:::.filter_eval_time(times_duplicated)
55
Condition
66
Warning:
7-
There were 0 inappropriate evaluation time points that were removed.
7+
There were 11 inappropriate evaluation time points that were removed. They were:
8+
* 11 duplicate values.
89
Output
910
[1] 0 1 2 3 4 5 6 7 8 9 10
1011

@@ -22,7 +23,10 @@
2223
parsnip:::.filter_eval_time(times_remove_plural)
2324
Condition
2425
Warning:
25-
There were 3 inappropriate evaluation time points that were removed.
26+
There were 3 inappropriate evaluation time points that were removed. They were:
27+
* 1 missing value.
28+
* 1 infinite value.
29+
* 1 negative value.
2630
Output
2731
[1] 0 1 2 3 4 5 6 7 8 9 10
2832

@@ -32,7 +36,8 @@
3236
parsnip:::.filter_eval_time(times_remove_singular)
3337
Condition
3438
Warning:
35-
There was 1 inappropriate evaluation time point that was removed.
39+
There was 1 inappropriate evaluation time point that was removed. It was:
40+
* 1 negative value.
3641
Output
3742
[1] 0 1 2 3 4 5 6 7 8 9 10
3843

0 commit comments

Comments
 (0)