-
Notifications
You must be signed in to change notification settings - Fork 0
survival model fit augmentation testing #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2ec3bfa
nocov
topepo fa3d158
move files into default folder
hfrick 94d5583
capture call in snapshot
hfrick b4b13a6
swap order
hfrick 249e3d4
add test on skipping `.pred_time`
hfrick 2ba6cf9
changed time points
topepo c9bb389
Merged origin/main into survival-augment
hfrick cb12fad
bump finetune version requirement
topepo 7236ef8
Merged origin/main into survival-augment
hfrick 3859d93
declare usage of `aorsf` package
hfrick 9b1d057
update snapshot test
hfrick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ License: MIT + file LICENSE | |
Depends: | ||
tidymodels | ||
Suggests: | ||
aorsf, | ||
baguette, | ||
bonsai, | ||
BradleyTerry2, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# augmenting survival models | ||
|
||
Code | ||
augment(sr_fit, new_data = sim_tr) | ||
Error <purrr_error_indexed> | ||
i In index: 1. | ||
i With name: 1. | ||
Caused by error in `.f()`: | ||
! argument "eval_time" is missing, with no default | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
library(testthat) | ||
library(tidymodels) | ||
library(prodlim) | ||
library(censored) | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
|
||
test_that('augmenting survival models ', { | ||
skip_if_not_installed("parsnip", minimum_version = "1.1.0.9001") | ||
|
||
set.seed(1) | ||
sim_dat <- SimSurv(500) %>% | ||
mutate(event_time = Surv(time, event)) %>% | ||
select(event_time, X1, X2) | ||
|
||
set.seed(2) | ||
split <- initial_split(sim_dat) | ||
sim_tr <- training(split) | ||
sim_te <- testing(split) | ||
|
||
time_points <- c(1, 5, 10) | ||
|
||
# ------------------------------------------------------------------------------ | ||
|
||
sr_fit <- | ||
survival_reg() %>% | ||
fit(event_time ~ ., data = sim_tr) | ||
|
||
expect_snapshot(error = TRUE, { | ||
augment(sr_fit, new_data = sim_tr) | ||
}) | ||
|
||
sr_aug <- augment(sr_fit, new_data = sim_tr, eval_time = time_points) | ||
expect_equal(nrow(sr_aug), nrow(sim_tr)) | ||
expect_equal(names(sr_aug), c(".pred", ".pred_time", "event_time", "X1", "X2")) | ||
expect_true(is.list(sr_aug$.pred)) | ||
expect_equal( | ||
names(sr_aug$.pred[[1]]), | ||
c(".eval_time", ".pred_survival", ".weight_time", ".pred_censored", | ||
".weight_censored") | ||
) | ||
|
||
# ------------------------------------------------------------------------------ | ||
|
||
glmn_fit <- | ||
proportional_hazards(penalty = 0.1) %>% | ||
set_engine("glmnet") %>% | ||
fit(event_time ~ ., data = sim_tr) | ||
|
||
glmn_aug <- augment(glmn_fit, new_data = sim_tr, eval_time = time_points) | ||
expect_equal(nrow(glmn_aug), nrow(sim_tr)) | ||
expect_equal(names(glmn_aug), c(".pred", ".pred_time", "event_time", "X1", "X2")) | ||
expect_true(is.list(glmn_aug$.pred)) | ||
expect_equal( | ||
names(glmn_aug$.pred[[1]]), | ||
c(".eval_time", ".pred_survival", ".weight_time", ".pred_censored", | ||
".weight_censored") | ||
) | ||
}) | ||
|
||
test_that("augment() for survival models skips unavailble prediction type", { | ||
skip_if_not_installed("parsnip", minimum_version = "1.1.0.9001") | ||
skip_if_not_installed("prodlim") | ||
skip_if_not_installed("aorsf") | ||
|
||
set.seed(1) | ||
sim_dat <- prodlim::SimSurv(500) %>% | ||
mutate(event_time = Surv(time, event)) %>% | ||
select(event_time, X1, X2) | ||
|
||
time_points <- c(1, 5, 10) | ||
|
||
# this engine does not provide predictions of type = "time" | ||
rf_fit <- | ||
rand_forest() %>% | ||
set_engine("aorsf") %>% | ||
set_mode("censored regression") %>% | ||
fit(event_time ~ ., data = sim_dat) | ||
|
||
rf_aug <- augment(rf_fit, new_data = sim_dat, eval_time = time_points) | ||
expect_equal(nrow(rf_aug), nrow(sim_dat)) | ||
expect_equal(names(rf_aug), c(".pred", "event_time", "X1", "X2")) | ||
expect_true(is.list(rf_aug$.pred)) | ||
expect_equal( | ||
names(rf_aug$.pred[[1]]), | ||
c(".eval_time", ".pred_survival", ".weight_time", ".pred_censored", | ||
".weight_censored") | ||
) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is not relevant for testing
augment()
, I would remove it here to keep the tests focussed on the essentials.