This function assigns each patient to a treatment arm (e.g., control or treatment) according to specified allocation ratios.
Arguments
- data
A data frame of patients (e.g., output of
simulate_patients).- arm_names
Character vector of arm names. Default is c("Control","Treatment") for a two-arm trial.
- ratio
Numeric vector of allocation ratios for each arm (same length as
arm_names). Default is c(1,1) for equal allocation.
Value
The input data frame with an added factor column Arm indicating the treatment group for each patient.
Details
Allocation is done by simple random sampling. The ratio vector is normalized to probabilities. If an arm receives no patients by chance (especially when sample size is small), the Arm factor will still include that level (to preserve all arm levels).
Examples
df <- simulate_patients(10)
# Allocate patients equally to "Placebo" vs "Drug" arms
df <- simulate_allocation(df, arm_names = c("Placebo","Drug"), ratio = c(1,1))
table(df$Arm)
#>
#> Placebo Drug
#> 6 4