Skip to contents

predict() gives the linear predictor, the risk score, or the survival curve of each row of newdata (or of the data the model was fit to). The linear predictor is \((x - \bar{x})^\top \beta\), centered at the weighted means of the covariates in the fitted data, and the risk score is its exponential. The survival curve is \(\exp(-H_0(t) \exp(\eta))\), with \(H_0\) Breslow's estimate of the baseline cumulative hazard (for the centered linear predictor \(\eta\)) at the penalized coefficients. as.data.frame() gives the coefficients and hazard ratios, with the coefficients set to 0 by the lasso kept in the table.

Usage

# S3 method for class 'cox_net'
predict(object, newdata, type = c("lp", "risk", "survival"), times, ...)

# S3 method for class 'cox_net'
as.data.frame(x, row.names = NULL, optional = FALSE, ...)

Arguments

object, x

A cox_net() fit.

newdata

A data frame with the covariates of the subjects to predict for. The default is the data the model was fit to.

type

What to predict: "lp" (the default), "risk", or "survival".

times

For "survival", the times to read the curves at. The default is every distinct time in the fitted data.

...

Unused.

row.names, optional

Unused. They're part of the as.data.frame() generic.

Value

For "lp" and "risk", a numeric vector with one value per row of newdata. For "survival", a data frame with one row per subject and time: subject (the row of newdata), time, and estimate. as.data.frame() gives a data frame with the columns term, estimate, and hazard_ratio.

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)
)
fit <- cox_net(Surv(time, status) ~ age + sex, data = lung, penalizer = 0.05)
as.data.frame(fit)
#>   term    estimate hazard_ratio
#> 1  age -0.04632768    0.9547291
#> 2  sex -0.02661094    0.9737400
predict(fit, type = "risk")
#>  [1] 0.6109743 0.8067558 1.4066316 1.3429520 1.1686943 0.6109743 0.7855704
#>  [8] 0.6836369 1.6163668 1.1157864 1.3429520 0.7855704 0.7855704 1.1686943
#> [15] 1.3429520 0.8450102 0.7353640 1.0170478 1.4066316 1.3429520
predict(fit, newdata = data.frame(age = 60, sex = 2), type = "survival", times = c(200, 400))
#>   subject time  estimate
#> 1       1  200 0.7255483
#> 2       1  400 0.4333906