R语言使用nls拟合,为啥总说循环次数大于50

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R语言使用nls拟合,为啥总说循环次数大于50相关的知识,希望对你有一定的参考价值。

使用R语言进行模型拟合,start那个里面应该填什么呀,运行后总是提示错误“循环次数大于50”,这是为什么呀

nls的数据源必须有误差。不能精确等于公式返回值(零残差)。循环次数大于50通常是使用 函数精确返回值 作为数据源去拟合函数。必须给y值加上随机误差。


z=function(x,a,b)a*sin(x)+b*cos(x)
x=seq(1,10,9/500)
y=z(x,1,1) # a=1 b=1 是期望拟合出的结果。
cor=data.frame(x=x,y=y)
cor$res=runif(length(cor$x),min=-0.005,max=0.005)
cor$yres=cor$y+cor$res
#yres =y加上随机误差,y是精确返回值
> nls(cor$yres~z(cor$x,a,b),data=cor,start=list(a=0.8,b=1.3))
Nonlinear regression model
  model: cor$yres ~ z(cor$x, a, b)
   data: cor
     a      b 
0.9999 1.0002 
 residual sum-of-squares: 0.004213

Number of iterations to convergence: 1 
Achieved convergence tolerance: 2.554e-07

#使用精确返回值拟合就会出错。
> nls(cor$y~z(cor$x,a,b),data=cor,start=list(a=1,b=1))
Error in nls(cor$y ~ z(cor$x, a, b), data = cor, start = list(a = 1, b = 1)) : 
  循环次数超过了50这个最大值
参考技术A 每张身份证.注册财付通的次数最大只能注册十次.比如说.你这个身份证注册了财付通.然后注销了.再第二次用这身份证注册财付通. 这身份证也就使用了两次了.无论是你注销.重...身份已超注册次数能解除吗

使用 nls 函数错误拟合

【中文标题】使用 nls 函数错误拟合【英文标题】:Wrong Fit using nls function 【发布时间】:2017-12-25 08:51:19 【问题描述】:

当我尝试拟合指数衰减并且我的 x 轴有十进制数时,拟合永远不会正确。以下是我的数据:

exp.decay = data.frame(time,counts)
   time counts
1   0.4   4458
2   0.6   2446
3   0.8   1327
4   1.0    814
5   1.2    549
6   1.4    401
7   1.6    266
8   1.8    182
9   2.0    140
10  2.2    109
11  2.4     83
12  2.6     78
13  2.8     57
14  3.0     50
15  3.2     31
16  3.4     22
17  3.6     23
18  3.8     20
19  4.0     19
20  4.2      9
21  4.4      7
22  4.6      4
23  4.8      6
24  5.0      4
25  5.2      6
26  5.4      2
27  5.6      7
28  5.8      2
29  6.0      0
30  6.2      3
31  6.4      1
32  6.6      1
33  6.8      2
34  7.0      1
35  7.2      2
36  7.4      1
37  7.6      1
38  7.8      0
39  8.0      0
40  8.2      0
41  8.4      0
42  8.6      1
43  8.8      0
44  9.0      0
45  9.2      0
46  9.4      1
47  9.6      0
48  9.8      0
49 10.0      1

fit.one.exp <- nls(counts ~ A*exp(-k*time),data=exp.decay, start=c(A=max(counts),k=0.1))
plot(exp.decay, col='darkblue',xlab = 'Track Duration (seconds)',ylab = 'Number of Particles', main = 'Exponential Fit')
lines(predict(fit.one.exp), col = 'red', lty=2, lwd=2) 

我总是得到这种奇怪的配合。在我看来,拟合无法识别正确的 x 轴,因为当我使用不同的数据集时,x 轴(时间)中只有整数,拟合有效!我不明白为什么它与不同的单位不同。

【问题讨论】:

因为这是您的第一个问题:请不要忘记点赞有用的答案,并点击答案旁边的绿色勾号接受最好的答案;然后支票变成绿色 【参考方案1】:

你需要做一个小的修改:

lines(predict(fit.one.exp), col = 'red', lty=2, lwd=2)

应该是

lines(exp.decay$time, predict(fit.one.exp), col = 'red', lty=2, lwd=2)

这样,您可以确保在横坐标上绘制所需的值。

我是这样测试的:

data = read.csv('exp_fit_r.csv')

A0 <- max(data$count)
k0 <- 0.1

fit <- nls(data$count ~ A*exp(-k*data$time), start=list(A=A0, k=k0), data=data)

plot(data)
lines(data$time, predict(fit), col='red')

这给了我以下输出:

如您所见,拟合很好地描述了实际数据,它只是根据正确的横坐标值绘制的问题。

【讨论】:

以上是关于R语言使用nls拟合,为啥总说循环次数大于50的主要内容,如果未能解决你的问题,请参考以下文章

r语言多个gam拟合图怎么放在 一个坐标里

r语言怎么做每一列和第一列线性回归

nls:在确定的迭代次数中循环和中断

尝试使用 ggplot2 的 geom_smooth() 显示原始和拟合数据(nls + dnorm)

使用 nls 函数错误拟合

在 R 中拟合函数