lundi 26 juin 2017

Programming a randomization scheme, while loops

I'm trying to simulate a randomization process. I think I'll have to use a while loop, and I'm unfamiliar with how to best structure what I'm trying to accomplish in my R code.

Let's say I have 3 classes, a,b, and c in that I want to be distributed in a 3:2:1 ratio, respectively. A vector containing a minimally 'balanced' set of these classes in this ratio would look something like this:

class_1<-"a"
class_2<-"b"
class_3<-"c"

ratio_a<-3
ratio_b<-2
ratio_c<-1
min_set<-c(rep(class_1,ratio_a),rep(class_2,ratio_b),rep(class_3,ratio_c))

This minimum set would look something like this:

min_set
"a""a""a""b""b""c"

Let's then say I want to have 'k' number of this minimally balanced set, I could create that like this:

block_1<-matrix(0,k,length(min_set))
for (i in 1:k){
block_1[i,]<-min_set
}

This would create a new matrix with my min_setvector for k rows.

Let's now say I want to sample from block_1 without replacement (a treatment allocation would be determined by the class (a,b,c) of the sample) This can be done as:

sample(as.vector(block_1),n,replace=F)

However, here is where I'd like some programming help. I'd like to sample from block_1 without replacement until I have sampled the min_set, i.e."a""a""a""b""b""c". I'd like to store these samples from block_1 in some new, empty vector, called block_2.

At this point, I'd like to 'return' the samples which fulfill the min_set back to block_1, and keep all of my discordant samples (those which exceed the min_set ratio) in block_2. I'd like to repeat this process of collecting samples in block_2 until a min_set is achieved, keeping the discordant samples, and returning the samples which achieve the min_set back to block_1 until I have reached some pre-specified number of treatment allocations.

I hope my question is clear, and that the code I provided can be used as a reproducible sample.

Thanks




Aucun commentaire:

Enregistrer un commentaire