|
| 1 | +test_that('survival helpers', { |
| 2 | + skip_if_not_installed("parsnip", minimum_version = "1.0.4.9001") |
| 3 | + library(survival) |
| 4 | + library(parsnip) |
| 5 | + |
| 6 | + times <- seq(1, 100, length.out = 5) |
| 7 | + times2 <- seq(100, 200, length.out = 5) |
| 8 | + events <- c(1, 0, 1, 0, 1) |
| 9 | + |
| 10 | + right_c <- Surv(times, events) |
| 11 | + left_c <- Surv(times, events, type = "left") |
| 12 | + intv_c <- Surv(times, times2, events, type = "interval") |
| 13 | + count_c <- Surv(times, times2, events) |
| 14 | + |
| 15 | + expect_true(parsnip:::.is_surv(right_c)) |
| 16 | + expect_false(parsnip:::.is_surv(1, fail = FALSE)) |
| 17 | + expect_snapshot_error(parsnip:::.is_surv(1)) |
| 18 | + |
| 19 | + expect_true(.is_censored_right(right_c)) |
| 20 | + expect_false(.is_censored_right(left_c)) |
| 21 | + |
| 22 | + expect_equal( |
| 23 | + .extract_surv_time(right_c), |
| 24 | + times |
| 25 | + ) |
| 26 | + expect_equal( |
| 27 | + .extract_surv_time(left_c), |
| 28 | + times |
| 29 | + ) |
| 30 | + expect_equal( |
| 31 | + .extract_surv_time(intv_c), |
| 32 | + tibble::tibble(time1 = times, time2 = rep(1.0, 5)) |
| 33 | + ) |
| 34 | + expect_equal( |
| 35 | + .extract_surv_time(count_c), |
| 36 | + tibble::tibble(start = times, stop = times2) |
| 37 | + ) |
| 38 | + |
| 39 | + expect_equal( |
| 40 | + .extract_surv_status(right_c), |
| 41 | + events |
| 42 | + ) |
| 43 | + expect_equal( |
| 44 | + .extract_surv_status(left_c), |
| 45 | + events |
| 46 | + ) |
| 47 | + expect_equal( |
| 48 | + .extract_surv_status(intv_c), |
| 49 | + events |
| 50 | + ) |
| 51 | + expect_equal( |
| 52 | + .extract_surv_status(count_c), |
| 53 | + events |
| 54 | + ) |
| 55 | + |
| 56 | +}) |
0 commit comments