Rev 88598 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/stats/man/nlm.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2018 R Core Team% Distributed under GPL 2 or later\name{nlm}\alias{nlm}\title{Non-Linear Minimization}\concept{optimization}\usage{nlm(f, p, \dots, hessian = FALSE, typsize = rep(1, length(p)),fscale = 1, print.level = 0, ndigit = 12, gradtol = 1e-6,stepmax = max(1000 * sqrt(sum((p/typsize)^2)), 1000),steptol = 1e-6, iterlim = 100, check.analyticals = TRUE)}\description{This function carries out a minimization of the function \code{f}using a Newton-type algorithm. See the references for details.}\arguments{\item{f}{the function to be minimized, returning a single numericvalue. This should be a function with first argument a vector ofthe length of \code{p} followed by any other arguments specified bythe \code{\dots} argument.If the function value has an attribute called \code{gradient} orboth \code{gradient} and \code{hessian} attributes, these will beused in the calculation of updated parameter values. Otherwise,numerical derivatives are used. \code{\link{deriv}} returns afunction with suitable \code{gradient} attribute and optionally a\code{hessian} attribute.}\item{p}{starting parameter values for the minimization.}\item{\dots}{additional arguments to be passed to \code{f}.}\item{hessian}{if \code{TRUE}, the hessian of \code{f}at the minimum is returned.}\item{typsize}{an estimate of the size of each parameterat the minimum.}\item{fscale}{an estimate of the size of \code{f} at the minimum.}\item{print.level}{this argument determines the level of printingwhich is done during the minimization process. The defaultvalue of \code{0} means that no printing occurs, a value of \code{1}means that initial and final details are printed and a valueof 2 means that full tracing information is printed.}\item{ndigit}{the number of significant digits in the function \code{f}.}\item{gradtol}{a positive scalar giving the tolerance at which thescaled gradient is considered close enough to zero toterminate the algorithm. The scaled gradient is ameasure of the relative change in \code{f} in each direction\code{p[i]} divided by the relative change in \code{p[i]}.}\item{stepmax}{a positive scalar which gives the maximum allowablescaled step length. \code{stepmax} is used to prevent steps whichwould cause the optimization function to overflow, to prevent thealgorithm from leaving the area of interest in parameter space, or todetect divergence in the algorithm. \code{stepmax} would be chosensmall enough to prevent the first two of these occurrences, but shouldbe larger than any anticipated reasonable step.}\item{steptol}{A positive scalar providing the minimum allowablerelative step length.}\item{iterlim}{a positive integer specifying the maximum number ofiterations to be performed before the program is terminated.}\item{check.analyticals}{a logical scalar specifying whether theanalytic gradients and Hessians, if they are supplied, should bechecked against numerical derivatives at the initial parametervalues. This can help detect incorrectly formulated gradients orHessians.}}\details{Note that arguments after \code{\dots} must be matched exactly.If a gradient or hessian is supplied but evaluates to the wrong modeor length, it will be ignored if \code{check.analyticals = TRUE} (thedefault) with a warning. The hessian is not even checked unless thegradient is present and passes the sanity checks.The C code for the \dQuote{perturbed} Cholesky, \code{choldc()} hashad a bug in all \R versions before 3.4.1.From the three methods available in the original source, we always usemethod \dQuote{1} which is line search.The functions supplied should always return finite (including not\code{NA} and not \code{NaN}) values: for the function value itselfnon-finite values are replaced by the maximum positive value with a warning.}\value{A list containing the following components:\item{minimum}{the value of the estimated minimum of \code{f}.}\item{estimate}{the point at which the minimum value of\code{f} is obtained.}\item{gradient}{the gradient at the estimated minimum of \code{f}.}\item{hessian}{the hessian at the estimated minimum of \code{f} (ifrequested).}\item{code}{an integer indicating why the optimization process terminated.\describe{\item{1:}{relative gradient is close to zero, current iterate isprobably solution.}\item{2:}{successive iterates within tolerance, current iterateis probably solution.}\item{3:}{last global step failed to locate a point lower than\code{estimate}. Either \code{estimate} is an approximate localminimum of the function or \code{steptol} is too small.}\item{4:}{iteration limit exceeded.}\item{5:}{maximum step size \code{stepmax} exceeded five consecutivetimes. Either the function is unbounded below,becomes asymptotic to a finite value from above insome direction or \code{stepmax} is too small.}}}\item{iterations}{the number of iterations performed.}}\source{The current code is by Saikat DebRoy and the R Core team, using a Ctranslation of Fortran code by Richard H. Jones.}\references{\bibshow{R:Dennis+Schnabel:1983,R:Schnabel+Koonatz+Weiss:1985}}\seealso{\code{\link{optim}} and \code{\link{nlminb}}.\code{\link{constrOptim}} for constrained optimization,\code{\link{optimize}} for one-dimensionalminimization and \code{\link{uniroot}} for root finding.\code{\link{deriv}} to calculate analytical derivatives.For nonlinear regression, \code{\link{nls}} may be better.}\examples{f <- function(x) sum((x-1:length(x))^2)nlm(f, c(10,10))nlm(f, c(10,10), print.level = 2)utils::str(nlm(f, c(5), hessian = TRUE))f <- function(x, a) sum((x-a)^2)nlm(f, c(10,10), a = c(3,5))f <- function(x, a){res <- sum((x-a)^2)attr(res, "gradient") <- 2*(x-a)res}nlm(f, c(10,10), a = c(3,5))## more examples, including the use of derivatives.\dontrun{demo(nlm)}}\keyword{nonlinear}\keyword{optimize}