【问题标题】:fit exponential decay in increase form in R拟合R中增加形式的指数衰减
【发布时间】:2014-04-09 11:08:32
【问题描述】:
我想以指数衰减(或渐近曲线)的增加形式拟合一个函数,这样:
Richness = C*(1-exp(k*Abundance)) # k < 0
我在this page 上阅读过有关expn() 函数的信息,但根本找不到它(或nls 包)。我发现的只是一个 nlstools 包,但它没有expn()。我尝试使用通常的 nls 和 exp 函数,但我只会得到递增的指数......
我想拟合如下图(在 Paint 中绘制),但我不知道曲线应该在哪里稳定(Richness = C)。提前致谢。
【问题讨论】:
标签:
r
plot
curve-fitting
exponential
【解决方案1】:
谢谢,杰霍华德。在阅读了 shujaa 发送的链接后,我遇到了类似的事情。
R <- function(a, b, abT) a*(1 - exp(-b*abT))
form <- Richness ~ R(a,b,Abundance)
fit <- nls(form, data=d, start=list(a=20,b=0.01))
plot(d$Abundance,d$Richness, xlab="Abundance", ylab="Richness")
lines(d$Abundance, predict(fit,list(x=d$Abundance)))
不过,我通过反复试验找到了初始值。所以你的解决方案看起来更好:)
编辑:结果:
【解决方案2】:
这应该可以帮助您入门。阅读nls(...) 上的文档(在命令提示符下键入?nls)。还要查找 ?summary 和 ?predict。
set.seed(1) # so the example is reproduceable
df <- data.frame(Abundance=sort(sample(1:70,30)))
df$Richness <- with(df, 20*(1-exp(-0.03*Abundance))+rnorm(30))
fit <- nls(Richness ~ C*(1-exp(k*Abundance)),data=df,
algorithm="port",
start=c(C=10,k=-1),lower=c(C=0,k=-Inf), upper=c(C=Inf,k=0))
summary(fit)
# Formula: Richness ~ C * (1 - exp(k * Abundance))
#
# Parameters:
# Estimate Std. Error t value Pr(>|t|)
# C 20.004173 0.726344 27.54 < 2e-16 ***
# k -0.030183 0.002334 -12.93 2.5e-13 ***
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
# Residual standard error: 0.7942 on 28 degrees of freedom
#
# Algorithm "port", convergence message: relative convergence (4)
df$pred <- predict(fit)
plot(df$Abundance,df$Richness)
lines(df$Abundance,df$pred, col="blue",lty=2)