Skip to contents

Get the predicted survival probability at a given time. A convenience wrapper for survival::summary.survfit.

Usage

predict_surv_prob(
  object,
  df = NULL,
  conf.level = 0.95,
  times,
  scale = 1,
  extend = FALSE,
  ...
)

Arguments

object

An object of either

  • survival::coxph, or

  • Survival formula using survival::Surv

df

A data frame in which to interpret the variables in object.

  • For object = survival::coxph, may be NULL or the usual newdata style argument.

  • For object = formula, must be the data frame used for fitting in survival::survfit.

conf.level

The level for a two-sided confidence interval on the survival curve(s). Default is 0.95.

times

A numeric vector of times.

scale

A numeric value to rescale the survival time, e.g., if the input data to survfit were in days, scale = 365.25 would scale the output to years.

extend

TRUE or FALSE. if TRUE, prints information for all specified times, even if there are no subjects left at the end of the specified times.

...

Optional arguments passed to survival::survfit.

Value

data.frame

Examples

#----------------------------------------------------------------------------
# predict_surv_prob() examples.
#----------------------------------------------------------------------------
library(survival)
library(bkstat)

bkstat::predict_surv_prob(
  object = Surv(time, status) ~ 1,
  df = lung,
  times = c(12, 48),
  extend = TRUE
)
#>   time   n n_at_risk n_events n_censored surv_prob  lower_ci  upper_ci ci_level
#> 1   12 228       224        5          0 0.9780702 0.9592437 0.9972662     0.95
#> 2   48 228       217        6          0 0.9517544 0.9243423 0.9799794     0.95
#>   ci_type    estimator
#> 1     log Kaplan-Meier
#> 2     log Kaplan-Meier

bkstat::predict_surv_prob(
  object = coxph(Surv(time, status) ~ 1, lung),
  times = c(12, 48)
)
#>   time   n n_at_risk n_events surv_prob  lower_ci  upper_ci ci_level ci_type
#> 1   12 228       224        5 0.9781182 0.9593321 0.9972722     0.95     log
#> 2   48 228       217        6 0.9518600 0.9245056 0.9800238     0.95     log
#>                  estimator
#> 1 Cox Proportional Hazards
#> 2 Cox Proportional Hazards

bkstat::predict_surv_prob(
  object = Surv(time, status) ~ sex,
  df = lung,
  times = c(12, 48),
  extend = TRUE
)
#>   sex time   n n_at_risk n_events n_censored surv_prob  lower_ci  upper_ci
#> 1   1   12 138       135        4          0 0.9710145 0.9434235 0.9994124
#> 2   2   12  90        89        1          0 0.9888889 0.9674682 1.0000000
#> 3   1   48 138       128        6          0 0.9275362 0.8852745 0.9718155
#> 4   2   48  90        89        0          0 0.9888889 0.9674682 1.0000000
#>   ci_level ci_type    estimator
#> 1     0.95     log Kaplan-Meier
#> 2     0.95     log Kaplan-Meier
#> 3     0.95     log Kaplan-Meier
#> 4     0.95     log Kaplan-Meier

bkstat::predict_surv_prob(
  object = coxph(Surv(time, status) ~ sex, lung),
  df = data.frame(expand.grid(sex = 1:2)),
  times = c(12, 48)
)
#>   sex time   n n_at_risk n_events surv_prob  lower_ci  upper_ci ci_level
#> 1   1   12 228       224        5 0.9739246 0.9515103 0.9968670     0.95
#> 2   2   12 228       224        5 0.9845842 0.9707930 0.9985714     0.95
#> 3   1   48 228       217        6 0.9426477 0.9099254 0.9765468     0.95
#> 4   2   48 228       217        6 0.9658671 0.9447602 0.9874456     0.95
#>   ci_type                estimator
#> 1     log Cox Proportional Hazards
#> 2     log Cox Proportional Hazards
#> 3     log Cox Proportional Hazards
#> 4     log Cox Proportional Hazards

bkstat::predict_surv_prob(
  object = Surv(time, status) ~ sex + ph.ecog,
  df = lung,
  times = 48,
  extend = TRUE
)
#>   sex ph.ecog time  n n_at_risk n_events n_censored surv_prob  lower_ci
#> 1   1       0   48 36        33        3          0 0.9166667 0.8306861
#> 2   1       1   48 71        69        2          0 0.9718310 0.9340973
#> 3   1       2   48 29        24        5          0 0.8275862 0.7009179
#> 4   1       3   48  1         1        0          0 1.0000000 1.0000000
#> 5   2       0   48 27        26        1          0 0.9629630 0.8942996
#> 6   2       1   48 42        42        0          0 1.0000000 1.0000000
#> 7   2       2   48 21        21        0          0 1.0000000 1.0000000
#>    upper_ci ci_level ci_type    estimator
#> 1 1.0000000     0.95     log Kaplan-Meier
#> 2 1.0000000     0.95     log Kaplan-Meier
#> 3 0.9771457     0.95     log Kaplan-Meier
#> 4 1.0000000     0.95     log Kaplan-Meier
#> 5 1.0000000     0.95     log Kaplan-Meier
#> 6 1.0000000     0.95     log Kaplan-Meier
#> 7 1.0000000     0.95     log Kaplan-Meier

bkstat::predict_surv_prob(
  object = coxph(Surv(time, status) ~ sex + ph.ecog, lung),
  df = data.frame(expand.grid(sex = 1:2, ph.ecog = 0:3)),
  times = 48
)
#>   sex ph.ecog time   n n_at_risk n_events surv_prob  lower_ci  upper_ci
#> 1   1       0   48 227       216       11 0.9652506 0.9432245 0.9877910
#> 2   1       1   48 227       216       11 0.9440402 0.9120054 0.9772003
#> 3   1       2   48 227       216       11 0.9104975 0.8587416 0.9653727
#> 4   1       3   48 227       216       11 0.8584133 0.7692114 0.9579595
#> 5   2       0   48 227       216       11 0.9798608 0.9661619 0.9937539
#> 6   2       1   48 227       216       11 0.9674166 0.9471580 0.9881086
#> 7   2       2   48 227       216       11 0.9474920 0.9144093 0.9817716
#> 8   2       3   48 227       216       11 0.9159244 0.8578189 0.9779657
#>   ci_level ci_type                estimator
#> 1     0.95     log Cox Proportional Hazards
#> 2     0.95     log Cox Proportional Hazards
#> 3     0.95     log Cox Proportional Hazards
#> 4     0.95     log Cox Proportional Hazards
#> 5     0.95     log Cox Proportional Hazards
#> 6     0.95     log Cox Proportional Hazards
#> 7     0.95     log Cox Proportional Hazards
#> 8     0.95     log Cox Proportional Hazards

bkstat::predict_surv_prob(
  object = coxph(Surv(time, status) ~ 1, lung),
  df = data.frame(expand.grid(sex = 1:2, ph.ecog = 0:3)),
  times = c(12, 48)
)
#>   sex ph.ecog time   n n_at_risk n_events surv_prob  lower_ci  upper_ci
#> 1   1       0   12 228       224        5 0.9781182 0.9593321 0.9972722
#> 2   1       1   12 228       224        5 0.9781182 0.9593321 0.9972722
#> 3   1       2   12 228       224        5 0.9781182 0.9593321 0.9972722
#> 4   1       3   12 228       224        5 0.9781182 0.9593321 0.9972722
#> 5   2       0   48 228       217        6 0.9518600 0.9245056 0.9800238
#> 6   2       1   48 228       217        6 0.9518600 0.9245056 0.9800238
#> 7   2       2   48 228       217        6 0.9518600 0.9245056 0.9800238
#> 8   2       3   48 228       217        6 0.9518600 0.9245056 0.9800238
#>   ci_level ci_type                estimator
#> 1     0.95     log Cox Proportional Hazards
#> 2     0.95     log Cox Proportional Hazards
#> 3     0.95     log Cox Proportional Hazards
#> 4     0.95     log Cox Proportional Hazards
#> 5     0.95     log Cox Proportional Hazards
#> 6     0.95     log Cox Proportional Hazards
#> 7     0.95     log Cox Proportional Hazards
#> 8     0.95     log Cox Proportional Hazards