optimr: General-purpose optimization in optimr: Expanded Replacement and Extension of the 'optim' Function (2024)

In optimr: Expanded Replacement and Extension of the 'optim' Function

Description Usage Arguments Details Value Source References

Description

General-purpose optimization wrapper function that calls otherR tools for optimization, including the existing optim() function.optimr also tries to unify the calling sequence to allowa number of tools to use the same front-end, in fact using thecalling sequence of the R function optim().

Usage

1234
optimr(par, fn, gr=NULL, hess=NULL, lower=-Inf, upper=Inf, method=NULL, hessian=FALSE, control=list(), ...)

Arguments

par

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

fn

A function to be minimized (or maximized), with firstargument the vector of parameters over which minimization is to takeplace. It should return a scalar result.

gr

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 codeis used. For many methods, this is a finite-difference approximation, but some methods require user input for the gradient and will failotherwise. 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.(See the code in package optextras for details.) Method "grnd" uses the grad() function from package numDeriv.

hess

A function to return (as a matrix) the hessian for those methods thatcan use this information.

lower, upper

Bounds on the variables for methods such as "L-BFGS-B" that canhandle 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 boundsequal to the starting value. (It is possible that the starting value could be differentfrom the lower/upper bounds set,but this behaviour has NOT yet been defined and users are cautioned.)

method

A character string giving the name of the optimization method to beapplied. See the list allmeth in file ctrldefault.R which is part of this package.

hessian

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 necessarilyuse the same approximation as may be provided by the method called. Instead, the function hessian() from package numDeriv is used if no gradientgr is supplied, else the function jacobian() from numDerivis applied to the gradient function gr.

control

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.

Details

Note that arguments after ... should be matched exactly.

By default this function performs minimization, but it will maximizeif control$maximize is TRUE. The original optim() function allowscontrol$fnscale to be set negative to accomplish this. DO NOTuse both mechanisms simultaneously.

Possible method choices are specified by the list allmeth in the filectrldefault.R which is part of this package. Fewer methods are available in packge optimr on CRAN than package optimrx which is NOT on CRAN toavoid issues if packages on which function optimr() is dependent becomeunavailable.

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 setof parameters. Some methods can handle NA or Inf if the function cannot be evaluated at the supplied value. However, some methods, ofwhich "L-BFGS-B" is known to be a case, require that the valuesreturned should always be finite.

While methods from the base R function optim() can be used recursively, and for a single parameteras 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 theabove 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 ofa very crude optimization method; it is NOT intended as a production method, butmay be useful for didactic purposes.

The control argument is a list that can supply any of thecomponents in the file ctrldefault.R which is part of this package. It may supply others that areuseful or required for particular methods, but users are warned to be careful toensure 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 tofn and gr. Apparently no other attributes of parare copied over, but this may need to be verified, especially if parametersare passed to non-R routines.

Value

A list with components:

par

The best set of parameters found.

value

The value of ‘fn’ corresponding to ‘par’.

counts

A two-element integer vector giving the number of calls to‘fn’ and ‘gr’ respectively. This excludes those calls neededto compute the Hessian, if requested, and any calls to ‘fn’to compute a finite-difference approximation to the gradient.

convergence

An integer code. ‘0’ indicates successful completion. Thedocumentation for function opm() gives some other possible values andtheir meaning.

message

A character string giving any additional information returnedby the optimizer, or ‘NULL’.

hessian

If requested, an approximation to the hessian of ‘fn’at the final parameters.

Source

See the manual pages for optim() and the packages listed in NAMESPACE.

References

See the manual pages for optim() and the packages listed in NAMESPACE.

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–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.


optimr documentation built on May 2, 2019, 6:49 p.m.

Related to optimr in optimr...

optimr index

Using and extending the optimr package Using and extending the optimr package

optimr: General-purpose optimization in optimr: Expanded Replacement and Extension of the 'optim' Function (2024)

References

Top Articles
Latest Posts
Article information

Author: Nathanial Hackett

Last Updated:

Views: 5684

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Nathanial Hackett

Birthday: 1997-10-09

Address: Apt. 935 264 Abshire Canyon, South Nerissachester, NM 01800

Phone: +9752624861224

Job: Forward Technology Assistant

Hobby: Listening to music, Shopping, Vacation, Baton twirling, Flower arranging, Blacksmithing, Do it yourself

Introduction: My name is Nathanial Hackett, I am a lovely, curious, smiling, lively, thoughtful, courageous, lively person who loves writing and wants to share my knowledge and understanding with you.