Random survival forests and extra survival trees
Source:R/survival-tree.R
random_survival_forest.Rdrandom_survival_forest() grows n_estimators log-rank survival trees (see survival_tree()),
each on a bootstrap sample of the subjects (drawn with replacement), with a random subset of
max_features covariates tried at each split. A prediction is the average of the trees' curves,
and the risk score is the average cumulative hazard summed over the event times (the ensemble
mortality), larger for an earlier event. Averaging many trees that differ by chance gives much
steadier predictions than any one of them, while still finding nonlinear effects and
interactions without assuming proportional hazards (Ishwaran et al., 2008).
Usage
random_survival_forest(
formula,
data,
subset,
na.action = stats::na.omit,
n_estimators = 100,
max_depth = NULL,
min_samples_split = 6,
min_samples_leaf = 3,
max_features = "sqrt",
bootstrap = TRUE,
oob_score = FALSE
)
extra_survival_trees(
formula,
data,
subset,
na.action = stats::na.omit,
n_estimators = 100,
max_depth = NULL,
min_samples_split = 6,
min_samples_leaf = 3,
max_features = "sqrt",
bootstrap = FALSE,
oob_score = FALSE
)Arguments
- formula
A formula with a right-censored
Surv()response on the left and the covariates on the right, such asSurv(time, status) ~ age + sex. Factors are coded with treatment contrasts, and each column is split on separately.- data
A data frame holding the variables in
formula.- subset
An optional expression selecting the rows of
datato use.- na.action
What to do with rows that have missing values. The default drops them.
- n_estimators
The number of trees, at least 1. The default is
100.- max_depth
The largest number of splits from the first group to a leaf, or
NULL(the default) for no limit.- min_samples_split
The smallest group that can be split, at least 2. The default is
6.- min_samples_leaf
The fewest subjects on each side of a split, at least 1. The default is
3.- max_features
The number of covariates tried at each split:
"sqrt"(the square root of the number of columns, rounded down, the default),"log2",NULL(all of them), a whole number, or a fraction between 0 and 1 of the columns.- bootstrap
Whether to grow each tree on a bootstrap sample. The default is
TRUEforrandom_survival_forest()andFALSEforextra_survival_trees(). The out-of-bag concordance andvariable_importance()need it.- oob_score
Whether to compute the out-of-bag concordance. The default is
FALSE.
Value
A random_survival_forest or extra_survival_trees object (both are
survival_forest objects). Use predict() for risk scores and survival or cumulative hazard
curves, and variable_importance() for the permutation importance of each covariate. The
object also holds times (the event times the curves are given at), oob_score (the
out-of-bag concordance, or NA), n, and n_event.
Details
extra_survival_trees() grows extremely randomized trees: each covariate tried at a split gets
a single cut-point drawn at random between its smallest and largest values, and the best of
those is used. Following the extra-trees convention, each tree is grown on all of the subjects
(bootstrap = FALSE) by default.
With bootstrap samples, each subject is left out of about a third of the trees. The out-of-bag
concordance (with oob_score = TRUE) scores each subject with only those trees, which estimates
how well the forest ranks new subjects without setting data aside, and variable_importance()
measures how much worse that score gets when a covariate's values are shuffled. The forests use
R's random number generator, so call set.seed() first for results that can be repeated.
References
Ishwaran, H., Kogalur, U. B., Blackstone, E. H., and Lauer, M. S. (2008). Random survival forests. The Annals of Applied Statistics, 2(3), 841-860.
Geurts, P., Ernst, D., and Wehenkel, L. (2006). Extremely randomized trees. Machine Learning, 63(1), 3-42.
Examples
lung <- data.frame(
time = c(306, 455, 1010, 210, 883, 1022, 310, 361, 218, 166, 170, 654, 728, 71, 567, 144,
613, 707, 61, 88, 301, 81, 624, 371, 394, 520, 574, 118, 390, 12),
status = c(2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2),
age = c(74, 68, 56, 57, 60, 74, 68, 71, 53, 61, 57, 68, 68, 60, 57, 67, 70, 63, 56, 57, 67,
49, 50, 58, 72, 70, 60, 70, 53, 74),
ph.ecog = c(1, 0, 0, 1, 0, 1, 2, 2, 1, 2, 1, 2, 2, 1, 1, 2, 1, 0, 1, 1, 2, 0, 0, 1, 1, 1, 1,
2, 1, 1),
sex = c(1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 2, 1, 1,
1, 1)
)
set.seed(1)
forest <- random_survival_forest(Surv(time, status) ~ age + ph.ecog + sex,
data = lung, n_estimators = 50, oob_score = TRUE
)
forest
#> <random_survival_forest> Surv(time, status) ~ age + ph.ecog + sex
#> 50 log-rank trees, 1 of 3 features tried per split, bootstrap samples. n = 30, events = 26
#> Out-of-bag concordance = 0.4926
# The risk scores and survival curves of two new subjects
newdata <- data.frame(age = c(55, 72), ph.ecog = c(0, 2), sex = c(2, 1))
predict(forest, newdata = newdata)
#> [1] 16.26656 20.30964
predict(forest, newdata = newdata, type = "survival", times = c(100, 300))
#> subject time estimate
#> 1 1 100 0.6943120
#> 2 1 300 0.6278376
#> 3 2 100 0.9467694
#> 4 2 300 0.6448585
# Extra trees, grown on bootstrap samples so they can be scored out of bag
extra <- extra_survival_trees(Surv(time, status) ~ age + ph.ecog + sex,
data = lung, n_estimators = 50, bootstrap = TRUE, oob_score = TRUE
)
variable_importance(extra, n_repeats = 3)
#> term importance
#> 1 ph.ecog 0.09150327
#> 2 age 0.05882353
#> 3 sex 0.02042484