nlfb: nlfb: nonlinear least squares modeling by functions

View source: R/nlfb.R

nlfbR Documentation

nlfb: nonlinear least squares modeling by functions

Description

A simplified and hopefully robust alternative to finding the nonlinear least squares minimizer that causes 'formula' to give a minimal residual sum of squares.

Usage

nlfb(
  start,
  resfn,
  jacfn = NULL,
  trace = FALSE,
  lower = -Inf,
  upper = Inf,
  weights = NULL,
  data = NULL,
  ctrlcopy = FALSE,
  control = list(),
  ...
)

Arguments

start

a numeric vector with all elements present e.g., start=c(b1=200, b2=50, b3=0.3) ?? does it need names?

resfn

A function that evaluates the residual vector for computing the elements of the sum of squares function at the set of parameters start. Where this function is created by actions on a formula or expression in nlxb, this residual vector will be created by evaluation of the 'model - data', rather than the conventional 'data - model' approach. The sum of squares is the same.

jacfn

A function that evaluates the Jacobian of the sum of squares function, that is, the matrix of partial derivatives of the residuals with respect to each of the parameters. If NULL (default), uses an approximation.

The Jacobian MUST be returned as the attribute "gradient" of this function, allowing jacfn to have the same name and be the same code block as resfn, which may permit some efficiencies of computation.

trace

TRUE for console output during execution

lower

a vector of lower bounds on the parameters. If a single number, this will be applied to all. Default -Inf.

upper

a vector of upper bounds on the parameters. If a single number, this will be applied to all parameters. Default Inf.

weights

A vector of fixed weights. The objective function that will be minimized is the sum of squares where each residual is multiplied by the square root of the corresponding weight. Default NULL implies unit weights.

data

a data frame of variables used by resfn and jacfn to compute the required residuals and Jacobian.

ctrlcopy

If TRUE use control supplied as is. Saves reprocessing controls.

control

a list of control parameters. See nlsr.control().

...

additional data needed to evaluate the modeling functions

Details

nlfb is particularly intended to allow for the resolution of very ill-conditioned or else near zero-residual problems for which the regular nls() function is ill-suited.

This variant uses a qr solution without forming the sum of squares and cross products t(J)

Neither this function nor nlxb have provision for parameter scaling (as in the parscale control of optim and package optimx). This would be more tedious than difficult to introduce, but does not seem to be a priority feature to add.

Value

A list of the following items:

coefficients

A named vector giving the parameter values at the supposed solution.

ssquares

The sum of squared residuals at this set of parameters.

resid

The residual vector at the returned parameters.

jacobian

The jacobian matrix (partial derivatives of residuals w.r.t. the parameters) at the returned parameters.

feval

The number of residual evaluations (sum of squares computations) used.

jeval

The number of Jacobian evaluations used.

Author(s)

J C Nash 2014-7-16 nashjc _at_ uottawa.ca

Examples

library(nlsr)
# Scaled Hobbs problem
shobbs.res  <-  function(x){ # scaled Hobbs weeds problem -- residual
 # This variant uses looping
 if(length(x) != 3) stop("shobbs.res -- parameter vector n!=3")
 y  <-  c(5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443, 
          38.558, 50.156, 62.948, 75.995, 91.972)
 tt  <-  1:12
 res  <-  100.0*x[1]/(1+x[2]*10.*exp(-0.1*x[3]*tt)) - y
}
shobbs.jac  <-  function(x) { # scaled Hobbs weeds problem -- Jacobian
  jj  <-  matrix(0.0, 12, 3)
  tt  <-  1:12
  yy  <-  exp(-0.1*x[3]*tt)
  zz  <-  100.0/(1+10.*x[2]*yy)
  jj[tt,1]   <-   zz
  jj[tt,2]   <-   -0.1*x[1]*zz*zz*yy
  jj[tt,3]   <-   0.01*x[1]*zz*zz*yy*x[2]*tt
  attr(jj, "gradient") <- jj
  jj
}
st <- c(b1=2, b2=1, b3=1) # a default starting vector (named!)
anlf1bm <- nlfb(start=st, resfn=shobbs.res, jacfn=shobbs.jac, lower=c(2,0,0),
upper=c(2,6,3))
anlf1bm
## Short output
pshort(anlf1bm)
anlf2bm <- nlfb(start=st, resfn=shobbs.res, jacfn=shobbs.jac, lower=c(2,0,0),
upper=c(2,6,9))
anlf2bm
## Short output
pshort(anlf2bm)



nlsr documentation built on Aug. 17, 2022, 1:09 a.m.