1

Possible Duplicate:
Exception handling in R

I use glm.nb function to fit an NB model, but sometimes the program gives an error:

Error in while ((it <- it + 1) < limit && abs(del) > eps) { : missing value where TRUE/FALSE needed

The whole program then breaks down. Now I write another function that can address such convergence issue (it's due to extremely large likelihood value; see this post).

My question is: how can I tell R to switch to my own function when the glm.nb function breaks down?

I was told to look for the error message in the source code of glm.nb and then modify that part. However, I didn't find such codes explicitly, and wonder if there is an easier way in R to jump out of a breaking-down function and switching to another function by some if-else statements?

Community
  • 1
  • 1
alittleboy
  • 10,616
  • 23
  • 67
  • 107
  • 1
    I suggest this link: http://stackoverflow.com/questions/2622777/exception-handling-in-r –  Nov 02 '12 at 04:03
  • @xin guo: thanks! I also find this link very helpful: http://stackoverflow.com/questions/3440373/functions-and-try-in-r?rq=1 – alittleboy Nov 02 '12 at 06:26

1 Answers1

0

Two posts answer this question (thanks you @xin guo for pointing out the right direction):

catching an error and then branching logic

Functions and try() in R

On my experiments, I notice the differences when you use warning = function(w) and error = function(e) in tryCatch(). If there is only warning, then the error part won't be recognized, and vice versa. Make sure you get the correct message from R, and use corresponding argument in tryCatch() :)

Community
  • 1
  • 1
alittleboy
  • 10,616
  • 23
  • 67
  • 107