Learn R Programming

broom (version 0.3.4)

survfit_tidiers: tidy survival curve fits

Description

Construct tidied data frames showing survival curves over time.

Usage

## S3 method for class 'survfit':
tidy(x, ...)

## S3 method for class 'survfit': glance(x, ...)

Arguments

x
"survfit" object
...
extra arguments, not used

Value

  • All tidying methods return a data.frame without rownames, whose structure depends on the method chosen.

    tidy returns a row for each time point, with columns

  • timetimepoint
  • n.risknumber of subjects at risk at time t0
  • n.eventnumber of events at time t
  • n.censornumber of censored events
  • estimateestimate of survival
  • std.errorstandard error of estimate
  • conf.highupper end of confidence interval
  • conf.lowlower end of confidence interval
  • glance returns one-row data.frame with the columns displayed by print.survfit
  • recordsnumber of observations
  • n.maxn.max
  • n.startn.start
  • eventsnumber of events
  • medianmedian survival
  • conf.lowlower end of confidence interval on median
  • conf.highupper end of confidence interval on median

Details

glance does not work on multi-state survival curves, since the values glance outputs would be calculated for each state. tidy does work for multi-state survival objects, and includes a state column to distinguish between them.

Examples

Run this code
if (require("survival", quietly = TRUE)) {
    cfit <- coxph(Surv(time, status) ~ age + sex, lung)
    sfit <- survfit(cfit)

    head(tidy(sfit))
    glance(sfit)

    library(ggplot2)
    ggplot(tidy(sfit), aes(time, estimate)) + geom_line() +
        geom_ribbon(aes(ymin=conf.low, ymax=conf.high), alpha=.25)

    # multi-state
    fitCI <- survfit(Surv(stop, status * as.numeric(event), type = "mstate") ~ 1,
                  data = mgus1, subset = (start == 0))
    td_multi <- tidy(fitCI)
    head(td_multi)
    tail(td_multi)
    ggplot(td_multi, aes(time, estimate, group = state)) +
        geom_line(aes(color = state)) +
        geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = .25)

    # perform simple bootstrapping
    library(dplyr)
    bootstraps <- lung %>% bootstrap(100) %>%
        do(tidy(survfit(coxph(Surv(time, status) ~ age + sex, .))))

    ggplot(bootstraps, aes(time, estimate, group = replicate)) +
        geom_line(alpha = .25)

    bootstraps_bytime <- bootstraps %>% group_by(time) %>%
        summarize(median = median(estimate),
                  low = quantile(estimate, .025),
                  high = quantile(estimate, .975))

    ggplot(bootstraps_bytime, aes(x = time, y = median)) + geom_line() +
        geom_ribbon(aes(ymin = low, ymax = high), alpha = .25)

    # bootstrap for median survival
    glances <- lung %>% bootstrap(100) %>%
        do(glance(survfit(coxph(Surv(time, status) ~ age + sex, .))))

    qplot(glances$median, binwidth = 15)
    quantile(glances$median, c(.025, .975))
}

Run the code above in your browser using DataLab