declare_assignment | R Documentation |
Declare Data Strategy: Assignment
declare_assignment(..., handler = assignment_handler, label = NULL)
assignment_handler(data, ..., legacy = FALSE)
... |
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. |
A function that takes a data.frame as an argument and returns a data.frame with assignment columns appended.
# declare_assignment in use
## Two-arm randomized experiment
design <-
declare_model(
N = 500,
X = rep(c(0, 1), each = N / 2),
U = rnorm(N, sd = 0.25),
potential_outcomes(Y ~ 0.2 * Z + X + U)
) +
declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0)) +
declare_sampling(S = complete_rs(N = N, n = 200)) +
declare_assignment(Z = complete_ra(N = N, m = 100)) +
declare_measurement(Y = reveal_outcomes(Y ~ Z)) +
declare_estimator(Y ~ Z, inquiry = "ATE")
run_design(design)
# Set up population to assign
model <- declare_model(
villages = add_level(
N = 30,
N_households = sample(c(50:100), N, replace = TRUE)
),
households = add_level(
N = N_households,
N_members = sample(c(1, 2, 3, 4), N,
prob = c(0.2, 0.3, 0.25, 0.25), replace = TRUE)
),
individuals = add_level(
N = N_members,
age = sample(18:90, N, replace = TRUE),
gender = rbinom(n = N, size = 1, prob = .5)
)
)
# Assignment procedures
## Complete random assignment
design <-
model +
declare_assignment(Z = complete_ra(N = N, m = 1000))
head(draw_data(design))
## Cluster random assignment
design <-
model +
declare_assignment(Z = cluster_ra(clusters = villages,
n = 15))
head(draw_data(design))
## Block and cluster random assignment
design <-
model +
declare_assignment(Z = block_and_cluster_ra(
blocks = villages,
clusters = households,
block_m = rep(20, 30)
))
head(draw_data(design))
## Block random assignment
design <-
model +
declare_assignment(Z = block_ra(blocks = gender, m = 100))
head(draw_data(design))
## Block random assignment using probabilities
design <-
model +
declare_assignment(Z = block_ra(blocks = gender,
block_prob = c(1 / 3, 2 / 3)))
head(draw_data(design))
## Factorial assignment
design <-
model +
declare_assignment(Z1 = complete_ra(N = N, m = 100),
Z2 = block_ra(blocks = Z1))
head(draw_data(design))
## Assignment using functions outside of randomizr
design <-
model +
declare_assignment(Z = rbinom(n = N, size = 1, prob = 0.35))
head(draw_data(design))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.