The nls
function is used to perform a non-linear regression. It uses iterative processes to get estimates of the parameters of your model. In theory these iterative processes are gradually approaching the actual value of the function parameter that best fits your data.
The problem with nonlinear regressions is that they can, in extreme cases, have no solution. That is, the algorithm that is looking for the estimates for the parameters of your model may never converge, especially if the model to be adjusted to the data is poorly specified. To avoid this, it is necessary to enter stop criteria for the algorithm. Your code has one of them, called maxiter = 1000
. It means that if the nls
function does not converge in 1,000 iterations, the algorithm should stop.
The error that occurred in your example refers to another stop criterion, called minFactor
. It tells you how small is the step used to achieve the convergence of parameters (more information in this link ). By default, the nls
function uses a step equal to 1/1024 (that is, (1/2) ^ 10).
So just play with the values of maxiter
and minFactor
, within nls.control
, to make your algorithm converge. See below 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% w / w% to (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 fit was quite reasonable. Notice how the predicted curve closely matches the original data:
plot(temp, taxa)
lines(temp, predict(fit, temp))