dicreteRoot: Dichotomic search for monotone function

discreteRootR Documentation

Dichotomic search for monotone function

Description

Find the root of a monotone function on a discrete grid of value using dichotomic search

Usage

discreteRoot(
  fn,
  grid,
  increasing = TRUE,
  check = TRUE,
  tol = .Machine$double.eps^0.5
)

Arguments

fn

[function] objective function to minimize in absolute value.

grid

[vector] possible minimizers.

increasing

[logical] is the function fn increasing?

check

[logical] should the program check that fn takes a different sign for the first vs. the last value of the grid?

tol

[numeric] the absolute convergence tolerance.

Examples


### find the position of a value in a vector
f <- function(x){abs(vec[x]-1)}
discreteRoot(function(x){x},grid = seq(-20,10,1))

### find level of the confidence interval
if(require(nlme)){
fm1 <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), data = Ovary,
               correlation = corAR1(form = ~ 1 | Mare))

fctIC1 <- function(x){    
   IC.tempo <- intervals(fm1, level = 1-x)
   return( IC.tempo[["coef"]][1,"upper"])
}
fctIC2 <- function(x){    
   IC.tempo <- intervals(fm1, level = 1-x)
   return( IC.tempo[["coef"]][2,"upper"])
}
fctIC3 <- function(x){    
   IC.tempo <- intervals(fm1, level = 1-x)
   return( IC.tempo[["coef"]][3,"upper"])
}

summary(fm1)$tTable
discreteRoot(fctIC2,grid = seq(1/1000,1,0.001), increasing = FALSE)$par
discreteRoot(fctIC3,grid = seq(1/1000,1,0.001), increasing = FALSE)$par

## negative coefficient
fctIC <- function(x){    
   IC.tempo <- intervals(fm1, level = x)
   return( IC.tempo[["coef"]][3,"upper"])
}
discreteRoot(fctIC,grid = seq(0,1-1/1000,0.001))$par
}

bozenne/butils documentation built on July 3, 2024, 2:34 p.m.