非线性拟合r

时间:2017-05-25 14:45:05

标签: r nls

我的数据包含两列 - 时间和累计数字如下:

time <- c(1:14)
cum.num <- c(20, 45, 99, 195, 301, 407, 501, 582, 679, 753, 790, 861, 1011, 1441)

我的非线性函数是:

B/(B*C*exp(-A*B*time) + 1)

我的目标是使用非线性方法对数据建模。我尝试了以下方法:

m1 < -nls(cum.num ~ B/((B*C)*exp(-A*B*time) + 1)

我尝试了几个初始值但出现以下错误:

Error in nls(cum.vul ~ B/((B * C) * exp(-A * B * time) + 1), 
start = list(B = 60,  : singular gradient

1 个答案:

答案 0 :(得分:1)

当发生这种情况时,您通常需要做一些侦探工作来了解您的函数的参数,并通过查看图表来粗略估计值。

time <- c(1:14)
cum.num <- c(20, 45, 99, 195, 301, 407, 501, 582, 679,
           753, 790, 861, 1011, 1441)
  • 首先要注意的是,函数的顶级结构是双曲线的(即1/(1+x)形式)。如果我们反转y值,那么可视化数据和估算参数会更容易,因此我们有1/cum.num ~ C*exp(-A*B*time) + 1/B
plot(time,1/cum.num,log="y",xlim=c(0,14),ylim=c(0.0005,0.5))

(以log-y比例绘制,并根据我在下面发现的内容扩展y轴限制...)

enter image description here

  • 从上面的等式中,我们知道渐近线(大y的值)应为1 / B,因此我们有1/B ~ 0.001B ~ 1000
  • 在时间0,值应为C + 1/B = C + 0.001。查看图表,我们有C ~ 0.5
  • 最后,1/(A*B)是减少的特征尺度(即电子折叠减少的时间)。它看起来像电子折叠时间〜1(从t = 1到t = 2)所以1/(A*B) ~ 1所以A ~ 0.001

使用这些起始值:

m1 <- nls(cum.num ~ B/((B*C)*exp(-A*B*time) + 1),
                   start=list(A=0.001,B=1000,C=0.5))

似乎工作。绘制预测值:

tpred <- seq(0,14,length=101)
cpred <- predict(m1,newdata=data.frame(time=tpred))
par(las=1,bty="l")
plot(time,cum.num)
lines(tpred,cpred)

enter image description here