R/utils.R

########################################################################
# Logit function
#
# Return the logit of a functon or its inverse.
logit <- function(x) log(x/(1-x))
expit <- function(x) exp(x)/(1 + exp(x))

#################################
# One Dimensional Optimization
#
# Perform an initial search inside a grid of points before performing
# standard optimization routine.
#
# param res Number of subdivisions.
# param ... See basic function optimize

goptimize <- function(fn, interval, res = 20, ...){

  igrid <- seq(interval[1],interval[2], len = res)
  id <- which.min(sapply(igrid,fn))

  if(id == 1) bnd0 <- igrid[1:2]
  else if(id == res) bnd0 <- igrid[c(res-1,res)]
  else bnd0 <- igrid[c(id-1,id)]

  return(optimize(fn, bnd0, ...))
}

############################################
#
#
martindurocher/floodStat documentation built on May 31, 2019, 12:42 a.m.