Learn R Programming

ssanv (version 1.1)

uniroot.integer: 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 $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'.

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

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.

See Also

uniroot, used by ss.fromdata.neff, ss.fromdata.pois, ss.nonadh

Examples

Run this code
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)

Run the code above in your browser using DataLab