wrapOptim: Function to address 'optim' and 'optimize' with the same set...

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

Description

This function takes basically the same arguments as optim with a lower and upper bound for optimization as required by the method "L-BFGS-B", and is intended for optimization over the parameter set of a LuckModel.

Usage

1
2
wrapOptim(par, fn, method = "L-BFGS-B", control = list(fnscale = 1),
          lower, upper, ...)

Arguments

par

Initial values for the parameters to be optimzed over, typically one of the "corners" of the prior parameter set defined by a LuckModel.

fn

The function to be minimized or maximized. The first argument must be named .n0y0, and contains the two canonical parameters n^(0) and y^(0) of a LuckModel as a vector, typically c(n0,y0).

method

The optimization method to be used, see optim. Default is "L-BFGS-B" as the one offering box-constrained optimization.

control

A list of control parameters, see optim. Default is list(fnscale = 1), making wrapOptim perform a minimization. (fnscale=-1 performs minimization.)

lower

Lower bound for the optimization, in the same format as par and in the function fn, i.e. as a vector containing first the lower bound for the n^(0) and second the lower bound for the y^(0) parameter.

upper

Upper bound for the optimization, in the same format as par and in the function fn, i.e. as a vector containing first the upper bound for the n^(0) and second the upper bound for the y^(0) parameter.

...

Further arguments to be passed to optim, optimize, or fn.

Details

Depending wether lower and upper coincide in none or either of n^(0) or y^(0), optim or optimize are called. If lower and upper coincide in both elements, then no optimization is performed and the value of fn at lower (=upper) is returned.

This function was programmed before the method "Brent" in optim existed, which seems to provide a similar functionality.

Value

The return value is the return object of optim (or pretends to be so). See optim for details.

Author(s)

Gero Walter

See Also

luck for a general description of the package, optim, optimize

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# function to optimize over
testfn <- function (.n0y0, arg2) {
  if (arg2) return(.n0y0[1]^2 + .n0y0[2])
  else return (.n0y0[2]^2 + .n0y0[1])
}
# minimize
wrapOptim(par = c(1,1), fn = testfn, lower = c(1,1), upper = c(5,5), arg2 = FALSE)
wrapOptim(par = c(1,1), fn = testfn, lower = c(1,1), upper = c(5,5), arg2 = TRUE)
wrapOptim(par = c(1,1), fn = testfn, lower = c(1,1), upper = c(1,1), arg2 = TRUE)
wrapOptim(par = c(1,5), fn = testfn, lower = c(1,1), upper = c(1,5), arg2 = TRUE)
wrapOptim(par = c(2,1), fn = testfn, lower = c(1,1), upper = c(5,1), arg2 = TRUE)
# maximize
wrapOptim(par = c(1,1), fn = testfn, lower = c(1,1), upper = c(5,5), arg2 = FALSE, 
          control = list(fnscale=-1))

luck documentation built on May 2, 2019, 4:43 p.m.