Random Allocation to Two Independent Groups
Menu location: Analysis_Randomization_Two Independent Groups
This function allocates a given number of subjects at random to one of two independent groups.
Randomization reduces opportunities for bias and confounding in experimental designs, and leads to treatment groups which are random samples of the population sampled, thus helping to meet assumptions of subsequent statistical analysis (Bland, 2000).
Two independent groups might be intervention and control groups, for example to examine the effect of a new treatment. For a randomized controlled trial of a new treatment you would randomly allocate some subjects to receive the new treatment and the other subjects to receive the control treatment (e.g. placebo drug).
Illustration
For a total of 30 subjects, 15 to each group, you would enter 30 into this function. The allocation below was produced with 10 as the seed: entering the same seed reproduces it, and leaving the seed blank gives a fresh allocation each time.
Unpaired random allocation to intervention or control group
Randomized with seed: 10
| Intervention | 2 | Control | 1 |
| Intervention | 3 | Control | 4 |
| Intervention | 6 | Control | 5 |
| Intervention | 7 | Control | 9 |
| Intervention | 8 | Control | 11 |
| Intervention | 10 | Control | 12 |
| Intervention | 15 | Control | 13 |
| Intervention | 16 | Control | 14 |
| Intervention | 19 | Control | 17 |
| Intervention | 22 | Control | 18 |
| Intervention | 23 | Control | 20 |
| Intervention | 25 | Control | 21 |
| Intervention | 27 | Control | 24 |
| Intervention | 29 | Control | 26 |
| Intervention | 30 | Control | 28 |
- here the first subject would be allocated to the control group and the second to the intervention group, and so on.
Technical validation
Robust (pseudo-)random number generation is used, see random number generation.
R code
This R code reproduces the illustration above with R's own random number generator, which gives a different allocation for the same seed. It needs no packages and was checked with R 4.6.1. Paste it into R, or save it as a script and run it.
# Random allocation to two independent groups: the StatsDirect help illustration (30
# subjects, 15 to each of an intervention and a control group, seed 10) in R
n <- 30
seed <- 10
# StatsDirect shuffles the subject numbers 1 to n with its own Mersenne Twister
# generator, gives the first half to the intervention group and the second half to
# the control group, and lists each group in ascending order. In R, sample(n) draws
# a random permutation of 1 to n, and the groups are taken from it in the same way.
# R's default generator is also a Mersenne Twister, but set.seed() sets its state
# from the seed in R's own way, so for the same seed R draws a different sequence
# from StatsDirect's: the allocation printed here is another valid random
# allocation, not a copy of the one in the help. Any allocation from a stated seed
# can be reproduced by running the same code again with that seed.
set.seed(seed)
if (n %% 2 != 0) stop("the number of subjects must be even")
perm <- sample(n)
half <- n / 2
intervention <- sort(perm[1:half])
control <- sort(perm[(half + 1):n])
cat("Unpaired random allocation to intervention or control group\n")
cat("Randomized with seed: ", seed, "\n", sep = "")
cat("Subjects: ", n, " (", half, " to each group)\n", sep = "")
cat(sprintf("Intervention %2d Control %2d", intervention, control), sep = "\n")
# Checks that the two groups partition the subjects
stopifnot(length(intervention) == half, length(control) == half)
stopifnot(identical(sort(c(intervention, control)), 1:n))