randomizr is a small r package that contains easy functions for common forms of random assignment: simple, complete, blocked, and clustered. Version 0.3.0 is now available on CRAN. (Development version available on github.) Execute the following code to install it. Please email me with questions, bugs, or requests.
install.packages("devtools") devtools::install_github("acoppock/randomizr") library(randomizr)
What’s new in Version 0.3.0:
The biggest change in version 0.3.0 is the declare_ra() functionality. The declare_ra function is used in three ways:
- To obtain some basic facts about a randomization:
declaration <- declare_ra(N=100, m_each=c(30, 30, 40)) declaration
- To conduct a random assignment:
Z <- conduct_ra(declaration) table(Z)
- To obtain observed condition probabilities
probs <- obtain_condition_probabilities(declaration, Z) table(probs, Z)
Complete random assignment. This is the workhorse random assignment function in randomizr. complete_ra() assigns a specific number of units to each treatment condition.
Z <- complete_ra(N=100, m=50) table(Z) Z <- complete_ra(N=100, m_each=c(30, 30, 40), condition_names=c("control", "placebo", "treatment")) table(Z)
Block random assignment. This function takes a vector that is of length N and indicates which block each unit belongs to. It performs complete random assignment within each block. There are a couple of ways to specify how many units in each block should be assigned to each treatment condition.
block_var <- rep(c("A", "B","C"), times=c(50, 100, 200)) Z <- block_ra(block_var=block_var) table(block_var, Z) block_m <- rbind(c(10, 20, 20), c(30, 50, 20), c(50, 75, 75)) Z <- block_ra(block_var=block_var, block_m=block_m ) table(block_var, Z) Z <- block_ra(block_var=block_var, block_prob=c(.1, .1, .8)) table(block_var, Z)
Clustered random assignment. If your experiment needs to assign groups of units together (students in classrooms, voters in media markets, citizens in towns), you have to use clustered random assignment. Typically, clustered designs yield larger standard errors than the equivalently sized experiment randomized at the individual level.
clust_var <- rep(letters, times=1:26) Z <- cluster_ra(clust_var=clust_var) table(Z, clust_var) Z <- cluster_ra(clust_var=clust_var, m_each=c(7, 7, 12)) table(Z, clust_var)