README.md

Lifecycle: experimental Build Status Build status codecov

Simulate an Arm of a Clinical Trial

library(trialsim)
library(purrr)
library(knitr)

# Generate enrollment with 1 enrolee per time period.
# Sample 2 trials each with 3 responses.
arm_enroll(10) %>%
  arm_bin_resample(3, size = 2) %>% 
  kable()
period enrolled response sim 1 1 0 1 2 1 0 1 3 1 1 1 4 1 0 1 5 1 1 1 6 1 0 1 7 1 1 1 8 1 0 1 9 1 0 1 10 1 0 1 1 1 1 2 2 1 1 2 3 1 0 2 4 1 0 2 5 1 0 2 6 1 1 2 7 1 0 2 8 1 0 2 9 1 0 2 10 1 0 2

Simulate an Arm of a Clinical Trial with Poisson Enrollment

# Generate enrollment based on a poisson distribution with rate parameter 0.8
# then create all trial combinations with 3 responders.
arm_enroll(10, partial(rpois, n = 1, lambda = 0.8)) %>%
  arm_bin_resample(3) %>%
  kable()
period enrolled response sim 2 2 0 1 3 2 1 1 5 1 0 1 9 1 0 1 15 2 1 1 18 1 1 1 22 1 0 1

Simulate a Trial

library(doParallel)
library(doRNG)
library(dplyr)

registerDoParallel()
registerDoRNG()

# Create the vemurafenib data set.
resps <- c(8, 0, 1, 1, 6, 2)
size <- c(19, 10, 26, 8, 14, 7)
name <- c("NSCLC", "CRC (vemu)", "CRC (vemu+cetu)", "Bile Duct", "ECD or LCH",
          "ATC")

# Assume that the enrollment rate is inversely proportional to the
# number enrolled.
lambda <- size / max(size)

# Use the sampler to change the enrollement duration.
# Resample 1000 trials in parallel and keep track of their lengths
trials <- bin_trial_resample(resps, size, name, 1000, sampler = poisson_sampler(lambda)) %>%
  group_by(name, sim) %>% 
  summarize(trial_length = max(period))

Get the Expected Arm Duration and SD

# Get the mean and sd of the trial durations.
trials %>%
  group_by(name) %>% 
  summarize(mean_length = mean(trial_length), sd_length = sd(trial_length)) %>%
  kable()
name mean_length sd_length ATC 26.465 9.886785 Bile Duct 26.632 9.528030 CRC (vemu) 25.792 8.130735 CRC (vemu+cetu) 26.643 5.233253 ECD or LCH 26.203 6.597068 NSCLC 26.463 6.039960

Plot the Density of the Arm Durations

library(ggplot2)
ggplot(trials, aes(x = trial_length, fill = name)) +
  geom_density(alpha = 0.7) +
  facet_grid( name ~ ., labeller = label_wrap_gen(width = 10)) +
  scale_fill_manual(values = rep("black", length(unique(trials$name))), guide = FALSE) +
  xlab("Enrollment Duration Density") +
  ylab("Density") +
  theme_minimal()



kaneplusplus/trialsim documentation built on Aug. 6, 2019, 2:50 p.m.