-
Notifications
You must be signed in to change notification settings - Fork 1
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
test fit_best.workflow_set()
with survival
#200
Merged
Merged
Changes from 1 commit
Commits
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 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
This file contains 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,223 @@ | ||
suppressPackageStartupMessages(library(tidymodels)) | ||
suppressPackageStartupMessages(library(censored)) | ||
|
||
skip_if_not_installed("parsnip", minimum_version = "1.1.0.9003") | ||
skip_if_not_installed("censored", minimum_version = "0.2.0.9000") | ||
skip_if_not_installed("tune", minimum_version = "1.1.2.9012") | ||
skip_if_not_installed("yardstick", minimum_version = "1.2.0.9001") | ||
skip_if_not_installed("workflowsets", minimum_version = "1.0.1.9002") | ||
|
||
test_that("fit best with static metric", { | ||
skip_if_not_installed("prodlim") | ||
|
||
# standard setup start ------------------------------------------------------- | ||
|
||
set.seed(1) | ||
sim_dat <- prodlim::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) | ||
sim_rs <- vfold_cv(sim_tr) | ||
|
||
time_points <- c(10, 1, 5, 15) | ||
|
||
mod_spec <- | ||
proportional_hazards(penalty = tune(), mixture = 1) %>% | ||
set_engine("glmnet") %>% | ||
set_mode("censored regression") | ||
|
||
grid <- tibble(penalty = 10^c(-4, -2, -1)) | ||
|
||
gctrl <- control_grid(save_workflow = TRUE) | ||
|
||
wf_set <- | ||
workflow_set( | ||
preproc = list(f1 = event_time ~ X1 + X2), | ||
models = list(mod = mod_spec) | ||
) | ||
|
||
# standard setup end ------------------------------------------------------- | ||
|
||
stc_mtrc <- metric_set(concordance_survival) | ||
|
||
set.seed(2193) | ||
grid_static_res <- | ||
workflow_map( | ||
wf_set, | ||
resamples = sim_rs, | ||
grid = grid, | ||
metrics = stc_mtrc, | ||
control = gctrl | ||
) | ||
|
||
expect_silent(static_res <- fit_best(grid_static_res)) | ||
expect_s3_class(static_res, "workflow") | ||
expect_true(is_trained_workflow(static_res)) | ||
}) | ||
|
||
test_that("grid tuning survival models with integrated metric", { | ||
skip_if_not_installed("prodlim") | ||
|
||
# standard setup start ------------------------------------------------------- | ||
|
||
set.seed(1) | ||
sim_dat <- prodlim::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) | ||
sim_rs <- vfold_cv(sim_tr) | ||
|
||
time_points <- c(10, 1, 5, 15) | ||
|
||
mod_spec <- | ||
proportional_hazards(penalty = tune(), mixture = 1) %>% | ||
set_engine("glmnet") %>% | ||
set_mode("censored regression") | ||
|
||
grid <- tibble(penalty = 10^c(-4, -2, -1)) | ||
|
||
gctrl <- control_grid(save_workflow = TRUE) | ||
|
||
wf_set <- | ||
workflow_set( | ||
preproc = list(f1 = event_time ~ X1 + X2), | ||
models = list(mod = mod_spec) | ||
) | ||
|
||
# standard setup end ------------------------------------------------------- | ||
|
||
sint_mtrc <- metric_set(brier_survival_integrated) | ||
|
||
set.seed(2193) | ||
grid_integrated_res <- | ||
workflow_map( | ||
wf_set, | ||
resamples = sim_rs, | ||
grid = grid, | ||
metrics = sint_mtrc, | ||
eval_time = time_points, | ||
control = gctrl | ||
) | ||
|
||
expect_silent(integrated_res <- fit_best(grid_integrated_res)) | ||
expect_s3_class(integrated_res, "workflow") | ||
expect_true(is_trained_workflow(integrated_res)) | ||
|
||
}) | ||
|
||
test_that("grid tuning survival models with dynamic metric", { | ||
skip_if_not_installed("prodlim") | ||
|
||
# standard setup start ------------------------------------------------------- | ||
|
||
set.seed(1) | ||
sim_dat <- prodlim::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) | ||
sim_rs <- vfold_cv(sim_tr) | ||
|
||
time_points <- c(10, 1, 5, 15) | ||
|
||
mod_spec <- | ||
proportional_hazards(penalty = tune(), mixture = 1) %>% | ||
set_engine("glmnet") %>% | ||
set_mode("censored regression") | ||
|
||
grid <- tibble(penalty = 10^c(-4, -2, -1)) | ||
|
||
gctrl <- control_grid(save_workflow = TRUE) | ||
|
||
wf_set <- | ||
workflow_set( | ||
preproc = list(f1 = event_time ~ X1 + X2), | ||
models = list(mod = mod_spec) | ||
) | ||
|
||
# standard setup end ------------------------------------------------------- | ||
|
||
dyn_mtrc <- metric_set(brier_survival) | ||
|
||
set.seed(2193) | ||
grid_dynamic_res <- | ||
workflow_map( | ||
wf_set, | ||
resamples = sim_rs, | ||
grid = grid, | ||
metrics = dyn_mtrc, | ||
eval_time = time_points, | ||
control = gctrl | ||
) | ||
|
||
expect_silent(dynamic_res <- fit_best(grid_dynamic_res)) | ||
dynamic_res <- fit_best(grid_dynamic_res, eval_time = 1) | ||
expect_s3_class(dynamic_res, "workflow") | ||
expect_true(is_trained_workflow(dynamic_res)) | ||
|
||
}) | ||
|
||
test_that("grid tuning survival models mixture of metric types", { | ||
skip_if_not_installed("prodlim") | ||
|
||
# standard setup start ------------------------------------------------------- | ||
|
||
set.seed(1) | ||
sim_dat <- prodlim::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) | ||
sim_rs <- vfold_cv(sim_tr) | ||
|
||
time_points <- c(10, 1, 5, 15) | ||
|
||
mod_spec <- | ||
proportional_hazards(penalty = tune(), mixture = 1) %>% | ||
set_engine("glmnet") %>% | ||
set_mode("censored regression") | ||
|
||
grid <- tibble(penalty = 10^c(-4, -2, -1)) | ||
|
||
gctrl <- control_grid(save_workflow = TRUE) | ||
|
||
wf_set <- | ||
workflow_set( | ||
preproc = list(f1 = event_time ~ X1 + X2), | ||
models = list(mod = mod_spec) | ||
) | ||
|
||
# standard setup end ------------------------------------------------------- | ||
|
||
mix_mtrc <- metric_set(brier_survival, brier_survival_integrated, concordance_survival) | ||
|
||
set.seed(2193) | ||
grid_mixed_res <- | ||
workflow_map( | ||
wf_set, | ||
resamples = sim_rs, | ||
grid = grid, | ||
metrics = mix_mtrc, | ||
eval_time = time_points, | ||
control = gctrl | ||
) | ||
|
||
expect_silent(mixed_res <- fit_best(grid_mixed_res)) | ||
mixed_res <- fit_best(grid_mixed_res, eval_time = 1) | ||
expect_s3_class(mixed_res, "workflow") | ||
expect_true(is_trained_workflow(mixed_res)) | ||
}) |
Oops, something went wrong.
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.
I added these two lines to mirror the existing
fit_best()
tests but am a bit unsure of the intent.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.
The
expect_silent()
is there because we want this function to make a choice for metric and eval_time silently, unlike most other functions where it warns you about that.The second one I'm not entirely sure about. Ideally, we'd test that it does indeed pick the best model for a specific eval time (but there is nothing currently confirming that) so it's just making sure that it doesn't error 🤔