Declare Assignment Procedure

declare_assignment(..., assignment_function = assignment_function_default)

Arguments

...

Arguments to the assignment function.

assignment_function

A function that takes a data.frame, adds an assignment variable and optionally assignment probabilities or other relevant quantities, and returns a data.frame. By default, the assignment_function uses the randomizr functions conduct_ra and obtain_condition_probabilities to conduct random assignment and obtain the probabilities of assignment to each condition.

Value

a function that takes a data.frame as an argument and returns a data.frame with additional columns appended including an assignment variable and (optionally) probabilities of assignment.

Details

While declare_assignment can work with any assignment_function that takes data and returns data, most random assignment procedures can be easily implemented with randomizr. The arguments to conduct_ra can include N, block_var, clust_var, m, m_each, prob, prob_each, block_m, block_m_each = NULL, block_prob, block_prob_each, num_arms, and condition_names. The arguments you need to specify are different for different designs. Check the help files for complete_ra, block_ra, cluster_ra, or block_and_cluster_ra for details on how to execute many common designs.

Examples

my_population <- declare_population(N = 100, female = rbinom(N, 1, .5)) df <- my_population() # Complete random assignment using randomizr # use any arguments you would use in conduct_ra. my_assignment <- declare_assignment(m = 50) df <- my_assignment(df) head(df)
#> ID female Z Z_cond_prob #> 1 001 0 1 0.5 #> 2 002 1 1 0.5 #> 3 003 1 0 0.5 #> 4 004 0 0 0.5 #> 5 005 0 0 0.5 #> 6 006 0 1 0.5
table(df$Z)
#> #> 0 1 #> 50 50
# Block random assignment my_blocked_assignment <- declare_assignment(block_var = female) df <- my_population() df <- my_blocked_assignment(df) head(df)
#> ID female Z Z_cond_prob #> 1 001 0 1 0.5 #> 2 002 1 1 0.5 #> 3 003 0 0 0.5 #> 4 004 0 1 0.5 #> 5 005 0 1 0.5 #> 6 006 1 1 0.5
with(df, table(Z, female))
#> female #> Z 0 1 #> 0 22 28 #> 1 22 28
# Custom random assignment functions df <- my_population() my_assignment_function <- function(data) { data$Z <- rbinom(n = nrow(data), size = 1, prob = 0.5) data } my_assignment_custom <- declare_assignment( assignment_function = my_assignment_function) df <- my_assignment_custom(df) head(df)
#> ID female Z #> 1 001 0 0 #> 2 002 1 0 #> 3 003 1 0 #> 4 004 0 1 #> 5 005 0 0 #> 6 006 0 1
table(df$Z)
#> #> 0 1 #> 49 51