Skip to contents

cv_coxnet() fits cox_net() at each of a sequence of penalizers, scoring each on held-out folds, so the penalizer can be chosen for how well the model predicts for new subjects. The rows are split into k folds. At each penalizer, the model is fit to all but one fold and scored on the fold left out, for every fold. The score is Harrell's concordance of the linear predictor ("concordance", higher is better, see concordance_index()), or the integrated Brier score over times ("brier", lower is better, see integrated_brier_score()).

Usage

cv_coxnet(
  formula,
  data,
  weights,
  subset,
  na.action = stats::na.omit,
  l1_ratio = 1,
  penalizers = NULL,
  n_penalizers = 50,
  eps = 0.001,
  k = 5,
  metric = c("concordance", "brier"),
  times = NULL,
  standardize = TRUE,
  max_iter = 1000,
  tol = 1e-07,
  stratified = TRUE
)

Arguments

formula

A formula with a right-censored Surv() response on the left and the covariates on the right, such as Surv(time, status) ~ age + sex.

data

A data frame holding the variables in formula.

weights

Optional case weights, a column of data or a numeric vector.

subset

An optional expression selecting the rows of data to use.

na.action

What to do with rows that have missing values. The default drops them.

l1_ratio

The elastic-net mixing \(\alpha\), between 0 (ridge) and 1 (lasso), the same for every penalizer. The default is 1.

penalizers

The penalizers to try. The default, NULL, gives the sequence described above.

n_penalizers

The number of penalizers in the default sequence. The default is 50.

eps

The ratio of the smallest penalizer in the default sequence to the largest. The default is 1e-3.

k

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

metric

The score: "concordance" (the default) or "brier".

times

At least two times, for the "brier" metric.

standardize

Whether to penalize each covariate on the scale of its standard deviation. The default is TRUE.

max_iter

The largest number of iterations. The default is 1000.

tol

The convergence tolerance for the relative change in the coefficients. The default is 1e-7.

stratified

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

Value

A cv_coxnet object. path is a data frame with one row per penalizer, from the largest to the smallest: penalizer, mean_score and std_score (the mean and standard deviation of the scores over the folds), and n_nonzero (the mean number of nonzero coefficients over the folds). scores has one row per penalizer and fold: penalizer, fold, score, and n_nonzero. best_penalizer, best_score, penalizer_1se, and score_1se give the choices, and folds the fold of each row used. as.data.frame() gives path.

Details

Without penalizers, the sequence runs from \(\lambda_{max}\) down to eps times it, in n_penalizers steps equally spaced on the log scale. \(\lambda_{max}\) is the smallest lasso penalizer that sets every coefficient to 0, \(\max_j |U_j(0)| / (W \max(\alpha, 0.01))\), with \(U(0)\) the score of the log partial likelihood of the standardized covariates at \(\beta = 0\) and \(W\) the total weight.

The best penalizer has the best mean score over the folds. The one-standard-error penalizer is the largest one whose mean score is within one standard deviation (over the folds) of the best, giving a simpler model that predicts about as well. Refit cox_net() at the penalizer chosen.

With stratified = TRUE (the default), the folds are drawn separately among the subjects with and without an event, so every fold gets about the same share of events. Folds whose training or held-out rows have no events are left out of the scores. The folds are drawn from R's random number generator, so call set.seed() first for results that can be repeated.

Examples

set.seed(4)
n <- 60
sim <- data.frame(x1 = rnorm(n), x2 = rnorm(n), x3 = rnorm(n), x4 = rnorm(n))
sim$time <- round(rexp(n, rate = 0.1 * exp(0.8 * sim$x1 - 0.5 * sim$x2)), 1) + 0.1
sim$status <- rbinom(n, 1, 0.8)

set.seed(1)
cv <- cv_coxnet(Surv(time, status) ~ x1 + x2 + x3 + x4, data = sim, n_penalizers = 10, k = 3)
cv
#> <cv_coxnet> Surv(time, status) ~ x1 + x2 + x3 + x4
#> 3-fold cross-validation, l1_ratio = 1, concordance (higher is better)
#> 10 penalizers from 0.0004975 to 0.4975
#> 
#> Best penalizer:  0.1072 (mean concordance 0.7143)
#> 1-SE penalizer:  0.2309 (mean concordance 0.7121)
as.data.frame(cv)
#>       penalizer mean_score  std_score n_nonzero
#> 1  0.4975352191  0.5784314 0.13584712 0.3333333
#> 2  0.2309353917  0.7121037 0.02024123 1.0000000
#> 3  0.1071907136  0.7143408 0.01816525 1.3333333
#> 4  0.0497535219  0.7056686 0.02254011 2.3333333
#> 5  0.0230935392  0.6995099 0.02012957 3.6666667
#> 6  0.0107190714  0.6947593 0.03367624 3.6666667
#> 7  0.0049753522  0.6950356 0.02749477 3.6666667
#> 8  0.0023093539  0.6950356 0.02749477 4.0000000
#> 9  0.0010719071  0.6950356 0.02749477 4.0000000
#> 10 0.0004975352  0.6930748 0.02518905 4.0000000

# Refit at the penalizer chosen
cox_net(Surv(time, status) ~ x1 + x2 + x3 + x4, data = sim, penalizer = cv$best_penalizer,
  l1_ratio = 1
)
#> <cox_net> Surv(time, status) ~ x1 + x2 + x3 + x4
#> Elastic net, penalizer = 0.1071907, l1_ratio = 1. n = 60, events = 52
#> 
#>    estimate hazard_ratio
#> x1    0.792        2.208
#> x2    0.000        1.000
#> x3    0.000        1.000
#> x4    0.000        1.000
#> 
#> Nonzero coefficients: 1 of 4. Effective df = 1, log partial likelihood = -149.63