Trial: Simulate a multi‑arm clinical trial
Trial.RdThe Trial class coordinates one or more Population objects and a Timer
to simulate a clinical trial.
At each unique time defined in the trial's Timer, the Trial:
applies enrollment and dropout updates to each
Populationbuilds a snapshot of all currently enrolled subjects
evaluates all conditions in the
Timerstores both the snapshot
locked_dataand the analysis outputsresults
Use run() to execute the simulation. Trigger conditions are best added with
helper functions trigger_by_calendar() or trigger_by_fraction().
Public fields
namecharacterUnique trial identifier.seednumericorNULLRandom seed for reproducibility.timerTimerobject with timepoints and conditions.populationlistof Population objects, one per arm.locked_datalistSnapshots at each timepoint.resultslistAnalysis outputs per condition.
Methods
Method new()
Create a new Trial instance.
Arguments
namecharacterUnique identifier for the trial.seednumericorNULLOptional random seed for reproducibility.timerTimerobject defining timepoints and conditions.populationlistof Population objects, one per arm.locked_datalistGenerated at each$run()call.resultslistAnalysis outputs generated at each$run()call.
Examples
t <- Timer$new(name="simple_timer")
pop <- Population$new(
name = "simple_pop",
data = vector_to_dataframe(rnorm(5))
)
pop$set_enrolled(5, 1)
Trial$new(name = "simple_trial", timer=t, population = list(pop))Method run()
Execute a trial simulation.
At each unique time defined by the trial's Timer:
Apply enrollment and dropout actions to each
PopulationBuild a combined snapshot of all currently enrolled subjects
Attach a
timecolumn to the snapshotEvaluate all condition readers via
Timer$check_conditions()Store snapshots and condition outputs under time‑indexed list keys
Examples
# Create two populations
popA <- Population$new("A", data = vector_to_dataframe(rnorm(10)))
popB <- Population$new("B", data = vector_to_dataframe(rnorm(12)))
# Create a timer and add timepoints
t <- Timer$new("Timer")
t$add_timepoint(time = 1, arm = "A", dropper = 0L, enroller = 4L)
t$add_timepoint(time = 1, arm = "B", dropper = 0L, enroller = 5L)
t$add_timepoint(time = 2, arm = "A", dropper = 1L, enroller = 2L)
t$add_timepoint(time = 2, arm = "B", dropper = 2L, enroller = 3L)
# Create a trial
trial <- Trial$new(
name = "ExampleTrial",
seed = 123,
timer = t,
population = list(popA, popB)
)
# Run the simulation
trial$run()
prettify_results(trial$results)Examples
# Create two populations
popA <- Population$new("A", data = vector_to_dataframe(rnorm(10)))
popB <- Population$new("B", data = vector_to_dataframe(rnorm(12)))
# Create a timer and add timepoints
t <- Timer$new("Timer")
t$add_timepoint(time = 1, arm = "A", dropper = 0L, enroller = 4L)
t$add_timepoint(time = 1, arm = "B", dropper = 0L, enroller = 5L)
t$add_timepoint(time = 2, arm = "A", dropper = 1L, enroller = 2L)
t$add_timepoint(time = 2, arm = "B", dropper = 2L, enroller = 3L)
# Add trigger for final analysis at time 2
trigger_by_calendar(2, t, analysis = function(df, current_time) {
nrow(df)
})
# Create a trial
trial <- Trial$new(
name = "ExampleTrial",
seed = 123,
timer = t,
population = list(popA, popB)
)
# Run the simulation
trial$run()
prettify_results(trial$results)
#> time cal_time_2
#> 1 2 14
## ------------------------------------------------
## Method `Trial$new`
## ------------------------------------------------
t <- Timer$new(name="simple_timer")
pop <- Population$new(
name = "simple_pop",
data = vector_to_dataframe(rnorm(5))
)
pop$set_enrolled(5, 1)
Trial$new(name = "simple_trial", timer=t, population = list(pop))
#> <Trial>
#> Public:
#> clone: function (deep = FALSE)
#> initialize: function (name, seed = NULL, timer = NULL, population = list(),
#> locked_data: list
#> name: simple_trial
#> population: list
#> results: list
#> run: function ()
#> seed: NULL
#> timer: Timer, R6
## ------------------------------------------------
## Method `Trial$run`
## ------------------------------------------------
# Create two populations
popA <- Population$new("A", data = vector_to_dataframe(rnorm(10)))
popB <- Population$new("B", data = vector_to_dataframe(rnorm(12)))
# Create a timer and add timepoints
t <- Timer$new("Timer")
t$add_timepoint(time = 1, arm = "A", dropper = 0L, enroller = 4L)
t$add_timepoint(time = 1, arm = "B", dropper = 0L, enroller = 5L)
t$add_timepoint(time = 2, arm = "A", dropper = 1L, enroller = 2L)
t$add_timepoint(time = 2, arm = "B", dropper = 2L, enroller = 3L)
# Create a trial
trial <- Trial$new(
name = "ExampleTrial",
seed = 123,
timer = t,
population = list(popA, popB)
)
# Run the simulation
trial$run()
prettify_results(trial$results)
#> NULL