Skip to contents

cross_validate() estimates how well a model predicts for new subjects. It splits the rows into k folds, fits the model to all but one fold, scores the predictions on the fold left out, and repeats this for every fold. Scoring a model on the data it was fit to overstates its accuracy, and the held-out folds avoid that.

Usage

cross_validate(
  model,
  formula,
  data,
  ...,
  k = 5,
  metrics = "concordance",
  times = NULL,
  stratified = TRUE
)

Arguments

model

The model function: cox_ph(), cox_net(), aft(), royston_parmar(), survival_tree(), random_survival_forest(), extra_survival_trees(), or gradient_boosting_survival_analysis().

formula

The model formula, such as Surv(time, status) ~ age + sex. The response is right-censored data.

data

A data frame holding the variables in formula.

...

Further arguments for model, such as dist = "weibull" for aft(). A weights argument must name a column of data, so it can be split along with the rows.

k

The number of folds, at least 2 and at most the number of rows. The default is 5.

metrics

One or more of "concordance" (the default), "brier", and "auc".

times

At least two times, for the "brier" and "auc" metrics.

stratified

Draw the folds within each event status. The default is TRUE.

Value

A greenwood_cv object. results is a data frame with one row per metric: metric, mean (the average over the folds), and sd (the standard deviation over the folds). scores is a data frame with one row per fold and metric: fold, metric, and score. folds gives the fold of each row used, k the number of folds, and n_dropped the number of rows dropped for missing values. as.data.frame() gives results.

Details

The metrics are Harrell's concordance ("concordance", see concordance_index()), the integrated Brier score over times ("brier", see integrated_brier_score()), and the integrated time-dependent AUC over times ("auc", see integrated_auc()). Higher is better for the concordance and the AUC, and lower is better for the Brier score. Every metric is computed on the same folds from the same fits. The concordance and the AUC rank subjects by the linear predictor of a cox_ph(), cox_net(), or royston_parmar() fit, by the negative linear predictor of an aft() fit (where a larger value means longer survival), and by the risk score of a survival_tree(), random_survival_forest(), extra_survival_trees(), or gradient_boosting_survival_analysis() fit.

With stratified = TRUE (the default), the folds are drawn separately among the subjects with each event status (censored, or each kind of event), so every fold gets about the same share of events. Folds with few events give unstable fits and scores, and a warning is given when there are fewer than two events per fold. Rows with a missing value in any variable of formula are dropped before the folds are drawn.

The folds are drawn from R's random number generator, so call set.seed() first for results that can be repeated.

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),
  status = c(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),
  sex = c(1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1)
)

set.seed(1)
cross_validate(cox_ph, Surv(time, status) ~ age + sex, data = lung, k = 4)
#> <cross_validate> Surv(time, status) ~ age + sex
#> 4-fold cross-validation of cox_ph()
#> 
#>       metric   mean         sd
#>  concordance 0.6125 0.06291529

# Several metrics from the same folds
cross_validate(aft, Surv(time, status) ~ age + sex,
  data = lung, dist = "weibull", k = 4, metrics = c("concordance", "brier"),
  times = c(150, 300, 450)
)
#> <cross_validate> Surv(time, status) ~ age + sex
#> 4-fold cross-validation of aft()
#> 
#>       metric      mean         sd
#>  concordance 0.5625000 0.11086779
#>        brier 0.2197758 0.02927669