knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(herosurv)

Once you've created survival distributions, herosurv provides various methods for combining modifying them.

Apply Hazard Ratio:

Hazard ratios can be applied to a survival distribution using the apply_hr function.

  dist1 <- define_surv_param('weibull', shape = 1.2, scale = 5.3)
  dist2 <- apply_hr(dist1, 0.85)
  print(dist2)

Apply Odds Ratio

Odds ratios can be applied to a survival distribution using the apply_or function.

  dist1 <- define_surv_param('weibull', shape = 1.2, scale = 5.3)
  dist2 <- apply_or(dist1, 0.45)
  print(dist2)

Apply Accleration Factor

Accleration factors, which proportionally scale event times, can be applied using the apply_af function.

  dist1 <- define_surv_param('weibull', shape = 1.2, scale = 5.3)
  dist2 <- apply_af(dist1, 0.75)
  print(dist2)

Apply Time Shift

Hazards for a distribution can be shifted forward or backward by a fixed amount of time using the apply_shift function. Note that a positive value of shift will move hazards forward and a negative value will shift hazards backward (resulting in an initial period with no event risk).

  dist1 <- define_surv_param('weibull', shape = 1.2, scale = 5.3)
  dist2 <- apply_shift(dist1, 2.1)
  print(dist2)

Mix Distributions

Survival distributions can be mixed according to specified weights using the mix function.

  dist1 <- define_surv_param('weibull', shape = 1.2, scale = 5.3)
  dist2 <- define_surv_param('weibull', shape = 1.4, scale = 4.1)
  dist3 <- mix(dist1, 0.8, dist2, 0.2) # Mix of 80% dist1 and 20% dist2
  print(dist3)

Join Distributions

Survival distributions can be joined together at specified cutpoints using the join function.

  dist1 <- define_surv_param('weibull', shape = 1.2, scale = 5.3)
  dist2 <- define_surv_param('weibull', shape = 1.4, scale = 4.1)
  dist3 <- join(dist1, 3, dist2) # Join dist2 and dist3 at time 3
  print(dist3)

Add Hazards

Survival distributions can be combined as independent risks using the add_hazards function. This results in a survival distribution with survival function equal to the product of the survival functions of combined distributions.

  dist1 <- define_surv_param('weibull', shape = 1.2, scale = 5.3)
  dist2 <- define_surv_param('weibull', shape = 1.4, scale = 4.1)
  dist3 <- add_hazards(dist1, dist2) # combined hazards of dist1 and dist2
  print(dist3)

Set Covariates

Models fitted using survfit or flexsurvreg with predictors can be used to generate a survival distribution representing a subject or set of subjects using set_covariates.

library(flexsurv)
library(tibble)

# Fit a model using bc dataset using good as predictor
print(as_tibble(bc))
fs1 <- flexsurvreg(Surv(recyrs, censrec)~group, data = bc, dist = 'weibull')

# Survival distribution representing patient w/ group = "Good"
good_model <- set_covariates(fs1, data.frame(group = 'Good'))
print(good_model)

# Survival distribution with patient representing cohort w/
# equal mix of group = "Good" and group = "Poor"
good_and_poor_model <- set_covariates(fs1, data.frame(group = c('Good', 'Poor')))
print(good_and_poor_model)


PolicyAnalysisInc/herosurv documentation built on May 21, 2023, 10:12 a.m.