Avoid default abort when error occurs

3 replies [Last post]
mverdam's picture
Offline
Joined: 02/04/2014

I'm trying to run multiple SEM-models within a loop (i.e., for simulation purposes), but am experiencing some problems when a model does not run properly (i.e., objective function returns NaN). The default action is to abort and display an error message, which results in aborting the whole loop.

I am looking for a way to make it possible to make sure the loop will continue when one of the models is not returning a result (and of course, I would also like to save the non-result in some way).

I read about the omx_set_error_handler and omx_fatal_errors functions, but cannot seem to find them or how they might work.

Any suggestion on how to solve this problem is greatly appreciated!

RobK's picture
Offline
Joined: 04/19/2011
If an error is occurring

If an error is occurring while running a model, consider doing something like this:
myFit <- try(mxRun(myModel))
If no error occurs, then myFit is exactly the same as it would be be if you didn't use try(). If an error does occur, myFit will be of class "try-error", and the error message will be displayed to the console, but it will not abort your loop.

You will probably want to condition certain parts of your syntax on whether or not the model runs successfully, such as via if(class(myFit)=="try-error") or if(class(myFit)!="try-error"). For instance, you don't want to try to extract parameter estimates from myFit if running the model failed.

tbates's picture
Offline
Joined: 07/31/2009
?mxRun

If you look at the help for mxRun, you will see options to be silent, to suppressWarnings, and (what you want) to be "unsafe" == ignore errors, among others.

mverdam's picture
Offline
Joined: 02/04/2014
That would indeed solve the

That would indeed solve the problem. Thanks for both suggestions.