[R] tryCatch - Continuing for/next loop after error

From: Costas <risk2009_at_ath.forthnet.gr>
Date: Thu, 10 Mar 2011 10:51:15 +0200

Dear all,

I am not sure I understand fully the functionality of "tryCatch" and "try" commands and how these are used to continue a for/next loop if an error occurs within the loop.

Can somebody point me to material (or share some code) with more extensive examples than the ones in the help/FAQ pages?

Do explain my problem in more detail:

for (i in 100:1000)

      ## do some other stuff



My for/next loop reads in data (increasing the dataset by one point at every loop run) and then estimates a model. When the problem is computationally singular, the loop exits. I want to continue the loop and register an "error" estimation value for that step. However when I use use the try tryCatch(estimatemodel(data)) (where estimatemodel() is a wrapper function calling the model estimation and optimization routines), the problem still persists.

Is this the correct way to use tryCatch (or try) or should these go inside the actual code bits (i.e., in a more low level fashion) that conduct the optimization and model estimation?

Apologies if this is not clear enough.


R-help_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 10 Mar 2011 - 08:54:52 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 10 Mar 2011 - 14:30:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive