[R] nls function error

Rolf Turner r.turner at auckland.ac.nz
Mon Oct 28 01:18:08 CET 2013


On 10/28/13 00:10, Hansol Yu wrote:
> data(Boston, package='MASS')
> y <- Boston$nox
> x <- Boston$dis
> nls(y~ A + B * exp(C * x), start=list(A=1, B=1, C=1))
>
> Error in nls(y ~ A + B * exp(C * x), start = list(A = 1, B = 1, C = 1),  :
>    step factor 0.000488281 reduced below 'minFactor' of 0.000976562
>
> I don't know how to fix this error. I think my problem is that I set the
> wrong start. Could somebody help please?

Different starting values will indeed fix the problem.  Did you *try* 
different starting
values?  Take some initiative!

It is pretty clear that C has to be negative, so make the starting value 
negative.
Using 1, 1, -1 still doesn't work, but 0.5, 0.5, -0.5 does work.

The function optim() is more robust to starting values.  Try

foo <- function(par,x,y){
A <- par[1]
B <- par[2]
C <- par[3]
sum((y - (A+B*exp(C*x)))^2)
}

optim(c(1,1,-1),foo,x=x,y=y,method="BFGS")

The estimates given by optim() are the same as those given by nls() with the
0.5, 0.5, -0.5 starting values.

     cheers,

     Rolf Turner



More information about the R-help mailing list