rplotcurve-fittingexponential

fit exponential decay in increase form in R


I want to fit a function in the increase form of exponential decay (or asymptotic curve), such that:

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

I've read on this page about expn() function, but simply can't find it (or a nls package). All I found was a nlstools package, but it has no expn(). I tried with the usual nls and exp function, but I only get increasing exponentials...

I want to fit the graph like below (drawn in Paint), and I don't know where the curve should stabilize (Richness = C). Thanks in advance.

asymptotic curve


Solution

  • This should get you started. Read the documentation on nls(...) (type ?nls at the command prompt). Also look up ?summary and ?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)