Initial values in the nls function in R

3

Hello, I have the following code in R

temp<-c(10,12,14,15,17,20,22,23,25,26,28,29,30)
taxa<-c(2,4.5,6.1,7.0,11,16,19,21,22,11,2,0.1,0.001)
taxa<-taxa/9
plot(temp,taxa)
df<- data.frame(temp,taxa)
x<- seq(1,45,length=100)
library(nlsMicrobio)
preview(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))
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 = 1000))
summary(fit)

When running fit the following error is occurring:

Error in nls(taxa ~ exp(p * temp) - exp(p * TL - (TL - temp)/delta) +  : 
  fator de passos 0.000488281 reduzido abaixo de 'minFactor' de 0.000976562

Can anyone help me with this mistake? Thank you.

    
asked by anonymous 19.01.2018 / 12:28

1 answer

7

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))

    
19.01.2018 / 13:35