-2

I am trying write a function that generates simulated data but if the simulated data does not meet the condition, I need to skip it and if it does meet the condition, then I will apply the function summary.

I would like to loop it until I find 10 valid datasets and then stop. (I actually have to do this until it reaches 10000). Here is the code. The code sort of works except it does not stop. I think I probably placed the next and break function in the wrong place. I hope someone could help me write this together.

Another way I could approach this is to generate all the valid data first and then apply the function find_MLE (summary) to the final list.

Edit: I put break inside repeat. I edit the code to make it reproducible. Still the code keeps generating data and does not break.

here is a reproducible version

      validData <-  function(GM, GSD,sampleSize, p) {

            count=0
            for (i in 1:n) {  
              repeat {
                lod <- quantile(rlnorm(1000000, log(GM), log(GSD)), p = p)
                X_before <- rlnorm(sampleSize, log(GM), log(GSD))
                Xs <-  ifelse(X_before <= lod, lod,  X_before)
                delta <- ifelse(X_before <= lod, 1,  0) 
                pct_cens <- sum(delta)/length(delta)
                print(pct_cens)
                if (pct_cens == 0 & pct_cens ==1) next

                else {

                  sumStats <- summary(Xs)

                  Med <- sumStats[3]
                  Ave <- sumStats[4]

                      }

                  count<- count+1

                if (count == 10) break
              }}
            return(c(pct_cens, Med, Ave))
          }



          validData(GM=1,GSD=2,sampleSize=10,p=0.1)            
Amateur
  • 1,247
  • 6
  • 20
  • 30
  • I do not see any `break` inside your infinite `repeat` loop (there is one outside). And any code after `return` would not be executed. – Vincent Zoonekynd Mar 24 '12 at 03:17
  • @ Vincent. I put break inside now but code still not work. – Amateur Mar 24 '12 at 03:38
  • 2
    `break` only leaves the current (innermost) loop. If you put the `return` statement where the `break` currently is, it should work. – Vincent Zoonekynd Mar 24 '12 at 05:59
  • Not sure that's right @VincentZoonekynd. Try the following `for(i in 1:3){ for(j in 1:3){ if(j == 2) break; print(paste(i,j)) } }` and you'll get `[1] "1 1" [1] "2 1" [1] "3 1"`, which means the outer loop has been broken too. `next` is needed for that. – geotheory May 22 '14 at 14:28

3 Answers3

2

Thanks for your help. I was able to write a function without using break function! I posted it here in case other people might find it helpful.

    dset <- function (GM, GSD, n, p) {

          Mean <- array()
          Median <- array()

          count = 0
          while(count < 10) { 

            lod <- quantile(rlnorm(1000000, log(GM), log(GSD)), p = p)
            X_before <- rlnorm(n, log(GM), log(GSD))
            Xs <-  ifelse(X_before <= lod, lod,  X_before)
            delta <- ifelse(X_before <= lod, 1,  0) 
            pct_cens <- sum(delta)/length(delta)
            print(pct_cens)
            if (pct_cens == 0 | pct_cens == 1 ) next
            else {count <-  count +1
                  if (pct_cens > 0 & pct_cens < 1) {
                    sumStats <- summary(Xs)
                    Median[count] <- sumStats[3]
                    Mean [count]<- sumStats[4]

                    print(list(pct_cens=pct_cens,Xs=Xs, delta=delta, Median=Median,Mean=Mean))
          }
            }
          }

          return(data.frame( Mean=Mean, Median=Median)) }
Amateur
  • 1,247
  • 6
  • 20
  • 30
0

To skip to the outer loop based on a condition, simply use break()

Here's a simple example where the inner loop will try to run 10 times, but a condition will usually be met which prevents it

# OUTER LOOP
for(i in 1:2) {

  print(paste("Outer loop iteration", i))

  # INNER LOOP (will run max 10 times)
  for(j in 1:10) {

    print(paste("Inner loop iteration", j))

    if (runif(1) > 0.4) { # Randomly break the inner loop
      print(paste("Breaking inner loop", j))
      break()

    }
  }
}

If you want to skip to the outer loop when there's an error (rather than based on a condition), see here

stevec
  • 41,291
  • 27
  • 223
  • 311
0

Since your code isn't replicable, I cannot fully test and debug your code, but here is what I think it would look like without being able to replicate with an MLE function. This is roughly how I would set it up. But check out the documentation/Google on break, next, for/while loops related to R when testing your code.

validData <-  function(GM, GSD,Size, p) {

for (i in 1:20) {  
 count <- 1  
 repeat {   
    lod <- quantile(rlnorm(1000000, log(GM), log(GSD)), p = p)
    X_before <- rlnorm(Size, log(GM), log(GSD))
    Xs <-  ifelse(X_before <= lod, lod,  X_before)
    delta <- ifelse(X_before <= lod, 1,  0) 
    pct_cens <- sum(delta)/length(delta)


    if (pct_cens == 0 & pct_cens ==1) 
    function() #your foo goes here

    else {
      mles <- find_MLE(c(0,0), Xs, delta)
      GM_est <- mles[1]
      GSD_est <- mles[2] 
      AM_est <- exp(log(GM_est) + 1 )
      SD_est<- sqrt((AM_est)^2*exp(log(GSD_est)^2))
      D95th_est <- GM_est*(GSD_est^1.645)
    } }

      return(c(GM_est,GSD_est,AM_est,SD_est,D95th_est))

    count<- count+1   

    if (count == 10) break

  }}
Captain Murphy
  • 855
  • 3
  • 15
  • 23
  • `count <- 1` should be outside the repeat. – huon Mar 24 '12 at 03:20
  • @ Captain Murphy. Thank you for taking a stab at it. I read about next and break and got up to that point. At the line: if (pct_cens == 0 & pct_cens ==1) I just want it to skip it but not apply any function. – Amateur Mar 24 '12 at 03:23