[R] catching errors in a loop
Spencer Graves
spencer.graves at pdf.com
Sun Jul 31 03:27:31 CEST 2005
Unfortunately, I don't know how to get any more information from nls.
My approach to this kind of problem is to write my own function to
compute the sum of squares and then use "optim(..., hessian=TRUE)".
This is less likely to choke, because "optim" will continue with a
singular hessian, while "nls" may not. As I recall, when "optim"
stopped without giving me an answer, the function I asked "optim" to
either stopped itself or returned NAs to "optim". Often a little work
with something like Taylor's theorem produced a decent asymptotic
approximation for numbers close to the singularities, and I was able to
get through those cases. Sometimes, the hessian would be singular, but
"optim" still gave an answer. In that case, I knew that the optimum was
not unique.
Alternatively, you could continue with 'try' in the loop you
described and then explore later the cases where "nls" bombed.
If you still have time for this and want to try more, I encourage you
to try something else like the above and send us another question if you
get stuck again. Also I encourage you to read the posting guide!
"http://www.R-project.org/posting-guide.html", especially the part about
submitting a simple, self-contained example showing what you tried and
why it didn't do what you wanted. If someone can copy a few lines from
your email into R and try something different, it will typically
increase the chances that you will get a useful reply.
spencer graves
Anders Bjørgesæter wrote:
> Hello
>
> I can't figure out how to handle errors in R. I have a loop, e.g.
>
> for (i in 2:n) {
> .
> fit <- nls(model), start=list…
> if any type of error occur write i to a text file
> .
> }
>
> I putted “try” around the nls-expression and this let me run through the
> loop without R stopping (which I want because each loop takes some time so
> I do not want it to stop), but I also want to capture the variable when an
> error occur.
>
> Appreciate any help
>
> /Anders
> - - - - - - - - - - - - - - - - - - -
> I tried to use:
> **“options(error=write(variable.names(matrix[i]),
> file="..\\error.txt",append = TRUE))”, hoping this made R write to the text
> file every time an error occurred (but this made R write all i’s in the
> loop to the text file).
> **tryCatch(<- nls(model), start=list…), finally =write(…) also writes to a
> text file but not necessary when there is an error.
> **“if (Parameter>x errorM=9 else errorM =0” works but I want to capture any
> type of error.
> - - - - - - - - - - - - - - - - -
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
--
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA
spencer.graves at pdf.com
www.pdf.com <http://www.pdf.com>
Tel: 408-938-4420
Fax: 408-280-7915
More information about the R-help
mailing list