Page 1 of 1

Using the try command for RMark errors

PostPosted: Thu May 05, 2011 12:22 pm
by Snook
This is a followup to this thread: viewtopic.php?f=21&t=1807

I have solved the vast majority of my convergence issues by fixing/not fixing certain parameters, and by setting initial parameter values. However, there are still a few rare known parameter combinations that lead to rare instances of convergence failures. I am hoping to sensor the error message so that my simulation loops do not crash.

So far, I have tried both the 'try' and tryCatch' commands in hopes that I could identify this convergence failure as a 'try-error', and then return this as a 'NA' value in my output matrix. However, I do not think that these commands are recognizing the error message from RMark, and I continue to get the same error message as in the previous thread when using these commands, as opposed to returning a 'try-error' to keep the loop alive. I have used these commands as such:

> w<-try(mark(BARKER.ch,model="Barker",delete=TRUE,model.parameters=list(r=r.0,p=p.0)),TRUE)
Error in if (x4 > x2) { : argument is of length zero


********Following model failed to run : S(~1)p(~1)r(~1)R(~1)R'(~1)F(~1)F'(~1) **********

OR

> w<-tryCatch(mark(BARKER.ch,model="Barker",delete=TRUE,model.parameters=list(r=r.0,p=p.0)),error=function() next)
Error in if (x4 > x2) { : argument is of length zero


********Following model failed to run : S(~1)p(~1)r(~1)R(~1)R'(~1)F(~1)F'(~1) **********

Has anyone had success in silencing these errors? I have also tried setting SILENT=T in the RMark function, but still return this:

"********Following model failed to run : S(~1)p(~1)r(~1)R(~1)R'(~1)F(~1)F'(~1) **********"

Which still crashes the loop

Andrew

Re: Using the try command for RMark errors

PostPosted: Thu May 05, 2011 11:27 pm
by Eldar
I am not sure that it will help you but Jeff uses in his code of mark.wrapper() following lines:
if (run) {
mymodel = try(mark(model.parameters = model.parameters,
initial = initial, ...), silent = silent)
}
...
if (class(mymodel)[1] != "try-error") {
...
I found this example very useful.
Best,
Eldar

Re: Using the try command for RMark errors

PostPosted: Fri May 06, 2011 3:10 pm
by Snook
Hi All,

I found a workaround solution for this issue, for anyone interested.

First, I opened the 'mark' function and changed this section of code:

}
runmodel <- try(run.mark.model(model, invisible = invisible,
adjust = adjust, filename = filename, prefix = prefix,
realvcv = realvcv, delete = delete), silent = silent)
if (class(runmodel)[1] == "try-error") {
#cat("\n\n********Following model failed to run :",
#model$model.name, "**********\n\n")
return("Myerror")
#return(model)
}

Doing this removed the warning message, and instead returned "Myerror". I saved the 'mark' function with edited code as 'mymark.' Then, I ran RMark as such:

BARKER<mymark(BARKER.processed,BARKER.dll,model="Barker",delete=TRUE,
model.parameters=list(F=F.1,FPrime=FPrime.),initial=initial,silent=TRUE)

if(BARKER=="Myerror"){BARKEROutput=matrix(NA)}else{BARKEROutput=as.matrix(BARKER$results$real)}


That way, if the model failed to converge, the IfElse statement would return a NA, and if not, would pull the results for further manipulation.

One question I had is if there was any simple way to use the IfElse statement to tell R to repeat the simulation iteration if "Myerror" occurs, so that I would not have to deal with NA values.