R Functions
Randomization
Download Randomization.R (GitHub)
-
block_rand() is used for block randomization
Inputs
n
Total number of assignments needed. This should be (much) larger than the actual sample size.groups
Treatment group names, e.g.,c('Control','Treatment')
prob
Probabilities for block sizes. Does not need to sum to one (will be normalized).- If there are only two treatment groups, possible block sizes are 2, 4, or 6, with default probabilities of
c(0.2, 0.7, 0.1)
.
- If there are only two treatment groups, possible block sizes are 2, 4, or 6, with default probabilities of
allocation
Allocation ratio. e.g.,c(2,1,1)
for a 2:1:1 allocation ratio. UseNA
for equal allocation.seed
Random seed for reproducibility (UseNA
for a random random seed)
Example
Settingset.seed(620) out <- block_rand(n=40, groups=c('X','Z'), prob=c(2,7,1), allocation=c(2,1), seed=NA) > out $seed [1] 1760 $block_sizes [1] 3 6 6 9 6 9 6 $assignments [1] "Z" "X" "X" "Z" "X" "X" "Z" "X" "X" "X" "X" "X" "X" "Z" [15] "Z" "X" "X" "X" "Z" "Z" "X" "Z" "X" "X" "Z" "X" "Z" "X" [29] "X" "X" "X" "X" "X" "X" "X" "Z" "Z" "Z" "X" "X" "X" "X" [43] "Z" "Z" "X" table(out$assignments) X Z 30 15
seed=1760
allows the result to be reproduced.out <- block_rand(n=40, groups=c('X','Z'), prob=c(2,7,1), allocation=c(2,1), seed=1760)
-
The strat_rand() function performs stratified randomization, utilizing
block_rand()
internally.Inputs
Main Arguments
stratum_size
Sample size per stratum (each subgroup receives this number of allocations).groups
Treatment group names; refer toblock_rand()
.prob
Probabilities for block sizes; refer toblock_rand()
.allocation
Allocation ratios; refer toblock_rand()
.seed
Random seed for reproducibility; refer toblock_rand()
....
Stratification variables as named vectors (e.g.,Hospital=c('V','X','Y')
).
Output
assignments
containsS+1 columns, whereS is the number of strata.group
column contains the actual allocations, while the remaining columns identify each stratum. This is the format required by REDCap for its stratified randomization function.Example
set.seed(620) out <- strat_rand(stratum_size=40, groups=c('X','Z'), prob=c(2,7,1), allocation=c(2,1), seed=NA, Hospital=c('V','A','B'), Smoking=c('Yes','No')) > out$seed [1] 2032 > out$assignments group Hospital Smoking 1 X V Yes 2 X V Yes 3 Z V Yes 4 X V Yes 5 X V Yes 6 Z V Yes 7 Z V Yes 8 X V Yes 9 X V Yes 10 X V Yes ... ... > out$strata [[1]] [[1]]$seed [1] 6245 [[1]]$block_sizes [1] 6 3 6 6 6 6 6 6 [[1]]$stratum Hospital Smoking 1 V Yes [[2]] [[2]]$seed [1] 3140 [[2]]$block_sizes [1] 6 6 6 6 6 3 3 6 [[2]]$stratum Hospital Smoking 2 A Yes ... ...