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 model type differentiates basic modeling approaches, such as random forests, proportional hazards models, etc.,
the mode denotes in what kind of modeling context it will be used (here, censored regression), and
the computational engine indicates how the model is fit, such as with a specific R package implementation or even methods outside of R like Keras or Stan.
The following examples use the same data set throughout.
bag_tree()
models"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"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"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")
"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"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")
"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"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")
"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"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)
"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)
"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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.