How to solve an error using the nls function: Convergence failure: false convergence (8)

Viewed 191

I'm having a little problem using the nls function. Could you help me understand and solve the problem below? Note that I can generate for df1 database, but not for df2 database. How to solve?

Executable code below:

df1<-structure(list(Category = c("ABC", "ABC", "ABC"), Days=c(42,43,44),  Numbers = c(456.589136904762, 456.589136904762, 456.589136904762)), class= "data.frame", row.names = c(NA, -3L))
       
mod1 <- nls(Numbers ~ b1*Days^2+b2,start = list(b1 = 0,b2 = 0),data = df1, algorithm = "port")

> mod1
Nonlinear regression model
  model: Numbers ~ b1 * Days^2 + b2
   data: df1
       b1        b2 
1.513e-08 4.566e+02 
 residual sum-of-squares: 1.422e-10
Algorithm "port", convergence message: X-convergence (3)

df2<-structure(list(Category = c("ABC", "ABC", "ABC"), Days=c(42,43,44),  Numbers = c(456.594054487179, 456.589136904762, 456.589136904762)), class= "data.frame", row.names = c(NA, -3L))

mod2 <- nls(Numbers ~ b1*Days^2+b2,start = list(b1 = 0,b2 = 0),data = df2, algorithm = "port")

Error in nls(Numbers ~ b1 * Days^2 + b2, start = list(b1 = 0, b2 = 0),  : 
Convergence failure: false convergence (8)
2 Answers

From ?nls

Warning

The default settings of nls generally fail on artificial “zero-residual” data problems.

The nls function uses a relative-offset convergence criterion that compares the numerical imprecision at the current parameter estimates to the residual sum-of-squares. This performs well on data of the form y = f(x, θ) + eps (with var(eps) > 0).

It fails to indicate convergence on data of the form y = f(x, θ) because the criterion amounts to comparing two components of the round-off error. To avoid a zero-divide in computing the convergence testing value, a positive constant scaleOffset should be added to the denominator sum-of-squares; it is set in control, as in the example below; this does not yet apply to algorithm = "port".

The algorithm = "port" code appears unfinished, and does not even check that the starting value is within the bounds. Use with caution, especially where bounds are supplied.

Sure enough, testing your mod2 example with scaleOffset = 1 (as shown in an example of ?nls) still gives the Convergence failure error.

Use the default algorithm with scaleOffset = 1. This requires at least R 4.1.2:

fm <- nls(Numbers ~ b1 * Days^2 + b2, start = list(b1 = 0, b2 = 0), data = df2, 
  control = list(scaleOffset = 1))
fm

giving:

Nonlinear regression model
  model: Numbers ~ b1 * Days^2 + b2
   data: df2
        b1         b2 
-2.848e-05  4.566e+02 
 residual sum-of-squares: 4.125e-06

Number of iterations to convergence: 1 
Achieved convergence tolerance: 9.769e-07

or use lm

fm2 <- lm(Numbers ~ I(Days^2), data = df2); fm2

giving:

Call:
lm(formula = Numbers ~ I(Days^2), data = df2)

Coefficients:
(Intercept)    I(Days^2)  
  4.566e+02   -2.848e-05  

Note that Days^2 is not significant:

summary(fm2)

giving:

Call:
lm(formula = Numbers ~ I(Days^2), data = df2)

Residuals:
         1          2          3 
 0.0008387 -0.0016582  0.0008194 

Coefficients:
              Estimate Std. Error   t value Pr(>|t|)    
(Intercept)  4.566e+02  3.091e-02 14774.311 4.31e-05 ***
I(Days^2)   -2.848e-05  1.670e-05    -1.706    0.338    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.002031 on 1 degrees of freedom
Multiple R-squared:  0.7442,    Adjusted R-squared:  0.4883 
F-statistic: 2.909 on 1 and 1 DF,  p-value: 0.3376
Related