@@ -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