在R中以增量形式拟合指数衰减

时间:2014-03-06 16:50:32

标签: r plot curve-fitting exponential

我希望以指数衰减(或渐近曲线)的增量形式拟合函数,这样:

Richness = C*(1-exp(k*Abundance))  # k < 0

我已在this page上阅读expn()函数,但根本无法找到它(或nls包)。我发现的只是一个nlstools软件包,但没有expn()。我尝试使用通常的nlsexp函数,但我只是增加了指数......

我想要拟合如下图(在Paint中绘制),我不知道曲线应该在哪里稳定(Richness = C)。提前谢谢。

asymptotic curve

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

谢谢,jlhoward。在阅读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)))

但我通过反复试验找到了初始值。所以你的解决方案看起来更好:)

编辑:结果:

enter image description here