dot-fitCurve2: Curve fitting via 'stats::optim' L-BFGS-B with fall-back...

.fitCurve2R Documentation

Curve fitting via stats::optim L-BFGS-B with fall-back grid/pattern search if convergence is not achieved.

Description

Function to fit curve via stats::optim

Usage

.fitCurve2(
  par,
  x,
  y,
  fn,
  loss,
  lower = -Inf,
  upper = Inf,
  precision = 1e-04,
  density = c(2, 10, 5),
  step = 0.5/density,
  ...,
  loss_args = list(),
  span = 1,
  optim_only = FALSE,
  control = list(factr = 1e-08, ndeps = rep(1e-04, times = length(par)), trace = 0)
)

Arguments

par

numeric Vector of intial guesses for the parameters. For each index i of par, par[i] must be within the range (⁠lower\[i\]⁠, ⁠upper\[i\]⁠). If only a single upper or lower value is present, that range is used for all parameters in par.

x

numeric Values to evaluate fn for.

y

numeric Target output values to optimze fn against.

fn

function A function to optimize. Any fn arguments passed via ... will be treated as constant and removed from the optimization. It is assumed that the first argument is the x value to optimize over and any subsequent arguments are free parameters to be optimized. Transformed to be optim compatible via make_optim_function is the first arguement isn't already par.

loss

character(1) or function Either the name of one of the bundled loss functions (see details) or a custom loss function to compute for the output of fn over x.

lower

numeric(1) Lower bound for parameters. Parallel to par.

upper

numeric(1) Upper bound for paramteres. Parallel to par.

precision

numeric smallest step size used in pattern search, once step size drops below this value, the search terminates.

density

numeric how many points in the dimension of each parameter should be evaluated (density of the grid)

step

initial step size for pattern search.

...

pairlist Fall through arguments to fn.

loss_args

list Additional argument to the loss function. These get passed to losss via do.call analagously to using ....

span

numeric Can be safely kept at 1, multiplicative ratio for initial step size in pattern search. Must be larger than precision.

optim_only

logical(1) Should the fall back methods when optim fails be skipped? Default is FALSE.

control

list List of control parameters to pass to optim. See ?optim for details.

Details

TODO

Value

numeric Vector of optimal parameters for fn fit against y on the values of x.

Examples

## Not run: 
  # Four parameter hill curve equation
  hillEqn <- function(x, Emin, Emax, EC50, lambda) {
      (Emin + Emax * (x / EC50)^lambda) / (1 + (x / EC50)^lambda)
  }
  # Make some dummy data
  doses <- rev(1000 / (2^(1:20)))
  lambda <- 1
  Emin <- 1
  Emax <- 0.1
  EC50 <- median(doses)
  response <- hillEqn(doses, Emin=Emin, lambda=lambda, Emax=Emax, EC50=EC50)
  nresponse <- response + rnorm(length(response), sd=sd(response)*0.1) # add noise
  # 3-parameter optimization
  3par <- .fitCurve2(
      par=c(Emax, EC50, lambda),
      x=doses,
      y=nresponse,
      fn=hillEqn,
      Emin=Emin, # set this as constant in the function being optimized (via ...)
      loss=.normal_loss,
      loss_args=list(trunc=FALSE, n=1, scale=0.07),
      upper=c(1, max(doses), 6),
      lower=c(0, min(doses), 0)
  )
  # 2-parameter optimization
  2par <- .fitCurve2(
      par=c(Emax, EC50),
      x=doses,
      y=nresponse,
      fn=hillEqn,
      Emin=Emin, # set this as constant in the function being optimized (via ...)
      lambda=1,
      loss=.normal_loss,
      loss_args=list(trunc=FALSE, n=1, scale=0.07),
      upper=c(1, max(doses)),
      lower=c(0, min(doses))
  )

## End(Not run)


bhklab/CoreGx documentation built on March 14, 2024, 3:04 a.m.