| Title: | Clinical Trial Simulator |
|---|---|
| Description: | Simulate phase II and/or phase III clinical trials. It supports various types of endpoints and adaptive strategies. Tools for carrying out graphical testing procedure and combination test under group sequential design are also provided. |
| Authors: | Han Zhang [cre, aut] |
| Maintainer: | Han Zhang <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.19.1 |
| Built: | 2026-06-11 05:38:05 UTC |
| Source: | https://github.com/zhangh12/trialsimulator |
add one or more arms to a trial. This function can be used in two
scenarios: (1) adding arms right after a trial is created
(i.e., trial(...)). sample_ratio and arms added through
... must be of same length; (2) adding arms to an existing trial in
action functions of milestones.
This is a user-friendly wrapper of the member function of trial, i.e.,
Trials$add_arms(), which is used in vignettes. Users who are not
familiar with the concept of classes may consider using this wrapper
directly.
add_arms(trial, sample_ratio, ...)add_arms(trial, sample_ratio, ...)
trial |
a trial object returned by |
sample_ratio |
integer vector. Sample ratio for permuted block randomization. It will be appended to existing sample ratio in the trial. |
... |
one or more objects returned from |
Define an arm in a trial. This is a user-friendly wrapper for
the class constructor Arms$new(). Users who are not familiar with
the concept of classes may consider using this wrapper directly.
arm(name, ...)arm(name, ...)
name |
character. Name of arm, which is the arm's label in generated
trial data, i.e., the one retrieved by calling |
... |
subset condition that is compatible with |
risk <- data.frame( end_time = c(1, 10, 26.0, 52.0), piecewise_risk = c(1, 1.01, 0.381, 0.150) * exp(-3.01) ) pfs <- endpoint(name = 'pfs', type='tte', generator = PiecewiseConstantExponentialRNG, risk = risk, endpoint_name = 'pfs') orr <- endpoint( name = 'orr', type = 'non-tte', readout = c(orr = 2), generator = rbinom, size = 1, prob = .4) placebo <- arm(name = 'pbo') placebo$add_endpoints(pfs, orr) ## try to generate some data from the arm ## it is NOT a recommended way to use the package in simulation head(placebo$get_endpoints()[[1]]$get_generator()(n = 1e3)) ## get name of endpoints in the arm ## for illustration only, NOT recommended placebo$get_endpoints()[[2]]$get_name() ## run it in console to get summary report ## It is the recommended way to view an arm placeborisk <- data.frame( end_time = c(1, 10, 26.0, 52.0), piecewise_risk = c(1, 1.01, 0.381, 0.150) * exp(-3.01) ) pfs <- endpoint(name = 'pfs', type='tte', generator = PiecewiseConstantExponentialRNG, risk = risk, endpoint_name = 'pfs') orr <- endpoint( name = 'orr', type = 'non-tte', readout = c(orr = 2), generator = rbinom, size = 1, prob = .4) placebo <- arm(name = 'pbo') placebo$add_endpoints(pfs, orr) ## try to generate some data from the arm ## it is NOT a recommended way to use the package in simulation head(placebo$get_endpoints()[[1]]$get_generator()(n = 1e3)) ## get name of endpoints in the arm ## for illustration only, NOT recommended placebo$get_endpoints()[[2]]$get_name() ## run it in console to get summary report ## It is the recommended way to view an arm placebo
Create a class of arm.
Public methods in this R6 class are used in developing this package. Thus, we have to export the whole R6 class which exposures all public methods. However, only the public methods in the list below are useful to end users.
$add_endpoints()
$print()
new()
initialize an arm
Arms$new(name, ...)
namename of arm, which is the arm's label in generated data
...subset condition that is compatible with dplyr::filter.
This can be used to specify inclusion criteria of an arm.
By default it is not specified, i.e. all data generated by the generator
will be used as trial data. More than one conditions can be
specified in ....
add_endpoints()
add one or multiple endpoints to the arm.
Arms$add_endpoints(...)
...one or more objects returned from endpoint().
a <- arm(name = 'trt')
x <- endpoint(name = 'x', type = 'tte',
generator = rexp) # median = log(2)/1 = 0.7
y <- endpoint(name = 'y', type = 'non-tte', readout = c(y = 0),
generator = rnorm, sd = 1.4, mean = 0.7)
a$add_endpoints(y, x)
## run it in console to see the summary report
a
print(a) # use the print method
get_name()
return name of arm.
Arms$get_name()
get_number_endpoints()
return number of endpoints in the arm.
Arms$get_number_endpoints()
has_endpoint()
check if the arm has any endpoint. Return TRUE or FALSE.
Arms$has_endpoint()
get_endpoints()
return a list of endpoints in the arm.
Arms$get_endpoints()
get_endpoints_name()
return name of endpoints registered to the arm.
Arms$get_endpoints_name()
update_endpoint_generator()
update generator of an endpoint object
Arms$update_endpoint_generator(endpoint_name, generator, ...)
endpoint_namecharacter. A vector of endpoint names whose generator is updated.
generatora random number generation (RNG) function.
See generator of endpoint().
...optional arguments for generator.
generate_data()
generate arm data.
Arms$generate_data(n_patients_in_arm)
n_patients_in_arminteger. Number of patients randomized to the arm.
print()
print an arm.
Arms$print(categorical_vars = NULL)
categorical_varscharacter vector of categorical variables. This can be used to specify variables with limited distinct (numeric) values as categorical variables in summary report.
clone()
The objects of this class are cloneable with this method.
Arms$clone(deep = FALSE)
deepWhether to make a deep clone.
# Instead of using Arms$new(), please use arm(), a user-friendly # wrapper. See examples in ?arm ## ------------------------------------------------ ## Method `Arms$add_endpoints` ## ------------------------------------------------ a <- arm(name = 'trt') x <- endpoint(name = 'x', type = 'tte', generator = rexp) # median = log(2)/1 = 0.7 y <- endpoint(name = 'y', type = 'non-tte', readout = c(y = 0), generator = rnorm, sd = 1.4, mean = 0.7) a$add_endpoints(y, x) ## run it in console to see the summary report a print(a) # use the print method# Instead of using Arms$new(), please use arm(), a user-friendly # wrapper. See examples in ?arm ## ------------------------------------------------ ## Method `Arms$add_endpoints` ## ------------------------------------------------ a <- arm(name = 'trt') x <- endpoint(name = 'x', type = 'tte', generator = rexp) # median = log(2)/1 = 0.7 y <- endpoint(name = 'y', type = 'non-tte', readout = c(y = 0), generator = rnorm, sd = 1.4, mean = 0.7) a$add_endpoints(y, x) ## run it in console to see the summary report a print(a) # use the print method
Define a condition to trigger trial milestone by calendar time. The milestone will be triggered when a trial has been running for at least the specified duration since the first patient is enrolled. It can be used combined with conditions specified by enrollment and eventNumber.
Refer to the
vignette
to learn how to define milestones when performing simulation using
TrialSimulator.
calendarTime(time)calendarTime(time)
time |
numeric. Calendar time to trigger a milestone of a trial. |
an object of class 'Condition'
milestone(name = 'end of trial', when = calendarTime(time = 12))milestone(name = 'end of trial', when = calendarTime(time = 12))
Define a controller of a trial. This is a user-friendly wrapper for
the class constructor Controller$new(). Users who are not familiar with
the concept of classes may consider using this wrapper directly.
TrialSimulator uses a controller to coordinate a trial object
and a listener object to run simulations, in which the trial object defines
endpoints, arms, and other settings of a trial, while the listener object
monitors trials to triggered pre-defined milestones and execute action
functions. See vignettes of this package for more examples.
controller(trial, listener)controller(trial, listener)
trial |
an object returned from |
listener |
an object returned from |
# a minimum, meaningful, and executable example, # where a randomized trial with two arms is simulated and analyzed. control <- arm(name = 'control arm') active <- arm(name = 'active arm') pfs_in_control <- endpoint(name = 'PFS', type = 'tte', generator = rexp, rate = log(2) / 5) control$add_endpoints(pfs_in_control) pfs_in_active <- endpoint(name = 'PFS', type = 'tte', generator = rexp, rate = log(2) / 6) active$add_endpoints(pfs_in_active) accrual_rate <- data.frame(end_time = c(10, Inf), piecewise_rate = c(30, 50)) trial <- trial(name = 'trial', n_patients = 1000, duration = 40, enroller = StaggeredRecruiter, accrual_rate = accrual_rate, dropout = rweibull, shape = 2, scale = 38) trial$add_arms(sample_ratio = c(1, 1), control, active) action_at_final <- function(trial){ locked_data <- trial$get_locked_data('final analysis') fitLogrank(Surv(PFS, PFS_event) ~ arm, placebo = 'control arm', data = locked_data, alternative = 'less') invisible(NULL) } final <- milestone(name = 'final analysis', action = action_at_final, when = calendarTime(time = 40)) listener <- listener() listener$add_milestones(final) controller <- controller(trial, listener) controller$run(n = 1)# a minimum, meaningful, and executable example, # where a randomized trial with two arms is simulated and analyzed. control <- arm(name = 'control arm') active <- arm(name = 'active arm') pfs_in_control <- endpoint(name = 'PFS', type = 'tte', generator = rexp, rate = log(2) / 5) control$add_endpoints(pfs_in_control) pfs_in_active <- endpoint(name = 'PFS', type = 'tte', generator = rexp, rate = log(2) / 6) active$add_endpoints(pfs_in_active) accrual_rate <- data.frame(end_time = c(10, Inf), piecewise_rate = c(30, 50)) trial <- trial(name = 'trial', n_patients = 1000, duration = 40, enroller = StaggeredRecruiter, accrual_rate = accrual_rate, dropout = rweibull, shape = 2, scale = 38) trial$add_arms(sample_ratio = c(1, 1), control, active) action_at_final <- function(trial){ locked_data <- trial$get_locked_data('final analysis') fitLogrank(Surv(PFS, PFS_event) ~ arm, placebo = 'control arm', data = locked_data, alternative = 'less') invisible(NULL) } final <- milestone(name = 'final analysis', action = action_at_final, when = calendarTime(time = 40)) listener <- listener() listener$add_milestones(final) controller <- controller(trial, listener) controller$run(n = 1)
Create a class of controller to run a trial.
Public methods in this R6 class are used in developing this package. Thus, we have to export the whole R6 class which exposures all public methods. However, only the public methods in the list below are useful to end users.
$run()
$get_output()
$reset()
new()
initialize a controller of the trial
Controllers$new(trial, listener)
triala trial object returned from trial().
listenera listener object returned from listener().
get_listener()
return listener in a controller.
Controllers$get_listener()
get_trial()
return trial in a controller.
Controllers$get_trial()
mute()
mute all messages (not including warnings).
Controllers$mute()
silentlogical.
reset()
reset the trial and listener registered to the controller before running
additional replicate of simulation. This is usually done between two
calls of controller$run().
Controllers$reset()
get_output()
return a data frame of all current outputs saved by calling save().
Controllers$get_output(cols = NULL, simplify = TRUE, tidy = FALSE)
colscharacter vector. Columns to be returned from the data frame
of simulation outputs. If NULL, all columns are returned.
simplifylogical. Return vector rather than a data frame of one
column when length(cols) == 1 and simplify == TRUE.
tidylogical. TrialSimulator automatically records a set
of standard outputs at milestones, even when doNothing is used
as action functions. These includes time of triggering milestones,
number of observed events for time-to-event endpoints, and number of
non-missing readouts for non-TTE endpoints
(see vignette('actionFunctions')). This usually mean a large
number of columns in outputs. If users have no intent to summarize a
trial on these columns, setting tidy = TRUE can eliminate these
columns from get_output(). This is useful to reduced the size of
output data frame when a large number of replicates are done for
simulation. Note that currently we use regex
"^n_events_<.*?>_<.*?>$" and
"^milestone_time_<.*?>$" to match columns to be eliminated.
If users plan to use tidy = TRUE, caution is needed when naming
custom outputs in save(). Default FALSE.
run()
run trial simulation.
Controllers$run( n = 1, n_workers = 1, plot_event = TRUE, silent = FALSE, dry_run = FALSE )
ninteger. Number of replicates of simulation.
n = 1 by default. Simulation results can be accessed by
controller$get_output().
n_workersinteger. Number of parallel workers. When
n_workers = 1 (default), replicates are run sequentially.
When n_workers > 1, replicates are distributed across
parallel workers using the mirai package, which must be
installed separately. Each worker receives a serialized copy of
the trial and listener objects and runs its share of replicates
independently. If any replicate encounters an error, execution
stops and already-collected results are preserved in
$get_output(). To debug, manually set seed in
trial() and n_workers = 1 in run() for reproduced
results. Note that optimal n_workers may not be
parallel::detectCores(). For example, Macbook with M1/M2/M3 chips
may have performance cores and efficiency cores. To achieve the best
parallel performance, one may want to use the performance cores only.
For a M1 laptop with 4 performance cores, n_workers = 3 may give
the best performance.
plot_eventlogical. Create event plot if TRUE. Forced to
FALSE when n > 1 or n_workers > 1.
silentlogical. TRUE if muting all messages during a
trial. Note that warning messages are still displayed.
dry_runlogical. We are considering retire this argument.
TRUE if action function provided by users is
ignored and an internal default action .default_action is called
instead. This default function only locks data when the milestone is
triggered. Milestone time and number of endpoints' events or sample sizes
are saved. It is suggested to set dry_run = TRUE to estimate
distributions of triggering time and number of events before formally
using custom action functions if a fixed design is in use.
This helps determining planned maximum
information for group sequential design and reasonable time of milestone
of interest when planning a trial. Set it to FALSE for formal
simulations. However, for an adaptive design where arm(s) could
possibly be added or removed, setting dry_run to TRUE
is usually not helpful because adaption should be executed
before estimating the milestone time.
clone()
The objects of this class are cloneable with this method.
Controllers$clone(deep = FALSE)
deepWhether to make a deep clone.
####
Apply a milestone-triggered crossover to patients who are still in the trial.
Unlike a regimen registered via add_regimen() (which is applied once,
at enrollment), crossover() is meant to be called inside a
milestone's action function. At the earliest crossover (calendar) time
T = trial$get_current_time() + delay, eligible patients may switch to a
new treatment, and only their post-switch endpoint values are altered;
already-observed data is left intact. The crossover triplet is stacked onto
the trial's regimen, so it is also re-applied to patients enrolled later.
Eligibility (the pool passed to what()) consists of patients with at
least one endpoint still "open" (unobserved, dropout-/duration-aware) at
T; fully-observed patients are excluded. when() must return a
switch time with enroll_time + switch_time >= T (a crossover cannot
predate its opening) or an error is raised. how() may only change
post-switch outcomes; returning a changed value for a pre-switch/locked cell
raises an error.
Two helper columns are injected into patient_data for the triplet
functions: earliest_crossover_calendar_time (= T) and
earliest_crossover_time_from_enrollment (= max(T - enroll_time, 0)),
so when() can write e.g.
switch_time = pmax(pfs, earliest_crossover_time_from_enrollment).
Note that this function should only be called within action functions of
milestones. It is users' responsibility to ensure that and
TrialSimulator has no way to track it.
This is a user-friendly wrapper of the member function of trial, i.e.,
Trials$crossover(). Users who are not familiar with the concept of
classes may consider using this wrapper directly.
crossover(trial, what, how, when = NULL, delay = 0, ...)crossover(trial, what, how, when = NULL, delay = 0, ...)
trial |
a trial object returned by |
what |
a function selecting which eligible patients crossover, returning
one row per crossing-over patient with their |
how |
a function returning the modified post-switch endpoint values for
crossing-over patients. See |
when |
(optional) a function returning |
delay |
numeric. Time after the milestone before the crossover opens, so
|
... |
(optional) named arguments routed to |
This is an action function that does nothing when the corresponding milestone
is triggered. When the listener is monitoring a trial and determining the
time to trigger a milestone, data is automatically locked with other necessary
data manipulations (censoring, truncation, etc.) are executed.
If the users have no intent to modify the
trial adaptively at the milestone, e.g., adding (add_arms()) or
removing (remove_arms()) arm(s),
changing sampling ratio(s) (update_sample_ratio()),
modifying trial duration (set_duration()), carrying out statistical
testing, or saving intermediate results (save(), etc.),
then this function
can be used to set the argument action when creating a new milestone.
Note that the triggering time and number of observations/events of endpoints
at a milestone with action = doNothing is still recorded in output
automatically.
doNothing(trial, ...)doNothing(trial, ...)
trial |
an object returned from |
... |
(optional) arguments. This is for capturing redundant arguments
in |
This function returns NULL. Actually, nothing is done in this function.
This function may be useful to advanced users of TrialSimulator. It
creates a wrapper function of a random number generator, while fixing a
subset or all of arguments. This function is design to prevent inadvertent
changing to arguments of random number generator. See examples below.
DynamicRNGFunction(fn, ...)DynamicRNGFunction(fn, ...)
fn |
random number generator, e.g., |
... |
arguments for |
a function to generate random number based on fn and arguments in
.... Specified arguments will be fixed and cannot be changed when invoking
DynamicRNGFunction(fn, ...)(). For example,
if foo <- DynamicRNGFunction(rnorm, sd = 2),
then foo(n = 100) will always generate data from normal distribution of
variance 4. foo(n = 100, sd = 1) will trigger an error. However,
if an argument is not specified in DynamicRNGFunction, then it can be specified
later. For example, foo(n = 100, mean = -1) will generate data from N(-1, 4).
# example code dfunc <- DynamicRNGFunction(rnorm, sd = 3.2) x <- dfunc(1e3) # mean 0 and sd 3.2 hist(x) y <- dfunc(1e3, mean = 3.5) # mean can be changed mean(y) try(z <- dfunc(1e3, sd = 1)) # error because sd is fixed in dfunc# example code dfunc <- DynamicRNGFunction(rnorm, sd = 3.2) x <- dfunc(1e3) # mean 0 and sd 3.2 hist(x) y <- dfunc(1e3, mean = 3.5) # mean can be changed mean(y) try(z <- dfunc(1e3, sd = 1)) # error because sd is fixed in dfunc
Define one or multiple endpoints. This is a user-friendly wrapper for
the class constructor Endpoints$new. Users who are not familiar with
the concept of classes may consider using this wrapper directly.
Note that it is users' responsibility to assure that the units of readout of non-tte endpoints, dropout time, and trial duration are consistent.
endpoint(name, type, readout = NULL, generator, ...)endpoint(name, type, readout = NULL, generator, ...)
name |
character vector. Name(s) of endpoint(s) |
type |
character vector. Type(s) of endpoint(s) in |
readout |
numeric vector named by non-tte endpoint(s).
|
generator |
a RNG function. Its first argument must be |
... |
(optional) arguments of |
set.seed(12345) ## Example 1. Generate a time-to-event endpoint. ## Two columns are returned, one for time, one for event (1/0, 0 for ## A built-in RNG function is used to handle piecewise constant exponential ## distribution risk <- data.frame( end_time = c(1, 10, 26.0, 52.0), piecewise_risk = c(1, 1.01, 0.381, 0.150) * exp(-3.01) ) pfs <- endpoint(name = 'pfs', type='tte', generator = PiecewiseConstantExponentialRNG, risk = risk, endpoint_name = 'pfs') # run it in R console to display a summary report # event indicator takes values 0/1 pfs ## Example 2. Generate continuous and binary endpoints using R's built-in ## RNG functions, e.g. rnorm, rexp, rbinom, etc. ep1 <- endpoint( name = 'cd4', type = 'non-tte', generator = rnorm, readout = c(cd4=1), mean = 1.2) ep2 <- endpoint( name = 'resp_time', type = 'non-tte', generator = rexp, readout = c(resp_time=0), rate = 4.5) ep3 <- endpoint( name = 'orr', type = 'non-tte', readout = c(orr=3), generator = rbinom, size = 1, prob = .4) ep1 # run it in R console. Mean and sd should be comparable to (1.2, 1.0) ep2 # run it in R console. Median should be comparable to log(2)/4.5 = 0.154 ep3 # run it in R console. Mean and sd should be comparable to 0.4 and 0.49 ## Example3: delayed effect ## Use piecewise constant exponential random number generator ## Baseline hazards are piecewise constant ## Hazard ratios are piecewise constant, resulting a delayed effect. ## Note that this example is for explaining the concept of "endpoint". ## Generating endpoint data manually is not the recommended way to use this package. run <- TRUE if (!requireNamespace("survminer", quietly = TRUE)) { run <- FALSE message("Please install 'survminer' to run this example.") } if (!requireNamespace("survival", quietly = TRUE)) { run <- FALSE message("Please install 'survival' to run this example.") } if(run){ risk1 <- risk ep1 <- endpoint( name = 'pfs', type='tte', generator = PiecewiseConstantExponentialRNG, risk=risk1, endpoint_name = 'pfs') risk2 <- risk1 risk2$hazard_ratio <- c(1, 1, .6, .4) ep2 <- endpoint( name = 'pfs', type='tte', generator = PiecewiseConstantExponentialRNG, risk=risk2, endpoint_name = 'pfs') n <- 1000 tte <- rbind(ep1$get_generator()(n), ep2$get_generator()(n)) arm <- rep(0:1, each = n) dat <- data.frame(tte, arm) sfit <- survival::survfit( survival::Surv(time = pfs, event = pfs_event) ~ arm, dat) survminer::ggsurvplot(sfit, data = dat, pval = TRUE, # Show p-value conf.int = TRUE, # Show confidence intervals risk.table = TRUE, # Add risk table palette = c("blue", "red")) ## print summary reports for endpoint objects in console ep1 ep2 } ## Example 4: generate correlated pfs and os ## See vignette('simulatePfsAndOsIdm') and vignette('simulatePfsAndOsGumbel')set.seed(12345) ## Example 1. Generate a time-to-event endpoint. ## Two columns are returned, one for time, one for event (1/0, 0 for ## A built-in RNG function is used to handle piecewise constant exponential ## distribution risk <- data.frame( end_time = c(1, 10, 26.0, 52.0), piecewise_risk = c(1, 1.01, 0.381, 0.150) * exp(-3.01) ) pfs <- endpoint(name = 'pfs', type='tte', generator = PiecewiseConstantExponentialRNG, risk = risk, endpoint_name = 'pfs') # run it in R console to display a summary report # event indicator takes values 0/1 pfs ## Example 2. Generate continuous and binary endpoints using R's built-in ## RNG functions, e.g. rnorm, rexp, rbinom, etc. ep1 <- endpoint( name = 'cd4', type = 'non-tte', generator = rnorm, readout = c(cd4=1), mean = 1.2) ep2 <- endpoint( name = 'resp_time', type = 'non-tte', generator = rexp, readout = c(resp_time=0), rate = 4.5) ep3 <- endpoint( name = 'orr', type = 'non-tte', readout = c(orr=3), generator = rbinom, size = 1, prob = .4) ep1 # run it in R console. Mean and sd should be comparable to (1.2, 1.0) ep2 # run it in R console. Median should be comparable to log(2)/4.5 = 0.154 ep3 # run it in R console. Mean and sd should be comparable to 0.4 and 0.49 ## Example3: delayed effect ## Use piecewise constant exponential random number generator ## Baseline hazards are piecewise constant ## Hazard ratios are piecewise constant, resulting a delayed effect. ## Note that this example is for explaining the concept of "endpoint". ## Generating endpoint data manually is not the recommended way to use this package. run <- TRUE if (!requireNamespace("survminer", quietly = TRUE)) { run <- FALSE message("Please install 'survminer' to run this example.") } if (!requireNamespace("survival", quietly = TRUE)) { run <- FALSE message("Please install 'survival' to run this example.") } if(run){ risk1 <- risk ep1 <- endpoint( name = 'pfs', type='tte', generator = PiecewiseConstantExponentialRNG, risk=risk1, endpoint_name = 'pfs') risk2 <- risk1 risk2$hazard_ratio <- c(1, 1, .6, .4) ep2 <- endpoint( name = 'pfs', type='tte', generator = PiecewiseConstantExponentialRNG, risk=risk2, endpoint_name = 'pfs') n <- 1000 tte <- rbind(ep1$get_generator()(n), ep2$get_generator()(n)) arm <- rep(0:1, each = n) dat <- data.frame(tte, arm) sfit <- survival::survfit( survival::Surv(time = pfs, event = pfs_event) ~ arm, dat) survminer::ggsurvplot(sfit, data = dat, pval = TRUE, # Show p-value conf.int = TRUE, # Show confidence intervals risk.table = TRUE, # Add risk table palette = c("blue", "red")) ## print summary reports for endpoint objects in console ep1 ep2 } ## Example 4: generate correlated pfs and os ## See vignette('simulatePfsAndOsIdm') and vignette('simulatePfsAndOsGumbel')
Create a class of endpoint to specify its name, type, readout time (optional) and assign a random number generator.
Public methods in this R6 class are used in developing this package. Thus, I have to export the whole R6 class which exposures all public methods. However, none of the public methods is useful to end users except for the one below.
$print()
new()
initialize an endpoint.
Endpoints$new(name, type, readout = NULL, generator, ...)
namecharacter vector. Name(s) of endpoint(s)
typecharacter vector. Type(s) of endpoint(s). It supports
"tte" for time-to-event endpoints, and "non-tte" for
all other types of endpoints (e.g., continous, binary, categorical,
or repeated measurement. TrialSimulator will do some verification if
an endpoint is of type "tte". However, no special
manipulation is done for non-tte endpoints.
readouta named numeric vector with name to be non-tte endpoint(s).
readout must be specified for every non-tte endpoint. For
example, c(endpoint1 = 6, endpoint2 = 3), which means that it
takes 6 and 3 unit time to get readout of
endpoint1 and endpoint2 of a patient since being
randomized. Error message would be prompted if readout is not
named or readout is not specified for some non-tte endpoint.
If all endpoints are tte, readout should be NULL as default.
generatora random number generation (RNG) function.
It supports all built-in random number generators in stats, e.g.,
stats::rnorm, stats::rexp, etc. that with n as the
argument for number of observations and returns a vector.
A custom RNG function is also supported.
generator could be any functions as long as
(1) its first argument is n; and (2) it returns a vector of
length n (univariate endpoint) or a data frame of n rows
(multiple endpoints), i.e., custom RNG can return data of more than one
endpoint. This is useful when users need to simulate correlated endpoints
or longitudinal data. The column names of returned data frame should
match to name exactly, although order of columns does not matter.
If an endpoint is of type "tte", the custom generator
should also return a column as its event indicator. For example,
if "pfs" is "tte",
then custom generator should return at least two columns
"pfs" and "pfs_event". Usually pfs_event can be
all 1s if no censoring. Some RNG functions, e.g.,
TrialSimulator::PiecewiseConstantExponentialRNG() and
TrialSimulator::CorrelatedPfsAndOs4(), simulate TTE endpoint
data with censoring simultaneously, thus 0 exists in the columns of
event indicators. Users can implement censorship in their own RNG.
Censoring can also be specified later when
defining a trial object through argument dropout.
See ?trial.
Note that if covariates, e.g., biomarker, subgroup, are needed in
generating and analyzing trial data, they can and should be defined as
endpoints in endpoint() as well.
...optional arguments for generator.
test_generator()
test random number generator of the endpoints. It returns an example
dataset of an endpoint object. Note that users of TrialSimulator
does not need to call this function to generate trial data; instead,
the package will call this function at milestone automatically.
Users may see example in vignette where this function is called.
However, it is for illustration purpose only. In practice, this function
may be used for debugging if users suspect some issues in custom
generator, otherwise, this function should never been called in formal
simulation.
Endpoints$test_generator(n = 1000)
ninteger. Number of random numbers generated from the generator.
get_generator()
return random number generator of an endpoint
Endpoints$get_generator()
update_generator()
update endpoint generator
Endpoints$update_generator(generator, ...)
generatora random number generation (RNG) function.
See generator of endpoint().
...optional arguments for generator.
get_readout()
return readout function
Endpoints$get_readout()
get_uid()
return uid
Endpoints$get_uid()
get_name()
return endpoints' name
Endpoints$get_name()
get_type()
return endpoints' type
Endpoints$get_type()
print()
print an endpoint object
Endpoints$print(categorical_vars = NULL)
categorical_varsa character vector of endpoints. This can
be used to force variables with limited distinct values as categorical
variables in summary report. For example, a numeric endpoint may take
integer values 0, 1, 2. Instead of computing mean and standard derivation
in the summary report, put this endpoint in categorical_vars can
force it be a categorical variable and a barplot is generated in summary
report instead.
rng <- function(n){
data.frame(x = sample(1:3, n, replace = TRUE),
y = sample(1:3, n, replace = TRUE)
)
}
ep <- endpoint(name = c('x', 'y'),
type = c('non-tte', 'non-tte'),
readout = c(x = 0, y = 0),
generator = rng)
## x and y as continuous endpoints, thus mean and sd are reported
ep
## force y to be categorical to create barplot of it
print(ep, categorical_vars = 'y')
clone()
The objects of this class are cloneable with this method.
Endpoints$clone(deep = FALSE)
deepWhether to make a deep clone.
# Instead of using Endpoints$new(), please use endpoint(), a user-friendly # wrapper to define endpoints. See examples in ?endpoint. ## ------------------------------------------------ ## Method `Endpoints$print` ## ------------------------------------------------ rng <- function(n){ data.frame(x = sample(1:3, n, replace = TRUE), y = sample(1:3, n, replace = TRUE) ) } ep <- endpoint(name = c('x', 'y'), type = c('non-tte', 'non-tte'), readout = c(x = 0, y = 0), generator = rng) ## x and y as continuous endpoints, thus mean and sd are reported ep ## force y to be categorical to create barplot of it print(ep, categorical_vars = 'y')# Instead of using Endpoints$new(), please use endpoint(), a user-friendly # wrapper to define endpoints. See examples in ?endpoint. ## ------------------------------------------------ ## Method `Endpoints$print` ## ------------------------------------------------ rng <- function(n){ data.frame(x = sample(1:3, n, replace = TRUE), y = sample(1:3, n, replace = TRUE) ) } ep <- endpoint(name = c('x', 'y'), type = c('non-tte', 'non-tte'), readout = c(x = 0, y = 0), generator = rng) ## x and y as continuous endpoints, thus mean and sd are reported ep ## force y to be categorical to create barplot of it print(ep, categorical_vars = 'y')
Define a condition to trigger trial milestone by the number of randomized patients. The milestone will be triggered when a trial has enrolled at least the specified number of patients. It can be used combined with conditions specified by calendarTime and eventNumber.
Refer to the
vignette
to learn how to define milestones when performing simulation using
TrialSimulator.
enrollment(n, ..., arms = NULL, min_treatment_duration = 0)enrollment(n, ..., arms = NULL, min_treatment_duration = 0)
n |
integer. Number of randomized patients. |
... |
subset conditions compatible with |
arms |
vector of character. Name of arms on which the number of patients
is counted. If |
min_treatment_duration |
numeric. Zero or positive value.
minimum treatment duration of enrolled patients.
Default is 0, i.e., looking for triggering time based on number of enrolled
patients in population specified by |
an object of class 'Condition'
## ensure sufficient sample size of whole trial enrollment(n = 100) ## ensure sufficient sample size in sub-group of interest enrollment(n = 100, biomarker1 == 'positive' & biomarker2 == 'high') ## ensure sufficient sample size in high dose + placebo enrollment(n = 1000, arms = c('high dose', 'placebo')) ## ensure sufficient treatment duration enrollment(n = 500, min_treatment_duration = 2)## ensure sufficient sample size of whole trial enrollment(n = 100) ## ensure sufficient sample size in sub-group of interest enrollment(n = 100, biomarker1 == 'positive' & biomarker2 == 'high') ## ensure sufficient sample size in high dose + placebo enrollment(n = 1000, arms = c('high dose', 'placebo')) ## ensure sufficient treatment duration enrollment(n = 500, min_treatment_duration = 2)
Define a condition to trigger trial milestone by the number of events of a time-to-event endpoint or the number of non-missing observations of a non-time-to-event endpoint. The milestone will be triggered when a trial has observed at least the specified number of endpoint events (or non-missing observations). It can be used combined with conditions specified by calendarTime and enrollment.
Number of events for a time-to-event endpoint can vary at different milestones as more patients are randomized into a trial, or more events onset over time.
Number of non-missing observations for a non-time-to-event endpoint can vary
at different milestones as more patients are randomized into a trial, or more
patients have been treated until their readout time (thus, NA turns
to a value).
Both numbers are affected by dropout.
Refer to the
vignette
to learn how to define milestones when performing simulation using
TrialSimulator.
eventNumber(endpoint, n, ..., arms = NULL)eventNumber(endpoint, n, ..., arms = NULL)
endpoint |
character. Name of an endpoint. It should be something that
is specified in the argument |
n |
integer. Targeted number of events or non-missing obervations, depending on the type of endpoint. |
... |
subset conditions compatible with |
arms |
vector of character. Name of arms on which the number of
events/observations is counted. If |
an object of class 'Condition'
Expands the compact regimen_trajectory column in locked data
(returned by trial$get_locked_data()) into a long-format data frame
with one row per regimen segment per patient.
The regimen_trajectory column stores each patient's treatment history
as a semicolon-separated string of "name\@time" entries, e.g.
"placebo\@0;low dose\@8.5". expandRegimen parses this into
two additional columns:
regimen — name of the treatment regimen
switch_time_from_enrollment — time from enrollment at which
the patient switched to this regimen
The regimen_trajectory column is dropped from the result.
expandRegimen(data)expandRegimen(data)
data |
a data frame returned by |
a long-format data frame: one row per regimen segment per patient,
with regimen and switch_time_from_enrollment appended and
regimen_trajectory removed.
## Not run: locked <- trial$get_locked_data('final') long <- expandRegimen(locked) ## End(Not run)## Not run: locked <- trial$get_locked_data('final') long <- expandRegimen(locked) ## End(Not run)
Fit Cox proportional hazards model on an time-to-event endpoint.
Refer to this vignette for more information and examples.
fitCoxph(formula, placebo, data, alternative, scale, ..., tidy = TRUE)fitCoxph(formula, placebo, data, alternative, scale, ..., tidy = TRUE)
formula |
An object of class |
placebo |
Character. String indicating the placebo in |
data |
Data frame. Usually it is a data snapshot locked at a milestone. |
alternative |
a character string specifying the alternative hypothesis,
must be one of |
scale |
character. The type of estimate in the output. Must be one
of |
... |
(optional) subset conditions compatible with |
tidy |
logical. |
a data frame with three columns:
armname of the treatment arm.
placeboname of the placebo arm.
estimateestimate of main effect of arm, depending on scale.
pone-sided p-value for log hazard ratio (treated vs placebo).
infothe number of events of the endpoint in the subset.
zthe z statistics of log hazard ratios.
Test rate difference by comparing it to a pre-specified value using the Farrington-Manning test.
Refer to this vignette for more information and examples.
fitFarringtonManning(endpoint, placebo, data, alternative, ..., delta = 0)fitFarringtonManning(endpoint, placebo, data, alternative, ..., delta = 0)
endpoint |
Character. Name of the endpoint in |
placebo |
Character. String indicating the placebo in |
data |
Data frame. Usually it is a locked data set. |
alternative |
a character string specifying the alternative hypothesis,
must be one of |
... |
Subset conditions compatible with |
delta |
the rate difference between a treatment arm and placebo under the null. 0 by default. |
a data frame with three columns:
armname of the treatment arm.
placeboname of the placebo arm.
estimateestimate of rate difference.
pone-sided p-value for log odds ratio (treated vs placebo).
infosample size in the subset with NA being removed.
zthe z statistics of log odds ratio (treated vs placebo).
Farrington, Conor P., and Godfrey Manning. "Test statistics and sample size formulae for comparative binomial trials with null hypothesis of non-zero risk difference or non-unity relative risk." Statistics in medicine 9.12 (1990): 1447-1454.
Fit linear regression model on a continuous endpoint.
Refer to this vignette for more information and examples.
fitLinear(formula, placebo, data, alternative, ...)fitLinear(formula, placebo, data, alternative, ...)
formula |
an object of class |
placebo |
Character. String indicating the placebo arm in |
data |
Data frame. Usually it is a locked data set. |
alternative |
a character string specifying the alternative hypothesis,
must be one of |
... |
Subset conditions compatible with |
a data frame with columns:
armname of the treatment arm.
placeboname of the placebo arm.
estimateestimate of average treatment effect of arm.
pone-sided p-value for between-arm difference (treated vs placebo).
infosample size used in model with NA being removed.
zz statistics of between-arm difference (treated vs placebo).
Fit logistic regression model on an binary endpoint.
Refer to this vignette for more information and examples.
fitLogistic(formula, placebo, data, alternative, scale, ...)fitLogistic(formula, placebo, data, alternative, scale, ...)
formula |
An object of class |
placebo |
Character. String indicating the placebo in |
data |
Data frame. Usually it is a locked data set. |
alternative |
a character string specifying the alternative hypothesis,
must be one of |
scale |
character. The type of estimate in the output. Must be one
of |
... |
Subset conditions compatible with |
a data frame with columns:
armname of the treatment arm.
placeboname of the placebo arm.
estimateestimate depending on scale.
pone-sided p-value for log odds ratio (treated vs placebo).
infosample size used in model with NA being removed.
zz statistics of log odds ratio (treated vs placebo).
Compute log rank test statistic on an endpoint.
Refer to this vignette for more information and examples.
fitLogrank(formula, placebo, data, alternative, ..., tidy = TRUE)fitLogrank(formula, placebo, data, alternative, ..., tidy = TRUE)
formula |
An object of class |
placebo |
character. String of placebo in |
data |
data frame. Usually it is a locked data. |
alternative |
a character string specifying the alternative hypothesis,
must be one of |
... |
subset condition that is compatible with |
tidy |
logical. |
a data frame with three columns:
armname of the treatment arm.
placeboname of the placebo arm.
pone-sided p-value for log-rank test (treated vs placebo).
infothe number of events of the endpoint in the subset.
zthe z statistics of log hazard ratios.
Internal function that retrieves precomputed simulation results. Not meant for use by package users.
getAdaptiveDesignOutput()getAdaptiveDesignOutput()
A data frame containing simulation results of 1000 replicates.
Internal function that retrieves precomputed simulation results. Not meant for use by package users.
getDoseRangingOutput()getDoseRangingOutput()
A data frame containing simulation results of 10 replicates.
Internal function that retrieves precomputed simulation results. Not meant for use by package users.
getFixedDesignOutput()getFixedDesignOutput()
A data frame containing simulation results of 1000 replicates.
Perform graphical testing under group sequential design for one or multiple endpoints. See Maurer & Bretz (2013).
new()
Initialize an object for graphical testing procedure. Group sequential design is also supported.
GraphicalTesting$new( alpha, transition, alpha_spending, planned_max_info, hypotheses = NULL, silent = FALSE )
alphainitial alpha allocated to each of the hypotheses.
transitionmatrix of transition weights. Its diagonals should be all 0s. The row sums should be 1s (for better power) or 0s (if no outbound edge from a node).
alpha_spendingcharacter vector of same length of alpha.
Currently it supports 'asP', 'asOF', and 'asUser'.
planned_max_infovector of integers. Maximum numbers of events (tte endpoints) or patients (non-tte endpoints) at the final analysis of each hypothesis when planning a trial. The actual numbers could be different, which can be specified elsewhere.
hypothesesvector of characters. Names of hypotheses.
silentTRUE if muting all messages and not generating
plots.
reset()
reset an object of class GraphicalTesting to original status
so that it can be reused.
GraphicalTesting$reset()
is_valid_hid()
determine if index of a hypothesis is valid
GraphicalTesting$is_valid_hid(hid)
hidan integer
get_hypothesis_name()
get name of a hypothesis given its index.
GraphicalTesting$get_hypothesis_name(hid)
hidan integer
get_weight()
return weight between two nodes
GraphicalTesting$get_weight(hid1, hid2)
hid1an integer
hid2an integer
set_weight()
update weight between two nodes
GraphicalTesting$set_weight(hid1, hid2, value)
hid1an integer
hid2an integer
valuenumeric value to be set as a weight two nodes
get_alpha()
return alpha allocated to a hypothesis when calling this function.
Note that a function can be called several time with the graph is
updated dynamically. Thus, returned alpha can be different even for
the same hid.
GraphicalTesting$get_alpha(hid)
hidan integer
set_alpha()
update alpha of a hypothesis
GraphicalTesting$set_alpha(hid, value)
hidinteger. Index of a hypothesis
valuenumeric value to be allocated
get_hypotheses_ids()
return all valid hid
GraphicalTesting$get_hypotheses_ids()
get_number_hypotheses()
return number of hypotheses, including those been rejected.
GraphicalTesting$get_number_hypotheses()
get_hids_not_in_graph()
return index of hypotheses that are rejected.
GraphicalTesting$get_hids_not_in_graph()
get_testable_hypotheses()
return index of hypotheses with non-zero alphas, thus can be tested at the current stage.
GraphicalTesting$get_testable_hypotheses()
has_testable_hypotheses()
determine whether at least one hypothesis is testable.
If return FALSE, the testing procedure is completed.
GraphicalTesting$has_testable_hypotheses()
is_in_graph()
determine whether a hypothesis is not yet rejected (in graph).
GraphicalTesting$is_in_graph(hid)
hidinteger. Index of a hypothesis
is_testable()
determine whether a hypothesis has a non-zero alpha allocated.
GraphicalTesting$is_testable(hid)
hidinteger. Index of a hypothesis
get_hid()
convert hypothesis's name into (unique) index.
GraphicalTesting$get_hid(hypothesis)
hypothesischaracter. Name of a hypothesis. It is different from
hid, which is an index.
reject_a_hypothesis()
remove a node from graph when a hypothesis is rejected
GraphicalTesting$reject_a_hypothesis(hypothesis)
hypothesisname of a hypothesis. It is different from
hid, which is an index.
set_trajectory()
save new testing results at current stage
GraphicalTesting$set_trajectory(result)
resulta data frame of specific columns.
get_trajectory()
return testing results by the time this function is called. Note that graphical test is carried out in a sequential manner. Users may want to review the results anytime. Value returned by this function can possibly vary over time.
GraphicalTesting$get_trajectory()
test_hypotheses()
test hypotheses using p-values (and other information in stats)
base on the current graph. All rows should have the same order
number.
GraphicalTesting$test_hypotheses(stats)
statsa data frame. It must contain the following columns:
orderinteger. P-values (among others) of hypotheses that
can be tested at the same time (e.g., an interim, or final analysis)
should be labeled with the same order number.
If a hypothesis is not tested at a stage,
simply don't put it in stats with that order number.
hypothesescharacter. Name of hypotheses to be tested. They
should be identical to those when calling GraphicalTesting$new.
pnominal p-values.
infoobserved number of events or samples at test. These will be used to compute information fractions in group sequential design.
max_infointegers. Maximum information at test. At interim,
max_info should be equal to planned_max_info when
calling GraphicalTesting$new. At the final stage of a
hypothesis, one can update it with observed numbers.
test()
test hypotheses using p-values (and other information in stats)
base on the current graph. Users can call this function multiple times.
P-values of the same order should be passed through stats
together. P-values of multiple orders can be passed together as well.
For example, if users only have p-values at current stage, they can call
this function and update the graph accordingly. In this case, column
order in stats is a constant. They can call this
function again when p-values of next stage is available, where
order is another integer. In simulation, if p-values of all
stages are on hand, users can call this function to
test them all in a single pass. In this case, column order in
stats can have different values.
GraphicalTesting$test(stats)
statsa data frame. It must contain the following columns:
orderinteger. P-values (among others) of hypotheses that
can be tested at the same time (e.g., an interim, or final analysis)
should be labeled with the same order number.
If a hypothesis is not tested at a stage,
simply don't put it in stats with that order number.
If all p-values in stats are tested at the same stage, order
can be absent.
hypothesescharacter. Name of hypotheses to be tested. They
should be identical to those when calling
GraphicalTesting$new.
pnominal p-values.
infoobserved number of events or samples at test. These will be used to compute information fractions in group sequential design.
max_infointegers. Maximum information at test. At interim,
max_info should be equal to planned_max_info when
calling GraphicalTesting$new. At the final stage of a
hypothesis, one can update it with observed numbers.
alpha_spentaccumulative proportion of allocated alpha
to be spent if alpha_spending = "asUser". Set it to
NA_real_ otherwise. If no hypothesis uses "asUser" in
stats, this column could be ignored.
a data frame returned by get_current_testing_results.
It contains details of each of the testing steps.
get_current_testing_results()
return testing results with details by the time this function
is called. This function can be called by users by multiple
times, thus the returned value varies over time.
This function is called by GraphicalTesting::test,
and returns a data frame consisting of columns
hypothesisname of hypotheses.
obs_p_valueobserved p-values.
max_allocated_alphamaximum allocated alpha for the hypothesis.
decision'reject' or 'accept' the hypotheses.
stagesstage of a hypothesis.
orderorder number that this hypothesis is tested for the last time.
It is different from stages.
typeOfDesignname of alpha spending functions.
GraphicalTesting$get_current_testing_results()
get_current_decision()
get current decisions for all hypotheses. Returned value could changes over time because it depends on the stages being tested already.
GraphicalTesting$get_current_decision()
a named vector of values "accept" or "reject".
Note that if a hypothesis is not yet tested when calling this function,
the decision for that hypothesis would be "accept".
print()
generic function for print
GraphicalTesting$print(graph = TRUE, trajectory = TRUE, ...)
graphlogic. TRUE if visualizing the current graph,
which can vary over time.
trajectorylogic. TRUE if print the current data frame of
trajectory, which can vary over time.
...other arguments supported in gMCPLite::hGraph,
e.g., trhw and trhh to control the size of transition box,
and trdigits to control the digits displayed for transition
weights.
clone()
The objects of this class are cloneable with this method.
GraphicalTesting$clone(deep = FALSE)
deepWhether to make a deep clone.
## Example 1 ## dry-run to study the behavior of a graph ## without group sequential design if(interactive()){ eps <- .01 alpha <- c(.01, .04, 0, 0, 0) transition <- matrix(c( 0, 0, 0, 0, 1, 0, 0, .75, 0, .25, 0, 1/2-eps/2, 0, eps, 1/2-eps/2, 0, 0, 0, 0, 0, 0, 1/2, 1/2, 0, 0 ), nrow = 5, byrow = TRUE) ## dummy can be anything, we don't actually use it asf <- rep('asOF', 5) ## dummy can be anything, we don't actually use it max_info <- c(300, 1100, 1100, 1100, 500) hs <- c('H1: UPCR IgA', 'H2: eGFR GN', 'H3: eGFR GN 10wk', 'H5: 2nd Endpoints', 'H4: eGFR IgA') ## initialize an object gt <- GraphicalTesting$new(alpha, transition, asf, max_info, hs) print(gt) ## reject hypotheses based on customized order ## to understand the behavior of a testing strategy ## Any other rejection order is possible gt$reject_a_hypothesis('H1: UPCR IgA') print(gt) gt$reject_a_hypothesis('H2: eGFR GN') print(gt) gt$reject_a_hypothesis('H4: eGFR IgA') print(gt) gt$reject_a_hypothesis('H3: eGFR GN 10wk') print(gt) gt$reset() } ## Example 2 ## Example modified from vignettes in gMCPLite: ## Graphical testing for group sequential design if(interactive()){ ## initial alpha split to each of the hypotheses alpha <- c(.01, .01, .004, .0, .0005, .0005) ## transition matrix of the initial graph transition <- matrix(c( 0, 1, 0, 0, 0, 0, 0, 0, .5, .5, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, .5, .5, 0, 0, 0, 0, 0, 1, .5, .5, 0, 0, 0, 0 ), nrow = 6, byrow = TRUE) ## alpha spending functions per hypothesis asf <- c('asUser', 'asOF', 'asUser', 'asOF', 'asOF', 'asOF') ## planned maximum number of events per hypothesis max_info <- c(295, 800, 310, 750, 500, 1100) ## name of hypotheses hs <- c('H1: OS sub', 'H2: OS all', 'H3: PFS sub', 'H4: PFS all', 'H5: ORR sub', 'H6: ORR all') gt <- GraphicalTesting$new(alpha, transition, asf, max_info, hs) ## print initial graph gt ## nominal p-values at each stage ## Note: p-values with same order are calculated with the same locked data ## Note: alpha_spent is only specified for hypotheses using custom alpha ## spending function "asUser" stats <- data.frame( order = c(1:3, 1:3, 1:2, 1:2, 1, 1), hypotheses = c(rep('H1: OS sub', 3), rep('H2: OS all', 3), rep('H3: PFS sub', 2), rep('H4: PFS all', 2), 'H5: ORR sub', 'H6: ORR all'), p = c(.03, .0001, .000001, .2, .15, .1, .2, .001, .3, .2, .00001, .1), info = c(185, 245, 295, 529, 700, 800, 265, 310, 675, 750, 490, 990), is_final = c(F, F, T, F, F, T, F, T, F, T, T, T), max_info = c(rep(295, 3), rep(800, 3), rep(310, 2), rep(750, 2), 490, 990), alpha_spent = c(c(.1, .4, 1), rep(NA, 3), c(.3, 1), rep(NA, 2), NA, NA) ) ## test the p-values from the first analysis, plot the updated graph gt$test(stats %>% dplyr::filter(order==1)) ## test the p-values from the second analysis, plot the updated graph gt$test(stats %>% dplyr::filter(order==2)) ## test the p-values from the third analysis, plot the updated graph ## because no futher test would be done, displayed results are final gt$test(stats %>% dplyr::filter(order==3)) ## plot the final status of the graph print(gt, trajectory = FALSE) ## you can get final testing results as follow gt$get_current_testing_results() ## if you want to see step-by-step details print(gt$get_trajectory()) ## equivalently, you can call gt$test(stats) for only once to get same results. gt$reset() gt$test(stats) ## if you only want to get the final testing results gt$get_current_decision() }## Example 1 ## dry-run to study the behavior of a graph ## without group sequential design if(interactive()){ eps <- .01 alpha <- c(.01, .04, 0, 0, 0) transition <- matrix(c( 0, 0, 0, 0, 1, 0, 0, .75, 0, .25, 0, 1/2-eps/2, 0, eps, 1/2-eps/2, 0, 0, 0, 0, 0, 0, 1/2, 1/2, 0, 0 ), nrow = 5, byrow = TRUE) ## dummy can be anything, we don't actually use it asf <- rep('asOF', 5) ## dummy can be anything, we don't actually use it max_info <- c(300, 1100, 1100, 1100, 500) hs <- c('H1: UPCR IgA', 'H2: eGFR GN', 'H3: eGFR GN 10wk', 'H5: 2nd Endpoints', 'H4: eGFR IgA') ## initialize an object gt <- GraphicalTesting$new(alpha, transition, asf, max_info, hs) print(gt) ## reject hypotheses based on customized order ## to understand the behavior of a testing strategy ## Any other rejection order is possible gt$reject_a_hypothesis('H1: UPCR IgA') print(gt) gt$reject_a_hypothesis('H2: eGFR GN') print(gt) gt$reject_a_hypothesis('H4: eGFR IgA') print(gt) gt$reject_a_hypothesis('H3: eGFR GN 10wk') print(gt) gt$reset() } ## Example 2 ## Example modified from vignettes in gMCPLite: ## Graphical testing for group sequential design if(interactive()){ ## initial alpha split to each of the hypotheses alpha <- c(.01, .01, .004, .0, .0005, .0005) ## transition matrix of the initial graph transition <- matrix(c( 0, 1, 0, 0, 0, 0, 0, 0, .5, .5, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, .5, .5, 0, 0, 0, 0, 0, 1, .5, .5, 0, 0, 0, 0 ), nrow = 6, byrow = TRUE) ## alpha spending functions per hypothesis asf <- c('asUser', 'asOF', 'asUser', 'asOF', 'asOF', 'asOF') ## planned maximum number of events per hypothesis max_info <- c(295, 800, 310, 750, 500, 1100) ## name of hypotheses hs <- c('H1: OS sub', 'H2: OS all', 'H3: PFS sub', 'H4: PFS all', 'H5: ORR sub', 'H6: ORR all') gt <- GraphicalTesting$new(alpha, transition, asf, max_info, hs) ## print initial graph gt ## nominal p-values at each stage ## Note: p-values with same order are calculated with the same locked data ## Note: alpha_spent is only specified for hypotheses using custom alpha ## spending function "asUser" stats <- data.frame( order = c(1:3, 1:3, 1:2, 1:2, 1, 1), hypotheses = c(rep('H1: OS sub', 3), rep('H2: OS all', 3), rep('H3: PFS sub', 2), rep('H4: PFS all', 2), 'H5: ORR sub', 'H6: ORR all'), p = c(.03, .0001, .000001, .2, .15, .1, .2, .001, .3, .2, .00001, .1), info = c(185, 245, 295, 529, 700, 800, 265, 310, 675, 750, 490, 990), is_final = c(F, F, T, F, F, T, F, T, F, T, T, T), max_info = c(rep(295, 3), rep(800, 3), rep(310, 2), rep(750, 2), 490, 990), alpha_spent = c(c(.1, .4, 1), rep(NA, 3), c(.3, 1), rep(NA, 2), NA, NA) ) ## test the p-values from the first analysis, plot the updated graph gt$test(stats %>% dplyr::filter(order==1)) ## test the p-values from the second analysis, plot the updated graph gt$test(stats %>% dplyr::filter(order==2)) ## test the p-values from the third analysis, plot the updated graph ## because no futher test would be done, displayed results are final gt$test(stats %>% dplyr::filter(order==3)) ## plot the final status of the graph print(gt, trajectory = FALSE) ## you can get final testing results as follow gt$get_current_testing_results() ## if you want to see step-by-step details print(gt$get_trajectory()) ## equivalently, you can call gt$test(stats) for only once to get same results. gt$reset() gt$test(stats) ## if you only want to get the final testing results gt$get_current_decision() }
Perform group sequential test for a single endpoint based on sequential one-sided p-values at each stages. Selected alpha spending functions, including user-defined functions, are supported. Boundaries are calculated with 'rpact'. At the final analysis, adjustment can be applied for over-running or under-running trial where observed final information is greater or lower than the planned maximum information. See Wassmer & Brannath, 2016, p78f. The test is based on p-values not z statistics because it is easier to not handling direction of alternative hypothesis in current implementation. In addition, only one-sided test is supported which should be sufficient for common use in clinical design.
new()
initialize a group sequential test. Now only support one-sided test based on p-values.
GroupSequentialTest$new(
alpha = 0.025,
alpha_spending = c("asP", "asOF", "asUser"),
planned_max_info,
name = "H0",
silent = TRUE
)alphafamilywise error rate
alpha_spendingalpha spending function. Use "asUser"
if custom alpha spending schedule is used.
planned_max_infointeger. Planned maximum number of patients for non-tte endpoints or number of events for tte endpoints
namecharacter. Name of the hypothesis, e.g. endpoint, subgroup, etc. Optional.
silentTRUE if muting all messages.
get_name()
get name of hypothesis
GroupSequentialTest$get_name()
get_alpha()
get overall alpha
GroupSequentialTest$get_alpha()
set_alpha_spending()
set alpha spending function. This is useful when set 'asUser' at the final stage to adjust for an under- or over-running trial.
GroupSequentialTest$set_alpha_spending(asf)
asfcharacter of alpha spending function.
get_alpha_spending()
return character of alpha spending function
GroupSequentialTest$get_alpha_spending()
get_max_info()
return planned maximum information
GroupSequentialTest$get_max_info()
set_max_info()
set planned maximum information. This is used at the final stage to adjust for an under- or over-running trial.
GroupSequentialTest$set_max_info(obs_max_info)
obs_max_infointeger. Maximum information, which could be observed number of patients or events at the final stage.
get_stage()
get current stage.
GroupSequentialTest$get_stage()
reset()
an object of class GroupSequentialTest is designed to be used
sequentially by calling GroupSequentialTest$test. When all
planned tests are performed, no further analysis could be done. In that
case keep calling GroupSequentialTest$test will trigger an error.
To reuse the object for a new set of staged p-values, call this function
to reset the status to stage 1. See examples. This implementation can
prevent the error that more than the planned number of stages are tested.
GroupSequentialTest$reset()
set_trajectory()
save testing result at current stage
GroupSequentialTest$set_trajectory(result, is_final = FALSE)
resulta data frame storing testing result at a stage.
is_finallogical. TRUE if final test for the hypothesis,
FALSE otherwise.
get_trajectory()
return testing trajectory until current stage. This function can be called at any stage. See examples.
GroupSequentialTest$get_trajectory()
get_stage_level()
compute boundaries given current (potentially updated) settings. It returns different values if settings are changed over time.
GroupSequentialTest$get_stage_level()
test_one()
test a hypothesis with the given p-value at current stage
GroupSequentialTest$test_one( p_value, is_final, observed_info, alpha_spent = NA_real_ )
p_valuenumeric. A p-value.
is_finallogical. TRUE if this test is carried out for
the final analysis.
observed_infointeger. Observed information at current stage. It
can be the number of samples (non-tte) or number of events (tte) at test.
If the current stage is final, observed_info will be used to update
planned_max_info, the alpha spending function (typeOfDesign
in rpact) will be updated to 'asUser', and the argument
userAlphaSpending will be used when calling
rpact::getDesignGroupSequential.
alpha_spentnumeric if alpha_spending = "asUser". It must
be between 0 and alpha, the overall alpha of the test.
NA_real_ for other alpha spending functions "asOF" and
"asP".
test()
Carry out test based on group sequential design. If p_values
is NULL, dummy values will be use and boundaries are calculated
for users to review.
GroupSequentialTest$test( observed_info, is_final, p_values = NULL, alpha_spent = NULL )
observed_infoa vector of integers, observed information at stages.
is_finallogical vector. TRUE if the test is for the final
analysis.
p_valuesa vector of p-values. If specified, its length should
equal to the length of observed_info.
alpha_spentaccumulative alpha spent at observed information.
It is a numeric vector of values between 0 and 1, and of length that
equals length(observed_info) if alpha-spending
function is "asUser". Otherwise NULL.
print()
generic function for print
GroupSequentialTest$print()
clone()
The objects of this class are cloneable with this method.
GroupSequentialTest$clone(deep = FALSE)
deepWhether to make a deep clone.
## Note: examples showed here replicate the results from ## https://www.rpact.org/vignettes/planning/rpact_boundary_update_example/ ## Example 1. Generate boundaries for a pre-fix group sequential design gst <- GroupSequentialTest$new( alpha = .025, alpha_spending = 'asOF', planned_max_info = 387) ## without giving p-values, boundaries are returned without actual testing gst$test(observed_info = c(205, 285, 393), is_final = c(FALSE, FALSE, TRUE)) gst ## Example 2. Calculate boundaries with observed information at stages ## No p-values are provided ## get an error without resetting an used object try( gst$test(observed_info = 500, is_final = FALSE) ) ## reset the object for re-use gst$reset() gst$test(observed_info = c(205, 285, 393), is_final = c(FALSE, FALSE, TRUE)) gst ## Example 3. Test stagewise p-values sequentially gst$reset() gst$test(observed_info = 205, is_final = FALSE, p_values = .09) gst$test(285, FALSE, .006) ## print testing trajectory by now gst gst$test(393, TRUE, .002) ## print all testing trajectory gst ## you can also test all stages at once ## the result is the same as calling test() for each of the stages gst$reset() gst$test(c(205, 285, 393), c(FALSE, FALSE, TRUE), c(.09, .006, .002)) gst ## Example 4. use user-define alpha spending gst <- GroupSequentialTest$new( alpha = .025, alpha_spending = 'asUser', planned_max_info = 387) gst$test( observed_info = c(205, 285, 393), is_final = c(FALSE, FALSE, TRUE), alpha_spent = c(.005, .0125, .025)) gst## Note: examples showed here replicate the results from ## https://www.rpact.org/vignettes/planning/rpact_boundary_update_example/ ## Example 1. Generate boundaries for a pre-fix group sequential design gst <- GroupSequentialTest$new( alpha = .025, alpha_spending = 'asOF', planned_max_info = 387) ## without giving p-values, boundaries are returned without actual testing gst$test(observed_info = c(205, 285, 393), is_final = c(FALSE, FALSE, TRUE)) gst ## Example 2. Calculate boundaries with observed information at stages ## No p-values are provided ## get an error without resetting an used object try( gst$test(observed_info = 500, is_final = FALSE) ) ## reset the object for re-use gst$reset() gst$test(observed_info = c(205, 285, 393), is_final = c(FALSE, FALSE, TRUE)) gst ## Example 3. Test stagewise p-values sequentially gst$reset() gst$test(observed_info = 205, is_final = FALSE, p_values = .09) gst$test(285, FALSE, .006) ## print testing trajectory by now gst gst$test(393, TRUE, .002) ## print all testing trajectory gst ## you can also test all stages at once ## the result is the same as calling test() for each of the stages gst$reset() gst$test(c(205, 285, 393), c(FALSE, FALSE, TRUE), c(.09, .006, .002)) gst ## Example 4. use user-define alpha spending gst <- GroupSequentialTest$new( alpha = .025, alpha_spending = 'asUser', planned_max_info = 387) gst$test( observed_info = c(205, 285, 393), is_final = c(FALSE, FALSE, TRUE), alpha_spent = c(.005, .0125, .025)) gst
Define a listener. This is a user-friendly wrapper for
the class constructor Listener$new(). Users who are not familiar with
the concept of classes may consider using this wrapper directly.
Listener is an important concept of TrialSimulator. Used with a
trial object in a controller, a listener can monitor a running trial to
execute user-defined actions when it determine condition of triggering a
milestone is met. This mechanism allows the package users to focus on
the development of action functions in a simulation.
listener(silent = FALSE)listener(silent = FALSE)
silent |
logical. |
listener <- listener()listener <- listener()
Create a class of listener. A listener monitors the trial while checking condition of pre-defined milestones. Actions are triggered and executed automatically.
Public methods in this R6 class are used in developing this package. Thus, we have to export the whole R6 class which exposures all public methods. However, only the public methods in the list below are useful to end users.
$add_milestones()
new()
initialize a listener
Listeners$new(silent = FALSE)
silentlogical. TRUE to mute messages.
add_milestones()
register milestones with listener. Order in ... matter
as they are scanned and triggered in that order. It is users'
responsibility to use reasonable order when calling this function,
otherwise, the result of Listeners$monitor() can be problematic.
Listeners$add_milestones(...)
...one or more objects returned from milestone().
listener <- listener()
interim <- milestone(name = 'interim',
when = eventNumber('endpoint', n = 100)
)
final <- milestone(name = 'final',
when = calendarTime(time = 24)
)
listener$add_milestones(interim, final)
get_milestones()
return registered milestones
Listeners$get_milestones(milestone_name = NULL)
milestone_namereturn Milestone object with given name(s).
If NULL, all registered milestones are returned.
get_milestone_names()
return names of registered milestones
Listeners$get_milestone_names()
monitor()
scan, check, and trigger registered milestones.
Milestones are triggered in the order when calling
Listener$add_milestones.
Listeners$monitor(trial, dry_run)
triala Trial object.
dry_runlogical. See Controller::run for more information.
mute()
mute all messages (not including warnings)
Listeners$mute(silent)
silentlogical.
reset()
reset all milestones registered to the listener. Usually, this is called before a controller can run additional replicates of simulation.
Listeners$reset()
clone()
The objects of this class are cloneable with this method.
Listeners$clone(deep = FALSE)
deepWhether to make a deep clone.
## ## ------------------------------------------------ ## Method `Listeners$add_milestones` ## ------------------------------------------------ listener <- listener() interim <- milestone(name = 'interim', when = eventNumber('endpoint', n = 100) ) final <- milestone(name = 'final', when = calendarTime(time = 24) ) listener$add_milestones(interim, final)## ## ------------------------------------------------ ## Method `Listeners$add_milestones` ## ------------------------------------------------ listener <- listener() interim <- milestone(name = 'interim', when = eventNumber('endpoint', n = 100) ) final <- milestone(name = 'final', when = calendarTime(time = 24) ) listener$add_milestones(interim, final)
Define a milestone of a trial. This is a user-friendly wrapper for
the class constructor Milestones$new(). Users who are not familiar with
the concept of classes may consider using this wrapper directly.
A milestone means the time point to take an action, e.g., carrying out (futility, interim, final) analysis for adding/removing arms, or stopping a trial early. It can also be any more general time point where trial data is used in decision making or adaptation. For example, one can define a milestone for changing randomization scheme, sample size re-assessment, trial duration extension etc.
Refer to the
vignette
to learn how to define milestones when performing simulation using
TrialSimulator.
milestone(name, when, action = doNothing, ...)milestone(name, when, action = doNothing, ...)
name |
character. Name of milestone. |
when |
condition to check if this milestone should be
triggered. It taks value returned from functions |
action |
function to execute when the milestone triggers.
If no action to be executed but simply need to record triggering time and
number of events/non-missing observations of endpoints at
a milestone, |
... |
(optional) arguments of |
## See vignette('conditionSystem')## See vignette('conditionSystem')
Create a class of milestone. A milestone means the time point to take an action, e.g., carrying out (futility, interim, final) analysis for adding/removing arms, or stopping a trial early. It can also be any more general time point where trial data is used in decision making or adaptation. For example, one can define a milestone for changing randomization scheme, sample size re-assessment, trial duration extension etc.
Public methods in this R6 class are used in developing
this package. Thus, we have to export the whole R6 class which exposures all
public methods. However, none of the public methods on this page is
useful to end users. Instead, refer to the
vignette
to learn how to define milestones when performing simulation using
TrialSimulator.
new()
initialize milestone
Milestones$new(name, type = name, trigger_condition, action = doNothing, ...)
namecharacter. Name of milestone.
typecharacter vector. Milestone type(s) (futility, interim, final), a milestone can be of multiple types. This is for information purpose so can be any string.
trigger_conditionfunction to check if this milestone should
trigger. See vignette Condition System for Triggering Milestones in a Trial.
actionfunction to execute when the milestone triggers.
...(optional) arguments of action.
get_name()
return name of milestone
Milestones$get_name()
get_type()
return type(s) of milestone
Milestones$get_type()
get_trigger_condition()
return trigger_condition function
Milestones$get_trigger_condition()
get_action()
return action function
Milestones$get_action()
set_dry_run()
set if dry run should be carried out for the milestone. For more details,
refer to Controller::run.
Milestones$set_dry_run(dry_run)
dry_runlogical.
execute_action()
execute action function
Milestones$execute_action(trial)
triala Trial object.
get_trigger_status()
return trigger status
Milestones$get_trigger_status()
reset()
reset an milestone so that it can be triggered again. Usually, this is called before the controller of a trial can run additional replicates of simulation.
Milestones$reset()
trigger_milestone()
trigger an milestone (always TRUE) and execute action accordingly. It calls Trial$get_data_lock_time() to lock data based on conditions implemented in Milestones$trigger_condition. If time that meets the condition cannot be found, Trial$get_data_lock_time() will throw an error and stop the program. This means that user needs to adjust their trigger_condition (e.g., target number of events (target_n_events) is impossible to reach).
Milestones$trigger_milestone(trial)
triala Trial object.
mute()
mute all messages (not including warnings)
Milestones$mute(silent)
silentlogical.
clone()
The objects of this class are cloneable with this method.
Milestones$clone(deep = FALSE)
deepWhether to make a deep clone.
This function can be used as generator to define endpoint. Implementation is based on this algorithm. This distribution can be used to simulate delayed treatment effect.
PiecewiseConstantExponentialRNG(n, risk, endpoint_name)PiecewiseConstantExponentialRNG(n, risk, endpoint_name)
n |
integer. Number of random numbers |
risk |
a data frame of columns
|
endpoint_name |
character. Name of endpoint. This should be the same as
the |
a data frame of n rows and two columns
<endpoint_name>name of endpoint specified by users in
endpoint_name.
event indicator with 0/1 as censoring and
event, respectively. Note that due to the nature of the algorithm to
generate data from this distribution, it is possible to have the endpoint
being censoring at the last end_time unless it is set to Inf.
# example code # In this example, absolute risk in [0, 1) and [26, 52] are 0.0181 and # 0.0027, respectively. risk <- data.frame( end_time = c(1, 4.33, 26.0, 52.0), piecewise_risk = c(1, 1.01, 0.381, 0.150) * exp(-4.01) ) PiecewiseConstantExponentialRNG(10, risk, 'PFS')# example code # In this example, absolute risk in [0, 1) and [26, 52] are 0.0181 and # 0.0027, respectively. risk <- data.frame( end_time = c(1, 4.33, 26.0, 52.0), piecewise_risk = c(1, 1.01, 0.381, 0.150) * exp(-4.01) ) PiecewiseConstantExponentialRNG(10, risk, 'PFS')
Plot Triggering Time of Milestones in Simulated Trials
## S3 method for class 'milestone_time_summary' plot(x, ...)## S3 method for class 'milestone_time_summary' plot(x, ...)
x |
an object returned by |
... |
currently not supported. |
Plot result of three-state illness-death model
## S3 method for class 'three_state_model' plot(x, ...)## S3 method for class 'three_state_model' plot(x, ...)
x |
an object returned by |
... |
currently not supported. |
To generate endpoint that follows a piecewise exponential distribution and
is correlated with other endpoints, the copula method is commonly used. The
quantile function needs to be specified (e.g., in the simdata package).
If a piecewise exponential distributed endpoint is independent to other
endpoints, one can simply use
TrialSimulator::PiecewiseConstantExponentialRNG()
to specify the generator argument in endpoint().
There are many R packages implementing the quantile function of the
piecewise exponential distributed random variable. Why do I implement it
again? The reason is that this function is extremely important for simulating
time-to-event endpoint in clinical trial simulation, thus the speed matters.
qPiecewiseExponential() is implemented purely in R for
code transparency, and is much faster than other packages.
qPiecewiseExponential(p, times, piecewise_risk)qPiecewiseExponential(p, times, piecewise_risk)
p |
numeric. A vector of probabilities. |
times |
numeric. A vector of time points where risk (event
rates) change. 0 shouldn't be in |
piecewise_risk |
numeric. A vector of constant risk (event rates) in
a time window. |
A vector of quantiles.
## This code snippet can take > 10s to execute if(interactive()){ library(TrialSimulator) run <- TRUE if(!requireNamespace("rpact", quietly = TRUE)){ run <- FALSE message("Please install 'rpact' to run this example.") } if(!requireNamespace("PWEXP", quietly = TRUE)){ run <- FALSE message("Please install 'PWEXP' to run this example.") } if(!requireNamespace("PWEALL", quietly = TRUE)){ run <- FALSE message("Please install 'PWEALL' to run this example.") } if(run){ x <- solvePiecewiseConstantExponentialDistribution( surv_prob = c(.9, .75, .64, .42, .28), times = c(.4, 1.2, 4, 5.5, 9) ) p <- stats::runif(1e5) ## fast, and only five lines of R codes message('TrialSimulator::qPiecewiseExponential(): ') system.time( a <- qPiecewiseExponential( p, times = x$end_time, piecewise_risk = c(x$piecewise_risk, .1) ) ) |> print() ## > 10s message('rpact::getPiecewiseExponentialQuantile(): ') system.time( b <- rpact::getPiecewiseExponentialQuantile( p, piecewiseSurvivalTime = c(0, x$end_time), piecewiseLambda=c(x$piecewise_risk, .1) ) ) |> print() ## equally fast, but implemented in Fortran message('PWEALL::qpwe(): ') system.time( c <- PWEALL::qpwe(p, rate = c(x$piecewise_risk, .1), tchange = c(0, x$end_time))$q ) |> print() ## equally fast, long codes in R (maybe more versatile?) message('PWEXP::qpwexp(): ') system.time( d <- PWEXP::qpwexp(p, rate = c(x$piecewise_risk, .1), breakpoint = x$end_time) ) |> print() message('a == b: ') all.equal(a, b) |> print() message('a == c: ') all.equal(a, c) |> print() message('a == d: ') all.equal(a, d) |> print() } }## This code snippet can take > 10s to execute if(interactive()){ library(TrialSimulator) run <- TRUE if(!requireNamespace("rpact", quietly = TRUE)){ run <- FALSE message("Please install 'rpact' to run this example.") } if(!requireNamespace("PWEXP", quietly = TRUE)){ run <- FALSE message("Please install 'PWEXP' to run this example.") } if(!requireNamespace("PWEALL", quietly = TRUE)){ run <- FALSE message("Please install 'PWEALL' to run this example.") } if(run){ x <- solvePiecewiseConstantExponentialDistribution( surv_prob = c(.9, .75, .64, .42, .28), times = c(.4, 1.2, 4, 5.5, 9) ) p <- stats::runif(1e5) ## fast, and only five lines of R codes message('TrialSimulator::qPiecewiseExponential(): ') system.time( a <- qPiecewiseExponential( p, times = x$end_time, piecewise_risk = c(x$piecewise_risk, .1) ) ) |> print() ## > 10s message('rpact::getPiecewiseExponentialQuantile(): ') system.time( b <- rpact::getPiecewiseExponentialQuantile( p, piecewiseSurvivalTime = c(0, x$end_time), piecewiseLambda=c(x$piecewise_risk, .1) ) ) |> print() ## equally fast, but implemented in Fortran message('PWEALL::qpwe(): ') system.time( c <- PWEALL::qpwe(p, rate = c(x$piecewise_risk, .1), tchange = c(0, x$end_time))$q ) |> print() ## equally fast, long codes in R (maybe more versatile?) message('PWEXP::qpwexp(): ') system.time( d <- PWEXP::qpwexp(p, rate = c(x$piecewise_risk, .1), breakpoint = x$end_time) ) |> print() message('a == b: ') all.equal(a, b) |> print() message('a == c: ') all.equal(a, c) |> print() message('a == d: ') all.equal(a, d) |> print() } }
A random number generator returning only a constant. This can be used to
set dropout time. Currently it is the default value of dropout time, with
value = Inf.
This function can also be used as a generator of endpoint() if a
constant endpoint is needed.
rconst(n, value)rconst(n, value)
n |
integer. Number of observations. |
value |
scalar. Value of constant observations. |
Define a regimen of a trial. This is a user-friendly wrapper for
the class constructor Regimens$new(). Users who are not familiar with
the concept of classes may consider using this wrapper directly.
A regimen defines the rules to select patients who switch treatments, to determine the time of switching, and to update patients' endpoint data.
regimen(what, when, how, ...)regimen(what, when, how, ...)
what |
a function determining whether patients' data would be
updated due to switching treatment. It takes |
when |
a function determining the time at which a patient switches
to another treatment regimen, measured from the time of enrollment.
It takes |
how |
a function updating patients' data after treatment switching.
Only modified columns and |
... |
(optional) named arguments to be passed to one or more of
|
Create a class of regimen. A regimen defines the rules to select treatments for patients switch, to determine the time of switching, and to update patients' endpoint data.
new()
initialize regimen
Regimens$new(what, when, how, ..., earliest_crossover_calendar_time = 0)
whata function determining whether patients' data would be
updated due to switching treatment. It takes patient_data,
a data frame as argument, and returns a data frame of two columns
patient_id and new_treatment, with one row per switching
patient. The number of rows in the returned data frame may be smaller
than the number of patients in the input data frame; patients that are
left out are simply not switched.
Note that the returned object will be passed into function 'how()', which
is also provide by users. This argument can also be a
list of functions that will be executed sequentially. No default value.
whena function determining the time at which a patient switches
to another treatment regimen, measured from the time of enrollment.
It takes patient_data, a data frame as
argument, and returns a data frame of two columns patient_id and
switch_time (from enroll_time). The number of rows in the
returned data frame must equal the number of rows in patient_data,
i.e., a switching time must be specified for every patient (missing
values are not allowed).
Note that the returned object will be passed into function 'how()', which
is also provided by users. This argument can also be a
list of functions that will be executed sequentially. No default value.
howa function updating patients' data after treatment switching.
Only modified columns and patient_id are returned. For a cell that
should not change, return its original value. Only post-switch
outcomes may be changed:
returning a value that differs from the original for an endpoint whose
readout/event is at or before switch_time raises an error. This
argument can also be a list of functions that will be executed
sequentially. No default value.
...(optional) named arguments routed to one or more of
what, when, and how.
earliest_crossover_calendar_timenumeric. The earliest calendar
time at which the triplet(s) may take effect. 0 (default) is the
classic enrollment-time regimen, applied from the first enrollment. A
positive value marks the triplet(s) as a milestone-triggered crossover
(eligibility filtering, switch-time validation and the post-switch data
mask). This is set internally by trial$crossover(); it is not a
user argument of regimen().
get_number_treatment_allocator()
return number of treatment allocators for regimen
Regimens$get_number_treatment_allocator()
get_treatment_allocator()
return user-defined new treatment for a patient
Regimens$get_treatment_allocator(index = NULL)
indexinteger. Index of allocator. Return all allocators if NULL.
get_number_time_selector()
return number of time selector for regimen
Regimens$get_number_time_selector()
get_time_selector()
return user-defined time selector
Regimens$get_time_selector(index = NULL)
indexinteger. Index of selector. Return all selectors if NULL.
get_number_data_modifier()
return number of data modifier for regimen
Regimens$get_number_data_modifier()
get_data_modifier()
return user-defined endpoint data modifier
Regimens$get_data_modifier(index = NULL)
indexinteger. Index of selector. Return all modifiers if NULL.
get_treatment_allocator_args()
return pre-bound arguments for the i-th treatment allocator
Regimens$get_treatment_allocator_args(index)
indexinteger.
get_time_selector_args()
return pre-bound arguments for the i-th time selector
Regimens$get_time_selector_args(index)
indexinteger.
get_data_modifier_args()
return pre-bound arguments for the i-th data modifier
Regimens$get_data_modifier_args(index)
indexinteger.
get_earliest_crossover_calendar_time()
return the earliest crossover calendar time of triplet(s)
Regimens$get_earliest_crossover_calendar_time(index = NULL)
indexinteger. Index of triplet. Return all if NULL.
append_triplet()
append one more triplet to the regimen. Used by milestone-triggered
crossover to stack a new what/when/how (with its own
earliest_crossover_calendar_time) onto an existing regimen without
overwriting earlier triplets. Triplets are executed in append order.
Regimens$append_triplet( what, when, how, ..., earliest_crossover_calendar_time = 0 )
what, when, howsee regimen().
...(optional) named arguments routed to what, when,
and/or how.
earliest_crossover_calendar_timenumeric. Earliest calendar time for the appended triplet. A positive value marks it as a crossover.
clone()
The objects of this class are cloneable with this method.
Regimens$clone(deep = FALSE)
deepWhether to make a deep clone.
remove arms from a trial. The application of this function includes, but is not limited to, dose selection, enrichment analysis (select sub-population).
Note that this function should only be called within action functions of
milestones. It is users' responsibility to ensure that and
TrialSimulator has no way to track it. In addition, data of the
removed arms are censored or truncated by the time of arm removal.
This is a user-friendly wrapper of the member function of trial, i.e.,
Trials$remove_arms(), which is used in vignettes. Users who are not
familiar with the concept of classes may consider using this wrapper
directly.
remove_arms(trial, arms_name)remove_arms(trial, arms_name)
trial |
a trial object returned by |
arms_name |
character vector. Name of arms to be removed. |
resize a trial with a greater sample size. This function is used to update
the maximum sample size adaptively after sample size reassessment. Note that
this function should be called within action functions. It is users'
responsibility to ensure it and TrialSimulator has no way to track this.
This is a user-friendly wrapper of the member function of trial, i.e.,
Trials$resize(), which is used in vignettes. Users who are not
familiar with the concept of classes may consider using this wrapper
directly.
resize(trial, n_patients)resize(trial, n_patients)
trial |
a trial object returned by |
n_patients |
integer. Number of maximum sample size of a trial. |
set trial duration in an adaptive designed trial. New duration must be longer than the old one. All patients enrolled before resetting the duration are truncated (non-TTE endpoints) or censored (TTE endpoints) at the original duration. This helps maintain proper type I error or family-wise error rate and control multiplicity when conducting testing statistics. For more details of why this is necessary, please refer to Jorgens et al. 2019.
Note that this function should only be called within action functions of
milestones. It is users' responsibility to ensure that and
TrialSimulator has no way to track it.
This is a user-friendly wrapper of the member function of trial, i.e.,
Trials$set_duration(), which is used in vignettes. Users who are not
familiar with the concept of classes may consider using this wrapper
directly.
set_duration(trial, duration)set_duration(trial, duration)
trial |
a trial object returned by |
duration |
new duration of a trial. It must be greater than the current duration. |
This is a helper function to explore parameters for endpoint generator, likely in an enrichment design.
Assume that the overall population in an arm is a mixture of two exponential
distributions with medians median1 () and
median2 (). Given the proportion of the first component
() and the overall median , we have
This function computes or given and .
These parameters can be used in custom random number generator to define
exponential distributed endpoints.
Note that the math formula above may not be displayed correctly on a
html page. You can read it with better format by running
?solveMixtureExponentialDistribution.
solveMixtureExponentialDistribution( weight1, median1, median2 = NULL, overall_median = NULL )solveMixtureExponentialDistribution( weight1, median1, median2 = NULL, overall_median = NULL )
weight1 |
numeric. The proportion of the first component. |
median1 |
numeric. Median of the first component. |
median2 |
numeric. Median of the second component. If |
overall_median |
numeric. Median of the overall population. If
|
a named vector of median2 or overall_median.
library(dplyr) median2 <- solveMixtureExponentialDistribution( weight1 = .3, median1 = 10, overall_median = 8) median2 n <- 1e6 ifelse( runif(n) < .3, rexp(n, rate=log(2)/10), rexp(n, rate=log(2)/median2)) %>% median() ## should be close to 8 overall_median <- solveMixtureExponentialDistribution( weight1 = .4, median1 = 12, median2 = 4) overall_median ifelse( runif(n) < .4, rexp(n, rate=log(2)/12), rexp(n, rate=log(2)/4)) %>% median() ## should be close to overall_medianlibrary(dplyr) median2 <- solveMixtureExponentialDistribution( weight1 = .3, median1 = 10, overall_median = 8) median2 n <- 1e6 ifelse( runif(n) < .3, rexp(n, rate=log(2)/10), rexp(n, rate=log(2)/median2)) %>% median() ## should be close to 8 overall_median <- solveMixtureExponentialDistribution( weight1 = .4, median1 = 12, median2 = 4) overall_median ifelse( runif(n) < .4, rexp(n, rate=log(2)/12), rexp(n, rate=log(2)/4)) %>% median() ## should be close to overall_median
This function computes the rate parameters lambda in each of the time
windows. lambda are natural parameters of piecewise exponential
distribution, but in practice, users may define the distribution by
specifying the survival probabilities at time points where event rates
change.
This function returns a data frame, which can be used as input of the
argument risk of the data generator PiecewiseConstantExponentialRNG.
solvePiecewiseConstantExponentialDistribution(surv_prob, times)solvePiecewiseConstantExponentialDistribution(surv_prob, times)
surv_prob |
numeric. A vector of survival probabilities at |
times |
numeric. A vector of time points where event rates change.
|
a data frame of two columns
end_timeEnd time for a constant event rate. The start time of the first time window is 0.
A constant event rate in the time window ending with
end_time on the same row.
solvePiecewiseConstantExponentialDistribution( surv_prob = c(.9, .75, .64, .42, .28), times = c(.4, 1.2, 4, 5.5, 9) )solvePiecewiseConstantExponentialDistribution( surv_prob = c(.9, .75, .64, .42, .28), times = c(.4, 1.2, 4, 5.5, 9) )
The illness-death model consists of three states, initial, progression,
and death. It can be used to model the progression-free survival (PFS)
and overall survival (OS) in clinical trial simulation. It models the
correlation between PFS and OS without assumptions on latent status and copula.
Also, it does not assume PFS and OS satisfy the proportional hazard assumption
simultaneously. The three-state illness-death model ensures a nice property that
PFS <= OS with probability one. However, it requires three hazard parameters
under the homogeneous Markov assumption. In practice, hazard parameters are
hard to specify intuitively especially when no trial data is available at
the planning stage.
This function reparametrizes the illness-death model in term of three parameters,
i.e. median of PFS, median of OS, and correlation between PFS and OS. The
output of this function, which consists of the three hazard parameters, can
be used to generate PFS and OS with desired property. It can be used with
the built-in data generator CorrelatedPfsAndOs3() when defining
endpoints in TrialSimulator.
For more information, refer to this vignette.
solveThreeStateModel( median_pfs, median_os, corr, h12 = seq(0.05, 0.2, length.out = 50) )solveThreeStateModel( median_pfs, median_os, corr, h12 = seq(0.05, 0.2, length.out = 50) )
median_pfs |
numeric. Median of PFS. |
median_os |
numeric. Median of OS. |
corr |
numeric vector. Pearson correlation coefficients between PFS and OS. |
h12 |
numeric vector. A set of hazard from progression to
death that may induce the target correlation |
a data frame with columns:
corrtarget Peason's correlation coefficients.
h01hazard from stable to progression.
h02hazard from stable to death.
h12hazard from progression to death.
errorabsolute error between target correlation and correlation
derived from h01, h02, and h12.
dat <- CorrelatedPfsAndOs3(1e6, h01 = .1, h02 = .05, h12 = .12) cor(dat$pfs, dat$os) ## 0.65 median(dat$pfs) ## 4.62 median(dat$os) ## 9.61 ## find h01, h02, h12 that can match to median_pfs, median_os and corr ## should be close to h01 = 0.10, h02 = 0.05, h12 = 0.12 when corr = 0.65 ret <- solveThreeStateModel(median_pfs = 4.6, median_os = 9.6, corr = seq(.5, .7, length.out=5)) retdat <- CorrelatedPfsAndOs3(1e6, h01 = .1, h02 = .05, h12 = .12) cor(dat$pfs, dat$os) ## 0.65 median(dat$pfs) ## 4.62 median(dat$os) ## 9.61 ## find h01, h02, h12 that can match to median_pfs, median_os and corr ## should be close to h01 = 0.10, h02 = 0.05, h12 = 0.12 when corr = 0.65 ret <- solveThreeStateModel(median_pfs = 4.6, median_os = 9.6, corr = seq(.5, .7, length.out=5)) ret
It assumes a uniform enrollment with constant rate in each of the time
windows. This function can be used as the enroller when calling
trial() to define a trial.
StaggeredRecruiter(n, accrual_rate)StaggeredRecruiter(n, accrual_rate)
n |
integer. Number of random numbers. |
accrual_rate |
a data frame of columns
|
accrual_rate <- data.frame( end_time = c(12, 13:17, Inf), piecewise_rate = c(15, 15 + 6 * (1:5), 45) ) StaggeredRecruiter(30, accrual_rate) accrual_rate <- data.frame( end_time = c(3, 4, 5, 8, Inf), piecewise_rate = c(1, 2, 2, 3, 4) ) StaggeredRecruiter(30, accrual_rate)accrual_rate <- data.frame( end_time = c(12, 13:17, Inf), piecewise_rate = c(15, 15 + 6 * (1:5), 45) ) StaggeredRecruiter(30, accrual_rate) accrual_rate <- data.frame( end_time = c(3, 4, 5, 8, Inf), piecewise_rate = c(1, 2, 2, 3, 4) ) StaggeredRecruiter(30, accrual_rate)
A minimum alternative to summarytools::dfSummary to avoid package
dependency. This function is used to generate summary reports of endpoints
and arms. No meant to be used by end users. However, users may
find it helpful in their own applications if the interface is okay with them.
summarizeDataFrame( data, exclude_vars = NULL, tte_vars = NULL, event_vars = NULL, categorical_vars = NULL, title = "Summary", sub_title = "" )summarizeDataFrame( data, exclude_vars = NULL, tte_vars = NULL, event_vars = NULL, categorical_vars = NULL, title = "Summary", sub_title = "" )
data |
a data frame. |
exclude_vars |
columns to be excluded from summary. |
tte_vars |
character. Vector of time-to-event variables. |
event_vars |
character. Vector of event indicators. Every time-to-event variable should be corresponding to an event indicator. |
categorical_vars |
character. Vector of categorical variables. This can be used to specify variables with limited distinct values as categorical variables in summary. |
title |
character. Title of the summary report. |
sub_title |
character. Sub-title. |
a data frame of summary
set.seed(123) n <- 1000 data <- data.frame( age = rnorm(n, 65, 10), gender = sample(c('M', 'F', NA), n, replace = TRUE, prob = c(.4, .4, .2)), time_to_death = rexp(n, .01), death = rbinom(n, 1, .6), type = sample(LETTERS[1:8], n, replace = TRUE) ) summarizeDataFrame(data, tte_vars = 'time_to_death', event_vars = 'death')set.seed(123) n <- 1000 data <- data.frame( age = rnorm(n, 65, 10), gender = sample(c('M', 'F', NA), n, replace = TRUE, prob = c(.4, .4, .2)), time_to_death = rexp(n, .01), death = rbinom(n, 1, .6), type = sample(LETTERS[1:8], n, replace = TRUE) ) summarizeDataFrame(data, tte_vars = 'time_to_death', event_vars = 'death')
Summary of Milestone Time from Simulated Trials
summarizeMilestoneTime(output)summarizeMilestoneTime(output)
output |
a data frame.
It assumes that triggering time of milestones
are store in columns |
A data frame of class milestone_time_summary. It comes with a
plot method for visualization.
# a minimum, meaningful, and executable example, # where a randomized trial with two arms is simulated and analyzed. control <- arm(name = 'control arm') active <- arm(name = 'active arm') pfs_in_control <- endpoint(name = 'PFS', type = 'tte', generator = rexp, rate = log(2) / 5) control$add_endpoints(pfs_in_control) pfs_in_active <- endpoint(name = 'PFS', type = 'tte', generator = rexp, rate = log(2) / 6) active$add_endpoints(pfs_in_active) accrual_rate <- data.frame(end_time = c(10, Inf), piecewise_rate = c(30, 50)) trial <- trial(name = 'trial', n_patients = 1000, duration = 40, enroller = StaggeredRecruiter, accrual_rate = accrual_rate, dropout = rweibull, shape = 2, scale = 38, silent = TRUE) trial$add_arms(sample_ratio = c(1, 1), control, active) action_at_final <- function(trial){ locked_data <- trial$get_locked_data('final analysis') fitLogrank(Surv(PFS, PFS_event) ~ arm, placebo = 'control arm', data = locked_data, alternative = 'less') invisible(NULL) } final <- milestone(name = 'final analysis', action = action_at_final, when = eventNumber(endpoint = 'PFS', n = 300)) listener <- listener(silent = TRUE) listener$add_milestones(final) controller <- controller(trial, listener) controller$run(n = 10, plot_event = FALSE, silent = TRUE) output <- controller$get_output() time <- summarizeMilestoneTime(output) time plot(time)# a minimum, meaningful, and executable example, # where a randomized trial with two arms is simulated and analyzed. control <- arm(name = 'control arm') active <- arm(name = 'active arm') pfs_in_control <- endpoint(name = 'PFS', type = 'tte', generator = rexp, rate = log(2) / 5) control$add_endpoints(pfs_in_control) pfs_in_active <- endpoint(name = 'PFS', type = 'tte', generator = rexp, rate = log(2) / 6) active$add_endpoints(pfs_in_active) accrual_rate <- data.frame(end_time = c(10, Inf), piecewise_rate = c(30, 50)) trial <- trial(name = 'trial', n_patients = 1000, duration = 40, enroller = StaggeredRecruiter, accrual_rate = accrual_rate, dropout = rweibull, shape = 2, scale = 38, silent = TRUE) trial$add_arms(sample_ratio = c(1, 1), control, active) action_at_final <- function(trial){ locked_data <- trial$get_locked_data('final analysis') fitLogrank(Surv(PFS, PFS_event) ~ arm, placebo = 'control arm', data = locked_data, alternative = 'less') invisible(NULL) } final <- milestone(name = 'final analysis', action = action_at_final, when = eventNumber(endpoint = 'PFS', n = 300)) listener <- listener(silent = TRUE) listener$add_milestones(final) controller <- controller(trial, listener) controller$run(n = 10, plot_event = FALSE, silent = TRUE) output <- controller$get_output() time <- summarizeMilestoneTime(output) time plot(time)
Define a trial. This is a user-friendly wrapper for
the class constructor Trial$new(). Users who are not familiar with
the concept of classes may consider using this wrapper directly.
Trial's name, planned size/duration, enrollment plan, dropout mechanism and seeding are specified in this function. Note that many of these parameters can be altered adaptively during a trial.
Note that it is users' responsibility to assure that the units of dropout time, trial duration, and readout of non-tte endpoints are consistent.
trial( name, n_patients, duration, description = name, seed = NULL, enroller, dropout = NULL, stratification_factors = NULL, silent = FALSE, ... )trial( name, n_patients, duration, description = name, seed = NULL, enroller, dropout = NULL, stratification_factors = NULL, silent = FALSE, ... )
name |
character. Name of trial. Usually, hmm..., useless. |
n_patients |
integer. Maximum (and initial) number of patients could be enrolled when planning the trial. It can be altered adaptively during a trial. |
duration |
Numeric. Trial duration. It can be altered adaptively during a trial. |
description |
character. Optional for description of the trial. By
default it is set to be trial's |
seed |
random seed. If |
enroller |
a function returning a vector enrollment time for
patients. Its first argument |
dropout |
a function returning a vector of dropout time for patients.
It can be any random number generator with first argument |
stratification_factors |
character. Names of baseline characteristics
to define stratums in stratified permuted block randomization.
Stratification factors must be defined in |
silent |
logical. |
... |
(optional) arguments of |
risk1 <- data.frame( end_time = c(1, 10, 26.0, 52.0), piecewise_risk = c(1, 1.01, 0.381, 0.150) * exp(-3.01) ) pfs1 <- endpoint(name = 'pfs', type='tte', generator = PiecewiseConstantExponentialRNG, risk = risk1, endpoint_name = 'pfs') orr1 <- endpoint( name = 'orr', type = 'non-tte', readout = c(orr=1), generator = rbinom, size = 1, prob = .4) placebo <- arm(name = 'pbo') placebo$add_endpoints(pfs1, orr1) risk2 <- risk1 risk2$hazard_ratio <- .8 pfs2 <- endpoint(name = 'pfs', type='tte', generator = PiecewiseConstantExponentialRNG, risk = risk2, endpoint_name = 'pfs') orr2 <- endpoint( name = 'orr', type = 'non-tte', generator = rbinom, readout = c(orr=3), size = 1, prob = .6) active <- arm(name = 'ac') active$add_endpoints(pfs2, orr2) ## Plan a trial, Trial-3415, of up to 100 patients. ## Enrollment time follows an exponential distribution, with median 5 trial <- trial( name = 'Trial-3415', n_patients = 100, seed = 31415926, duration = 100, enroller = rexp, rate = log(2) / 5) trial trial$add_arms(sample_ratio = c(1, 2), placebo, active) ## updated information after arms are registered trialrisk1 <- data.frame( end_time = c(1, 10, 26.0, 52.0), piecewise_risk = c(1, 1.01, 0.381, 0.150) * exp(-3.01) ) pfs1 <- endpoint(name = 'pfs', type='tte', generator = PiecewiseConstantExponentialRNG, risk = risk1, endpoint_name = 'pfs') orr1 <- endpoint( name = 'orr', type = 'non-tte', readout = c(orr=1), generator = rbinom, size = 1, prob = .4) placebo <- arm(name = 'pbo') placebo$add_endpoints(pfs1, orr1) risk2 <- risk1 risk2$hazard_ratio <- .8 pfs2 <- endpoint(name = 'pfs', type='tte', generator = PiecewiseConstantExponentialRNG, risk = risk2, endpoint_name = 'pfs') orr2 <- endpoint( name = 'orr', type = 'non-tte', generator = rbinom, readout = c(orr=3), size = 1, prob = .6) active <- arm(name = 'ac') active$add_endpoints(pfs2, orr2) ## Plan a trial, Trial-3415, of up to 100 patients. ## Enrollment time follows an exponential distribution, with median 5 trial <- trial( name = 'Trial-3415', n_patients = 100, seed = 31415926, duration = 100, enroller = rexp, rate = log(2) / 5) trial trial$add_arms(sample_ratio = c(1, 2), placebo, active) ## updated information after arms are registered trial
Create a class of trial.
Public methods in this R6 class are used in developing this package. Thus, we have to export the whole R6 class which exposures all public methods. However, only the public methods in the list below are useful to end users.
$set_duration() set duration of a trial. This function can be
used to extend duration under adaptive designs.
$resize() set maximum sample size of a trial. This function can
be used to increase sample size under adaptive designs (e.g., sample size
reassessment).
$remove_arms() drop arms from a trial. This function can be
used in adaptive designs, e.g., dose selection, enrichment design, etc.
$update_sample_ratio() change sample ratio of arm. This function
can be used under adaptive designs, e.g., response-adaptive design, etc.
$update_generator() change endpoint generator of arm. This
function can be used in enrichment design.
$add_arms() add arms to a trial. This function is used to add
arms to a newly defined trial, or add arms under adaptive design, e.g.,
dose-ranging, etc.
$add_regimen() register a regimen object to a trial.
Must be called before $add_arms(). Applied at enrollment.
$crossover() apply a milestone-triggered crossover to eligible
patients in the trial. Called inside a milestone action; only alters patients'
post-switch endpoint values and leaves already-observed data intact.
$get_locked_data() request for data snapshot at a milestone.
Calling this function is recommended as the first action in any action
function as long as trial data is needed in statistical analysis or decision
making.
$save() save intermediate result for simulation summary.
Results across multiple replicates of simulation are saved, which can be
retrieved by calling get_output() anytime.
$bind() row bind and save intermediate results across
milestones if those results are data frames of similar formats. The life
cycle of the save results is within a single replicate of simulation and
is reset to NULL in next simulated trial. Saved results
can be retrieved by calling get() anytime.
$save_custom_data() save intermediate results of any format.
The life cycle of the saved result is within a single replicate of simulation
and is reset to NULL in next simulated trial. Saved results can be retrieved
by calling get() anytime.
$get() retrieve intermediate results saved by calling functions
save_custom_data() or bind().
$get_output() retrieve intermediate results saved by calling
function save().
$dunnettTest() perform Dunnett's test.
$closedTest() perform combination test based on Dunnett's test.
new()
initialize a trial
Trials$new( name, n_patients, duration, description = name, seed = NULL, enroller, dropout = NULL, stratification_factors = NULL, silent = FALSE, ... )
namecharacter. Name of trial. Usually, hmm..., useless.
n_patientsinteger. Maximum (and initial) number of patients could be enrolled when planning the trial. It can be altered adaptively during a trial.
durationNumeric. Trial duration. It can be altered adaptively during a trial.
descriptioncharacter. Optional for description of the trial. By
default it is set to be trial's name. Usually useless.
seedrandom seed. If NULL, seed is set for each simulated
trial automatically and saved in output. It can be retrieved in the
seed column in $get_output(). Setting it to be NULL
is recommended. For debugging, set it to a specific integer.
enrollera function returning a vector enrollment time for
patients. Its first argument n is the number of enrolled patients.
Set it to StaggeredRecruiter can handle most of the use cases.
See ?TrialSimulator::StaggeredRecruiter for more information.
dropouta function returning a vector of dropout time for patients.
It can be any random number generator with first argument n,
the number of enrolled patients. Usually rexp if dropout rate
is set at a single time point, or rweibull if dropout rates are
set at two time points. See ?TrialSimulator::weibullDropout.
stratification_factorscharacter. Names of baseline characteristics
to define stratums in stratified permuted block randomization.
Stratification factors must be defined in endpoint() with
readout = 0. As a natural assumption for randomized trial,
TrialSimulator assumes that the baseline
characteristics share the same distribution across arms, but endpoints
can have same or different distributions given baseline characteristics.
NULL by default, i.e., unstratified permuted block randomization is
executed.
silentlogical. TRUE to mute messages. However, warning
message is still displayed.
...(optional) arguments of enroller and dropout.
get_trial_data()
return trial data of enrolled patients at the time of this function is called
Trials$get_trial_data()
get_duration()
return maximum duration of a trial
Trials$get_duration()
set_duration()
set trial duration in an adaptive designed trial. All patients enrolled before resetting the duration are truncated (non-tte endpoints) or censored (tte endpoints) at the original duration. Remaining patients are re-randomized. New duration must be longer than the old one.
Trials$set_duration(duration)
durationnew duration of a trial. It must be greater than the current duration.
set_enroller()
set recruitment curve when initialize a trial.
Trials$set_enroller(func, ...)
funcfunction to generate enrollment time. It can be built-in function like 'rexp' or customized functions like 'StaggeredRecruiter'.
...(optional) arguments for func.
get_enroller()
get function of recruitment curve
Trials$get_enroller()
set_dropout()
set distribution of drop out time. This can be done when initialize a trial, or when updating a trial in adaptive design.
Trials$set_dropout(func, ...)
funcfunction to generate dropout time. It can be built-in function like 'rexp' or customized functions.
...(optional) arguments for func.
get_dropout()
get generator of dropout time
Trials$get_dropout()
roll_back()
roll back data to current time of trial. By doing so,
Trial$trial_data will be cut at current time, and data after then
are deleted. However, Trial$enroll_time after current time are
kept unchanged because that is planned enrollment curve.
Trials$roll_back()
remove_arms()
remove arms from a trial. enroll_patients() will be called
at the end of this function to enroll all remaining patients after
Trials$get_current_time(), i.e. no more unenrolled patients
could be randomized to removed arms. This function may be used with
futility analysis, dose selection, enrichment analysis (sub-population)
or interim analysis (early stop for efficacy).
Note that this function should only be called within action functions.
It is users' responsibility to ensure it and TrialSimulator has
no way to track this.
In addition, data of the removed arms are censored or truncated by
the time of arm removal.
Trials$remove_arms(arms_name)
arms_namecharacter vector. Name of arms to be removed.
update_sample_ratio()
update sample ratios of arms. This could happen after an arm is added or removed. Note that we may update sample ratios of unaffected arms as well. Once sample ratio is updated, trial data should be rolled back with updated randomization queue. Data of unenrolled patients are re-sampled as well.
Trials$update_sample_ratio(arm_names, sample_ratios)
arm_namescharacter vector. Name of arms.
sample_ratiosnumeric vector. New sample ratios of arms. If
sample ratio is a whole number, the permuted block randomization is
adopted; otherwise, sample() will be used instead, which can
cause imbalance between arms by chance. However, this is fine for
simulation.
update_generator()
update endpoint generator in an arm
Trials$update_generator(arm_name, endpoint_name, generator, ...)
arm_namecharacter. Name of an arm.
endpoint_namecharacter. A vector of endpoint names whose generator is updated.
generatora random number generation (RNG) function.
See generator of endpoint().
...optional arguments for generator.
resize()
resize a trial with a greater sample size. This function is used to
update the maximum sample size adaptively after sample size reassessment.
Note that this function should be called within action functions.
It is users' responsibility to ensure it and TrialSimulator has
no way to track this.
Trials$resize(n_patients)
n_patientsinteger. Number of maximum sample size of a trial.
add_arms()
add one or more arms to the trial. enroll_patients() will be
called at the end to enroll all remaining patients in
private$randomization_queue. This function can be used in two
scenarios:
(1) add arms right after a trial is created (i.e., Trials$new(...)).
sample_ratio and arms added through ... should be of same
length;
(2) add arms to a trial already with arm(s).
Note that this function should only be called within action functions.
It is users' responsibility to ensure it and TrialSimulator has
no way to track this.
Trials$add_arms(sample_ratio, ...)
sample_ratiointeger vector. Sample ratio for permuted block randomization. It will be appended to existing sample ratio in the trial.
...one or more objects returned from arm().
Randomization is carried out with updated
sample ratio of newly added arm. It rolls back all patients after
Trials$get_current_time(), i.e. redo randomization for those
patients. This can be useful to add arms one by one when creating a trial.
Note that we can run Trials$add_arm(sample_ratio1, arm1) followed
by Trials$add_arm(sample_ratio2, arm2).
We would expected similar result with
Trials$add_arms(c(sample_ratio1, sample_ratio2), arm1, arm2). Note
that these two method won't return exactly the same trial because
randomization_queue were generated twice in the first approach but only
once in the second approach. But statistically, they are equivalent and
of the same distribution.
add_regimen()
register regimen to a trial. The regimen consists of three functions to determine the patients who may switch to other treatment during a a trial, to determine the switching time and how to update patients' endpoint data accordingly.
Trials$add_regimen(regimen)
regimenan object created by regimen().
get_regimen()
return registered regimen.
Trials$get_regimen()
has_regimen()
return whether a regimen is registered
Trials$has_regimen()
crossover()
Apply a milestone-triggered crossover to eligible patients in the trial.
Unlike a regimen registered via add_regimen() (applied at
enrollment), crossover() is meant to be called inside a milestone's
action function. At the earliest crossover (calendar) time
T = get_current_time() + delay, eligible patients may switch to a
new treatment, and only their post-switch endpoint values are
altered. The triplet is stacked onto the trial's regimen (so it is also
re-applied to patients enrolled later), and applied immediately, in place,
to all currently-eligible patients.
Eligibility (the pool passed to what()) = patients with at least
one endpoint still "open" (unobserved, dropout-/duration-aware) at
T; fully-observed patients are excluded. when() must return
a switch time with enroll_time + switch_time >= T (a crossover
cannot predate its opening), otherwise an error is raised. how()
may only change post-switch outcomes; returning a changed value for a
pre-switch/locked cell raises an error.
Two helper columns are injected into patient_data for the triplet
functions: earliest_crossover_calendar_time (= T) and
earliest_crossover_time_from_enrollment (= max(T - enroll_time, 0)).
Trials$crossover(what, how, when = NULL, delay = 0, ...)
whata function selecting which eligible patients crossover and to
what new_treatment (NA = no crossover). See regimen().
howa function returning the modified post-switch endpoint values.
when(optional) a function returning switch_time from
enrollment. If NULL (default), patients switch at T
(switch_time = earliest_crossover_time_from_enrollment).
delaynumeric. Time after the milestone before crossover opens;
T = get_current_time() + delay. Default 0.
...(optional) named arguments routed to what, when,
and/or how.
get_name()
return name of trial
Trials$get_name()
get_description()
return description of trial
Trials$get_description()
get_arms()
return a list of arms in the trial
Trials$get_arms()
get_arms_name()
return arms' name of trial
Trials$get_arms_name()
get_number_arms()
get number of arms in the trial
Trials$get_number_arms()
has_arm()
check if the trial has any arm. Return TRUE or FALSE.
Trials$has_arm()
get_an_arm()
return an arm
Trials$get_an_arm(arm_name)
arm_namecharacter, name of arm to be extracted
get_sample_ratio()
return current sample ratio of the trial. The ratio can probably change during the trial (e.g., arm is removed or added)
Trials$get_sample_ratio(arm_names = NULL)
arm_namescharacter vector of arms.
get_number_patients()
return number of patients when planning the trial
Trials$get_number_patients()
get_number_enrolled_patients()
return number of enrolled (randomized) patients
Trials$get_number_enrolled_patients()
get_number_unenrolled_patients()
return number of unenrolled patients
Trials$get_number_unenrolled_patients()
get_stratum_queue()
return stratum queue of planned but not yet enrolled patients. This function does not update stratum_queue, just return its value for debugging purpose.
Trials$get_stratum_queue(index = NULL)
indexindex to be extracted. Return all queue if NULL.
get_randomization_queue()
return randomization queue of planned but not yet enrolled patients. This function does not update randomization_queue, just return its value for debugging purpose.
Trials$get_randomization_queue(index = NULL)
indexindex to be extracted. Return all queue if NULL.
get_enroll_time()
return enrollment time of planned but not yet enrolled patients. This function does not update enroll_time, just return its value for debugging purpose.
Trials$get_enroll_time(index = NULL)
indexindex to extract. Return all enroll time if NULL.
enroll_patients()
assign new patients to pre-planned randomization queue at pre-specified enrollment time.
Trials$enroll_patients(n_patients = NULL)
n_patientsnumber of new patients to be enrolled. If NULL,
all remaining patients in plan are enrolled. Error may be triggered if
n_patients is greater than remaining patients as planned.
set_current_time()
set current time of a trial. Any data collected before could not be changed. private$now should be set after a milestone is triggered (through Milestones class, futility, interim, etc), an arm is added or removed at a milestone
Trials$set_current_time(time)
timecurrent calendar time of a trial.
get_current_time()
return current time of a trial
Trials$get_current_time()
get_event_tables()
count accumulative number of events (for TTE) or non-missing samples (otherwise) over calendar time (enroll time + tte for TTE, or enroll time + readout otherwise)
Trials$get_event_tables(arms = NULL, ...)
armsa vector of arms' name on which the event tables are created.
if NULL, all arms in the trial will be used.
...subset conditions compatible with dplyr::filter.
Event tables will be counted on subset of trial data only.
get_data_lock_time_by_event_number()
given a set of endpoints and target number of events, determine the data lock time for a milestone (futility, interim, final, etc.). This function does not change trial object (e.g. rolling back not yet randomized patients after the found data lock time).
Trials$get_data_lock_time_by_event_number(
endpoints,
arms,
target_n_events,
type = c("all", "any"),
...
)endpointscharacter vector. Data lock time is determined by a set of endpoints.
armsa vector of arms' name on which number of events will be counted.
target_n_eventstarget number of events for each of the
endpoints.
typeall if all target number of events are reached.
any if the any target number of events is reached.
...subset conditions compatible with dplyr::filter. Number
Time of milestone is based on event counts on the subset of trial data.
data lock time
get_data_lock_time_by_enrollment()
given a target number of enrolled patients, determine the data lock time for a milestone (futility, interim, final, etc.). This function does not change trial object (e.g. rolling back not yet randomized patients after the found data lock time). It is similar to get_data_lock_time_by_event_number but only focus on patient_id.
Trials$get_data_lock_time_by_enrollment( arms, target_n_patients, min_treatment_duration, ... )
armsa vector of arms' name on which number of events will be counted.
target_n_patientstarget number of enrolled patients.
min_treatment_durationnumeric. Zero or positive value.
minimum treatment duration of enrolled patients.
If 0, it looks for triggering time based on number of enrolled
patients in population specified by ... and arms. If positive,
it means that milestone is triggered when a specific number of enrolled
patients have received treatment for at least min_treatment_duration
duration. It is users' responsibility to assure that the unit of
min_treatment_duration are consistent with
readout of non-tte endpoints, dropout time, and trial duration.
...subset conditions compatible with dplyr::filter. Number
Time of milestone is based on event counts on the subset of trial data.
data lock time
get_data_lock_time_by_calendar_time()
given the calendar time to lock the data, return it with event counts of each of the endpoints.
Trials$get_data_lock_time_by_calendar_time(calendar_time)
calendar_timenumeric. Calendar time to lock the data
data lock time
get_locked_data()
return locked data, i.e. snapshot at a milestone. TTE data is censored and non-TTE data is truncated accounting for readout time and dropout time simultaneously by the triggering time of milestone.
Trials$get_locked_data(milestone_name)
milestone_namecharacter. Milestone name of which the locked data to be extracted.
get_locked_data_name()
return names of locked data
Trials$get_locked_data_name()
get_event_number()
return number of events at lock time of milestones
Trials$get_event_number(milestone_name = NULL)
milestone_namenames of triggered milestones. Use all triggered milestones
if NULL.
save_milestone_time()
save time of a new milestone.
Trials$save_milestone_time(milestone_time, milestone_name)
milestone_timenumeric. Time of new milestone.
milestone_namecharacter. Name of new milestone.
get_milestone_time()
return milestone time when triggering a given milestone
Trials$get_milestone_time(milestone_name = NULL)
milestone_namecharacter. Name of milestone. If NULL,
time of all triggered milestones are returned.
lock_data()
lock data at specific calendar time.
For time-to-event endpoints, their event indicator *_event should be
updated accordingly. Locked data should be stored separately.
DO NOT OVERWRITE/UPDATE private$trial_data! which can lose actual
time-to-event information. For example, a patient may be censored at
the first data lock. However, he may have event being observed in a
later data lock.
Trials$lock_data(at_calendar_time, milestone_name)
at_calendar_timetime point to lock trial data
milestone_nameassign milestone name as the name of locked data for future reference.
event_plot()
plot of cumulative number of events/samples over calendar time.
Trials$event_plot()
censor_trial_data()
censor trial data at calendar time
Trials$censor_trial_data( censor_at = NULL, selected_arms = NULL, enrolled_before = Inf )
censor_attime of censoring. It is set to trial duration if
NULL.
selected_armscensoring is applied to selected arms (e.g.,
removed arms) only. If NULL, it will be set to all available arms
in trial data. Otherwise, censoring is applied to user-specified arms only.
This is necessary because number of events/sample size in removed arms
should be fixed unchanged since corresponding milestone is triggered. In that
case, one can update trial data by something like
censor_trial_data(censor_at = milestone_time, selected_arms = removed_arms).
enrolled_beforecensoring is applied to patients enrolled before
specific time. This argument would be used when trial duration is
updated by set_duration. Adaptation happens when set_duration
is called so we fix duration for patients enrolled before adaptation
to maintain independent increment. This should work when trial duration
is updated for multiple times.
save()
save a single value or a one-row data frame to trial's output for further analysis/summary later. Results saved by calling this function have a life cycle of the whole simulation. This means that all results are accumulated across multiple simulated trial and can be used for summary later.
Trials$save(value, name = "", overwrite = FALSE)
valuevalue to be saved. It can be a scalar (vector of length 1) or a data frame (of one row).
namecharacter to name the saved object. It will be used to
name a column in trial's output if value is a scalar.
If value is a data frame, name will be the prefix pasted
with the column name of value in trial's output.
If user want to use
value's column name as is in trial's output, set name
to be '' as default. Otherwise, column name would be, e.g.,
"{<name>}_<{colnames(value)}>".
overwritelogic. TRUE if overwriting existing entries
with warning, otherwise, throwing an error and stop.
bind()
row bind a data frame to existing data frame. If a data frame name
is not existing in a trial, then it is equivalent to
calling Trials$save_custom_data().
Extra columns in value are ignored. Columns in
Trials$custom_data[[name]] but not in value are filled
with NA.
This function can be used to save results across multiple milestones.
For example, p-values and effect estimates of endpoints may be computed
at multiple milestones. Users may want to bind them into a data frame
for combination test or graphical test. In this case, this function
can be called repeatedly in milestones. Once the data frame is fully
conducted, statistical test can be performed on its final version
retrieved by calling Trials$get().
Note that data saved by calling this function has a short life cycle
within a single simulated trial. It will be reset to NULL before
simulated another trial. Thus, it cannot be used to save results that
are used for summarizing the simulation.
Trials$bind(value, name)
valuea data frame to be saved. It can consist of one or multiple rows.
namecharacter. Name of object to be saved.
save_custom_data()
save arbitrary (number of) objects into a trial so that users can use those to control the workflow. Most common use case is to store simulation parameters to be used in action functions.
Trials$save_custom_data(value, name, overwrite = FALSE)
valuevalue to be saved. Any type.
namecharacter. Name of the value to be accessed later.
overwritelogic. TRUE if overwriting existing entries
with warning, otherwise, throwing an error and stop.
get_custom_data()
return custom data saved by calling Trials$save_custom_data()
or Trials$bind() with its name.
Trials$get_custom_data(name)
namecharacter. Name of custom data to be accessed.
get()
alias of function get_custom_data to make it short and cool.
Trials$get(name)
namecharacter. Name of custom data to be accessed.
get_output()
return a data frame of all current outputs saved by calling
Trials$save(). Usually this function is call at the end of
simulation for summary.
Trials$get_output(cols = NULL, simplify = TRUE, tidy = FALSE)
colscolumns to be returned from Trial$output. If
NULL, all columns are returned.
simplifylogical. Return value rather than a data frame of one
column when length(col) == 1 and simplify == TRUE.
tidylogical. TrialSimulator automatically records a set
of standard outputs at milestones, even when doNothing is used
as action functions. These includes time of triggering milestones,
number of observed events for time-to-event endpoints, and number of
non-missing readouts for non-TTE endpoints
(see vignette('actionFunctions')). This usually mean a large
number of columns in outputs. If users have no intent to summarize a
trial on these columns, setting tidy = TRUE can eliminate these
columns from get_output(). Note that currently we use regex
"^n_events_<.*?>_<.*?>$" and
"^milestone_time_<.*?>$" to match columns to be eliminated.
If users plan to use tidy = TRUE, caution is needed when naming
custom outputs in save(). Default FALSE.
mute()
mute all messages (not including warnings)
Trials$mute(silent)
silentlogical.
tidy_output()
save less information in trial output if no intent to use it in summary
Trials$tidy_output(tidy)
tidylogical. If TRUE, event count per arm per endpoint is
not computed and saved in trial output. This can speed up simulation by
up to 40% under some circumstances.
independentIncrement()
calculate independent increments from a given set of milestones
Trials$independentIncrement( formula, placebo, milestones, alternative, planned_info, ... )
formulaAn object of class formula that can be used with
survival::coxph. Must consist arm and endpoint in data.
No covariate is allowed. Stratification variables are supported and can be
added using strata(...).
placebocharacter. String of placebo in trial's locked data.
milestonesa character vector of milestone names in the trial, e.g.,
listener$get_milestone_names().
alternativea character string specifying the alternative hypothesis,
must be one of "greater" or "less". No default value.
"greater" means superiority of treatment over placebo is established
by an hazard ratio greater than 1 when a log-rank test is used.
planned_infoa vector of planned accumulative number of event of
time-to-event endpoint. It is named by milestone names.
Note: planned_info can also be a character
"oracle" so that planned number of events are set to be observed
number of events, in that case inverse normal z statistics equal to
one-sided logrank statistics. This is for the purpose of debugging only.
In formal simulation, "oracle" should not be used if adaptation
is present. Pre-fixed planned_info should be used to create
weights in combination test that controls the family-wise error rate
in the strong sense.
...subset condition that is compatible with dplyr::filter.
survdiff will be fitted on this subset only to compute one-sided
logrank statistics. It could be useful when a
trial consists of more than two arms. By default it is not specified,
all data will be used to fit the model.
This function returns a data frame with columns:
p_inverse_normalone-sided p-value for inverse normal test based on logrank test (alternative hypothesis: risk is higher in placebo arm). Accumulative data is used.
z_inverse_normalz statistics of p_inverse_normal.
Accumulative data is used.
p_lrone-sided p-value for logrank test (alternative hypothesis: risk is higher in placebo arm). Accumulative data is used.
z_lrz statistics of p_lr.
Accumulative data is used.
infoobserved accumulative event number.
planned_infoplanned accumulative event number.
info_pboobserved accumulative event number in placebo.
info_trtobserved accumulative event number in treatment arm.
wtweights in z_inverse_normal.
\dontrun{
trial$independentIncrement(Surv(pfs, pfs_event) ~ arm, 'pbo',
listener$get_milestone_names(),
'less', 'oracle')
}
dunnettTest()
carry out closed test based on Dunnett method under group sequential design.
Trials$dunnettTest( formula, placebo, treatments, milestones, alternative, planned_info, ... )
formulaAn object of class formula that can be used with
survival::coxph. Must consist arm and endpoint in data.
No covariate is allowed. Stratification variables are supported and can be
added using strata(...).
placebocharacter. Name of placebo arm.
treatmentscharacter vector. Name of treatment arms to be used in comparison.
milestonescharacter vector. Names of triggered milestones at which either
adaptation is applied or statistical testing for endpoint is performed.
Milestones in milestones does not need to be sorted by their triggering time.
alternativea character string specifying the alternative hypothesis,
must be one of "greater" or "less". No default value.
"greater" means superiority of treatment over placebo is established
by an hazard ratio greater than 1 when a log-rank test is used.
planned_infoa data frame of planned number of events of
time-to-event endpoint in each stage and each arm. Milestone names, i.e.,
milestones are row names of planned_info, and arm names, i.e.,
c(placebo, treatments) are column names.
Note that it is not the accumulative but stage-wise event numbers.
It is usually not easy to determine these numbers in practice, simulation
may be used to get estimates.
Note: planned_info can also be a character
"default" so that planned_info are set to be number
of newly randomized patients in the control arm in each of the stages.
This assumes that
event rate do not change over time and/or sample ratio between placebo
and a treatment arm does not change as well, which may not be true.
It is for the purpose of debugging or rapid implementation
only. Using simulation to pick planned_info is recommended in
formal simulation study. Another issue with planned_info set to
be "default" is that it is possible patient recruitment is done
before a specific stage, as a result, planned_info is zero which
can crash the program.
...subset condition that is compatible with dplyr::filter.
survdiff will be fitted on this subset only to compute one-sided
logrank statistics. It could be useful when comparison is made on a
subset of treatment arms. By default it is not specified,
all data (placebo plus one treatment arm at a time) in the locked data
are used to fit the model.
This function computes stage-wise p-values for each of the intersection
hypotheses based on Dunnett test. If only one treatment arm is present,
it is equivalent to compute the stage-wise p-values of elemental
hypotheses. This function also computes inverse normal combination
test statistics at each of the stages.
The choice of planned_info can affect the calculation of
stage-wise p-values. Specifically, it is used to compute
the columns observed_info and p_inverse_normal in returned
data frame, which will be used in Trial$closedTest().
The choice of planned_info can affect the result of
Trial$closedTest() so user should chose it with caution.
Note that in Trial$closedTest(),
observed_info, which is derived from planned_info, will
lead to the same closed testing results up to a constant. This is because
the closed test uses information fraction
observed_info/sum(observed_info). As a result, setting
planned_info to, e.g., 10 * planned_info should give same
closed test results.
Based on numerical study, setting planned_info = "default" leads
to a much higher power (roughly 10%) than setting planned_info to
median of event numbers at stages, which can be determined by simulation.
I am not sure if regulator would support such practice. For example,
if a milestone (e.g., interim analysis) is triggered at a pre-specified
calendar time, the number of randomized patients is random and is unknown
when planning the trial. If I understand it correctly, regulator may want
the information fraction in closed test (combined with Dunnett test) to
be pre-fixed. In addition, this choice for planned_info assumes
that the event rates does not change over time which is obviously not
true. It is recommended to always use pre-fixed planned_info for
restrict control of family-wise error rate. It should be pointed out
that the choice of pre-fixed planned_info can affect statistical
power significantly so fine-tuning may be required.
a list with element names like arm_name,
arm1_name|arm2_name, arm1_name|arm2_name|arm3_name, etc.,
i.e., all possible combination of treatment arms in comparison. Each
element is a data frame, with its column names self-explained. Specifically,
the columns p_inverse_normal, observed_info,
is_final can be used with GroupSequentialTest to perform
significance test.
\dontrun{
trial$dunnettTest(Surv(pfs, pfs_event) ~ arm, 'pbo', c('high dose', 'low dose'),
listener$get_milestone_names(), 'default')
}
closedTest()
perform closed test based on Dunnett test
Trials$closedTest(
dunnett_test,
treatments,
milestones,
alpha,
alpha_spending = c("asP", "asOF")
)dunnett_testobject returned by Trial$dunnettTest().
treatmentscharacter vector. Name of treatment arms to be used in comparison.
milestonescharacter vector. Names of triggered milestones at which
significance testing for endpoint is performed in closed test.
Milestones in milestones does not need to be sorted by their triggering time.
alphanumeric. Allocated alpha.
alpha_spendingalpha spending function. It can be "asP" or
"asOF". Note that theoretically it can be "asUser", but
it is not tested. It may be supported in the future.
a data frame of columns arm, decision
(final decision on a hypothesis at the end of trial, "accept" or "reject"),
milestone_at_reject, and reject_time.
If a hypothesis is accepted at then end of a trial,
milestone_at_reject is NA, and reject_time is Inf.
Note that if a hypothesis is tested at multiple milestones, the final
decision will be "accept" if it is accepted at at least
one milestone. The decision is "reject" only if the hypothesis
is rejected at all milestones.
\dontrun{
dt <- trial$dunnettTest(
Surv(pfs, pfs_event) ~ arm,
placebo = 'pbo',
treatments = c('high dose', 'low dose'),
milestones = c('dose selection', 'interim', 'final'),
data.frame(pbo = c(100, 160, 80),
low = c(100, 160, 80),
high = c(100, 160, 80),
row.names = c('dose selection', 'interim', 'final'))
trial$closedTest(dt, treatments = c('high dose', 'low dose'),
milestones = c('interim', 'final'),
alpha = 0.025, alpha_spending = 'asOF')
}
get_seed()
return random seed
Trials$get_seed()
print()
print a trial
Trials$print()
get_snapshot_copy()
return a snapshot of a trial before it is executed.
Trials$get_snapshot_copy()
make_snapshot()
make a snapshot before running a trial. This can be useful when resetting a trial. This is only called when initializing a 'Trial' object, when arms have not been added yet.
Trials$make_snapshot()
make_arms_snapshot()
make a snapshot of arms
Trials$make_arms_snapshot()
reset()
reset a trial to its snapshot taken before it was executed. Seed will be reassigned with a new one. Enrollment time are re-generated. If the trial already have arms when this function is called, they are added back to recruit patients again.
Trials$reset()
set_arm_added_time()
save time when an arm is added to the trial
Trials$set_arm_added_time(arm, time)
armname of added arm.
timetime when an arm is added.
get_arm_added_time()
get time when an arm is added to the trial
Trials$get_arm_added_time(arm)
armarm name.
set_arm_removal_time()
save time when an arm is removed to the trial
Trials$set_arm_removal_time(arm, time)
armname of removed arm.
timetime when an arm is removed.
get_arm_removal_time()
get time when an arm is removed from the trial
Trials$get_arm_removal_time(arm)
armarm name.
get_stratification_factors()
return stratification factors
Trials$get_stratification_factors()
has_stratification_factors()
has stratification factors
Trials$has_stratification_factors()
clone()
The objects of this class are cloneable with this method.
Trials$clone(deep = FALSE)
deepWhether to make a deep clone.
# Instead of using Trials$new, please use trial(), a user-friendly # wrapper. See examples in ?trial. ## ------------------------------------------------ ## Method `Trials$independentIncrement` ## ------------------------------------------------ ## Not run: trial$independentIncrement(Surv(pfs, pfs_event) ~ arm, 'pbo', listener$get_milestone_names(), 'less', 'oracle') ## End(Not run) ## ------------------------------------------------ ## Method `Trials$dunnettTest` ## ------------------------------------------------ ## Not run: trial$dunnettTest(Surv(pfs, pfs_event) ~ arm, 'pbo', c('high dose', 'low dose'), listener$get_milestone_names(), 'default') ## End(Not run) ## ------------------------------------------------ ## Method `Trials$closedTest` ## ------------------------------------------------ ## Not run: dt <- trial$dunnettTest( Surv(pfs, pfs_event) ~ arm, placebo = 'pbo', treatments = c('high dose', 'low dose'), milestones = c('dose selection', 'interim', 'final'), data.frame(pbo = c(100, 160, 80), low = c(100, 160, 80), high = c(100, 160, 80), row.names = c('dose selection', 'interim', 'final')) trial$closedTest(dt, treatments = c('high dose', 'low dose'), milestones = c('interim', 'final'), alpha = 0.025, alpha_spending = 'asOF') ## End(Not run)# Instead of using Trials$new, please use trial(), a user-friendly # wrapper. See examples in ?trial. ## ------------------------------------------------ ## Method `Trials$independentIncrement` ## ------------------------------------------------ ## Not run: trial$independentIncrement(Surv(pfs, pfs_event) ~ arm, 'pbo', listener$get_milestone_names(), 'less', 'oracle') ## End(Not run) ## ------------------------------------------------ ## Method `Trials$dunnettTest` ## ------------------------------------------------ ## Not run: trial$dunnettTest(Surv(pfs, pfs_event) ~ arm, 'pbo', c('high dose', 'low dose'), listener$get_milestone_names(), 'default') ## End(Not run) ## ------------------------------------------------ ## Method `Trials$closedTest` ## ------------------------------------------------ ## Not run: dt <- trial$dunnettTest( Surv(pfs, pfs_event) ~ arm, placebo = 'pbo', treatments = c('high dose', 'low dose'), milestones = c('dose selection', 'interim', 'final'), data.frame(pbo = c(100, 160, 80), low = c(100, 160, 80), high = c(100, 160, 80), row.names = c('dose selection', 'interim', 'final')) trial$closedTest(dt, treatments = c('high dose', 'low dose'), milestones = c('interim', 'final'), alpha = 0.025, alpha_spending = 'asOF') ## End(Not run)
update endpoint generator in an arm. This function can be useful in, e.g., enrichment design where generator is updated so that patients are enrolled from sub-population afterwards. This function can also be used when data model changes over time, i.e., generating data in a new way after a milestone. This function can be called multiple times to update generators of endpoints one by one.
Note that this function should only be called within action functions of
milestones. It is users' responsibility to ensure that and
TrialSimulator has no way to track it.
This is a user-friendly wrapper of the member function of trial, i.e.,
Trials$update_generator(), which is used in vignettes. Users who are
not familiar with the concept of classes may consider using this wrapper
directly.
update_generator(trial, arm_name, endpoint_name, generator, ...)update_generator(trial, arm_name, endpoint_name, generator, ...)
trial |
a trial object returned by |
arm_name |
character. Name of an arm. |
endpoint_name |
character. A vector of endpoint names whose generator is updated. |
generator |
a random number generation (RNG) function.
See generator of |
... |
optional arguments for generator. |
update sample ratios of arms. This could be called after an arm is added or removed. Sample ratios can be updated for any existing arms.
This is a user-friendly wrapper of the member function of trial, i.e.,
Trials$update_sample_ratio(), which is used in vignettes.
Users who are not familiar with the concept of classes may consider using
this wrapper directly.
update_sample_ratio(trial, arm_names, sample_ratios)update_sample_ratio(trial, arm_names, sample_ratios)
trial |
a trial object returned by |
arm_names |
character vector. Name of arms. |
sample_ratios |
numeric vector. New sample ratios of arms. If sample
ratio is a whole number, the permuted block randomization is adopted;
otherwise, |
Fit scale and shape parameters of the Weibull distribution to match dropout rates at two specified time points. Weibull distribution can be used as a dropout distribution because it has two parameters.
Note that It is users' responsibility to assure that the units of dropout time, readout of non-tte endpoints, and trial duration are consistent.
weibullDropout(time, dropout_rate)weibullDropout(time, dropout_rate)
time |
a numeric vector of two time points at which dropout rates are specified. |
dropout_rate |
a numeric vector of dropout rates at |
a named vector for scale and shape parameters.
## dropout rates are 8% and 18% at time 12 and 18. weibullDropout(time = c(12, 18), dropout_rate = c(.08, .18))## dropout rates are 8% and 18% at time 12 and 18. weibullDropout(time = c(12, 18), dropout_rate = c(.08, .18))