Skip to contents

sim_pw_surv() enables simulation of a clinical trial with essentially arbitrary patterns of enrollment, failure rates and censoring. The piecewise exponential distribution allows a simple method to specify a distribution and enrollment pattern where the enrollment, failure, and dropout rate changes over time. While the main purpose may be to generate a trial that can be analyzed at a single point in time or using group sequential methods, the routine can also be used to simulate an adaptive trial design. Enrollment, failure, and dropout rates are specified by treatment group, stratum and time period. Fixed block randomization is used; blocks must include treatments provided in failure and dropout specification. Default arguments are set up to allow very simple implementation of a non-proportional hazards assumption for an unstratified design.

Usage

sim_pw_surv(
  n = 100,
  stratum = data.frame(stratum = "All", p = 1),
  block = c(rep("control", 2), rep("experimental", 2)),
  enroll_rate = data.frame(rate = 9, duration = 1),
  fail_rate = data.frame(stratum = rep("All", 4), period = rep(1:2, 2), treatment =
    c(rep("control", 2), rep("experimental", 2)), duration = rep(c(3, 1), 2), rate =
    log(2)/c(9, 9, 9, 18)),
  dropout_rate = data.frame(stratum = rep("All", 2), period = rep(1, 2), treatment =
    c("control", "experimental"), duration = rep(100, 2), rate = rep(0.001, 2))
)

Arguments

n

Number of observations. If length(n) > 1, the length is taken to be the number required.

stratum

A data frame with stratum specified in stratum, probability (incidence) of each stratum in p.

block

Vector of treatments to be included in each block.

enroll_rate

Enrollment rates; see details and examples.

fail_rate

Failure rates; see details and examples; note that treatments need to be the same as input in block.

dropout_rate

Dropout rates; see details and examples; note that treatments need to be the same as input in block.

Value

A data frame with the following variables for each observation:

  • stratum.

  • enroll_time: Enrollment time for the observation.

  • Treatment: Treatment group; this will be one of the values in the input block.

  • fail_time: Failure time generated using rpwexp().

  • dropout_time: Dropout time generated using rpwexp().

  • cte: Calendar time of enrollment plot the minimum of failure time and dropout time.

  • fail: Indicator that cte was set using failure time; i.e., 1 is a failure, 0 is a dropout.

Examples

library(dplyr)

# Example 1
sim_pw_surv(n = 20)
#>    stratum enroll_time    treatment  fail_time dropout_time        cte fail
#> 1      All  0.07001605      control 19.5650346     26.24930 19.6350507    1
#> 2      All  0.11250082 experimental  1.2800017    823.88014  1.3925025    1
#> 3      All  0.16059246 experimental 99.3270563    946.28365 99.4876487    1
#> 4      All  0.18680088      control  0.1150366   1982.95198  0.3018375    1
#> 5      All  0.32348567      control  0.2659009    293.80946  0.5893865    1
#> 6      All  0.53924326 experimental 12.3655734    424.86428 12.9048166    1
#> 7      All  0.56067164 experimental 24.8934865   3601.37946 25.4541582    1
#> 8      All  0.56316045      control  1.9729803   1089.95618  2.5361408    1
#> 9      All  0.66056548      control 18.4483802    434.31466 19.1089457    1
#> 10     All  0.87646201      control  5.9717512    125.93169  6.8482132    1
#> 11     All  0.88612590 experimental  8.9691062    951.20200  9.8552321    1
#> 12     All  0.94573946 experimental  9.4852510   2118.15209 10.4309904    1
#> 13     All  1.01743409 experimental  1.0412043   1901.83435  2.0586384    1
#> 14     All  1.15743059 experimental  4.2301976    160.37688  5.3876282    1
#> 15     All  1.29455783      control  3.8388328    847.48951  5.1333906    1
#> 16     All  1.31063025      control  3.3195804     58.16193  4.6302107    1
#> 17     All  1.32489185      control  5.1065099   1375.31437  6.4314018    1
#> 18     All  1.40692309 experimental 32.7491726    197.03324 34.1560957    1
#> 19     All  1.51964417      control  3.7912304     11.34259  5.3108745    1
#> 20     All  1.63246681 experimental  2.2875405    804.91681  3.9200073    1

# Example 2
# 3:1 randomization
sim_pw_surv(
  n = 20,
  block = c(rep("experimental", 3), "control")
)
#>    stratum enroll_time    treatment   fail_time dropout_time        cte fail
#> 1      All   0.2367048      control   6.7831275   189.732031   7.019832    1
#> 2      All   0.3268209 experimental   3.5338628   255.836968   3.860684    1
#> 3      All   0.3384515 experimental   9.4928294   742.229574   9.831281    1
#> 4      All   0.3399433 experimental  12.4943694   685.656819  12.834313    1
#> 5      All   0.4244262 experimental  41.0686377  1134.605316  41.493064    1
#> 6      All   0.4436312      control   2.6178723   699.637074   3.061504    1
#> 7      All   0.7653554 experimental 116.8281381  1848.415820 117.593493    1
#> 8      All   0.8001982 experimental   1.1196077   162.507928   1.919806    1
#> 9      All   0.8266640 experimental  44.2434604     4.963087   5.789751    0
#> 10     All   0.8681263      control   3.2042041   706.781454   4.072330    1
#> 11     All   0.9199099 experimental   4.5214569   456.380132   5.441367    1
#> 12     All   0.9324011 experimental   6.9775576   276.484136   7.909959    1
#> 13     All   1.0572405 experimental  14.8730426   426.240944  15.930283    1
#> 14     All   1.1327757      control   2.3365045  1101.256078   3.469280    1
#> 15     All   1.2330737 experimental   0.1715598    30.568471   1.404633    1
#> 16     All   1.4624788 experimental  10.0687941  3025.328402  11.531273    1
#> 17     All   1.4645759 experimental  17.4354083  1147.682266  18.899984    1
#> 18     All   1.7309944 experimental  99.0519123    76.763221  78.494216    0
#> 19     All   1.8536219      control  20.8075822  1447.017415  22.661204    1
#> 20     All   1.8904926 experimental  19.0166778   440.688450  20.907170    1

# Example 3
# Simulate 2 stratum; will use defaults for blocking and enrollRates
sim_pw_surv(
  n = 20,
  # 2 stratum,30% and 70% prevalence
  stratum = data.frame(stratum = c("Low", "High"), p = c(.3, .7)),
  fail_rate = data.frame(
    stratum = c(rep("Low", 4), rep("High", 4)),
    period = rep(1:2, 4),
    treatment = rep(c(
      rep("control", 2),
      rep("experimental", 2)
    ), 2),
    duration = rep(c(3, 1), 4),
    rate = c(.03, .05, .03, .03, .05, .08, .07, .04)
  ),
  dropout_rate = data.frame(
    stratum = c(rep("Low", 2), rep("High", 2)),
    period = rep(1, 4),
    treatment = rep(c("control", "experimental"), 2),
    duration = rep(1, 4),
    rate = rep(.001, 4)
  )
)
#>    stratum enroll_time    treatment  fail_time dropout_time       cte fail
#> 1     High   0.1036777 experimental 20.2331348   4174.94331 20.336813    1
#> 2     High   0.1343389 experimental  4.1098911    465.29175  4.244230    1
#> 3     High   0.2064661      control 17.5605004   1004.53947 17.766967    1
#> 4     High   0.2796553      control 23.0282302    494.34540 23.307885    1
#> 5      Low   0.3086204      control 11.4731163     23.72062 11.781737    1
#> 6     High   0.3111598 experimental  1.8322780   2189.41244  2.143438    1
#> 7     High   0.9270572      control 11.6284285     21.84553 12.555486    1
#> 8      Low   0.9797810 experimental 15.8387307   3407.82733 16.818512    1
#> 9     High   1.0627224      control  6.7833167    699.12197  7.846039    1
#> 10    High   1.1896626 experimental  0.8167457    361.39980  2.006408    1
#> 11     Low   1.2618837      control 24.4847066    661.56404 25.746590    1
#> 12    High   1.4095626      control 29.7332577    788.59361 31.142820    1
#> 13     Low   1.6101124 experimental 54.1703944   1135.55339 55.780507    1
#> 14    High   1.7441575 experimental  8.6834669   3357.73901 10.427624    1
#> 15    High   1.9973926 experimental  0.6192459    238.07083  2.616639    1
#> 16     Low   2.3403839      control 45.4400769   2356.38747 47.780461    1
#> 17    High   2.5440934      control 11.0088796    382.11254 13.552973    1
#> 18    High   2.5440974      control  6.0942686    134.13149  8.638366    1
#> 19    High   2.6168482      control 10.6131452    485.16067 13.229993    1
#> 20     Low   2.6640095 experimental 49.5705882   2008.07518 52.234598    1
# Example 4
# If you want a more rectangular entry for a data.frame
fail_rate <- bind_rows(
  data.frame(stratum = "Low", period = 1, treatment = "control", duration = 3, rate = .03),
  data.frame(stratum = "Low", period = 1, treatment = "experimental", duration = 3, rate = .03),
  data.frame(stratum = "Low", period = 2, treatment = "experimental", duration = 3, rate = .02),
  data.frame(stratum = "High", period = 1, treatment = "control", duration = 3, rate = .05),
  data.frame(stratum = "High", period = 1, treatment = "experimental", duration = 3, rate = .06),
  data.frame(stratum = "High", period = 2, treatment = "experimental", duration = 3, rate = .03)
)

dropout_rate <- bind_rows(
  data.frame(stratum = "Low", period = 1, treatment = "control", duration = 3, rate = .001),
  data.frame(stratum = "Low", period = 1, treatment = "experimental", duration = 3, rate = .001),
  data.frame(stratum = "High", period = 1, treatment = "control", duration = 3, rate = .001),
  data.frame(stratum = "High", period = 1, treatment = "experimental", duration = 3, rate = .001)
)

sim_pw_surv(
  n = 12,
  stratum = data.frame(stratum = c("Low", "High"), p = c(.3, .7)),
  fail_rate = fail_rate,
  dropout_rate = dropout_rate
)
#>    stratum enroll_time    treatment fail_time dropout_time       cte fail
#> 1      Low   0.2572800      control 37.602886   465.177813 37.860166    1
#> 2     High   0.4370483      control 20.546177  1352.219300 20.983226    1
#> 3      Low   0.4944613 experimental 78.619566   338.976877 79.114027    1
#> 4      Low   0.5673761 experimental 15.853988   347.672350 16.421364    1
#> 5      Low   1.1069423      control  2.195484   723.838025  3.302426    1
#> 6     High   1.1680008 experimental 10.451312   633.099389 11.619313    1
#> 7      Low   1.2875002 experimental 38.684903  2506.432402 39.972403    1
#> 8     High   1.4881602      control  9.463377  1250.153280 10.951537    1
#> 9     High   1.5505213 experimental 15.439429  1193.758523 16.989950    1
#> 10     Low   1.6826246 experimental 11.089760   645.756427 12.772384    1
#> 11     Low   1.8512677      control 29.195486     8.994333 10.845600    0
#> 12    High   2.0465840      control 31.254707  2963.649460 33.301291    1