Skip to contents

simulate_competing_risks() draws competing-risks data from latent Weibull event times. Each subject gets an independent event time for every cause, from that cause's Weibull distribution, and the shortest one is the event observed. An independent exponential censoring time censors the subject when it comes first.

Usage

simulate_competing_risks(
  n = 500,
  n_causes = 2,
  shapes = NULL,
  scales = NULL,
  censoring_scale = 1.5,
  n_covariates = 0,
  coef = NULL,
  states = NULL
)

Arguments

n

The number of subjects. The default is 500.

n_causes

The number of competing causes. The default is 2.

shapes

The Weibull shape of each cause, one per cause. A shape of 1 gives a constant hazard, above 1 an increasing one, and below 1 a decreasing one. The default cycles through 1, 1.5, and 0.8.

scales

The Weibull scale of each cause, one per cause. The default is 10 for every cause.

censoring_scale

How much censoring there is. The censoring times are exponential with a mean of censoring_scale times the mean event time (the mean over subjects of the shortest latent time), so larger values censor less. NULL or 0 gives no censoring. The default, 1.5, censors about 40% of the subjects with the default shapes and scales.

n_covariates

The number of covariates. The default is 0.

coef

The effects of the covariates, a matrix with one row per covariate and one column per cause, on the log-hazard scale. By default the effects are drawn uniformly from -0.5 to 0.5.

states

The names of the causes, one per cause. The default is "cause1", "cause2", and so on.

Value

A data frame with one row per subject: time, event (a factor whose first level, "censor", marks censoring and whose other levels are the states), and the covariates x1, x2, and so on. Surv(time, event) is the competing-risks response. The data frame has three attributes for checking estimates against the truth: latent_times, a matrix of each subject's event time for every cause (one column per cause), censoring_time (Inf without censoring), and coef, the effects used (NULL without covariates).

Details

With n_covariates above 0, the covariates are standard normal and act on each cause's hazard through proportional hazards: for cause \(k\) with shape \(a_k\) and scale \(b_k\), a subject with covariates \(x\) has the scale \(b_k \exp(-x^\top \beta_k / a_k)\), which multiplies the cause's hazard by \(\exp(x^\top \beta_k)\).

The draws come from R's random number generator, so call set.seed() first for data that can be repeated.

Examples

set.seed(42)
sim <- simulate_competing_risks(n = 300)
head(sim)
#>        time  event
#> 1 0.8904321 cause1
#> 2 0.6499152 cause1
#> 3 8.2920407 censor
#> 4 0.3992159 censor
#> 5 2.5612285 cause2
#> 6 1.7294251 cause2
aalen_johansen(Surv(time, event) ~ 1, data = sim)
#> <aalen_johansen> Surv(time, event) ~ 1
#> n = 300. Events: cause1 = 98, cause2 = 72
#> 
#> Cumulative incidence at the last time (plain limits):
#>   cause time estimate std_error conf_low conf_high
#>  cause1 22.5   0.4916   0.03914   0.4149    0.5683
#>  cause2 22.5   0.4668   0.04351   0.3815    0.5521

# Covariates for a Fine-Gray model
sim <- simulate_competing_risks(n = 500, n_covariates = 2, states = c("relapse", "death"))
fine_gray(Surv(time, event) ~ x1 + x2, data = sim, cause = "relapse")
#> <fine_gray> Surv(time, event) ~ x1 + x2
#> Subdistribution hazards for cause "relapse". n = 500, events = 172
#> 
#>    estimate hazard_ratio robust_se statistic   p_value
#> x1   0.2923       1.3395   0.07641     3.825 1.308e-04
#> x2  -0.3540       0.7019   0.07570    -4.676 2.925e-06
#> 
#> Robust Wald test: 39.1 on 2 df, p = 3.2e-09