[R] nls max iterations reached, but parameter estimates are correct

David Carlson dcarlson at tamu.edu
Mon Aug 12 18:36:40 CEST 2013


Perhaps this clear warning on the manual page for nls (?nls) has
something to do with it:

Warning

Do not use nls on artificial "zero-residual" data.

The nls function uses a relative-offset convergence criterion
that compares the numerical imprecision at the current parameter
estimates to the residual sum-of-squares. This performs well on
data of the form

y = f(x, θ) + eps

(with var(eps) > 0). It fails to indicate convergence on data of
the form

y = f(x, θ)

because the criterion amounts to comparing two components of the
round-off error. If you wish to test nls on artificial data
please add a noise component, as shown in the example below.

The algorithm = "port" code appears unfinished, and does not
even check that the starting value is within the bounds. Use
with caution, especially where bounds are supplied.

-------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of natan
hoefnagel
Sent: Monday, August 12, 2013 9:54 AM
To: r-help at r-project.org
Subject: [R] nls max iterations reached, but parameter estimates
are correct

Dear All,

I am trying to fit a regression (growth curve) through my data
using nls().
I want to learn how this works, so I made up my own simple data:

PGC = function(K, P0, r, t)  { (K*P0*exp(r*t)) /
(K+P0*(exp(r*t)-1)) }
x <- seq(1:20)
y<-PGC(6, 0.1, 0.4, x)

Plotting this data yields a nice s-shaped growth curve including
a 
horizontal asymptote.

Then I wanted to test the function nls():
m<-nls(y~PGC(K,P0,r,x),start=list(K=5,P0=0.01,r=0.3),trace=TRUE)

I deliberately used starting values a bit off. I used trace=TRUE
based 
on an answer in the thread about changing maximum nr of
iterations - 
first check where the iterations are going. I want the parameter

estimates to be saved in 'm' for later use.
Running this line results in a list of 50 iterations for 3
parameters 
(K, P0,r) and the error message:
"Error in nls(y ~ PGC(K, P0, r, x), start = list(K = 5, P0 =
0.01, r = 
0.3),  :
   number of iterations exceeded maximum of 50"

However, I can see that the right parameter values are reached
after 12 
iterations (The values I used to create 'y').

My questions: Why does it continue to iterate?
Is there a way to still get the parameter estimates for further
use (for 
example plot the fitted line)? It seems that they are note saved
in 'm'.

Any help would be very welcome,

Evan

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible
code.



More information about the R-help mailing list