vendredi 5 février 2016

If Else Inside For Loop in R

I need to create a if/else statement in the middle of a for loop in R.

I am trying to create a hypothetical phase 1 clinical trial with 3-3 design in R. I start with sample1 ~ binomial(3, x/10), with x starting at 1. If the sample1 = 0, increase x by 1 and take a sample again. When sample1 > 0, take a second sample, sample2 ~ binomial(3, x/10), at the same x value. If sample2=0, increase x, and take sample1 again. If sample2>0, print that x value. I want to do this 10 times and see what the x value for is. Below is my code.

reps <- 10
MTD <- numeric(reps)
for (i in 1:reps) {
  x <- 1
  sample1 <- rbinom(1, 3, x/10)
  if (sample1>0) {sample2 <- rbinom(1,3, x/10)
  if (sample2>0) {MTD[i] <- (x)} else x<- x+1}
  else x <-x+1
  }
MTD

Looking at the results, all I get are zeros so something is not right. Could someone please assist. Thanks.

Aucun commentaire:

Enregistrer un commentaire