library(utils)
library(ggplot2)
theme_set(theme_bw())

These examples illustrate which models, engines, and prediction types are available in censored. As a reminder, in parsnip,

The following examples use the same data set throughout.

bag_tree() models

With the "rpart" engine

r knitr::spin_child("template-lung.R")

We can define the model with specific parameters:

r bt_spec <- bag_tree(cost_complexity = 0) %>% set_engine("rpart") %>% set_mode("censored regression") bt_spec

Now we create the model fit object:

r set.seed(1) bt_fit <- bt_spec %>% fit(Surv(time, status) ~ ., data = lung_train) bt_fit

The holdout data can be predicted for survival probability at different time points as well as event time.

r predict( bt_fit, lung_test, type = "survival", eval_time = c(100, 500, 1000) ) %>% slice(1) %>% tidyr::unnest(col = .pred) predict(bt_fit, lung_test, type = "time")

boost_tree() models

With the "mboost" engine

r knitr::spin_child("template-lung.R")

We can define the model with specific parameters:

r bt_spec <- boost_tree(trees = 15) %>% set_engine("mboost") %>% set_mode("censored regression") bt_spec

Now we create the model fit object:

r set.seed(1) bt_fit <- bt_spec %>% fit(Surv(time, status) ~ ., data = lung_train) bt_fit

The holdout data can be predicted for survival probability at different time points as well as the linear predictor.

r predict( bt_fit, lung_test, type = "survival", eval_time = c(100, 500, 1000) ) %>% slice(1) %>% tidyr::unnest(col = .pred) predict(bt_fit, lung_test, type = "linear_pred")

decision_tree() models

With the "rpart" engine

r knitr::spin_child("template-lung.R")

We can define the model with specific parameters:

r dt_spec <- decision_tree(cost_complexity = 0) %>% set_engine("rpart") %>% set_mode("censored regression") dt_spec

Now we create the model fit object:

r set.seed(1) dt_fit <- dt_spec %>% fit(Surv(time, status) ~ ., data = lung_train) dt_fit

The holdout data can be predicted for survival probability at different time points as well as event time.

r predict( dt_fit, lung_test, type = "survival", eval_time = c(100, 500, 1000) ) %>% slice(1) %>% tidyr::unnest(col = .pred) predict(dt_fit, lung_test, type = "time")

With the "partykit" engine

r knitr::spin_child("template-lung.R")

We can define the model with specific parameters:

r dt_spec <- decision_tree() %>% set_engine("partykit") %>% set_mode("censored regression") dt_spec

Now we create the model fit object:

r set.seed(1) dt_fit <- dt_spec %>% fit(Surv(time, status) ~ ., data = lung_train) dt_fit

The holdout data can be predicted for survival probability at different time points as well as event time.

r predict( dt_fit, lung_test, type = "survival", eval_time = c(100, 500, 1000) ) %>% slice(1) %>% tidyr::unnest(col = .pred) predict(dt_fit, lung_test, type = "time")

proportional_hazards() models

With the "survival" engine

r knitr::spin_child("template-lung.R")

We can define the model with specific parameters:

r ph_spec <- proportional_hazards() %>% set_engine("survival") %>% set_mode("censored regression") ph_spec

Now we create the model fit object:

r set.seed(1) ph_fit <- ph_spec %>% fit(Surv(time, status) ~ ., data = lung_train) ph_fit

The holdout data can be predicted for survival probability at different time points as well as the linear predictor and event time.

r predict( ph_fit, lung_test, type = "survival", eval_time = c(100, 500, 1000) ) %>% slice(1) %>% tidyr::unnest(col = .pred) predict(ph_fit, lung_test, type = "linear_pred") predict(ph_fit, lung_test, type = "time")

With the "glmnet" engine

r knitr::spin_child("template-lung.R")

We can define the model with specific parameters:

r ph_spec <- proportional_hazards(penalty = 0.1) %>% set_engine("glmnet") %>% set_mode("censored regression") ph_spec

Now we create the model fit object:

r set.seed(1) ph_fit <- ph_spec %>% fit(Surv(time, status) ~ ., data = lung_train) ph_fit

The holdout data can be predicted for survival probability at different time points as well as the linear predictor.

r predict( ph_fit, lung_test, type = "survival", eval_time = c(100, 500, 1000) ) %>% slice(1) %>% tidyr::unnest(col = .pred) predict(ph_fit, lung_test, type = "linear_pred")

rand_forest() models

With the "partykit" engine

r knitr::spin_child("template-lung.R")

We can define the model with specific parameters:

r rf_spec <- rand_forest(trees = 200) %>% set_engine("partykit") %>% set_mode("censored regression") rf_spec

Now we create the model fit object:

r set.seed(1) rf_fit <- rf_spec %>% fit(Surv(time, status) ~ ., data = lung_train) rf_fit

The holdout data can be predicted for survival probability at different time points as well as event time.

r predict( rf_fit, lung_test, type = "survival", eval_time = c(100, 500, 1000) ) %>% slice(1) %>% tidyr::unnest(col = .pred) predict(rf_fit, lung_test, type = "time")

With the "aorsf" engine

r knitr::spin_child("template-lung.R")

We can define the model with specific parameters:

r rf_spec <- rand_forest(trees = 200) %>% set_engine("aorsf") %>% set_mode("censored regression") rf_spec

Now we create the model fit object:

```r set.seed(1)

rf_fit <- rf_spec %>% fit(Surv(time, status) ~ ., data = lung_train) rf_fit ```

The holdout data can be predicted for survival probability at different time points as well as event time.

r predict( rf_fit, lung_test, type = "survival", eval_time = c(100, 500, 1000) ) %>% slice(1) %>% tidyr::unnest(col = .pred) predict(rf_fit, lung_test, type = "time")

survival_reg() models

With the "survival" engine

r knitr::spin_child("template-lung.R")

We can define the model with specific parameters:

r sr_spec <- survival_reg(dist = "weibull") %>% set_engine("survival") %>% set_mode("censored regression") sr_spec

Now we create the model fit object:

r set.seed(1) sr_fit <- sr_spec %>% fit(Surv(time, status) ~ ., data = lung_train) sr_fit

The holdout data can be predicted for survival probability at different time points as well as event time, linear predictor, quantile, and hazard.

r predict( sr_fit, lung_test, type = "survival", eval_time = c(100, 500, 1000) ) %>% slice(1) %>% tidyr::unnest(col = .pred) predict(sr_fit, lung_test, type = "time") predict(sr_fit, lung_test, type = "linear_pred") predict(sr_fit, lung_test, type = "quantile") %>% slice(1) %>% tidyr::unnest(col = .pred) predict(sr_fit, lung_test, type = "hazard", eval_time = c(100, 500, 1000)) %>% slice(1) %>% tidyr::unnest(col = .pred)

With the "flexsurv" engine

r knitr::spin_child("template-lung.R")

We can define the model with specific parameters:

r sr_spec <- survival_reg(dist = "weibull") %>% set_engine("flexsurv") %>% set_mode("censored regression") sr_spec

Now we create the model fit object:

r set.seed(1) sr_fit <- sr_spec %>% fit(Surv(time, status) ~ age + sex + ph.ecog, data = lung_train) sr_fit

The holdout data can be predicted for survival probability at different time points as well as event time, linear predictor, quantile, and hazard.

r predict( sr_fit, lung_test, type = "survival", eval_time = c(100, 500, 1000) ) %>% slice(1) %>% tidyr::unnest(col = .pred) predict(sr_fit, lung_test, type = "time") predict(sr_fit, lung_test, type = "linear_pred") predict(sr_fit, lung_test, type = "quantile") %>% slice(1) %>% tidyr::unnest(col = .pred) predict(sr_fit, lung_test, type = "hazard", eval_time = c(100, 500, 1000)) %>% slice(1) %>% tidyr::unnest(col = .pred)

With the "flexsurvspline" engine

r knitr::spin_child("template-lung.R")

We can define the model:

r sr_spec <- survival_reg() %>% set_engine("flexsurvspline") %>% set_mode("censored regression") sr_spec

Now we create the model fit object:

r set.seed(1) sr_fit <- sr_spec %>% fit(Surv(time, status) ~ age + sex + ph.ecog, data = lung_train) sr_fit

The holdout data can be predicted for survival probability at different time points as well as event time, linear predictor, quantile, and hazard.

r predict( sr_fit, lung_test, type = "survival", eval_time = c(100, 500, 1000) ) %>% slice(1) %>% tidyr::unnest(col = .pred) predict(sr_fit, lung_test, type = "time") predict(sr_fit, lung_test, type = "linear_pred") predict(sr_fit, lung_test, type = "quantile") %>% slice(1) %>% tidyr::unnest(col = .pred) predict(sr_fit, lung_test, type = "hazard", eval_time = c(100, 500, 1000)) %>% slice(1) %>% tidyr::unnest(col = .pred)



EmilHvitfeldt/survnip documentation built on April 23, 2024, 3:07 p.m.