# Fit a logistic growth curve with a custom function in R

(Tag suggestions welcome, I was looking for 'logistic-growth' or 'sigmoid-growth' but those tags don't exist)

I'm working through an exercise on this online textbook, 'Try it #3', almost halfway down the page.

Table 6 shows the population, in thousands, of harbor seals in the Wadden Sea over the years $$1997$$ to $$2012$$. $$\begin{array}{|c|c|c|c|} \hline \text{Year}& \text{Seal Population (Thousands)} &\text{Year} & \text{Seal Population (Thousands)} \\ \hline 1997 & 3,493&2005&19,590 \\ \hline 1998 & 5,282&2006&21,955\\ \hline 1999 & 6,357&2007&22,862\\ \hline 2000 & 9,201&2008&23,869\\ \hline 2001 & 11,224&2009&24,243\\ \hline 2002 & 12,964&2010&24,344\\ \hline 2003 & 16,226&2011&24,919\\ \hline 2004 & 18,137&2012&25,108\\ \hline \end{array}$$

(a) Let $$x$$ represent time in years starting with $$x=0$$ for the year $$1997$$. Let $$y$$ represent the number of seals in thousands. Use logical regression to fit a model to this data.
(b) Use the model to predict the seal population for the year 2020.
(c) To the nearest whole number, what is the limiting value of this model?

I would like to attempt this with a custom function in R. Here's the same data as part of a data frame:

ti3_df <- data.frame(
x = 0:15,
y = c(3.493, 5.282, 6.357, 9.201, 11.224, 12.964, 16.226, 18.137,
19.590, 21.955, 22.862, 23.869, 24.243, 24.344, 24.919, 25.108)
)


The textbook says that the logistic function is:

$$y=\frac{c}{1+ae^{-bx}}$$

I know that C is the carrying capacity or upper limit. But this is not provided in the question. I don't know if it's actually possible to solve this manually using the formula approach since I'm fitting a curve aka 'modeling' the data rather than calculating an exact point.

The books says 'The initial value of the model is $$\frac{c}{1+a}$$'

I wanted to build a function of the form:

my_log_growth_func <- function(c, a, dataframe) {... r code here}


Where I would calculate b and x. But I think I'm missing something and am unsure if it's possible to fit a model using the information I have without a calculator or package.

How can I fit a model to these data?

• You have "logical regression" where you intend "logistic regression" in your recital of the problem statement. Mar 19 at 21:01
• @EricTowers that typo is on the book authors! Mar 19 at 21:04
• Copy-pasting from the link you gave: "Use logistic regression to fit a model to these data." "logistic". Mar 19 at 21:09
• @EricTowers I think someone edited my post, I initially pasted a screen shot of the Q here. Mar 19 at 23:28
• That edit is entirely justified -- a screen shot cannot be found by search, so a future student with the same question cannot find that it is already answered. Mar 20 at 0:14

I suppose that you want to adjust $$(a,b,c)$$ in order to have the best fit.

Since $$y=\frac{c}{1+ae^{-bx}}$$

• as you wrote $$c$$ is a bit larger than the largest $$y$$; say $$c\sim 25.5$$ for an intial guess

• for $$x=0$$ you have (this is the hint) $$y=\frac c{1+a}$$; so, for $$c=25.5$$, this gives a guess $$a\sim 6.3$$

Now, to get an estimate of $$b$$ write (for the time being) $$\frac {c-y} {ay}=e^{-b x}\implies \log \left(\frac{a y}{c-y}\right)= b x$$ which a linear regression without intercept (easy). This would give $$b\sim 0.4$$.

At this point, we have all required estimates for starting a nonlinear regression. It will works like a charm and, with $$R^2=0.999737$$ (very high), you should get $$\begin{array}{clclclclc} \text{} & \text{Estimate} & \text{Standard Error} & \text{Confidence Interval} \\ a & 6.1137 & 0.2840 & \{5.4948,6.7325\} \\ b & 0.3852 & 0.0114 & \{0.3603,0.4101\} \\ c & 25.657 & 0.2345 & \{25.146,26.168\} \\ \end{array}$$

As you can notice, the guesses were quite good.

• When you fitted this regression in r did you use lm() or another function? Mar 19 at 20:46
• @DougFir : R's lm() fits linear models, models that are weighted sums of functions of the independent variable. The weights are the parameters to be fit, so a realization of the model is a dot product of a vector of terms and a vector of fitted coefficients. The logistic model is not a weighted sum of functions of the independent variable, so lm() is not the right choice of function. Maybe glm(). Mar 19 at 21:08

Yes, if $$y= \frac{c}{1+ ae^{bx}}$$ then when x= 0, $$y= \frac{c}{1+ a(e^0)}= \frac{c}{1+ a}$$.

You want to find values for a, b, and c so that you get approximately the values given. You realize, I hope, that there is no one "correct" answer. You can get different answers using different approximation techniques.

Since the problem is to determine three values, a, b, and, c, you need three equations. I suggest using three points as far apart as possible, the first point, (1997, 3493), so $$3493= \frac{c}{1+ ae^{1997b}}$$ the middle point, 2005, and the last point, 2012. Unfortunately, that data seems to have been cut off,