Skip to content

Commit e5611d2

Browse files
authored
Merge pull request #100 from tidymodels/survival-time-to-binary
add test for `.time_as_binary_event()`
2 parents 0a2592c + 50e383d commit e5611d2

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

tests/testthat/_snaps/parsnip-survival-standalone.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,38 @@
1919
Error <rlang_error>
2020
For this usage, the allowed censoring types are: 'right' and 'interval'
2121

22+
# .time_as_binary_event() converts survival data to a factor
23+
24+
Code
25+
.time_as_binary_event(surv_obj, 11:12)
26+
Error <simpleError>
27+
'eval_time' should be a single, complete, finite numeric value.
28+
29+
---
30+
31+
Code
32+
.time_as_binary_event(surv_obj, Inf)
33+
Error <simpleError>
34+
'eval_time' should be a single, complete, finite numeric value.
35+
36+
---
37+
38+
Code
39+
.time_as_binary_event(surv_obj, NA)
40+
Error <simpleError>
41+
'eval_time' should be a single, complete, finite numeric value.
42+
43+
---
44+
45+
Code
46+
.time_as_binary_event(surv_obj, -1)
47+
Error <simpleError>
48+
'eval_time' should be a single, complete, finite numeric value.
49+
50+
---
51+
52+
Code
53+
.time_as_binary_event(surv_obj, "potato")
54+
Error <simpleError>
55+
'eval_time' should be a single, complete, finite numeric value.
56+

tests/testthat/test-parsnip-survival-standalone.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,31 @@ test_that(".extract_surv_status() does not transform status for interval censori
111111
events_interval_12
112112
)
113113
})
114+
115+
test_that(".time_as_binary_event() converts survival data to a factor", {
116+
skip_if_not_installed("parsnip", minimum_version = "1.1.0.9003")
117+
times <- 1:10
118+
events <- rep(0:1, times = 5)
119+
surv_obj <- survival::Surv(times, events)
120+
121+
lvls <- c("event", "non-event")
122+
to_factor <- function(x) factor(x, levels = lvls)
123+
124+
obs_time_1.5 <- .time_as_binary_event(surv_obj, 1.5)
125+
exp_time_1.5 <- to_factor(c(NA, rep("non-event", 9)))
126+
expect_equal(obs_time_1.5, exp_time_1.5)
127+
128+
obs_time_5.5 <- .time_as_binary_event(surv_obj, 5.5)
129+
exp_time_5.5 <- to_factor(c(rep(c(NA, "event"), 2), NA, rep("non-event", 5)))
130+
expect_equal(obs_time_5.5, exp_time_5.5)
131+
132+
obs_time_11 <- .time_as_binary_event(surv_obj, 11)
133+
exp_time_11 <- to_factor(rep(c(NA, "event"), 5))
134+
expect_equal(obs_time_11, exp_time_11)
135+
136+
expect_snapshot(error = TRUE, .time_as_binary_event(surv_obj, 11:12))
137+
expect_snapshot(error = TRUE, .time_as_binary_event(surv_obj, Inf))
138+
expect_snapshot(error = TRUE, .time_as_binary_event(surv_obj, NA))
139+
expect_snapshot(error = TRUE, .time_as_binary_event(surv_obj, -1))
140+
expect_snapshot(error = TRUE, .time_as_binary_event(surv_obj, "potato"))
141+
})

0 commit comments

Comments
 (0)