gpt4 book ai didi

r - `nls` 拟合错误 : always reach maximum number of iterations regardless starting values

转载 作者:行者123 更新时间:2023-12-02 06:30:15 26 4
gpt4 key购买 nike

将此参数化用于增长曲线逻辑模型

enter image description here

我创建了一些点:K =0.7 ; y0=0.01 ; r =0.3

df = data.frame(x= seq(1, 50, by = 5))
df$y = 0.7/(1+((0.7-0.01)/0.01)*exp(-0.3*df$x))

enter image description here

谁能告诉我如果使用模型启动器创建数据怎么会出现拟合错误?

fo = df$y ~ K/(1+((K-y0)/y0)*exp(-r*df$x))

model<-nls(fo,
start = list(K=0.7, y0=0.01, r=0.3),
df,
nls.control(maxiter = 1000))
Error in nls(fo, start = list(K = 0.7, y0 = 0.01, r = 0.3), df, nls.control(maxiter = 1000)) :
number of iterations exceeded maximum of 1000

最佳答案

不要对人工“零残差”数据使用“nls”。,如 ?nls 中所述。

set.seed(0)
x <- seq(1, 50, by = 5)
y <- 0.7 / (1 + ((0.7 - 0.01) / 0.01) * exp(-0.3 * x))
y <- y + rnorm(length(x), sd = 0.05) ## add Gaussian error!!
dat <- data.frame(x = x, y = y); rm(x, y)
with(dat, plot(x, y))

fit <- nls(y ~ K / (1 + ((K - y0) / y0) * exp(-r * x)), data = dat,
start = list(K = 0.7, y0 = 0.01, r = 0.3))

#Nonlinear regression model
# model: y ~ K/(1 + ((K - y0)/y0) * exp(-r * x))
# data: dat
# K y0 r
#0.70013 0.01841 0.27950
# residual sum-of-squares: 0.02851
#
#Number of iterations to convergence: 12
#Achieved convergence tolerance: 4.145e-06

另外,避免在模型公式中使用$,否则以后使用predict时会遇到麻烦。

关于r - `nls` 拟合错误 : always reach maximum number of iterations regardless starting values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40230339/

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