uniroot.integer: Find the root of a function to the nearest integer

Description Usage Arguments Details Value Note Author(s) See Also Examples

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

1
2
3
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 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

1
2
3
4
5
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)

Example output

$iter
[1] 129

$f.root
[1] -0.1

$root
[1] 500

[1] "x= 0  f(x)= -500.1"
[1] "x= 1024  f(x)= 523.9"
[1] "x= 512  f(x)= 11.9"
[1] "x= 256  f(x)= -244.1"
[1] "x= 384  f(x)= -116.1"
[1] "x= 448  f(x)= -52.1"
[1] "x= 480  f(x)= -20.1"
[1] "x= 496  f(x)= -4.10000000000002"
[1] "x= 504  f(x)= 3.89999999999998"
[1] "x= 500  f(x)= -0.100000000000023"
[1] "x= 502  f(x)= 1.89999999999998"
[1] "x= 501  f(x)= 0.899999999999977"
$iter
[1] 12

$f.root
[1] -0.1

$root
[1] 500

ssanv documentation built on May 2, 2019, 2:44 a.m.

Related to uniroot.integer in ssanv...