Declare Data Strategy: Assignment

declare_assignment(..., handler = assignment_handler, label = NULL)

assignment_handler(data, ..., legacy = FALSE)

Arguments

...

arguments to be captured, and later passed to the handler

handler

a tidy-in, tidy-out function

label

a string describing the step

data

A data.frame.

legacy

Use the legacy randomizr functionality. This will be disabled in future; please use legacy = FALSE.

Value

A function that takes a data.frame as an argument and returns a data.frame with assignment columns appended.

Examples


# setting up a design stub
design <- declare_model(
  classrooms = add_level(10),
  individuals = add_level(20, female = rbinom(N, 1, 0.5))
) + NULL

# Declare assignment of m units to treatment
design + declare_assignment(Z = complete_ra(N = N, m = 100))
#> 
#> Design Summary
#> 
#> Step 1 (model): declare_model(classrooms = add_level(10), individuals = add_level(20,     female = rbinom(N, 1, 0.5))) 
#> 
#> N = 200 
#> 
#> Added variable: classrooms 
#>  N_missing N_unique     class
#>          0       10 character
#> 
#> Added variable: individuals 
#>  N_missing N_unique     class
#>          0      200 character
#> 
#> Added variable: female 
#>     0    1
#>    98  102
#>  0.49 0.51
#> 
#> Step 2 (assignment): declare_assignment(Z = complete_ra(N = N, m = 100)) -------
#> 
#> Added variable: Z 
#>     0    1
#>   100  100
#>  0.50 0.50
#> 

# Declare assignment specifying varying block probabilities
design + 
  declare_assignment(Z = block_ra(blocks = female, 
                                  block_prob = c(1/3, 2/3)))
#> 
#> Design Summary
#> 
#> Step 1 (model): declare_model(classrooms = add_level(10), individuals = add_level(20,     female = rbinom(N, 1, 0.5))) 
#> 
#> N = 200 
#> 
#> Added variable: classrooms 
#>  N_missing N_unique     class
#>          0       10 character
#> 
#> Added variable: individuals 
#>  N_missing N_unique     class
#>          0      200 character
#> 
#> Added variable: female 
#>     0    1
#>   104   96
#>  0.52 0.48
#> 
#> Step 2 (assignment): declare_assignment(Z = block_ra(blocks = female, block_prob = c(1/3,     2/3))) 
#> 
#> Added variable: Z 
#>     0    1
#>   101   99
#>  0.51 0.49
#> 

# Declare assignment of clusters with probability 1/4
design + declare_assignment(
  Z = cluster_ra(prob = 1/4, clusters = classrooms))
#> 
#> Design Summary
#> 
#> Step 1 (model): declare_model(classrooms = add_level(10), individuals = add_level(20,     female = rbinom(N, 1, 0.5))) 
#> 
#> N = 200 
#> 
#> Added variable: classrooms 
#>  N_missing N_unique     class
#>          0       10 character
#> 
#> Added variable: individuals 
#>  N_missing N_unique     class
#>          0      200 character
#> 
#> Added variable: female 
#>     0    1
#>    85  115
#>  0.42 0.57
#> 
#> Step 2 (assignment): declare_assignment(Z = cluster_ra(prob = 1/4, clusters = classrooms)) 
#> 
#> Added variable: Z 
#>     0    1
#>   140   60
#>  0.70 0.30
#> 
 
# Declare factorial assignment (Approach 1): 
#   Use complete random assignment to assign Z1 
#   then block on Z1 to assign Z2. 
design + 
   declare_assignment(Z1 = complete_ra(N = N, m = 100),
                      Z2 = block_ra(blocks = Z1))
#> 
#> Design Summary
#> 
#> Step 1 (model): declare_model(classrooms = add_level(10), individuals = add_level(20,     female = rbinom(N, 1, 0.5))) 
#> 
#> N = 200 
#> 
#> Added variable: classrooms 
#>  N_missing N_unique     class
#>          0       10 character
#> 
#> Added variable: individuals 
#>  N_missing N_unique     class
#>          0      200 character
#> 
#> Added variable: female 
#>     0    1
#>    99  101
#>  0.49 0.51
#> 
#> Step 2 (assignment): declare_assignment(Z1 = complete_ra(N = N, m = 100), Z2 = block_ra(blocks = Z1)) 
#> 
#> Added variable: Z1 
#>     0    1
#>   100  100
#>  0.50 0.50
#> 
#> Added variable: Z2 
#>     0    1
#>   100  100
#>  0.50 0.50
#> 
   
# Declare factorial assignment (Approach 2): 
#   Assign to four conditions and then split into Z1 and Z2 
design +  
  declare_assignment(Z = complete_ra(N = N, conditions = 1:4),
                     Z1 = as.numeric(Z %in% 2:3), 
                     Z2 = as.numeric(Z %in% 3:4))
#> 
#> Design Summary
#> 
#> Step 1 (model): declare_model(classrooms = add_level(10), individuals = add_level(20,     female = rbinom(N, 1, 0.5))) 
#> 
#> N = 200 
#> 
#> Added variable: classrooms 
#>  N_missing N_unique     class
#>          0       10 character
#> 
#> Added variable: individuals 
#>  N_missing N_unique     class
#>          0      200 character
#> 
#> Added variable: female 
#>     0    1
#>    96  104
#>  0.48 0.52
#> 
#> Step 2 (assignment): declare_assignment(Z = complete_ra(N = N, conditions = 1:4),     Z1 = as.numeric(Z %in% 2:3), Z2 = as.numeric(Z %in% 3:4)) 
#> 
#> Added variable: Z 
#>     1    2    3    4
#>    50   50   50   50
#>  0.25 0.25 0.25 0.25
#> 
#> Added variable: Z1 
#>     0    1
#>   100  100
#>  0.50 0.50
#> 
#> Added variable: Z2 
#>     0    1
#>   100  100
#>  0.50 0.50
#> 
   
# Declare assignment using functions outside randomizr package:
design + 
  declare_assignment(Z = rbinom(n = N, size = 1, prob = 0.35))
#> 
#> Design Summary
#> 
#> Step 1 (model): declare_model(classrooms = add_level(10), individuals = add_level(20,     female = rbinom(N, 1, 0.5))) 
#> 
#> N = 200 
#> 
#> Added variable: classrooms 
#>  N_missing N_unique     class
#>          0       10 character
#> 
#> Added variable: individuals 
#>  N_missing N_unique     class
#>          0      200 character
#> 
#> Added variable: female 
#>     0    1
#>    98  102
#>  0.49 0.51
#> 
#> Step 2 (assignment): declare_assignment(Z = rbinom(n = N, size = 1, prob = 0.35)) 
#> 
#> Added variable: Z 
#>     0    1
#>   134   66
#>  0.67 0.33
#>