gpt4 book ai didi

r - R : determining coefficients for nls

转载 作者:行者123 更新时间:2023-12-02 10:58:19 27 4
gpt4 key购买 nike

我正在尝试使用nls函数在R中拟合此非线性模型。

这是我要拟合的数据:

tab2 = data.frame(n = c(10,100,1000,10000,100000), Time = c(3.989220e-03, 
1.994681e-02, 3.311172e-01, 5.142252e+00, 1.314725e+03))

我们看到时间正在成倍增长,所以我想使用 nls对此建模。到目前为止,这是我尝试过的:
mod4 = nls(Time ~ exp(a + b*n), data = tab2, start = list(a = -3, b = 0))

但是,这不起作用,并且出现以下错误消息:
Error in numericDeriv(form[[3L]], names(ind), env) : Missing value or an 
infinity produced when evaluating the model

我不知道为什么会这样,但我想这与这些初始值有关吗?我通过使用lm估计模型来获得它们:
mod3 = lm(log(Time) ~ n, data = tab2); coef(mod3)
(Intercept) n
-2.5908574883 0.0001010623

由于这几乎是同一模型,因此我认为系数会匹配,但是当我使用接近它们的值作为初始值时, nls模型不起作用。

关于为什么发生这种情况有什么建议吗?

最佳答案

需要更好的起始值。如果我们取两边的对数,则它成为线性模型,任何起始值都应起作用,因此只需使用a = b = 1即可。 (我们可以交替使用lm。)然后将第一个模型的系数用作原始模型的起始值。

fo1 <- log(Time) ~ a + b*n
fm1 = nls(fo1, data = tab2, start = list(a = 1, b = 1))

fo2 <- Time ~ exp(a + b * n)
fm2 <- nls(fo2, tab2, start = coef(fm1))

fm2

给予:
Nonlinear regression model
model: Time ~ exp(a + b * n)
data: tab2
a b
3.567e-01 6.825e-05
residual sum-of-squares: 10.84

Number of iterations to convergence: 7
Achieved convergence tolerance: 2.051e-06

关于r - R : determining coefficients for nls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53398499/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com