Skip to contents

predict() gives the linear predictor, the risk score, or the survival curve or cumulative hazard of each row of newdata (or of the data the model was fit to).

Usage

# S3 method for class 'cox_ph'
predict(
  object,
  newdata,
  type = c("lp", "risk", "survival", "cumhaz", "quantile", "expectation"),
  times,
  conditional_after = NULL,
  trajectory = NULL,
  p = 0.5,
  tau,
  conf_level = object$conf_level,
  ...
)

Arguments

object

A cox_ph() fit.

newdata

A data frame with the covariates (and any stratifying variables) of the subjects to predict for. The default is the data the model was fit to.

type

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

times

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

conditional_after

For "survival" and "cumhaz", an optional time to condition on surviving to.

trajectory

A data frame with the path of covariates of one subject, in place of newdata: one row per interval, in time order and not overlapping, with its start tstart, its end tstop, and the covariates (and any stratifying variables) over it. Only for "survival" (the default with trajectory) and "cumhaz", and not with conditional_after.

p

For "quantile", the proportions of subjects who have had the event, between 0 and 1. The default, 0.5, gives the median.

tau

For "expectation", the end of the window for the restricted mean.

conf_level

The confidence level. The default is the level the model was fit with.

...

Unused.

Value

For "lp" and "risk", a numeric vector with one value per row of newdata. For "survival" and "cumhaz", a data frame with one row per subject and time: subject (the row of newdata), strata, time, estimate, std_error, conf_low, and conf_high. With trajectory, subject is 1 and strata is the stratum of the interval in effect at each time. For "quantile", a data frame with one row per subject and proportion: subject, strata, p, estimate, conf_low, and conf_high. For "expectation", one row per subject: subject, strata, tau, estimate, conf_low, and conf_high.

Details

The linear predictor is \((x - \bar{x})^\top \beta\), centered at the weighted means \(\bar{x}\) of the covariates in the fitted data, and the risk score is its exponential. The cumulative hazard of a subject is the baseline cumulative hazard of its stratum (Breslow's estimator, or Efron's when the model uses Efron's ties) times \(\exp(x^\top \beta)\), and its survival is \(\exp(-H(t))\). The standard error of the cumulative hazard accounts for the uncertainty in both the baseline hazard and \(\beta\). Confidence limits are built on the log scale of the cumulative hazard and of survival, with the upper survival limit capped at 1.

With conditional_after, the curve is conditional on surviving to that time: the cumulative hazard counts only the event times at or after it, so survival is \(S(t) / S(c^-)\) for \(t \ge c\), and 1 before.

With trajectory, the prediction is for one subject whose covariates change over time, such as the measurements of a model fit to the episodes from split_episodes(). Each row of trajectory gives the covariates over an interval \((a_k, b_k]\), and the cumulative hazard adds up the baseline hazard over each interval times the risk score of its covariates, \(H(t) = \sum_k \exp(x_k^\top \beta) (H_0(\min(t, b_k)) - H_0(a_k))\) over the intervals that started before \(t\). No hazard builds up before the first interval or in a gap between two, and the last interval holds on past its end, with a warning when times go beyond it. The standard error accounts for the uncertainty in the baseline hazard and in \(\beta\) across all the intervals. A stratified model takes the stratifying variables from trajectory, and they can change from one interval to the next.

The "quantile" of a subject's survival curve for a proportion \(p\) is the first time at which survival drops to \(1 - p\) or below, and its confidence limits are the same times for the lower and upper confidence curves. It's NA when the curve never drops that far. The "expectation" is the restricted mean survival time, the area under the survival curve from 0 to tau, and its confidence limits are the areas under the confidence curves.

Examples

lung <- data.frame(
  time = c(306, 455, 1010, 210, 883, 1022, 310, 361, 218, 166, 170, 654, 728, 71, 567),
  status = c(2, 2, 1, 2, 2, 1, 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),
  sex = c(1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1)
)
fit <- cox_ph(Surv(time, status) ~ age + sex, data = lung)
predict(fit, type = "risk")
#>  [1] 0.6359330 0.7945925 1.2405398 1.1953319 1.0693551 0.6359330 1.0012110
#>  [8] 0.8956927 1.3866832 1.0303855 1.1953319 1.0012110 1.0012110 1.0693551
#> [15] 1.1953319

newdata <- data.frame(age = c(55, 70), sex = c(1, 2))
predict(fit, newdata = newdata, type = "survival", times = c(200, 400))
#>   subject strata time  estimate std_error  conf_low conf_high
#> 1       1    all  200 0.7624423 0.1358211 0.5377424         1
#> 2       1    all  400 0.3896045 0.1898316 0.1499281         1
#> 3       2    all  200 0.8221503 0.1190461 0.6190113         1
#> 4       2    all  400 0.5063175 0.1875519 0.2449728         1

# Survival beyond day 400 for subjects alive at day 200
predict(
  fit,
  newdata = newdata,
  type = "survival",
  times = c(300, 400),
  conditional_after = 200
)
#>   subject strata time  estimate std_error  conf_low conf_high
#> 1       1    all  300 0.7982035 0.1425193 0.5625117         1
#> 2       1    all  400 0.5109955 0.2052839 0.2325204         1
#> 3       2    all  300 0.8498147 0.1134757 0.6541288         1
#> 4       2    all  400 0.6158454 0.1771728 0.3504203         1

# Median survival time, and the mean survival time over two years
predict(fit, newdata = newdata, type = "quantile")
#>   subject strata   p estimate conf_low conf_high
#> 1       1    all 0.5      310      210        NA
#> 2       2    all 0.5      455      218        NA
predict(fit, newdata = newdata, type = "expectation", tau = 730)
#>   subject strata tau estimate conf_low conf_high
#> 1       1    all 730 387.7593 250.4895  717.2363
#> 2       2    all 730 450.1616 301.4649  730.0000

# Survival for a subject whose age is updated at day 300
path <- data.frame(tstart = c(0, 300), tstop = c(300, 800), age = c(60, 61), sex = 1)
predict(fit, trajectory = path, times = c(200, 400, 800))
#>   subject strata time  estimate std_error   conf_low conf_high
#> 1       1    all  200 0.7982919 0.1084293 0.61171041 1.0000000
#> 2       1    all  400 0.4632735 0.1505992 0.24498078 0.8760783
#> 3       1    all  800 0.2112000 0.1234958 0.06713787 0.6643855