Initial values in function nls in R

Asked

Viewed 698 times

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)

While 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

Could someone help me with this mistake? Thank you.

1 answer

8


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

inserir a descrição da imagem aqui

  • Your suggestions helped me and very much. Problem solved. Thank you.

  • 2

    Consider accepting the reply since it was useful for you to solve your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.