Skip to content

Commit

Permalink
Follow-up to #131 (#147)
Browse files Browse the repository at this point in the history
* add tests on standalone functions

* for readability since survival pkg is available
  • Loading branch information
hfrick authored Nov 17, 2023
1 parent ed518ef commit 9a8cae3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
12 changes: 3 additions & 9 deletions tests/testthat/test-parsnip-survival-censoring-weights.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,13 @@ test_that("error for .censoring_weights_graf() from .check_censor_model()", {
expect_snapshot(error = TRUE, .censoring_weights_graf(wrong_model, mtcars))
})


test_that("no names in weight values", {
# See tidymodels/parsnip#1023 tidymodels/parsnip#1024
skip_if_not_installed("parsnip", minimum_version = "1.1.1.9002")

surv_obj <-
structure(
c(9, 13, 13, 18, 23, 28, 1, 1, 0, 1, 1, 0),
dim = c(6L, 2L),
dimnames = list(NULL, c("time", "status")),
type = "right",
class = "Surv"
)
times <- c(9, 13, 13, 18, 23, 28)
cens <- c(1, 1, 0, 1, 1, 0)
surv_obj <- survival::Surv(times, cens)

row_1 <- parsnip:::graf_weight_time_vec(surv_obj[1,,drop = FALSE], 1.0)
row_5 <- parsnip:::graf_weight_time_vec(surv_obj, 1.0)
Expand Down
41 changes: 41 additions & 0 deletions tests/testthat/test-parsnip-survival-standalone.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ test_that(".extract_surv_time()", {
)
})

test_that(".extract_surv_time() vector results are unnamed", {
skip_if_not_installed("parsnip", minimum_version = "1.1.1.9002")

times <- seq(1, 100, length.out = 5)
events <- c(1, 0, 1, 0, 1)

right_c <- survival::Surv(times, events)
left_c <- survival::Surv(times, events, type = "left")

expect_named(parsnip:::.extract_surv_time(right_c), NULL)
expect_named(parsnip:::.extract_surv_time(left_c), NULL)

# single observation
expect_named(parsnip:::.extract_surv_time(right_c[1]), NULL)
expect_named(parsnip:::.extract_surv_time(left_c[1]), NULL)
})

test_that(".extract_surv_status()", {
times <- seq(1, 100, length.out = 5)
times2 <- seq(100, 200, length.out = 5)
Expand Down Expand Up @@ -89,6 +106,30 @@ test_that(".extract_surv_status()", {
)
})

test_that(".extract_surv_status() results are unnamed", {
skip_if_not_installed("parsnip", minimum_version = "1.1.1.9002")

times <- seq(1, 100, length.out = 5)
times2 <- seq(100, 200, length.out = 5)
events <- c(1, 0, 1, 0, 1)

right_c <- survival::Surv(times, events)
left_c <- survival::Surv(times, events, type = "left")
intv_c <- survival::Surv(times, times2, events, type = "interval")
count_c <- survival::Surv(times, times2, events)

expect_named(parsnip:::.extract_surv_status(right_c), NULL)
expect_named(parsnip:::.extract_surv_status(left_c), NULL)
expect_named(parsnip:::.extract_surv_status(intv_c), NULL)
expect_named(parsnip:::.extract_surv_status(count_c), NULL)

# single observation
expect_named(parsnip:::.extract_surv_status(right_c[1]), NULL)
expect_named(parsnip:::.extract_surv_status(left_c[1]), NULL)
expect_named(parsnip:::.extract_surv_status(intv_c[1]), NULL)
expect_named(parsnip:::.extract_surv_status(count_c[1]), NULL)
})

test_that(".extract_surv_status() does not transform status for interval censoring", {
times <- seq(1, 100, length.out = 5)
times2 <- seq(100, 200, length.out = 5)
Expand Down

0 comments on commit 9a8cae3

Please sign in to comment.