cox_net() fits the Cox proportional hazards model with an elastic-net penalty, for when there
are many covariates for the number of events, or covariates that are strongly correlated. The
coefficients minimize the negative log partial likelihood, divided by the total weight
\(W\) (the number of rows without case weights), plus the penalty
$$\lambda \left(\alpha \|\beta\|_1 + \frac{1 - \alpha}{2} \|\beta\|_2^2\right),$$
with \(\lambda\) the penalizer and \(\alpha\) the l1_ratio. l1_ratio = 1 is the lasso,
which sets some coefficients to exactly 0 and so selects covariates. l1_ratio = 0 is ridge
regression, which shrinks all of the coefficients toward 0 without removing any. Values in
between mix the two. Tied event times are handled with Breslow's method, and penalizer = 0
gives the unpenalized Cox fit with Breslow ties.
Usage
cox_net(
formula,
data,
weights,
subset,
na.action = stats::na.omit,
penalizer = 0.1,
l1_ratio = 0.5,
standardize = TRUE,
max_iter = 1000,
tol = 1e-07
)Arguments
- formula
A formula with a right-censored or counting-process
Surv()response on the left and the covariates on the right, such asSurv(time, status) ~ age + sex. Factors are coded with treatment contrasts. Collinear covariates are allowed.- data
A data frame holding the variables in
formula.- weights
Optional case weights, a column of
dataor a numeric vector.- 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.
- penalizer
The penalty strength \(\lambda\), a non-negative number. The default is
0.1.- l1_ratio
The elastic-net mixing \(\alpha\), between 0 (ridge) and 1 (lasso). The default is
0.5.- 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.
Value
A cox_net object. Use coef() or as.data.frame() for the coefficients and
predict() for linear predictors, risk scores, and survival curves. The object also holds
loglik (the log partial likelihood at the estimate), effective_df, n_nonzero, center
and scale (each covariate's weighted mean and the scale it was penalized on), n,
n_event, iterations, and converged.
Details
With standardize = TRUE (the default), each covariate is penalized on the scale of its
standard deviation, so the penalty treats all of them alike whatever their units. The
coefficients are reported on the scale of the covariates either way. The fit uses accelerated
proximal gradient descent (FISTA) from \(\beta = 0\), with a backtracking line search, and
stops once the relative change in the coefficients is below tol.
Penalized coefficients are biased toward 0 by design, so no standard errors or p-values are
given. Use cv_coxnet() to choose the penalizer. The effective degrees of freedom are the
number of nonzero coefficients for the lasso, and otherwise the trace of
\(I_A (I_A + W \lambda (1 - \alpha))^{-1}\), with \(I_A\) the information matrix of the
standardized nonzero coefficients. logLik() uses them, so AIC() and BIC() work, with
BIC() using the number of events.
See also
cv_coxnet() to choose the penalizer, and cox_ph() for the unpenalized model.
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, 1, 2, 2, 1, 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),
ecog = c(1, 0, 0, 1, 0, 1, 2, 2, 1, 2, 1, 2, 1, 1, 1, 2, 2, 1, 2, 1)
)
# The lasso, which sets some coefficients to 0
fit <- cox_net(Surv(time, status) ~ age + sex + ecog, data = lung, penalizer = 0.1,
l1_ratio = 1
)
fit
#> <cox_net> Surv(time, status) ~ age + sex + ecog
#> Elastic net, penalizer = 0.1, l1_ratio = 1. n = 20, events = 16
#>
#> estimate hazard_ratio
#> age -0.08225 0.9210
#> sex -0.37344 0.6884
#> ecog 0.95298 2.5934
#>
#> Nonzero coefficients: 3 of 3. Effective df = 3, log partial likelihood = -32.561
# Ridge regression shrinks every coefficient
ridge <- cox_net(Surv(time, status) ~ age + sex + ecog, data = lung, penalizer = 0.1,
l1_ratio = 0
)
as.data.frame(ridge)
#> term estimate hazard_ratio
#> 1 age -0.09645227 0.9080532
#> 2 sex -0.67648692 0.5083999
#> 3 ecog 1.17304839 3.2318295
# Survival curves for new subjects
newdata <- data.frame(age = c(55, 70), sex = c(1, 2), ecog = c(0, 2))
predict(ridge, newdata = newdata, type = "survival", times = c(200, 400))
#> subject time estimate
#> 1 1 200 0.8573579
#> 2 1 400 0.6155514
#> 3 2 200 0.8250481
#> 4 2 400 0.5453359