# A declaration is used in three ways.
# 1. To obtain some basic facts about a randomization:
declaration <- declare_ra(N = 100, m_each = c(30, 30, 40))
declaration
# 2. To conduct a random assignment:
Z <- conduct_ra(declaration)
table(Z)
# 3. To obtain the probability that each unit is in the condition it is in:
probs <- obtain_condition_probabilities(declaration, Z)
table(probs, Z)
# Simple Random Assignment Declarations
declare_ra(N = 100, simple = TRUE)
declare_ra(N = 100, prob = 0.4, simple = TRUE)
declare_ra(N = 100, prob_each = c(0.3, 0.3, 0.4),
conditions = c("control", "placebo", "treatment"), simple = TRUE)
# Complete Random Assignment Declarations
declare_ra(N = 100)
declare_ra(N = 100, m_each = c(30, 70),
conditions = c("control", "treatment"))
declare_ra(N = 100, m_each = c(30, 30, 40))
# Block Random Assignment Declarations
blocks <- rep(c("A", "B", "C"), times = c(50, 100, 200))
declare_ra(blocks = blocks)
# One row per block, one column per arm
block_m_each <- rbind(c(10, 40),
c(30, 70),
c(50, 150))
declare_ra(blocks = blocks, block_m_each = block_m_each)
# Cluster Random Assignment Declarations
clusters <- rep(letters[1:10], times = 1:10)
declare_ra(clusters = clusters)
declare_ra(clusters = clusters, m_each = c(3, 3, 4))
# Blocked and Clustered Random Assignment Declarations
clusters <- rep(letters[1:12], times = 1:12)
blocks <- rep(NA, length(clusters))
blocks[clusters %in% letters[1:3]] <- "block_1"
blocks[clusters %in% letters[4:6]] <- "block_2"
blocks[clusters %in% letters[7:9]] <- "block_3"
blocks[clusters %in% letters[10:12]] <- "block_4"
table(blocks, clusters)
declare_ra(clusters = clusters, blocks = blocks)
declare_ra(clusters = clusters, blocks = blocks, prob_each = c(0.2, 0.5, 0.3))
# Balanced assignment (tight counts; probabilities may vary).
# Opt-in: without ra_type or prob_unit_each this remains complete assignment.
p <- c(0.2, 0.4, 0.6, 0.8, 0.5, 0.5)
declare_ra(prob_unit = p, ra_type = "balanced")
P <- cbind(c(0.15, 0.47), c(0.65, 0.48), c(0.20, 0.05))
declare_ra(prob_unit_each = P)
x <- c(0, 1, 5, 6, 8, 9)
declare_ra(formula = ~ x)
# Name the table the design is built from, and blocks, clusters and the
# formula's variables are its columns rather than whatever the calling
# environment happens to hold.
dat <- data.frame(bl = rep(c("a", "b"), each = 3), x = c(0, 1, 5, 6, 8, 9),
p = c(0.2, 0.4, 0.5, 0.5, 0.6, 0.8))
declare_ra(blocks = bl, data = dat)
declare_ra(formula = ~ x, data = dat)
declare_ra(prob_unit = p, ra_type = "balanced", data = dat)
Run the code above in your browser using DataLab