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 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'.

Details

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.

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 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)

[Package ssanv version 1.0 Index]