Skip to contents

Compute the gradient of the log partial likelihood at a particular fit for Cox model.

Usage

coxgrad(
  eta,
  y,
  w,
  std.weights = TRUE,
  diag.hessian = FALSE,
  cox.ties = c("breslow", "efron")
)

Arguments

eta

Fit vector (usually from glmnet at a particular lambda).

y

Survival response variable, must be a Surv or stratifySurv object.

w

Observation weights (default is all equal to 1).

std.weights

If TRUE (default), observation weights are standardized to sum to 1.

diag.hessian

If TRUE, compute the diagonal of the Hessian of the deviance (minus twice the log partial likelihood) as well. Default is FALSE.

cox.ties

Character; the method for handling ties. One of "breslow" (the current default) or "efron". The default will change to "efron" in glmnet 5.1 to match survival::coxph.

Value

A single gradient vector the same length as eta. If diag.hessian=TRUE, the diagonal of the Hessian of the deviance with respect to eta is included as an attribute "diag_hessian". It is on the deviance scale while the gradient is on the log partial likelihood scale, so it equals -2 times the derivative of the gradient.

Details

Compute a gradient vector at the fitted vector for the log partial likelihood. This is like a residual vector, and useful for manual screening of predictors for glmnet in applications where p is very large (as in GWAS).

Uses the C++ coxdev library for computation, supporting both Breslow and Efron methods for ties, as well as stratified and (start, stop] data.

See also

coxnet.deviance

Examples

set.seed(1)
eta <- rnorm(10)
time <- runif(10, min = 1, max = 10)
d <- ifelse(rnorm(10) > 0, 1, 0)
y <- survival::Surv(time, d)
coxgrad(eta, y)
#>  [1] -0.06992882 -0.01679888  0.04327001  0.03107979  0.06179160  0.05843140
#>  [7]  0.08926450 -0.03192038 -0.23269137  0.06750215

# return diagonal of Hessian as well
coxgrad(eta, y, diag.hessian = TRUE)
#>  [1] -0.06992882 -0.01679888  0.04327001  0.03107979  0.06179160  0.05843140
#>  [7]  0.08926450 -0.03192038 -0.23269137  0.06750215
#> attr(,"diag_hessian")
#>  [1] 0.12142788 0.03076665 0.10133077 0.09018764 0.06557935 0.07577252
#>  [7] 0.01916598 0.18368038 0.26131813 0.05895142

# example with (start, stop] data
y2 <- survival::Surv(time, time + runif(10), d)
coxgrad(eta, y2)
#>  [1]  0.000000e+00 -4.635984e-02  2.775558e-17  1.387779e-17  4.635984e-02
#>  [6]  8.261801e-02 -5.551115e-17 -1.565746e-01  0.000000e+00  7.395658e-02

# example with strata
y2 <- stratifySurv(y, rep(1:2, length.out = 10))
coxgrad(eta, y2)
#>  [1] -0.04165091 -0.01278177  0.06621056  0.04756065  0.04227714 -0.03552950
#>  [7]  0.07175852 -0.06887459 -0.13859532  0.06962520