Introduction to the R package rsurv

Example

In the code below we show how to simulate a sample of type I right-censored survival data, assuming that the failure times are generated from an accelerated failure time model with loglogistic baseline distribution. In addition, assume that we wish to consider two exploratory variables, say age and sex, and we want to include an interaction effect between them. Such a task can be easily accomplished by using the function raftreg() along with the function qllogis() available in the package flexsurv.

library(rsurv)
library(survstan)
library(flexsurv)

set.seed(1234567890)

n <-  1000
tau <- 10  # maximum follow up time
simdata <- data.frame(
  age = rnorm(n),
  sex = sample(c("f", "m"), size = n, replace = TRUE)
) %>%
  mutate(
    t = raftreg(runif(n), ~ age*sex, beta = c(1, 2, -0.5), 
                dist = "llogis", shape = 1.5, scale = 1),
  ) %>%
  rowwise() %>%
  mutate(
    time = min(t, tau),
    status = as.numeric(time == t)
  ) 

glimpse(simdata)
#> Rows: 1,000
#> Columns: 5
#> Rowwise: 
#> $ age    <dbl> 1.34592454, 0.99527131, 0.54622688, -1.91272392, 1.92128431, 1.…
#> $ sex    <chr> "m", "f", "f", "m", "m", "m", "m", "f", "f", "m", "f", "m", "m"…
#> $ t      <dbl> 15.2363453, 1.5259533, 2.1783746, 2.4354995, 58.7932958, 16.714…
#> $ time   <dbl> 10.0000000, 1.5259533, 2.1783746, 2.4354995, 10.0000000, 10.000…
#> $ status <dbl> 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, …

fit <- aftreg(
  Surv(time, status) ~ age*sex,
  data = simdata, dist = "loglogistic"
)
estimates(fit)
#>        age       sexm   age:sexm      alpha      gamma 
#>  0.9494630  2.0094422 -0.4812641  1.4946497  1.0226847