[Rd] nls (PR#1533)

Ben Bolker bolker@zoo.ufl.edu
Thu, 9 May 2002 10:17:39 -0400 (EDT)


  OK, this is a FAQ (perhaps it should be added as a "Note" in the nls
documentation??):

  nls() uses convergence criteria that don't work well for artificial data
where the fit is perfect (but work reasonably well for "real" data -- it's
a reasonable tradeoff)
  There may still be a "bug" in nls() in terms of what it actually reports
(it should still report what it is doing correctly, even in a pathological
case).
  Try

ys <- y+rnorm(10,sd=0.001)
nls(ys~a*x^m/(1+b*x^m),start=list(a=1.1,m=2.1,b=.11),trace=TRUE)

See:
http://maths.newcastle.edu.au/~rking/R/devel/00b/0102.html
(and following thread)
http://maths.newcastle.edu.au/~rking/R/help/00b/2547.html
(one-paragraph comment at the end)

On Thu, 9 May 2002 scs10@st-and.ac.uk wrote:

> Full_Name: Sophie Smout
> Version: 1.5.0
> OS: windows 2000, pc
> Submission from: (NULL) (138.251.190.17)
>
>
> I'm finding that nls iterates to the end of its iteration setting and does not
> jump out to report convergence even when it has clearly converged. It is also
> mis-reporting the number of iterations completed. These may not be
> unconnected...
>
> This is an example:
>
> [note - details of my computer, version etc are at the end of this message]
>
> ##### some 'data'
>
>
> > x
>  [1]  1  2  3  4  5  6  7  8  9 10
>
> > y
>  [1] 0.9090909 2.8571429 4.7368421 6.1538462 7.1428571 7.8260870 8.3050847
>  [8] 8.6486486 8.9010989 9.0909091
>
> ### call to nls
>
> > nls(y~a*x^m/(1+b*x^m),start=list(a=1.1,m=2.1,b=.11),trace=TRUE)
>
> ### nls spits out
>
> 1.474151 :  1.10 2.10 0.11
> 0.01060234 :  1.0092752 1.9893808 0.1012907
> 5.066689e-07 :  0.9999465 2.0000191 0.0999903
> 3.651161e-15 :  1.0 2.0 0.1
> 1.518557e-29 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> 8.09815e-30 :  1.0 2.0 0.1
> Error in nls(y ~ a * x^m/(1 + b * x^m), start = list(a = 1.1, m = 2.1,  :
>         number of iterations exceeded maximum of 5.27058e-308
> >
> ### ??? the converged parameter values are correct
> ### but nls has not reported convergence. Instead it's just gone on iterating,
> ### but not 5.27058e-308 times, which would in any case be tricky...
>
>
>
> ### try another simple example
> ### generate some data
>
>
>
> > x<-c(1:10)
> > y<-3*x^2+1
> > y
>  [1]   4  13  28  49  76 109 148 193 244 301
> > x
>  [1]  1  2  3  4  5  6  7  8  9 10
>
> ### call to nls
>
>
> > nls(y~a*x^b+c,start=list(a=1,b=1,c=1))
>
> Error in nls(y ~ a * x^b + c, start = list(a = 1, b = 1, c = 1)) :
>         number of iterations exceeded maximum of 5.27058e-308
>
> ### another call to nls with more details requested
>
> > nls(y~a*x^b+c,start=list(a=1,b=1,c=1),trace=TRUE,control=nls.control(maxiter=10))
> 210232 :  1 1 1
> 209552.9 :  0.3814959 1.4296089 1.7349535
> 191724.4 :  0.2824096 1.9134931 1.9513585
> 165276.9 :  0.4484658 1.9827257 1.8983138
> 125378.7 :  0.7671348 1.9977192 1.7865044
> 70298.31 :  1.325342 1.999960 1.589895
> 17571.14 :  2.162671 2.000005 1.294948
> 4.457052e-06 :  3.0000000 1.9999979 0.9999999
> 2.200966e-17 :  3 2 1
> 6.125505e-27 :  3 2 1
> 6.12235e-27 :  3 2 1
> Error in nls(y ~ a * x^b + c, start = list(a = 1, b = 1, c = 1), trace = TRUE,
> :
>         number of iterations exceeded maximum of 5.27058e-308
>
>
>
> #### same problem!!
> ##############################################
>
> here is what I get when I type 'version'
> to get details of computer etc.
>
>
>
> platform i386-pc-mingw32
> arch     i386
> os       mingw32
> system   i386, mingw32
> status
> major    1
> minor    5.0
> year     2002
> month    04
> day      29
> language R
> ###############################################
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
318 Carr Hall                                bolker@zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._