Skip to content
Prev 313569 / 398502 Next

Running MCMC in R

And if by "stuck" you mean "taking too long a time" you can generate an error at a given
time limit by using setTimeLimit() and tryCatch() or try() can catch that error. E.g.

   > timeOut <- function (expr, cpu = Inf, elapsed = Inf) 
   {
       setTimeLimit(cpu = cpu, elapsed = elapsed, transient = TRUE)
       on.exit(setTimeLimit()) # should not be needed, but averts future error message
       expr
   }
   > timeOut({s<-0 ; for(i in 1:1e7)s <- s + 1/i ; s}, elapsed=1)
   Error: reached elapsed time limit
   > timeOut({s<-0 ; for(i in 1:1e7)s <- s + 1/i ; s}, elapsed=10) # log(1e7) + gamma
   [1] 16.69531
   > tryCatch(timeOut({s<-0 ; for(i in 1:1e7)s <- s + 1/i ; s}, elapsed=1),
   +          error = function(e) NA_real_)
   [1] NA

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com