| Title: | Create Randomization Lists |
|---|---|
| Description: | Randomization lists are an integral component of randomized clinical trials. 'randotools' provides tools to easily create such lists. |
| Authors: | Alan G Haynes [aut, cre] (ORCID: <https://orcid.org/0000-0003-1374-081X>) |
| Maintainer: | Alan G Haynes <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.2.6 |
| Built: | 2026-05-22 08:42:23 UTC |
| Source: | https://github.com/CTU-Bern/randotools |
Before committing to a randomisation plan (in terms of the number of strata, block sizes etc) it can be useful to estimate the imbalance that might be expected. This function simulates trials of a given sample size and returns the imbalance that might be expected.
check_plan( n_rando, n_strata, arms = c("A", "B"), blocksizes = c(1, 2), n_sim = 1000 ) ## S3 method for class 'checkplan' print(x, ...)check_plan( n_rando, n_strata, arms = c("A", "B"), blocksizes = c(1, 2), n_sim = 1000 ) ## S3 method for class 'checkplan' print(x, ...)
n_rando |
number of participants to randomise |
n_strata |
number of strata |
arms |
arms that will be randomised |
blocksizes |
number of each randomisation group per block (e.g. 1 = one of each arm per block, 2 = per of each arm per block) |
n_sim |
number of simulations |
x |
check_plan object |
... |
options passed to print.data.frame |
list of class checkplan with slots the same slots as input to the function plus mean (mean imbalance), counts (counts of the imbalances) and worst_case (randomisation results with the worst observed imbalance)
print(checkplan): Print method for check_plan output
https://www.sealedenvelope.com/randomisation/simulation/
check_plan(50, 3, n_sim = 50)check_plan(50, 3, n_sim = 50)
It can be useful to see how imbalance changes through time. This function allows such a depiction by plotting the maximum imbalance as a function of randomisation number (assuming that the observations are in the randomisation order). This is especially useful in the case of randomisation via minimisation. As well as the overall imbalance, the function also depicts the imbalance within each strata (i.e. the interaction among stratifying variables) and within strata identified by each stratifying variable itself.
imbalance_seq_plots( data, randovar, stratavars = NULL, cross = TRUE, stack = TRUE )imbalance_seq_plots( data, randovar, stratavars = NULL, cross = TRUE, stack = TRUE )
data |
a data frame |
randovar |
variable name containing the randomisation result |
stratavars |
variable names of stratification variables |
cross |
logical whether to cross the stratification variables to create the individual strata |
stack |
logical whether to use |
Up to six ggplots. Each has the randomisation sequence along the x-axis and imbalance on the y-axis. The different lines denotes different groupings. All plots are paired: the first plot shows the observed balance, the second shows the balance in a simulated dataset. There are up to three pairs of plots.
First the overall values are shown.
Second, each line represents a group as defined by the stratification variables. E.g., if there is a 2-level stratification variable and a 3-level variable, there will be 5 lines.
The third pair shows the individual strata - the combination of all stratification
variables. For the 2- and 3-level example mentioned above, this would result
in 6 lines. This can be skipped by setting cross to FALSE.
If stack = FALSE, a list of ggplots is returned.
data(rando_balance) # without stratification variables imbalance_seq_plots(rando_balance, "rando_res") # with stratification factors imbalance_seq_plots(rando_balance, "rando_res", c("strat1", "strat2")) # do not cross the stratification factors imbalance_seq_plots(rando_balance, "rando_res", c("strat1", "strat2"), cross = FALSE)data(rando_balance) # without stratification variables imbalance_seq_plots(rando_balance, "rando_res") # with stratification factors imbalance_seq_plots(rando_balance, "rando_res", c("strat1", "strat2")) # do not cross the stratification factors imbalance_seq_plots(rando_balance, "rando_res", c("strat1", "strat2"), cross = FALSE)
This function tests whether the observed imbalance is less than might be expected via a random draw, via a permutation test.
imbalance_test( data, randovar, n_iter = 1000, stratavars = NULL, arms = NULL, cross = TRUE, ... )imbalance_test( data, randovar, n_iter = 1000, stratavars = NULL, arms = NULL, cross = TRUE, ... )
data |
a dataframe with the variables indicated in |
randovar |
character with the variable name indicating the randomisation |
n_iter |
integer. number of simulations to perform |
stratavars |
character vector with the variable names indicating the stratification variables |
arms |
character vector of arms in the appropriate balance. If NULL the
levels in |
cross |
logical. Whether to cross the stratification variables. |
... |
other arguments passed onto other methods |
a list with:
n_rando: the number of randomisations
stratavars: the names of the stratification variables
arms: the arms
observed: a dataframe with the observed imbalance
simulated: a dataframe with the simulated imbalances (number of rows = nrow(n_iter))
tests: a dataframe with the p-values
data(rando_balance) # without stratification variables imbalance_test(rando_balance, "rando_res", n_iter = 50) imb <- imbalance_test(rando_balance, "rando_res", stratavars = "strat1", n_iter = 50) imbalance_test(rando_balance, "rando_res", stratavars = c("strat1", "strat2"), n_iter = 50) imb <- imbalance_test(rando_balance, "rando_res2", stratavars = c("strat1", "strat2"), n_iter = 50)data(rando_balance) # without stratification variables imbalance_test(rando_balance, "rando_res", n_iter = 50) imb <- imbalance_test(rando_balance, "rando_res", stratavars = "strat1", n_iter = 50) imbalance_test(rando_balance, "rando_res", stratavars = c("strat1", "strat2"), n_iter = 50) imb <- imbalance_test(rando_balance, "rando_res2", stratavars = c("strat1", "strat2"), n_iter = 50)
Plot histograms of imbalance values from simulated random allocation and a vertical lines to indicate the observed imbalance for each randomisation level (overall, stratification variable level, and strata level, where appropriate). The p-values from the tests are included in the figure captions.
imbalance_test_plot(test, vline_col = "red", stack = TRUE)imbalance_test_plot(test, vline_col = "red", stack = TRUE)
test |
|
vline_col |
colour for the vertical line indicating the observed imbalance |
stack |
logical, whether to use |
list of ggplots or a patchwork off ggplots (if stack = TRUE)
# example code data(rando_balance) # without stratification variables imb <- imbalance_test(rando_balance, "rando_res2", stratavars = c("strat1", "strat2"), n_iter = 50) imbalance_test_plot(imb)# example code data(rando_balance) # without stratification variables imb <- imbalance_test(rando_balance, "rando_res2", stratavars = c("strat1", "strat2"), n_iter = 50) imbalance_test_plot(imb)
A synthetic dataset used in examples. The dataset contains two randomisation
result variables (rando_res and rando_res2) and two stratification variables (strat1 and
strat2).
rando_balancerando_balance
rando_balanceA data frame with 100 rows and 3 columns:
Binary stratification variables
Balanced randomisation result
Unbalanced randomisation result
Randomisation lists are central to randomised trials. This function allows to generate randomisation lists simply, via (optionally) stratified block randomisation
randolist( n, arms = LETTERS[1:2], strata = NA, blocksizes = 1:3, pascal = TRUE, ... )randolist( n, arms = LETTERS[1:2], strata = NA, blocksizes = 1:3, pascal = TRUE, ... )
n |
total number of randomizations (per stratum) |
arms |
arms to randomise |
strata |
named list of stratification variables (see examples) |
blocksizes |
numbers of each arm to include in blocks (see details) |
pascal |
logical, whether to use pascal's triangle to determine block sizes |
... |
arguments passed on to other methods |
blocksizes defines the number of allocations to each arm in a block.
For example, if there are two arms, and blocksizes = 1, each block will
contain 2 randomisations. If blocksizes = 1:2, each block will
contain either one of each arm, or two of each arm. Total block sizes are
therefore blocksizes * length(arms).
By default, frequency of the different block sizes is determined using Pascal's triangle. This has the advantage that small and large block sizes are less common than intermediate sized blocks, which helps with making it more difficult to guess future allocations, and reduces the risk of finishing in the middle of a large block.
Unbalanced randomization is possible by specifying the same arm label multiple times.
To disable block randomisation, set blocksizes to the same value as n.
object of class randolist which is a dataframe with additional
attributes ratio (randomisation ratio, e.g. 1:1, 2:1), arms (arm labels),
stratified (logical whether the list is stratified), and stratavars (the
stratification variables)
# example code randolist(10) # one stratifying variable randolist(10, strata = list(sex = c("M", "F"))) # two stratifying variables randolist(10, strata = list(sex = c("M", "F"), age = c("child", "adult"))) # different arm labels randolist(10, arms = c("arm 1", "arm 2")) # unbalanced (2:1) randomization randolist(10, arms = c("arm 1", "arm 1", "arm 2"))# example code randolist(10) # one stratifying variable randolist(10, strata = list(sex = c("M", "F"))) # two stratifying variables randolist(10, strata = list(sex = c("M", "F"), age = c("child", "adult"))) # different arm labels randolist(10, arms = c("arm 1", "arm 2")) # unbalanced (2:1) randomization randolist(10, arms = c("arm 1", "arm 1", "arm 2"))
Databases generally require a specific format to be able to import a randomization list. This function converts the randolist object to the format required by REDCap or secuTrial.
randolist_to_db( randolist, target_db = c("REDCap", "secuTrial"), strata_enc = NA, rando_enc = NA )randolist_to_db( randolist, target_db = c("REDCap", "secuTrial"), strata_enc = NA, rando_enc = NA )
randolist |
a randolist object from |
target_db |
the target database, either "REDCap" or "secuTrial" |
strata_enc |
a list of data frames with the encoding of each stratification
variable. Should have two columns - the value used in |
rando_enc |
a data frame with the randomization encoding |
rando_enc should contain an arm column containing the values supplied
to randolist, and a variable with the name required by the
database with the values that map to those in arm. See the examples.
dataframe with columns required for import into target_db
r <- randolist(10, strata = list(sex = c("M", "F")), arms = c("T1", "T2")) randolist_to_db(r, rando_enc = data.frame(arm = c("T1", "T2"), rando_res = c(1, 2)), strata_enc = list(sex = data.frame(sex = c("M", "F"), code = 1:2)), target_db = "REDCap") randolist_to_db(r, rando_enc = data.frame(arm = c("T1", "T2"), rando_res = c(1, 2)), strata_enc = list(sex = data.frame(sex = c("M", "F"), code = 1:2)), target_db = "secuTrial")r <- randolist(10, strata = list(sex = c("M", "F")), arms = c("T1", "T2")) randolist_to_db(r, rando_enc = data.frame(arm = c("T1", "T2"), rando_res = c(1, 2)), strata_enc = list(sex = data.frame(sex = c("M", "F"), code = 1:2)), target_db = "REDCap") randolist_to_db(r, rando_enc = data.frame(arm = c("T1", "T2"), rando_res = c(1, 2)), strata_enc = list(sex = data.frame(sex = c("M", "F"), code = 1:2)), target_db = "secuTrial")
Create a short summary report of the aspects of the randomisation list, which could be used for quality control.
## S3 method for class 'randolist' summary(object, ...)## S3 method for class 'randolist' summary(object, ...)
object |
randolist object |
... |
additional arguments (currently unused) |
object of class randolistsum, which is a list with elements
n_rando: total number of randomisations
n_blocks: maximum number of blocks
block_sizes: table of block sizes
arms: table of arms
ratio: randomisation ratio (character)
stratified: logical
stratavars: names of stratifying variables (character)
stratavars_tabs: tabulation of arms by each stratifcation variable
strata: names of each individual stratum
stratum_tabs: list with an element for each strata with n_rando,
n_blocks, block_sizes, arms and ratio.
r <- randolist(20) print(summary(r)) r2 <- randolist(20, strata = list(sex = c("M", "F"))) print(summary(r2)) # NOTE: explicitly printing isn't technically necessaryr <- randolist(20) print(summary(r)) r2 <- randolist(20, strata = list(sex = c("M", "F"))) print(summary(r2)) # NOTE: explicitly printing isn't technically necessary