I have a reproducible example here with an attempt to use nls to fit a nonlinear function:
y = ax/(b+x) + c
Even when I set the starting values to be a good, known optimization, the nls
call fails with singular convergence
. When I restrict the bounds to be right around the known optimization, it succeeds. Of course I won't always know the upper and lower bounds in all cases of these types of curves. How can I improve the original nls
call to help find the optimized solution?
X = c(5.1,5.3,5.4,5.6,5.7,5.8,6.0,6.3,6.5,6.8,7.0,7.3,7.3,7.6,8.2,8.5,8.5,8.8,8.8,9.1,9.3,9.3,9.8,10.2,10.7,11.1,11.1)
Y = c(118,116,115,114,115,115,114,114,114,112,108,107,111,109,107,108,109,105,105,107,107,107,106,105,104,101,101)
plot(X,Y,xlim=c(3,12),ylim=c(98,120))
#KNOWN, GOOD STARTING VALUES
a = 12
b = -18
c = 119
newx = seq(-100,100,by=0.01)
newy = newx*a/(b+newx)+c
lines(newx,newy)
#Gives singular convergence
myfit = nls(Y~a*X/(b+X)+c,data=NB,start=list(a=a,b=b,c=c),
control=nls.control(maxiter = 10000),algorithm="port")
#Finds solution if I put in the lower and upper bounds
myfit = nls(Y~a*X/(b+X)+c,data=NB,start=list(a=a,b=b,c=c),
control=nls.control(maxiter = 10000),
lower=c(8,-20,110),upper=c(12,-15,130),algorithm = "port")