1. Generate list using block randomization
# load packages
library("blockrand", character.only = TRUE)
library("png", character.only = TRUE)
library("writexl", character.only = TRUE)
# reproducible results
seed <- 2020
set.seed(seed)
# sample size
N <- 200
# groups
grupos <- c("CFT", "PLT")
K = length(grupos)
# blocks
B <- 10 / K
# randomize
lista <- blockrand(
N,
num.levels = K,
# treatments
levels = grupos,
# treatment names
block.sizes = seq(1:B)
)
lista <- as.data.frame(lista)
# save list im disk
write_xlsx(lista, "lista_alocacao.xlsx")
2. Check for balanced allocation
print("Participants per group, n:", quote = FALSE)
print(table(lista[, 4]), quote = FALSE)
print("Participants per group, %", quote = FALSE)
print(table(lista[, 4])/sum(summary(lista[, 4]))*100, quote = FALSE)
[1] Participants per group, n:
CFT PLT
100 100
[1] Participants per group, %
CFT PLT
50 50
3. Generate folders with envelopes
# load packages
library("fontawesome", character.only = TRUE)
library("grid", character.only = TRUE)
# create dir with eachvirtual envelopes (folders) following the randomization list
for (i in 1:dim(lista)[1]) {
# set participant ID
dir.name <- formatC(lista$id[i], width = 3, flag = "0")
# create dir
dir.create(file.path("ids"))
# create subdir
dir.create(file.path("ids", dir.name))
# maxime plot 'envelope' area
par(mar = c(0, 0, 0, 0))
par(mai = c(0, 0, 0, 0))
par(oma = c(0, 0, 0, 0))
par(width = 3,
height = 4,
unit = "in")
# initialize image
png(file = file.path("ids", dir.name, paste0(dir.name, ".png")), bg = "transparent")
plot(
NULL,
xlim = c(-1, 1),
ylim = c(-1, 1),
ylab = NA,
xlab = NA,
bty = "n",
axes = FALSE,
frame.plot = TRUE,
asp = 1
)
text(
x = 0,
y = 0,
labels = lista$treatment[i],
cex = 10
)
title(paste0('Participant ID ', dir.name), cex.main = 4)
grid.roundrect(
height = 0.99,
width = 0.99,
gp = gpar(fill = "#00000000", col = "grey", lwd = 2)
)
dev.off()
}