Declare sampling procedure

declare_sampling(..., handler = sampling_handler, label = NULL)

sampling_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 sampling declaration, which is a function that takes a data.frame as an argument and returns a data.frame subsetted to sampled observations and (optionally) augmented with inclusion probabilities and other quantities.

Examples


design <- declare_model(

  classrooms = add_level(10),
  individuals = add_level(20, female = rbinom(N, 1, 0.5))

) + NULL

# Complete random sampling

design + declare_sampling(
  S = complete_rs(N = N, n = 50), 
  filter = S == 1)
#> 
#> 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
#>   100  100
#>  0.50 0.50
#> 
#> Step 2 (sampling): declare_sampling(S = complete_rs(N = N, n = 50), filter = S ==     1) 
#> 
#> N = 50 (150 subtracted) 
#> 
#> Added variable: S 
#>     1
#>    50
#>  1.00
#> 
#> Altered variable: classrooms 
#>   Before: 
#>  N_missing N_unique     class
#>          0       10 character
#> 
#>   After:
#>  N_missing N_unique     class
#>          0       10 character
#> 
#> Altered variable: individuals 
#>   Before: 
#>  N_missing N_unique     class
#>          0      200 character
#> 
#>   After:
#>  N_missing N_unique     class
#>          0       50 character
#> 
#> Altered variable: female 
#>   Before: 
#>     0    1
#>   100  100
#>  0.50 0.50
#> 
#>   After:
#>     0    1
#>    21   29
#>  0.42 0.58
#> 

# equivalently, by default filter is set to S == 1
design + declare_sampling(S = complete_rs(N = N, n = 50), 
                          legacy = FALSE)
#> 
#> 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
#>   102   98
#>  0.51 0.49
#> 
#> Step 2 (sampling): declare_sampling(S = complete_rs(N = N, n = 50), legacy = FALSE) 
#> 
#> N = 50 (150 subtracted) 
#> 
#> Added variable: S 
#>     1
#>    50
#>  1.00
#> 
#> Altered variable: classrooms 
#>   Before: 
#>  N_missing N_unique     class
#>          0       10 character
#> 
#>   After:
#>  N_missing N_unique     class
#>          0       10 character
#> 
#> Altered variable: individuals 
#>   Before: 
#>  N_missing N_unique     class
#>          0      200 character
#> 
#>   After:
#>  N_missing N_unique     class
#>          0       50 character
#> 
#> Altered variable: female 
#>   Before: 
#>     0    1
#>   102   98
#>  0.51 0.49
#> 
#>   After:
#>     0    1
#>    25   25
#>  0.50 0.50
#> 

# Stratified random sampling

design + declare_sampling(S = strata_rs(strata = female), 
                          legacy = FALSE)
#> 
#> 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
#>   106   94
#>  0.53 0.47
#> 
#> Step 2 (sampling): declare_sampling(S = strata_rs(strata = female), legacy = FALSE) 
#> 
#> N = 100 (100 subtracted) 
#> 
#> Added variable: S 
#>     1
#>   100
#>  1.00
#> 
#> Altered variable: classrooms 
#>   Before: 
#>  N_missing N_unique     class
#>          0       10 character
#> 
#>   After:
#>  N_missing N_unique     class
#>          0       10 character
#> 
#> Altered variable: individuals 
#>   Before: 
#>  N_missing N_unique     class
#>          0      200 character
#> 
#>   After:
#>  N_missing N_unique     class
#>          0      100 character
#> 
#> Altered variable: female 
#>   Before: 
#>     0    1
#>   106   94
#>  0.53 0.47
#> 
#>   After:
#>     0    1
#>    53   47
#>  0.53 0.47
#>