This function repeatedly randomizes treatment
assignment (using .randomizer
) until
baseline equivalency is achieved across specified
variables, as measured by the checkBaseline
function from
the im
package. It can optionally stratify the randomization by
specified groups.
randomize(
data,
variables,
standard = "Not Concerned",
seed = NULL,
max_attempts = 100,
pr_treated = 0.5,
group_by = NULL
)
A new data frame with the added "treated" column, if baseline equivalency is achieved within the specified number of attempts. Otherwise, an error is thrown.
The input data frame containing pre-intervention variables.
A vector of the names of the pre-intervention variables to check for baseline equivalency.
The desired level of baseline equivalence. Must be one of "Not Concerned", "Concerned", or "Very Concerned". Default is "Not Concerned". ("Not Concerned", "Concerned", or "Very Concerned"). Must be one of "Not Concerned", "Concerned", or "Very Concerned".
(Optional) An integer to set the random seed for reproducibility of the initial randomization attempt. Subsequent attempts will use new random seeds.
The maximum number of randomization attempts to make before stopping and returning an error.
(Optional) The probability of a row being assigned to the treatment group (TRUE). Default is 0.5.
(Optional) A character vector of column names to stratify the randomization. If provided, the randomization will be done within each group defined by the specified columns.