1

code:

for(j in 1:100){......     
for(k in 1:20) {
        fit.mod = fit(mod)
        if(AIC(fit.mod) < best) { best_model = fit.mod 
        best = AIC(fit.mod)
        } 
      fit.mod <- best_model
........}

when the error

Error in fb(init = init, A = trDens, B = dens, ntimes = ntimes(object),  : 
  NA/NaN/Inf in foreign function call (arg 10)

occurs, the loop automatically stops. I tried

if(is.na(fit.mod)){next}

and

if(!identical(fit.mod@message,c("Log likelihood converged to within tol. (relative change)"))){next}

But the loop still stops. Is there a method where I can ignore this error and continue the loop? Thanks in advance!

IRTFM
  • 258,963
  • 21
  • 364
  • 487
Chp
  • 105
  • 7
  • 1
    Use the `try` function. There should be worked examples on SO already. https://stackoverflow.com/search?q=%5Br%5D+error+try+catch – IRTFM Jan 29 '20 at 17:22
  • Thanks a lot. I realize that this question has nothing to do with HMM – Chp Jan 29 '20 at 17:32
  • 1
    I edited your tags to make them more on topic. – IRTFM Jan 29 '20 at 17:34
  • Thx. The answer is: if(inherits(try(fit(mod)),"try-error")){next} – Chp Jan 29 '20 at 17:43
  • 1
    Looks ok. You might want to have an `else{.}` in there as well. – IRTFM Jan 29 '20 at 17:46
  • Oops..the method "inherits"can detect error but the loop still stops. The right answer is here"https://stackoverflow.com/questions/14748557/skipping-error-in-for-loop Thank for your help! – Chp Jan 29 '20 at 18:04
  • Generally one would attempt to assign the results of a fit-operation to a value so that it can then be examined when there is no error. – IRTFM Jan 29 '20 at 18:10

0 Answers0