rSurvTime: Simulate Arbitrary Survival Times

Description Usage Arguments Details Value Note References Examples

View source: R/rSurvTime.R

Description

Simulates arbitrary (Cox-type) survival times using the inversion method. The survival times do not need to follow a known distribution, as e.g., the exponential or Weibull distribution. Furthermore, time-varying effects can be sampled, i.e., the data does not neccessarily need to statisfy the proportinal hazards assumption.

Usage

1
rSurvTime(lambda, x, cens_fct, upper = 1000, ..., file = NULL)

Arguments

lambda

function. Baseline hazard lambda(t,x) where time must be first argument.

x

matrix. (Sampled) values for covariates (without time).

cens_fct

function. Function to compute (random) censoring.

upper

upper boundary of the interval the random survival times fall into.

...

further arguments to be passed to lambda or cens_fct.

file

character. name of the data file the generated data set should be stored into (e.g., "survtimes.RData") or NULL if the dataset should directly be returned in R.

Details

By specifying an (arbitrary) hazard rate and a censoring function it is possible to sample survival times according to this scheme. The concept of Bender et al. (2005) is expanded to even allow for time-varying effects in the hazard rate.

The function lambda needs to be vectorized in its first argument, the time. To achieve this even if the hazard rate is specified as beeing time-constant a nice trick is to spefify 0 * time explizitly in lambda (see example for more details). Another important remark is that lambda expects x to be a vector, i.e., we just pass one row of the matrix of coefficients to lambda (see example below).

The censoring function cens_fct takes as first argument the uncensored (sampled) survival times. Further arguments can be specified (and accessed via the ... argument in rSurvTime). The function must return a matrix consisting of the observed survival times and the non-censoring indicator (see example below).

Value

A data.frame consisting of the observed survival time (time), the non-censoring indicator (event) and further covariates x is returned. If file is specified, the data.frame is additionally stored on the disc.

Note

Note that the covariate values should not become too large as otherwise numerical problems may occure. A linear predictor in the range from -3 to 3 has shown to be sufficiently small.

References

Ralph Bender and Thomas Augustin and Maria Blettner (2005), Generating Survival Times to Simulate Cox Proportional Hazards Models. Statistics in Medicine, 24, 1713–1723.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
set.seed(1234)

## sample covariates first
X <- matrix(NA, nrow=400, ncol=3)
X[,1] <- runif(400, -1, 1)
X[,2] <- runif(400, -1, 1)
X[,3] <- runif(400, -1, 1)

## time-constant hazard rate (needs to be a vectororized in the first argument time)
lambda <- function(time, x){
  exp(0 * time + 0.7 * x[1] + x[2]^2 + 0 * x[3])
}

## time-dependent hazard rate
lambda <- function(time, x){
  exp(log(time) + 0.7 * x[1] + x[2]^2 + 0 * x[3])
}

## specify censoring function
cens_fct <- function(time, mean_cens){
  ## censoring times are independent exponentially distributed
  censor_time <- rexp(n = length(time), rate = 1/mean_cens)
  event <- (time <= censor_time)
  t_obs <- apply(cbind(time, censor_time), 1, min)
  ## return matrix of observed survival times and event indicator
  return(cbind(t_obs, event))
}

data <- rSurvTime(lambda, X, cens_fct, mean_cens = 5)

## it is also possible to sample time-varying effects
lambda <-  function(time, x){
  exp(log(time) + 0.7 * x[1] + x[2]^2 + (x[3] * time)/3)
}

data <- rSurvTime(lambda, X, cens_fct, mean_cens = 5)

CoxFlexBoost documentation built on May 2, 2019, 6:53 p.m.