rsurv() is a generic function for randomly drawing survival times for multiple individuals very efficiently. The most flexible function is rsurv.survival_curves(), which will randomly draw survival times from survival curves.

rsurv(object, ...)

# S3 method for survival_curves
rsurv(object, n_rep = 1, ...)

Arguments

object

An object of the appropriate class.

...

Further arguments passed to or from other methods. Currently unused.

n_rep

The number of times to randomly draw survival times per individual (id) in object.

Value

A vector with randomly sampled survival times for individuals (ids) in object repeated n_rep times. The returned vector is of length length(unique(object$id)) times n_rep.

Details

Survival times are randomly sampled from survival curves by partitioning times into intervals and using the survival probabilities to compute the probability of dying within each interval, conditional on survival up until that interval. Death is simulated within each interval from a Bernoulli distribution and a given individual's observed death time is the time at the end of the first interval for which that individual is simulated to have died. This process is repeated across all individuals. The code is written in C++ so looping across individuals is fast.

See also

A survival_curves object can be constructed using the function survival_curves() or converted from an existing data frame using as_survival_curves.data.frame().

Examples

# Survival curves for multiple subjects based on exponential distributions set.seed(21) N <- 1000 # 1000 individuals rates <- runif(N, .2, .5) sc <- example_survival_curves(n = N, rates = rates) # Random number generation of 1000 individuals from (1) arbitrary survival # curves and (2) exponential distributions sim_rsurv <- rsurv(sc) sim_rexp <- rexp(N, rate = rates) summary(sim_rsurv)
#> Min. 1st Qu. Median Mean 3rd Qu. Max. #> 0.08333 0.83333 2.00000 2.82617 3.75000 26.33333
summary(sim_rexp)
#> Min. 1st Qu. Median Mean 3rd Qu. Max. #> 0.000361 0.824767 1.965348 3.032663 4.031558 25.952823
# Random number generation of 1000 individuals replicated 2 times sim_rsurv <- rsurv(sc, n_rep = 2) summary(sim_rsurv)
#> Min. 1st Qu. Median Mean 3rd Qu. Max. #> 0.08333 0.83333 2.00000 2.97721 4.08333 26.25000