简体   繁体   English

如何在 R 中绘制 inc 指数衰减?

[英]How to graph inc exponential decay in R?

My prof decided that our first experience with coding was going to be trying to fit the function z(t) = A(1-e^(-t/T)) into a given data-set from class using R.我的教授决定,我们的第一次编码经验是尝试将 function z(t) = A(1-e^(-t/T)) 拟合到来自 class 的给定数据集中,使用 ZE1E1D3D40087AF27D9EE042。 I'm completely lost.我完全迷路了。 I keep using lm and nls functions, without quite knowing how they work.我一直在使用lmnls函数,但不太清楚它们是如何工作的。 So far, I have the data graphed but I have no clue how to get any sort of line more complicated than到目前为止,我已经绘制了数据,但我不知道如何获得比

mod3<-lm(y~I(x^1/5))
pre3<-predict(mod3)
lines(pre3)

to sum up: how do I find the A and T parameters?总结一下:如何找到 A 和 T 参数? Do I use nls for the formula?我是否将nls用于公式? Anything helps.任何事情都有帮助。 I'll include a picture of the graph and the data.我将包括图表和数据的图片。 Please ignore the random lines on the plot.请忽略 plot 上的随机线。 graph depicting my dataset dataset I have to use描绘我必须使用的数据集数据集的图表

One could attempt transform your expression into a linear relationship, but sometimes it is easier to just let the computer do the work.可以尝试将您的表达式转换为线性关系,但有时让计算机完成工作更容易。 As mention in the comments, R has the nls function to perform the nonlinear regression.正如评论中提到的,R 具有nls function 来执行非线性回归。

Here is an example using some dummy data.这是一个使用一些虚拟数据的示例。 The supply the nls function with your equation, the data frame containing the data and supply it with the initial estimates of the parameters.nls function 提供您的方程、包含数据的数据框,并为其提供参数的初始估计值。

See comments for additional details.有关其他详细信息,请参阅评论。

#create dummy data
A= 0.8
T1 = 13

t <- seq(2, 50, 3)
z <- A*(1-exp(-t/T1)) 
z<- z +rnorm(length(z), 0, 0.005) #add noise

#starting data frame
df <-data.frame(t, z)

#solve non-linear model
model <- nls(z ~ A*(1-exp(-t/Tc)), data=df, start = list(A=1, Tc=1))
print(summary(model))
#predict
pred_y <-predict(model, data.frame(t))

#plot
plot(x=t, y=z)
lines(y=pred_y, x= t, col="blue")

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

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