`nls` 拟合错误:无论起始值如何,始终达到最大迭代次数 [英] `nls` fitting error: always reach maximum number of iterations regardless starting values

查看:77
本文介绍了`nls` 拟合错误:无论起始值如何,始终达到最大迭代次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using this parametrization for a growth curve logistic model

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

I created some points with: 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))

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

Can someone tell me how can I have a fitting error if create the data with the model starters?

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时会遇到麻烦.

Also, avoid using $ in model formula, otherwise you will get into trouble when using predict later.

这篇关于`nls` 拟合错误:无论起始值如何,始终达到最大迭代次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆