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

标签 r regression nls non-linear-regression

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

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/

相关文章:

matlab - 为什么我会收到尝试使用Matlab交叉验证PCR的错误消息?

r - 初始参数估计时 nls 奇异梯度矩阵的误差

r - 使用 R,将可变行数中的文本组合成单个文本元素

python - 用Python预测正弦波

r - R中不同时间序列数据值的互相关

python - 在计算最小二乘法时,为什么要添加 1 的向量?

r - 用 ggpmisc 显示 nls 模型的方程

R 脚本问题 - is.na 告诉我条件长度 > 1

r - 带有彩色边框但没有填充的ggplot2

r - 从 R 调用时,Libreoffice 给出 "Application Error"