nlfb | R Documentation |
A simplified and hopefully robust alternative to finding the nonlinear least squares minimizer that causes 'formula' to give a minimal residual sum of squares.
nlfb(
start,
resfn,
jacfn = NULL,
trace = FALSE,
lower = -Inf,
upper = Inf,
weights = NULL,
data = NULL,
ctrlcopy = FALSE,
control = list(),
...
)
start |
a numeric vector with all elements present
e.g., start=c(b1=200, b2=50, b3=0.3)
The start vector for this |
resfn |
A function that evaluates the residual vector for
computing the elements of the sum of squares function at the set of
parameters |
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 |
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 |
upper |
a vector of upper bounds on the parameters. If a single number,
this will be applied to all parameters. Default |
weights |
A vector of fixed weights or a function producing one. See the Details below. |
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. This avoids reprocessing controls. |
control |
a list of control parameters. See nlsr.control(). |
... |
additional data needed to evaluate the modeling functions |
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.
The weights
argument can be a vector of fixed weights, in which
case 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. weights
may alternatively be a function with header function(parms, resids)
to compute such a vector.
A list of the following items:
A named vector giving the parameter values at the supposed solution.
The sum of squared residuals at this set of parameters.
The weighted residual vector at the returned parameters.
The jacobian matrix (partial derivatives of residuals w.r.t. the parameters) at the returned parameters.
The number of residual evaluations (sum of squares computations) used.
The number of Jacobian evaluations used.
The weights argument as specified.
The weights vector at the final fit.
J C Nash 2014-7-16 nashjc _at_ uottawa.ca
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!)
# Default controls, standard Nash-Marquardt algorithm
anlf0 <- nlfb(start=st, resfn=shobbs.res, jacfn=shobbs.jac,
trace=TRUE, control=list(prtlvl=1))
anlf0
# Hartley with step reduction factor of .2
anlf0h <- nlfb(start=st, resfn=shobbs.res, jacfn=shobbs.jac,
trace=TRUE, control=list(prtlvl=1, lamda=0, laminc=1.0,
lamdec=1.0, phi=0, stepredn=0.2))
anlf0h
anlf1bm <- nlfb(start=st, resfn=shobbs.res, jacfn=shobbs.jac, lower=c(2,0,0),
upper=c(2,6,3), trace=TRUE, control=list(prtlvl=1))
anlf1bm
cat("backtrack using stepredn=0.2\n")
anlf1bmbt <- nlfb(start=st, resfn=shobbs.res, jacfn=shobbs.jac, lower=c(2,0,0),
upper=c(2,6,3), trace=TRUE, control=list(stepredn=0.2, prtlvl=1))
anlf1bmbt
## Short output
pshort(anlf1bm)
anlf2bm <- nlfb(start=st, resfn=shobbs.res, jacfn=shobbs.jac, lower=c(2,0,0),
upper=c(2,6,9), trace=TRUE, control=list(prtlvl=1))
anlf2bm
cat("backtrack using stepredn=0.2\n")
anlf2bmbt <- nlfb(start=st, resfn=shobbs.res, jacfn=shobbs.jac, lower=c(2,0,0),
upper=c(2,6,9), trace=TRUE, control=list(stepredn=0.2, prtlvl=1))
anlf2bmbt
## Short output
pshort(anlf2bm)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.