[R] problems with nonlinear fits using nls

Douglas Bates bates at stat.wisc.edu
Wed Feb 23 18:29:15 CET 2005


Corey Bradshaw wrote:
> Hello colleagues,
> 
>  
> 
> I am attempting to determine the nonlinear least-squares estimates of
> the nonlinear model parameters using nls. I have come across a common
> problem that R users have reported when I attempt to fit a particular
> 3-parameter nonlinear function to my dataset:
> 
>  
> 
> Error in nls(r ~ tlm(a, N.fix, k, theta), data = tlm.data, start =
> list(a = a.st,  : 
> 
>         step factor 0.000488281 reduced below `minFactor' of 0.000976563
> 
>  
> 
> Despite modifying minFactor using nls.control, I am unable to counter
> the apparent singularity in the model fit. I have also tried changing
> the tolerance and start parameter values to no avail. If anyone can
> provide a relatively simple solution (perhaps adjusting the gradient,
> but I'm not sure how to do this), I would be most appreciative. My
> dataset is:
> 
>  
> 
> 
>>tlm.data
> 
> 
>              r N.fix
> 
> 1  -0.52407085    76
> 
> 2   0.10536052    45
> 
> 3  -0.17435339    50
> 
> 4   0.19415601    42
> 
> 5   0.48701498    51
> 
> 6  -0.50681760    83
> 
> 7  -0.17435339    50
> 
> 8   0.55278982    42
> 
> 9   0.15219182    73
> 
> 10  0.49899117    85
> 
> 11  0.10821358   140
> 
> 12 -0.83034830   156
> 
> 13 -0.30748470    68
> 
> 14 -0.22314355    50
> 
> 15  0.04879016    40
> 
> 16 -0.04879016    42
> 
> 17  0.75377180    40
> 
> 18 -0.12516314    85
> 
> 19 -0.36624439    75
> 
>  
> 
> My function is:
> 
>  
> 
> tlm <- function(a,N,k,theta) (a*(1-((N/k)^theta)))
> 
>  
> 
> The nls fit I've coded is:
> 
>  
> 
> tlm.fit <- try(nls(r~tlm(a,N.fix,k,theta), data=tlm.data,
> start=list(a=a.st,k=k.st,theta=1),
> 
>             trace=TRUE,
> control=nls.control(maxiter=6000,tol=1e-05,minFactor=1/1024)))
> 
>  
> 
> I'm using start values parsed in from another (previous, but not shown)
> model fit. In this case, 
> 
>  
> 
> 
>>a.st
> 
> 
> [1] 0.3812922
> 
> 
>>k.st
> 
> 
> [1] 64.66529
> 
>  
> 
> I happen to know the true values for the optimised parameters (from
> another application), but I can't get nls to reproduce them. They are:
> 
>  
> 
> a = 2.0466
> 
> k = 60.8275
> 
> theta = 0.2277
> 
>  
> 
> Any ideas?
> 
>  
> 
> Regards,
> 
> Corey Bradshaw
> 

It is possible to fit this model to these data using nls as shown in the 
enclosed transcript.  You have one conditionally linear parameter ('a') 
in the model so I used the plinear algorithm and I also generated 
analytic derivatives for the tls function using the deriv function.

There are several things to note:

  - Your data are very noisy.  It is not surprising that it is difficult 
to fit a 3-parameter nonlinear model to such data.

  - The fitted model has negative values for both 'a' and 'theta'.

  - The estimates are highly imprecise.

 > summary(fm1)

Formula: r ~ tlm(N.fix, k, theta)

Parameters:
       Estimate Std. Error t value Pr(>|t|)
k      49.0724     6.9153   7.096 2.53e-06
theta  -4.6676     5.1805  -0.901    0.381
.lin   -0.2333     0.1685  -1.385    0.185

Residual standard error: 0.3662 on 16 degrees of freedom

Correlation of Parameter Estimates:
             k   theta
theta  0.8353
.lin  -0.3458 -0.7104
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: nls.Rout
Url: https://stat.ethz.ch/pipermail/r-help/attachments/20050223/a9055f2d/nls.pl


More information about the R-help mailing list