Skip to contents

For a Cox model (survival::coxph) or an accelerated failure time model (survival::survreg), the survival curve S(t) of each level of modelterm with pointwise confidence limits, and the median survival time with its limits.

Usage

survmeans(
  model,
  modelterm,
  at = NULL,
  times = NULL,
  conf_level = 0.95,
  data = NULL,
  marginal = FALSE
)

# S3 method for class 'pm_survmeans'
autoplot(object, limits = TRUE, ...)

# S3 method for class 'pm_survmeans'
plot(x, ...)

Arguments

model

A coxph or survreg fit, or a brmsfit of a survival family.

modelterm

A factor of the model, or an interaction of factors ("A:B"), whose levels get a curve.

at

A named list: levels of the other factors (one each), or values of numeric covariates.

times

The times at which to give S(t). Default: coxph, every time of the data (events and censorings) up to the last; survreg, 101 points from 0 to the largest time.

conf_level

The confidence level of the limits.

data

The data the model was fitted to, when they cannot be found.

marginal

Bayesian fits only (brms with a weibull(), lognormal() or exponential() family): FALSE gives the curves of a typical cluster (random effects at zero), TRUE the curves averaged over the random effects of each draw (the survival of the population). A Bayesian fit's curves and medians are posterior medians of each draw's S(t) and median, with equal-tailed pointwise limits. brms cox(): S(t) = exp(-H0(t) exp(eta)) with H0 the integrated spline baseline hazard of each draw (the curves stop at the spline's upper boundary); medians whose draws partly lie beyond it are given when fewer than half do, with their share in column beyond. Not available: a shape or sigma modelled by predictors. With random slopes the variance at each pattern row is z' Sigma z. The average over the random effects is the checked quadrature of bias_adjust (Gauss-Hermite, then the trapezoid rule in z, then adaptive integration, until two rules agree to 1e-8).

object

A survmeans() result.

limits

Draw the pointwise limits as bands.

...

Not used.

x

A survmeans() result, for plot(): it draws autoplot() and returns it invisibly.

Value

An object of class pm_survmeans: curves (the levels, time, surv, conf.low, conf.high), medians (the levels, median, conf.low, conf.high; NA when the curve does not fall to 0.5) and settings (the pattern of the other variables, the method). autoplot() draws the curves.

Details

Each curve is that of one covariate pattern: the level of modelterm, numeric covariates at their means (as predictmeans), and the other factors at the levels in at (by default their first level, with a message). A curve averaged over the levels of another factor would need the covariance between curves; it is not given, so that no limits are approximate. Give the factor in modelterm to see a curve for each of its levels.

  • coxph: survival::survfit() with that pattern as newdata (the Breslow or Efron estimate of the baseline hazard of the fit, and its variance); in a stratified model the strata factor is one of the pattern (in modelterm or at), and each curve uses the baseline hazard of its stratum. The curves stop (NA) after the last follow-up time of the data (of the stratum). The median and its limits are those of quantile() of the curve.

  • survreg: S(t) = 1 - F((log t - eta) / sigma) for the fitted distribution, with the delta method over the coefficients and log(sigma); the median is exp(eta + sigma q50). Distributions on the log-time scale only, and one scale (no strata()).

The limits are on the log survival scale (conf.type = "log", as survfit()'s default), at conf_level; they are pointwise, not a band for the whole curve.

See also

predictmeans for hazard ratios and time ratios between the levels.

Examples

library(survival)
lung2 <- na.omit(lung[, c("time", "status", "ph.ecog", "sex", "age")])
lung2$ecog <- factor(lung2$ph.ecog)
lung2$sex <- factor(lung2$sex)
sm <- survmeans(coxph(Surv(time, status) ~ ecog + sex + age, data = lung2), "ecog",
                at = list(sex = "2"))
sm$medians
#>   ecog median conf.low conf.high
#> 1    0    567      433       814
#> 2    1    394      337       567
#> 3    2    288      218       426
#> 4    3    147       60        NA
ggplot2::autoplot(sm)