Skip to contents

kaplan_meier() estimates the survival function from censored event times with the product-limit method. It gives one curve for the whole sample, or one curve per group when the right-hand side of the formula names grouping variables.

Usage

kaplan_meier(
  formula,
  data,
  weights,
  subset,
  na.action = stats::na.omit,
  conf_type = c("log", "log-log", "plain"),
  conf_level = 0.95,
  robust = FALSE,
  cluster
)

Arguments

formula

A formula with a Surv() response on the left and either 1 (one curve) or one or more grouping variables on the right, such as Surv(time, status) ~ sex.

data

A data frame holding the variables in formula.

weights

Optional case weights, a column of data or a numeric vector.

subset

An optional expression selecting the rows of data to use.

na.action

What to do with rows that have missing values. The default drops them.

conf_type

The scale for confidence intervals: "log" (the default), "log-log", or "plain".

conf_level

The confidence level, between 0 and 1. The default is 0.95.

robust

Use the robust (infinitesimal jackknife) variance instead of Greenwood's formula. The default is FALSE, or TRUE when cluster is given.

cluster

Optional cluster identifiers, a column of data or a vector, for observations that are correlated within clusters (such as repeated events per subject or patients within a center). Implies robust = TRUE.

Value

A kaplan_meier object. Use as.data.frame() for the estimate at every time, and median() or quantile() for survival times. Printing it shows the number of subjects, the number of events, and the median survival time with its confidence interval for each group.

Every table from a fit has the same layout whether it holds one curve or several. The first column, strata, labels the curve: "all" for a single curve, or name=value pairs such as "sex=1" or "sex=1, ph.ecog=0". The grouping variables follow as their own columns, so results can be filtered on them directly.

The curves come in the order of the grouping variables, not the order of the rows: a factor's levels, and otherwise sorted values, with numbers in numeric order and text in code-point order ("B" before "a") in every locale. With several grouping variables the first varies slowest. Every estimator and test that works by group uses the same order.

Details

The estimate at time \(t\) is the product of \((1 - d_i / n_i)\) over the event times \(t_i \le t\), where \(n_i\) is the number at risk and \(d_i\) the number of events. Standard errors use Greenwood's formula by default. With robust = TRUE or a cluster, they use the infinitesimal jackknife (a sandwich estimator) instead, which stays valid for non-integer weights and, with cluster, for observations that are correlated within clusters. Confidence intervals are built on the log scale by default, or on the log-log or plain (identity) scale.

Right-censored data and counting-process data are supported. With counting-process data, Surv(start, stop, event), a subject is at risk only after its start time, which handles delayed entry (left truncation).

Examples

lung <- data.frame(
  time = c(306, 455, 1010, 210, 883, 1022, 310, 361, 218, 166),
  status = c(2, 2, 1, 2, 2, 1, 2, 2, 2, 2),
  sex = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2)
)

fit <- kaplan_meier(Surv(time, status) ~ 1, data = lung)
fit
#> <kaplan_meier> Surv(time, status) ~ 1
#> 
#>      n events median 0.95 LCL 0.95 UCL
#> all 10      8    310      218       NA

# The estimate at every time
as.data.frame(fit)
#>    strata time n_risk n_event n_censor estimate  std_error   conf_low conf_high
#> 1     all  166     10       1        0      0.9 0.09486833 0.73201164 1.0000000
#> 2     all  210      9       1        0      0.8 0.12649111 0.58681770 1.0000000
#> 3     all  218      8       1        0      0.7 0.14491377 0.46653323 1.0000000
#> 4     all  306      7       1        0      0.6 0.15491933 0.36171953 0.9952462
#> 5     all  310      6       1        0      0.5 0.15811388 0.26902736 0.9292735
#> 6     all  361      5       1        0      0.4 0.15491933 0.18723673 0.8545332
#> 7     all  455      4       1        0      0.3 0.14491377 0.11639970 0.7731979
#> 8     all  883      3       1        0      0.2 0.12649111 0.05790057 0.6908394
#> 9     all 1010      2       0        1      0.2 0.12649111 0.05790057 0.6908394
#> 10    all 1022      1       0        1      0.2 0.12649111 0.05790057 0.6908394

# One curve per group
kaplan_meier(Surv(time, status) ~ sex, data = lung)
#> <kaplan_meier> Surv(time, status) ~ sex
#> 
#>       n events median 0.95 LCL 0.95 UCL
#> sex=1 5      4    455      306       NA
#> sex=2 5      4    310      218       NA