Re: [R] How do I modify uniroot function to return .0001 if error ?

From: Hans W Borchers <>
Date: Mon, 04 Apr 2011 03:25:10 +0000

eric <ericstrom <at>> writes:

> I am calling the uniroot function from inside another function using these
> lines (last two lines of the function) :
> d <- uniroot(k, c(.001, 250), tol=.05)
> return(d$root)
> The problem is that on occasion there's a problem with the values I'm
> passing to uniroot. In those instances uniroot stops and sends a message
> that it can't calculate the root because f.upper * f.lower is greater than
> zero. All I'd like to do in those cases is be able to set the return value
> of my calling function "return(d$root)" to .0001. But I'm not sure how to
> pull that off. I tried a few modifications to uniroot but so far no luck.

Do not modify uniroot(). Use 'try' or 'tryCatch', for example

    	e <- try( d <- uniroot(k, c(.001, 250), tol=.05), silent = TRUE )
	if (class(e) == "try-error") {
	} else {

--Hans Werner mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 04 Apr 2011 - 03:29:06 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 Mon 04 Apr 2011 - 04:00:26 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive