如何使用nls函数解决一个错误:Convergence failure: false convergence (8)

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

我在使用 nls 函数时遇到一点问题。你能帮我理解并解决下面的问题吗?请注意,我可以为 df1 数据库生成,但不能为 df2 数据库生成。如何解决?

下面的可执行代码:

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)

来自?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.

果然,使用 scaleOffset = 1 测试您的 mod2 示例(如 ?nls 的示例所示)仍然给出 Convergence failure 错误。

使用scaleOffset = 1的默认算法。这至少需要R 4.1.2:

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

给予:

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

或使用 lm

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

给予:

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

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

请注意,Days^2 并不重要:

summary(fm2)

给予:

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