uniroot.integer {ssanv} | R Documentation |
Let f be a monotonic function that changes sign within the interval specified.
If pos.side
=TRUE (or FALSE) then uniroot.integer
finds the integer i such that
f(i) is closest to the sign change and is positive (or negative).
uniroot.integer(f, interval, lower = min(interval), upper = max(interval), step.power = 6, step.up = TRUE, pos.side = FALSE, print.steps = FALSE, maxiter = 1000, ...)
f |
function for which a root is needed |
interval |
an interval giving minimum and maximum allowable values for root |
lower |
minimum allowable root |
upper |
maximum allowable root |
step.power |
initial step size is 2^step.power |
step.up |
if TRUE steps up from 'lower', if FALSE steps down from 'upper' |
pos.side |
if TRUE finds integer, i, closest to the root such that f(i) > zero |
print.steps |
if TRUE, prints iterations |
maxiter |
maximum number of iterations |
... |
additional arguments to 'f'. |
The algorithm evaluates f(i) iteratively, increasing (or decreasing if step.up=FALSE) i by 2^step.power until the value of f(i) switches sign. Then the change in 'i' is halved each iteration until convergence.
A list with the following elements:
root |
the integer on the correct side of the root |
f.root |
value of f at root |
iter |
number of times f was evaluated |
Unlike uniroot
, the function is not evaluated at both extremes. This makes
uniroot.integer
an efficient method to use when the calculation time of f(i)
increases with the value of 'i'. For an example of the importance of this see
ss.fromdata.pois
.
Michael P. Fay
uniroot
, used by ss.fromdata.neff
, ss.fromdata.pois
,
ss.nonadh
root.func<-function(i) i - 500.1 ## initial step sizes = 2^2 =4 uniroot.integer(root.func,c(0,Inf),step.power=2) ## more efficient to use bigger initial step sizes = 2^10 =1024 uniroot.integer(root.func,c(0,Inf),step.power=10,print.steps=TRUE)