[R-sig-eco] Providing derivative information to nls()

Philip Dixon pdixon at iastate.edu
Thu Oct 30 12:51:43 CET 2008


Most non-linear least squares algorithms estimate the derivatives
numerically.  SAS PROC NLIN does that when you omit the expressions
for the analytical derivatives (DER.A = ...).  My experience has
been that numerical derivatives work just fine for most problems.

According to the nls() help file, you can provide gradient
information by writing a function to compute the residuals (obs -
pres) and adding a gradient attribute to those residuals.  The left
hand side of the formula is blank.  An example is in the
documentation for the nls() function.  Look for the
weighted.MM.grad() function.  The example is more complicated than
you need because it is also weighting the responses.  However, nls()
no longer seems to accept a blank response.

An alternative is to use mle() in the stats4 package.  This doesn't
come with quite as many helper functions as nls(), but the important
ones are available: vcov() to get the variance-covariance matrix,
confint() for profile confidence intervals and logLik() to get the
maximized log likelihood to use in tests.  To use mle() you would
need to:

1) write a function to calculate the sum of squared residuals given
A, C, and D

2) write a function to calculate the gradient of the SS w.r.t. A, C,
and D.

3) mle(SSfn, start=list(A=.. C=.., D=..), gr=GRfn) 

Best wishes,
Philip Dixon



More information about the R-sig-ecology mailing list