| 1 |
#' @templateVar class survfit |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x An `survfit` object returned from [survival::survfit()]. |
| 5 |
#' @param ... For `glance.survfit()`, additional arguments passed |
| 6 |
#' to [summary()]. Otherwise ignored. |
| 7 |
#' |
| 8 |
#' @evalRd return_tidy( |
| 9 |
#' "time", |
| 10 |
#' "n.risk", |
| 11 |
#' "n.event", |
| 12 |
#' "n.censor", |
| 13 |
#' estimate = "estimate of survival or cumulative incidence rate when |
| 14 |
#' multistate", |
| 15 |
#' "std.error", |
| 16 |
#' "conf.low", |
| 17 |
#' "conf.high", |
| 18 |
#' state = "state if multistate survfit object input", |
| 19 |
#' strata = "strata if stratified survfit object input" |
| 20 |
#' ) |
| 21 |
#' |
| 22 |
#' @examplesIf rlang::is_installed(c("survival", "ggplot2")) |
| 23 |
#' |
| 24 |
#' # load libraries for models and data |
| 25 |
#' library(survival) |
| 26 |
#' |
| 27 |
#' # fit model |
| 28 |
#' cfit <- coxph(Surv(time, status) ~ age + sex, lung) |
| 29 |
#' sfit <- survfit(cfit) |
| 30 |
#' |
| 31 |
#' # summarize model fit with tidiers + visualization |
| 32 |
#' tidy(sfit) |
| 33 |
#' glance(sfit) |
| 34 |
#' |
| 35 |
#' library(ggplot2) |
| 36 |
#' |
| 37 |
#' ggplot(tidy(sfit), aes(time, estimate)) + |
| 38 |
#' geom_line() + |
| 39 |
#' geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = .25) |
| 40 |
#' |
| 41 |
#' # multi-state |
| 42 |
#' fitCI <- survfit(Surv(stop, status * as.numeric(event), type = "mstate") ~ 1, |
| 43 |
#' data = mgus1, subset = (start == 0) |
| 44 |
#' ) |
| 45 |
#' |
| 46 |
#' td_multi <- tidy(fitCI) |
| 47 |
#' |
| 48 |
#' td_multi |
| 49 |
#' |
| 50 |
#' ggplot(td_multi, aes(time, estimate, group = state)) + |
| 51 |
#' geom_line(aes(color = state)) + |
| 52 |
#' geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = .25) |
| 53 |
#' |
| 54 |
#' @aliases survfit_tidiers |
| 55 |
#' @export |
| 56 |
#' @seealso [tidy()], [survival::survfit()] |
| 57 |
#' @family survfit tidiers |
| 58 |
#' @family survival tidiers |
| 59 |
#' |
| 60 |
tidy.survfit <- function(x, ...) { |
| 61 |
if (inherits(x, "survfitms")) { |
| 62 |
# c() coerces to vector |
| 63 |
ret <- data.frame( |
| 64 |
time = x$time, |
| 65 |
n.risk = c(x$n.risk), |
| 66 |
n.event = c(x$n.event), |
| 67 |
n.censor = c(x$n.censor), |
| 68 |
estimate = c(x$pstate), |
| 69 |
std.error = c(x$std.err), |
| 70 |
conf.high = c(x$upper), |
| 71 |
conf.low = c(x$lower), |
| 72 |
state = rep(x$states, each = nrow(x$pstate)) |
| 73 |
) |
| 74 |
|
| 75 |
ret <- ret[ret$state != "", ] |
| 76 |
} else { |
| 77 |
ret <- data.frame( |
| 78 |
time = x$time, |
| 79 |
n.risk = x$n.risk, |
| 80 |
n.event = x$n.event, |
| 81 |
n.censor = x$n.censor, |
| 82 |
estimate = x$surv, |
| 83 |
std.error = x$std.err, |
| 84 |
conf.high = x$upper, |
| 85 |
conf.low = x$lower |
| 86 |
) |
| 87 |
} |
| 88 |
# strata are automatically recycled if there are multiple states |
| 89 |
if (!is.null(x$strata)) { |
| 90 |
ret$strata <- rep(names(x$strata), x$strata) |
| 91 |
} |
| 92 |
as_tibble(ret) |
| 93 |
} |
| 94 |
|
| 95 |
#' @templateVar class survfit |
| 96 |
#' @template title_desc_glance |
| 97 |
#' |
| 98 |
#' @param ... Additional arguments passed to [survival::summary.survfit()]. Important |
| 99 |
#' arguments include `rmean`. |
| 100 |
#' |
| 101 |
#' @inherit tidy.survfit params examples |
| 102 |
#' |
| 103 |
#' @evalRd return_glance( |
| 104 |
#' "records", |
| 105 |
#' "n.max", |
| 106 |
#' "n.start", |
| 107 |
#' "events", |
| 108 |
#' "rmean", |
| 109 |
#' "rmean.std.error", |
| 110 |
#' conf.low = "lower end of confidence interval on median", |
| 111 |
#' conf.high = "upper end of confidence interval on median", |
| 112 |
#' median = "median survival", |
| 113 |
#' "nobs" |
| 114 |
#' ) |
| 115 |
#' |
| 116 |
#' @export |
| 117 |
#' @seealso [glance()], [survival::survfit()] |
| 118 |
#' @family cch tidiers |
| 119 |
#' @family survival tidiers |
| 120 |
#' |
| 121 |
glance.survfit <- function(x, ...) { |
| 122 |
if (inherits(x, "survfitms")) { |
| 123 |
cli::cli_abort("Cannot {.fn glance} a multi-state {.cls survfit} object.") |
| 124 |
} |
| 125 |
if (!is.null(x$strata)) { |
| 126 |
cli::cli_abort("Cannot {.fn glance} a multi-strata {.cls survfit} object.") |
| 127 |
} |
| 128 |
|
| 129 |
s <- summary(x, ...) |
| 130 |
ret <- unrowname(as.data.frame(t(s$table))) |
| 131 |
|
| 132 |
colnames(ret) <- dplyr::recode( |
| 133 |
colnames(ret), |
| 134 |
"*rmean" = "rmean", |
| 135 |
"*se(rmean)" = "rmean.std.error", |
| 136 |
"se(rmean)" = "rmean.std.error" |
| 137 |
) |
| 138 |
|
| 139 |
colnames(ret)[utils::tail(seq_along(ret), 2)] <- c("conf.low", "conf.high") |
| 140 |
|
| 141 |
ret$nobs <- stats::nobs(x) |
| 142 |
|
| 143 |
as_tibble(ret) |
| 144 |
} |