[R] Use of nls command

Douglas Bates bates at stat.wisc.edu
Tue Apr 23 23:47:17 CEST 2002


Peter.Watkins at foodscience.afisc.csiro.au writes:

> Hello.
> 
> I am trying to do a non-linear fit using the 'nls' command.
> 
> The data that I'm using is as follows
>     pH     k
> 1 3.79 34.21
> 2 4.14 25.85
> 3 4.38 20.45
> 4 4.57 15.61
> 5 4.74 12.42
> 6 4.92  9.64
> 7 5.11  7.30
> 8 5.35  5.15
> 9 5.67  3.24
> 	
> with a transformation of pH to H <- 10^-pH
> 
> When using the nls command for a set of parameters - a, b and c, I receive
> two sets of errors:
> 
> > ba.nls <- nls( k ~ a/(1+(H/b)) +c*(H/b)/(1+(H/b)), data = obs, start =
> list( a= 1, b = 1, c = 2), trace = TRUE )
> 2579.16 :  1 1 2 
> 1266.912 :   8.041114e+00 -1.811360e+08 -1.809427e+08 
> 1243.136 :  8.237232e+00 2.304521e+15 2.269853e+15 
> Error in nls(k ~ a/(1 + (H/b)) + c * (H/b)/(1 + (H/b)), data = obs, start =
> list(a = 1,  : 
>         step factor 0.000488281 reduced below `minFactor' of 0.000976563
> 
> > ba.nls <- nls( k ~ a/(1+(H/b)) +c*(H/b)/(1+(H/b)), data = obs, start =
> list( a= 5, b = 1, c = 2), trace = TRUE )
> 1724.262 :  5 1 2 
> Error in nls(k ~ a/(1 + (H/b)) + c * (H/b)/(1 + (H/b)), data = obs, start =
> list(a = 5,  : 
>         singular gradient
> 
> I suspect that my initial set of starting values are the problem but I'm
> open for comments.

Two comments:

 - As you indicate, your starting values are poor.  For one thing, the
   value of b has to be on the same order as a typical value of H
   before the expression 1+H/b will be influenced by b.

 - This is a situation where a and c occur linearly in the model.  The
   'plinear' algorithm reduced the optimization from a
   three-dimensional problem to a one-dimensional problem, which is a
   great advantage.

 > obs$H <- 10^-(obs$pH)
 > obs
     pH     k            H
 1 3.79 34.21 1.621810e-04
 2 4.14 25.85 7.244360e-05
 3 4.38 20.45 4.168694e-05
 4 4.57 15.61 2.691535e-05
 5 4.74 12.42 1.819701e-05
 6 4.92  9.64 1.202264e-05
 7 5.11  7.30 7.762471e-06
 8 5.35  5.15 4.466836e-06
 9 5.67  3.24 2.137962e-06
 > ba.nls <- nls(k ~ cbind(1, H/b)/(1+(H/b)), data = obs, start = list(b = 1),
 + algorithm = 'plinear', trace = TRUE)
 108.8751 : 1.000000e+00 7.652868e+00 1.868911e+05 
 Error in nls(k ~ cbind(1, H/b)/(1 + (H/b)), data = obs, start = list(b = 1),  : 
         step factor 0.000488281 reduced below `minFactor' of 0.000976562
 > ba.nls <- nls(k ~ cbind(1, H/b)/(1+(H/b)), data = obs, start = list(b = 1e-5),
 + algorithm = 'plinear', trace = TRUE)
 112.2968 :  0.000010 -7.344264 29.216662 
 27.55333 :  2.471682e-05 -1.713283e+00  3.574606e+01 
 3.158453 : 4.369706e-05 8.857246e-01 4.174971e+01 
 0.4362722 : 5.529106e-05 1.801907e+00 4.487485e+01 
 0.3711344 : 5.763303e-05 1.954527e+00 4.547583e+01 
 0.3709906 : 5.774858e-05 1.961829e+00 4.550526e+01 
 0.3709905 : 5.775135e-05 1.962004e+00 4.550597e+01 
 0.3709905 : 5.775142e-05 1.962008e+00 4.550598e+01 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list