Skip to content
Prev 333592 / 398506 Next

Randomize two categories testing a specific condition R version 3.0.2 windows 32 bit

It is not clear to me what your exact boundary conditions are for changing probabilities, or even what your real goal is, but maybe something like the following will get you started.  Stage=1 is before there are greater than 2  'a's and 2 'b's. 

set.seed(3724)
x <- character(0)
p <- .5
stage <- 1
for(i in 1:20){
  x <- c(x, sample(c('a','b'),1,prob=c(p,1-p)))
  suma <- sum(x=='a')
  sumb <- sum(x=='b') 
  #check if first condition is met
  if(suma > 2 & sumb > 2) {
    #select a starting value for changed p value and do this only once
    if(stage == 1) {stage <- 2; if(suma >= sumb) p <-.8 else p <- .2}
    #check for excess of 'a's or 'b's    if(suma - sumb > 2) p <- .2
    if(sumb - suma > 2) p <- .8
    }
}      

If this doesn't do what you want, then write back to R-help describing what your ultimate goal is (i.e., what this method of randomization is supposed to accomplish), and someone may be able to give you better advice.

Hope this is helpful,

Dan 

Daniel Nordlund
Bothell, WA USA