The function nls
is used for nonlinear regression. It uses iterative processes to obtain estimates of the parameters of your model. In theory, these iterative processes gradually approach the real value of the function parameter that best fits your data.
The problem with nonlinear regressions is that they can, in extreme cases, be unsolved. That is, it may be that the algorithm that is looking for the estimates for the parameters of your model never fits, especially if the model to be adjusted to the data is poorly specified. To avoid this, you need to inform halting criteria for the algorithm. Your code has one of them, called maxiter = 1000
. He means that if the function nls
not converge in 1,000 iterations, the algorithm must stop.
The error in your example refers to another stopping criterion, called minFactor
. It tells how small is the step used to achieve convergence of parameters (more information at this link). By default, the function nls
uses a step equal to 1/1024 (i.e., (1/2) 10).
So just play with the values of maxiter
and minFactor
, inside nls.control
, to make your algorithm fit. See below for my suggestion:
fit <- nls (taxa~exp(p*temp)-exp(p*TL-(TL-temp)/delta)+z,data=df,
start=list (p=0.2, TL=30, delta=4.9,z=-0.5),
control=nls.control(maxiter = 10000, minFactor=(1/2)^30))
summary(fit)
Formula: taxa ~ exp(p * temp) - exp(p * TL - (TL - temp)/delta) + z
Parameters:
Estimate Std. Error t value Pr(>|t|)
p 0.09981 0.51807 0.193 0.851505
TL 31.04889 5.73290 5.416 0.000424 ***
delta 6.33825 18.63529 0.340 0.741569
z -1.73670 7.20604 -0.241 0.814952
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.4274 on 9 degrees of freedom
Number of iterations to convergence: 1057
Achieved convergence tolerance: 9.783e-06
I decreased the minFactor
for (1/2) 30 and increased the maximum number of iterations to 10,000. Note that the algorithm required 1057 iterations to converge, more than the 1,000 originally defined.
Note, also, that visually the adjustment became quite reasonable. Notice how the predicted curve approaches very well the original data:
plot(temp, taxa)
lines(temp, predict(fit, temp))
Your suggestions helped me and very much. Problem solved. Thank you.
– spiderman123
Consider accepting the reply since it was useful for you to solve your problem.
– Rafael Cunha