# General-purpose optimization

### Description

General-purpose optimization wrapper function that calls other
R tools for optimization, including the existing optim() function.
`optimx`

also tries to unify the calling sequence to allow
a number of tools to use the same front-end. These include
`spg`

from the BB package, `ucminf`

, `nlm`

, and
`nlminb`

. Note that
optim() itself allows Nelder–Mead, quasi-Newton and
conjugate-gradient algorithms as well as box-constrained optimization
via L-BFGS-B. Because SANN does not return a meaningful convergence code
(conv), optimx() does not call the SANN method.

### Usage

1 2 |

### Arguments

`par` |
a vector of initial values for the parameters for which optimal values are to be found. Names on the elements of this vector are preserved and used in the results data frame. |

`fn` |
A function to be minimized (or maximized), with first argument the vector of parameters over which minimization is to take place. It should return a scalar result. |

`gr` |
A function to return (as a vector) the gradient for those methods that can use this information. |

`hess` |
A function to return (as a symmetric matrix) the Hessian of the objective function for those methods that can use this information. |

`lower, upper` |
Bounds on the variables for methods such as |

`control` |
A list of control parameters. See ‘Details’. |

`...` |
For |

### Details

Note that arguments after `...`

must be matched exactly.

While it can be envisaged that a user would have an analytic hessian but not an analytic gradient, we do NOT permit the user to test the hessian in this situation.

Any names given to `par`

will be copied to the vectors passed to
`fn`

and `gr`

. Note that no other attributes of `par`

are copied over. (We have not verified this as at 2009-07-29.)

### Value

A list of the following items:

- grOK
TRUE if the analytic gradient and a numerical approximation via

`numDeriv`

agree within the`control$grtesttol`

as per the`R`

code in function`grchk`

.`NULL`

if no analytic gradient function is provided.- grOK
TRUE if the analytic hessian and a numerical approximation via

`numDeriv::jacobian`

agree within the`control$hesstesttol`

as per the`R`

code in function`hesschk`

. NULL if no analytic hessian or no analytic gradient is provided. Note that since an analytic gradient must be available for this test, we use the Jacobian of the gradient to compute the Hessian to avoid one level of differencing, though the`hesschk`

function can work without the gradient.- scalebad
TRUE if the larger of the

`scaleratios`

exceeds`control$scaletol`

- scaleratios
A vector of the parameter and bounds scale ratios. See the function code of

`scalecheck`

for the computation of these values.

### References

See the manual pages for `optim()`

and the packages the DESCRIPTION `suggests`

.

Nash JC, and Varadhan R (2011). Unifying Optimization Algorithms to Aid Software System Users:
**optimx** for R., *Journal of Statistical Software*, 43(9), 1-14.,
URL http://www.jstatsoft.org/v43/i09/.

Nash JC (2014). On Best Practice Optimization Methods in R.,
*Journal of Statistical Software*, 60(2), 1-14.,
URL http://www.jstatsoft.org/v60/i02/.

### Examples

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ```
fr <- function(x) { ## Rosenbrock Banana function
x1 <- x[1]
x2 <- x[2]
100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}
grr <- function(x) { ## Gradient of 'fr'
x1 <- x[1]
x2 <- x[2]
c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1),
200 * (x2 - x1 * x1))
}
myctrl<- ctrldefault(2)
myctrl$trace <- 3
mychk <- optchk(par=c(-1.2,1), fr, grr, lower=rep(-10,2), upper=rep(10,2), control=myctrl)
cat("result of optchk\n")
print(mychk)
``` |