| assign_treatment | R Documentation |
Randomizes the rows of data to treatment groups, independently within each
combination of the strata variables. Balanced assignment allocates the
group sizes a stratum is due, rounding the shares to whole units and giving
the remainder to the groups with the largest fractional part, then permutes
them; unbalanced assignment draws each row independently with probabilities
proportional to ratios.
assign_treatment(
data,
groups = 2L,
balanced = TRUE,
strata = NULL,
ratios = NULL,
name = "treatment",
seed = NULL
)
data |
Base |
groups |
Number of treatment groups, as a single whole number of at
least 2 (defaulting to |
balanced |
Use exact allocation within each stratum. A single flag,
defaulting to |
strata |
Optional character vector of stratification variables, all
present in |
ratios |
Relative allocation ratios, a positive numeric vector with one
value per group. Defaults to |
name |
Name of the treatment variable, which must not already exist in
|
seed |
Optional random seed. A single number, or |
A simulab_sim base data.frame with the columns of data followed
by the treatment variable named by name, one row per input row. The
component allocation, reached with
as.data.frame(x, what = "allocation"), holds one row per stratum and
group, with columns stratum, treatment and observations.
data <- data.frame(id = 1:100, site = rep(c("north", "south"), each = 50))
result <- assign_treatment(data, groups = 2, strata = "site", seed = 1)
head(result)
as.data.frame(result, what = "allocation")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.