uniroot.integer {ssanv} | R Documentation |
Find the root of a function to the nearest integer
Description
Let f be a monotonic function that changes sign within the interval specified. If f(i)=0 for some i within the interval specified (including the ends of the interval), then the root is i. Otherwise 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).
Usage
uniroot.integer(f, interval, lower = min(interval), upper = max(interval),
step.power = 6, step.up = TRUE, pos.side = FALSE, print.steps = FALSE,
maxiter = 1000, ...)
Arguments
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 |
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) |
print.steps |
if TRUE, prints iterations |
maxiter |
maximum number of iterations |
... |
additional arguments to 'f'. |
Details
The algorithm evaluates f(i) iteratively, increasing (or decreasing if step.up=FALSE)
i by 2^{step.power}
until either f(i)=0 or f(i) switches sign.
If f(i)=0, then stop. If f(i) switches sign, then the change in 'i' is halved each iteration until convergence.
Value
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 |
Note
Unlike uniroot
, the function is not automatically 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
.
Author(s)
Michael P. Fay
See Also
uniroot
, used by ss.fromdata.neff
, ss.fromdata.pois
,
ss.nonadh
Examples
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)