[Rd] trace in uniroot() ?

J C Nash profjcn@@h @ending from gm@il@com
Mon Jul 30 20:35:49 CEST 2018

In looking at rootfinding for the histoRicalg project (see gitlab.com/nashjc/histoRicalg),
I thought I would check how uniroot() solves some problems. The following short example

ff <- function(x){ exp(0.5*x) - 2 }
uniroot(ff, 0, 10)
uniroot(ff, c(0, 10), trace=1)
uniroot(ff, c(0, 10), trace=TRUE)

shows that the trace parameter, as described in the Rd file, does not seem to
be functional except in limited situations (and it suggests an
integer, then uses a logical for the example, e.g.,
 ## numerically, f(-|M|) becomes zero :
     u3 <- uniroot(exp, c(0,2), extendInt="yes", trace=TRUE)

When extendInt is set, then there is some information output, but trace alone
produces nothing.

I looked at the source code -- it is in R-3.5.1/src/library/stats/R/nlm.R and
calls zeroin2 code from R-3.5.1/src/library/stats/src/optimize.c as far as I
can determing. My code inspection suggests trace does not show the iterations
of the rootfinding, and only has effect when the search interval is allowed
to be extended. It does not appear that there is any mechanism to ask
the zeroin2 C code to display intermediate work.

This isn't desperately important for me as I wrote an R version of the code in
package rootoned on R-forge (which Martin Maechler adapted as unirootR.R in
Rmpfr so multi-precision roots can be found). My zeroin.R has 'trace' to get
the pattern of different steps. In fact it is a bit excessive. Note
unirootR.R uses 'verbose' rather than 'trace'. However, it would be nice to be
able to see what is going on with uniroot() to verify equivalent operation at
the same precision level. It is very easy for codes to be very slightly
different and give quite widely different output.

Indeed, even without the trace, we see (zeroin from rootoned here)

> zeroin(ff, c(0, 10), trace=FALSE)
[1] 1.386294

[1] -5.658169e-10

[1] 7.450581e-09

[1] 9

> uniroot(ff, c(0, 10), trace=FALSE)
[1] 1.38629

[1] -4.66072e-06

[1] 10

[1] NA

[1] 6.103516e-05


Is the lack of trace a bug, or at least an oversight? Being able to follow iterations is a
classic approach to checking that computations are proceeding as they should.

Best, JN

More information about the R-devel mailing list