mardi 5 décembre 2023

Equal group randomization in groups already assigned treatment

I have a list of 80 subjects already randomized into 4 groups (1-4). Now, I want to introduce a crossover design so each subject will eventually be in all 4 groups. Tried to do this with the sample function, but I need an equal distribution of each possibility of the 3 remaining groups to be randomized.

For example: Subject A was already randomized to group 1. Now I need to randomize the remaining 3 groups (2, 3, 4) There are 6 options to randomize the 3 remaining groups: 234 243 324 342 423 432

So I need to ensure that every subject who was initially assigned to group 1 (20 subjects) gets an even distribution of one of those 6 permutations above. The same for every subject initially assigned to group 2, 3 and 4

I tried

#for loop going through each row of the dataframe
for(x in 1:nrow(crossover)) {
  #TRIAL 2
  #randomly selects an integer between 1 and 4

 y<- sample(1:4, 1, replace = TRUE)
  
  #if it equals Assigned.Arm.Index, then randomly select again
 while (y == crossover$Assigned.Arm.Index[x]){

    y <- sample(1:4, 1, replace = FALSE)
  }
  
  #set New.Arm.Index column to y
  crossover$Trial_2[x] <- y

crossover is the name of my dataframe, assigned.arm.index is the premade initial randomization. I created 3 new columns for the remaining randomization I'm trying to fill.

When i asked for help regarding equal randomization across all subjects, I was told to use the rep function, something like:

sample(rep(1:6, 4) , rep= FALSE) 

but it's not fully making sense to me. Im new to R and appreciate any help!




Aucun commentaire:

Enregistrer un commentaire