Skip to content

Commit e8b9c9f

Browse files
topepo‘topepo’hfrick
authored
move .filter_eval_time to standalone (#1034)
* move filter function to standalone * reverse changelog order * versioning and docs * comments and updated date * move corresponding test * revert msg formatting --------- Co-authored-by: ‘topepo’ <‘mxkuhn@gmail.com’> Co-authored-by: Hannah Frick <hannah@posit.co>
1 parent 0acbf8d commit e8b9c9f

File tree

7 files changed

+94
-76
lines changed

7 files changed

+94
-76
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: parsnip
22
Title: A Common API to Modeling and Analysis Functions
3-
Version: 1.1.1.9004
3+
Version: 1.1.1.9005
44
Authors@R: c(
55
person("Max", "Kuhn", , "max@posit.co", role = c("aut", "cre")),
66
person("Davis", "Vaughan", , "davis@posit.co", role = "aut"),

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# parsnip (development version)
22

3+
* `.filter_eval_time()` was moved to the survival standalone file.
4+
35
* Improved errors and documentation related to special terms in formulas. See `?model_formula` to learn more. (#770, #1014)
46

57
* Improved errors in cases where the outcome column is mis-specified. (#1003)

R/standalone-survival.R

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
# ---
22
# repo: tidymodels/parsnip
33
# file: standalone-survival.R
4-
# last-updated: 2023-06-14
4+
# last-updated: 2023-12-08
55
# license: https://unlicense.org
66
# ---
77

8-
# This file provides a portable set of helper functions for Surv objects
8+
# This file provides a portable set of helper functions for survival analysis.
9+
#
910

1011
# ## Changelog
11-
12-
# 2023-02-28:
13-
# * Initial version
12+
# 2023-12-08
13+
# * move .filter_eval_time to this file
1414
#
15-
# 2023-05-18
16-
# * added time to factor conversion
15+
# 2023-11-09
16+
# * make sure survival vectors are unnamed.
1717
#
1818
# 2023-06-14
1919
# * removed time to factor conversion
2020
#
21-
# 2023-11-09
22-
# * make sure survival vectors are unnamed.
23-
21+
# 2023-05-18
22+
# * added time to factor conversion
23+
#
24+
# 2023-02-28:
25+
# * Initial version
26+
#
27+
# ------------------------------------------------------------------------------
28+
#
2429
# @param surv A [survival::Surv()] object
2530
# @details
2631
# `.is_censored_right()` always returns a logical while
@@ -51,17 +56,21 @@
5156
attr(surv, "type")
5257
}
5358

54-
.check_cens_type <- function(surv, type = "right", fail = TRUE, call = rlang::caller_env()) {
55-
.is_surv(surv, call = call)
56-
obj_type <- .extract_surv_type(surv)
57-
good_type <- all(obj_type %in% type)
58-
if (!good_type && fail) {
59-
c_list <- paste0("'", type, "'")
60-
msg <- cli::format_inline("For this usage, the allowed censoring type{?s} {?is/are}: {c_list}")
61-
rlang::abort(msg, call = call)
59+
.check_cens_type <-
60+
function(surv,
61+
type = "right",
62+
fail = TRUE,
63+
call = rlang::caller_env()) {
64+
.is_surv(surv, call = call)
65+
obj_type <- .extract_surv_type(surv)
66+
good_type <- all(obj_type %in% type)
67+
if (!good_type && fail) {
68+
c_list <- paste0("'", type, "'")
69+
msg <- cli::format_inline("For this usage, the allowed censoring type{?s} {?is/are}: {c_list}")
70+
rlang::abort(msg, call = call)
71+
}
72+
good_type
6273
}
63-
good_type
64-
}
6574

6675
.is_censored_right <- function(surv) {
6776
.check_cens_type(surv, type = "right", fail = FALSE)
@@ -88,12 +97,45 @@
8897
.is_surv(surv)
8998
res <- surv[, "status"]
9099
un_vals <- sort(unique(res))
91-
event_type_to_01 <- !(.extract_surv_type(surv) %in% c("interval", "interval2", "mstate"))
100+
event_type_to_01 <-
101+
!(.extract_surv_type(surv) %in% c("interval", "interval2", "mstate"))
92102
if (
93103
event_type_to_01 &&
94104
(identical(un_vals, 1:2) | identical(un_vals, c(1.0, 2.0))) ) {
95105
res <- res - 1
96106
}
97107
unname(res)
98108
}
109+
99110
# nocov end
111+
112+
# ------------------------------------------------------------------------------
113+
114+
# @param eval_time A vector of numeric time points
115+
# @details
116+
# `.filter_eval_time` checks the validity of the time points.
117+
#
118+
# @return A potentially modified vector of time points.
119+
.filter_eval_time <- function(eval_time, fail = TRUE) {
120+
if (!is.null(eval_time)) {
121+
eval_time <- as.numeric(eval_time)
122+
}
123+
eval_time_0 <- eval_time
124+
# will still propagate nulls:
125+
eval_time <- eval_time[!is.na(eval_time)]
126+
eval_time <- eval_time[eval_time >= 0 & is.finite(eval_time)]
127+
eval_time <- unique(eval_time)
128+
if (fail && identical(eval_time, numeric(0))) {
129+
cli::cli_abort(
130+
"There were no usable evaluation times (finite, non-missing, and >= 0).",
131+
call = NULL
132+
)
133+
}
134+
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+
139+
}
140+
eval_time
141+
}

R/survival-censoring-weights.R

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,6 @@ trunc_probs <- function(probs, trunc = 0.01) {
1818
probs
1919
}
2020

21-
.filter_eval_time <- function(eval_time, fail = TRUE) {
22-
if (!is.null(eval_time)) {
23-
eval_time <- as.numeric(eval_time)
24-
}
25-
eval_time_0 <- eval_time
26-
# will still propagate nulls:
27-
eval_time <- eval_time[!is.na(eval_time)]
28-
eval_time <- eval_time[eval_time >= 0 & is.finite(eval_time)]
29-
eval_time <- unique(eval_time)
30-
if (fail && identical(eval_time, numeric(0))) {
31-
rlang::abort(
32-
"There were no usable evaluation times (finite, non-missing, and >= 0).",
33-
call = NULL
34-
)
35-
}
36-
if (!identical(eval_time, eval_time_0)) {
37-
diffs <- setdiff(eval_time_0, eval_time)
38-
msg <-
39-
cli::pluralize(
40-
"There {?was/were} {length(diffs)} inappropriate evaluation time point{?s} that {?was/were} removed.")
41-
rlang::warn(msg)
42-
}
43-
eval_time
44-
}
45-
4621
# nocov start
4722
# these are tested in extratests
4823

File renamed without changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
test_that(".filter_eval_time()", {
2+
times_basic <- 0:10
3+
expect_equal(
4+
parsnip:::.filter_eval_time(times_basic),
5+
times_basic
6+
)
7+
8+
times_dont_reorder <- c(10, 1:9)
9+
expect_equal(
10+
parsnip:::.filter_eval_time(times_dont_reorder),
11+
times_dont_reorder
12+
)
13+
14+
expect_null(parsnip:::.filter_eval_time(NULL))
15+
16+
times_duplicated <- c(times_basic, times_basic)
17+
expect_snapshot(
18+
parsnip:::.filter_eval_time(times_duplicated)
19+
)
20+
21+
expect_snapshot(error = TRUE, parsnip:::.filter_eval_time(-1))
22+
23+
times_remove_plural <- c(Inf, NA, -3, times_basic)
24+
expect_snapshot(parsnip:::.filter_eval_time(times_remove_plural))
25+
26+
times_remove_singular <- c(-3, times_basic)
27+
expect_snapshot(parsnip:::.filter_eval_time(times_remove_singular))
28+
})

tests/testthat/test-survival-censoring-weights.R

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,3 @@ test_that("probability truncation via trunc_probs()", {
2121
probs
2222
)
2323
})
24-
25-
test_that(".filter_eval_time()", {
26-
times_basic <- 0:10
27-
expect_equal(
28-
parsnip:::.filter_eval_time(times_basic),
29-
times_basic
30-
)
31-
32-
times_dont_reorder <- c(10, 1:9)
33-
expect_equal(
34-
parsnip:::.filter_eval_time(times_dont_reorder),
35-
times_dont_reorder
36-
)
37-
38-
expect_null(parsnip:::.filter_eval_time(NULL))
39-
40-
times_duplicated <- c(times_basic, times_basic)
41-
expect_snapshot(
42-
parsnip:::.filter_eval_time(times_duplicated)
43-
)
44-
45-
expect_snapshot(error = TRUE, parsnip:::.filter_eval_time(-1))
46-
47-
times_remove_plural <- c(Inf, NA, -3, times_basic)
48-
expect_snapshot(parsnip:::.filter_eval_time(times_remove_plural))
49-
50-
times_remove_singular <- c(-3, times_basic)
51-
expect_snapshot(parsnip:::.filter_eval_time(times_remove_singular))
52-
})

0 commit comments

Comments
 (0)