General-purpose optimization wrapper function that calls other
R tools for optimization, including the existing optim() function.
optimr
also tries to unify the calling sequence to allow
a number of tools to use the same front-end, in fact using the
calling sequence of the R function optim()
.
optimr(par, fn, gr=NULL, hess=NULL, lower=-Inf, upper=Inf,
method=NULL, hessian=FALSE,
control=list(),
...)
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.
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.
A function to return (as a vector) the gradient for those methods that can use this information.
If 'gr' is NULL
, whatever default action is specified for the
chosen method for the case of a null gradient code
is used. For many methods, this is a finite-difference approximation,
but some methods require user input for the gradient and will fail
otherwise. In such cases, we try to return convergence
of 9998.
If 'gr' is a character string, then that string is taken as the name of
a gradient approximation function, for example, "grfwd", "grback" and
"grcentral" for standard forward, backward and central approximations.
Method "grnd"
uses the grad()
function from package numDeriv
.
A function to return (as a matrix) the hessian for those methods that can use this information.
Bounds on the variables for methods such as "L-BFGS-B"
that can
handle box (or bounds) constraints. A small set of methods can handle masks, that is,
fixed parameters, and these can be specified by making the lower and upper bounds
equal to the starting value. (It is possible that the starting value could be different
from the lower/upper bounds set,
but this behaviour has NOT yet been defined and users are cautioned.)
A character string giving the name of the optimization method to be
applied. See the list allmeth
in file
ctrldefault.R
which is part of this package.
A logical control that if TRUE forces the computation of an approximation
to the Hessian at the final set of parameters. Note that this will NOT necessarily
use the same approximation as may be provided by the method called. Instead,
the function hessian()
from package numDeriv
is used if no gradient
gr
is supplied, else the function jacobian()
from numDeriv
is applied to the gradient function gr
.
A list of control parameters. See ‘Details’.
Further arguments to be passed to fn
and gr
if needed for computation of these quantities; otherwise, further
arguments are not used.
A list with components:
The best set of parameters found.
The value of <U+2018>fn<U+2019> corresponding to <U+2018>par<U+2019>.
A two-element integer vector giving the number of calls to <U+2018>fn<U+2019> and <U+2018>gr<U+2019> respectively. This excludes those calls needed to compute the Hessian, if requested, and any calls to <U+2018>fn<U+2019> to compute a finite-difference approximation to the gradient.
An integer code. <U+2018>0<U+2019> indicates successful completion. The
documentation for function opm()
gives some other possible values and
their meaning.
A character string giving any additional information returned by the optimizer, or <U+2018>NULL<U+2019>.
If requested, an approximation to the hessian of <U+2018>fn<U+2019> at the final parameters.
Note that arguments after …
should be matched exactly.
By default this function performs minimization, but it will maximize
if control$maximize
is TRUE. The original optim() function allows
control$fnscale
to be set negative to accomplish this. DO NOT
use both mechanisms simultaneously.
Possible method choices are specified by the list allmeth
in the file
ctrldefault.R
which is part of this package. Fewer methods are available in
packge optimr
on CRAN than package optimrx
which is NOT on CRAN to
avoid issues if packages on which function optimr()
is dependent become
unavailable.
If no method is specified, the method specified by defmethod
in file
ctrldefault.R
(which is part of this package) will be attempted.
Function fn
must return a finite scalar value at the initial set
of parameters. Some methods can handle NA
or Inf
if the function cannot be evaluated at the supplied value. However, some methods, of
which "L-BFGS-B"
is known to be a case, require that the values
returned should always be finite.
While methods from the base R function optim()
can be used recursively,
and for a single parameter
as well as many, this may not be true for other methods in optimrx
.
optim
also accepts a zero-length par
, and just evaluates the function
with that argument.
Generally, you are on your own if you choose to apply constructs mentioned in the above two paragraphs.
For details of methods, please consult the documentation of the individual methods.
(The NAMESPACE file lists the packages from which functions are imported.)
However, method "hjn"
is a conservative implementation of a Hooke and
Jeeves (1961) and is part of this package. It is provided as a simple example of
a very crude optimization method; it is NOT intended as a production method, but
may be useful for didactic purposes.
The control
argument is a list that can supply any of the
components in the file ctrldefault.R
which is part of this
package. It may supply others that are
useful or required for particular methods, but users are warned to be careful to
ensure that extraneous or incorrect components and values are not passed.
Note that some control
elements apply only to some of methods.
See individual packages for details.
Any names given to par
will be copied to the vectors passed to
fn
and gr
. Apparently no other attributes of par
are copied over, but this may need to be verified, especially if parameters
are passed to non-R routines.
CAUTION: because there is a seldom-used parameter hess
, you should NOT
make a call like
ans <- optimr(start, myf, myg, lower, upper)
or you will likely get wrong results. Instead use
ans <- optimr(start, myf, myg, lower=lower, upper=upper)
NOTE: The default update formula for the "CG" option of optim()
is type=2
or Polak-Ribiere.
See the manual pages for optim()
.
Hooke R. and Jeeves, TA (1961). Direct search solution of numerical and statistical problems. Journal of the Association for Computing Machinery (ACM). 8 (2): 212<U+2013>229.
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/.
Nocedal J, and Wright SJ (1999). Numerical optimization. New York: Springer. 2nd Edition 2006.
# NOT RUN {
# Simple Test Function 1:
tryfun.f = function(x) {
fun <- sum(x^2 )
## if (trace) ... to be fixed
print(c(x = x, fun = fun))
fun
}
tryfun.g = function(x) {
grad<-2.0*x
grad
}
tryfun.h = function(x) {
n<-length(x)
t<-rep(2.0,n)
hess<-diag(t)
}
strt <- c(1,2,3)
ansfgh <- optimr(strt, tryfun.f, tryfun.g, tryfun.h, method="nlm",
hessian=TRUE, control=list(trace=2))
proptimr(ansfgh) # compact output of result
# }
Run the code above in your browser using DataCamp Workspace