简体   繁体   English

r中的非线性拟合

[英]Non linear fit in r

My data consist of two columns - time and cumulative number like below: 我的数据包括两列-时间和累积数,如下所示:

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

My non linear function is: 我的非线性函数是:

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

My objective is to model my data using non linear method. 我的目标是使用非线性方法对数据进行建模。 I tried the following: 我尝试了以下方法:

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

I have tried several initial values but got the following error: 我尝试了几个初始值,但出现以下错误:

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

When this happens you generally have to do a bit of detective work to understand the parameters of your function and get a rough estimate of the values by looking at plots. 发生这种情况时,通常需要做一些侦探工作,以了解函数的参数并通过查看图来粗略估计值。

time <- c(1:14)
cum.num <- c(20, 45, 99, 195, 301, 407, 501, 582, 679,
           753, 790, 861, 1011, 1441)
  • The first thing to notice is that the top-level structure of the function is hyperbolic (ie, of the form 1/(1+x) ). 首先要注意的是,函数的顶级结构是双曲线的(即形式为1/(1+x) )。 It will be easier to visualize the data and estimate parameters if we invert the y value, so that we have 1/cum.num ~ C*exp(-A*B*time) + 1/B . 如果我们将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))

(plotting on a log-y scale, and extending the y-axis limits, based on what I discovered below ...) (根据我在下面发现的内容,进行对数y缩放,并扩展y轴限制...)

在此处输入图片说明

  • From the equation above, we know that the asymptote (value for large y) should be 1/B, so we have 1/B ~ 0.001 or B ~ 1000 . 从上面的等式中,我们知道渐近线(大y的值)应为1 / B,因此我们有1/B ~ 0.001B ~ 1000
  • at time 0, the value should be C + 1/B = C + 0.001 . 在时间0,该值应为C + 1/B = C + 0.001 Looking at the graph, we have C ~ 0.5 看图,我们有C ~ 0.5
  • Finally, 1/(A*B) is the characteristic scale of decrease (ie the time for an e-fold decrease). 最后, 1/(A*B)是下降的特征尺度(即e倍下降的时间)。 It looks like the e-folding time ~ 1 (from t=1 to t=2) so 1/(A*B) ~ 1 so A ~ 0.001 看起来电子折叠时间〜1(从t = 1到t = 2)所以1/(A*B) ~ 1所以A ~ 0.001

Use these starting values to fit: 使用以下起始值来拟合:

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

Seems to work. 似乎可以工作。 Plot the predicted values: 绘制预测值:

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM