Licensed under the Creative Commons attribution-noncommercial license (http://creativecommons.org/licenses/by-nc/3.0/). Please share & remix noncommercially, mentioning its origin. |
This lab will be somewhat shorter in terms of “R stuff” than the previous labs, because more of the new material is algebra and calculus than R commands. Try to do a reasonable amount of the work with paper and pencil before resorting to messing around in R.
Here are some R commands used to generate different versions of the Ricker curve, . They just use curve, with add=FALSE (the default, which draws a new plot) and add=TRUE (adds the curve to an existing plot), particular values of from and to, and various graphical parameters (ylim ( limits), ylab (-axis label), lty (line type)).
The only new thing in this figure is the use of expression to add a mathematical formula to an R graphic. text(x,y,"x^2") puts x^2 on the graph at position ; text(x,y,expression(x^2)) (no quotation marks) puts on the graph. See ?plotmath or ?demo(plotmath) for (much) more information. The adj=0 argument just changes the alignment of the text.
An alternate way of plotting the exponential parts of this curve:
or, since both exponential vectors are the same length, we could cbind them together and use matplot:
The advantage of curve() is that you don’t have to define any vectors: the advantage of doing things the other way arises when you want to keep the vectors around to do other calculations with them.
Exercise 1 *: Draw a set of sinusoidal curves (cosine and sine, which are sin and cos in R, and which take arguments in radians — 0 to ) with different amplitudes and phases, in different colors:
Exercise 2 **: Construct a curve of some sort (your choice) that has a maximum at (, ). Write the equation, draw the curve in R, and explain how you got there.
The ifelse command in R is useful for constructing piecewise functions. Its basic syntax is ifelse(condition,value_if_true,value_if_false), where condition is a logical vector (e.g. x>0), value_if_true is a vector of alternatives to use if condition is TRUE, and value_if_false is a vector of alternatives to use if condition is FALSE. If you specify just one value, it will be expanded (recycled in R jargon) to be the right length. A simple example:
These commands produce a warning message, but it’s OK to ignore it since you know you’ve taken care of the problem (if you said sqrt(ifelse(x<0,0,x)) instead you wouldn’t get a warning: why not?)
Here are some examples of using ifelse to generate (1) a simple threshold; (2) a Holling type I or “hockey stick”; (3) a more complicated piecewise model that grows exponentially and then decreases linearly; (4) a double-threshold model.
The double-threshold example (nested ifelse commands) probably needs more explanation. In words, this command would go “if is less than 2, set to 1; otherwise (), if is less than 4 (i.e. ), set to 3; otherwise (), set to 5”.
A double piecewise linear example (the expressions are complicated because I wanted to make sure the curve would be continuous):
For simpler applications, you can use pmin (“parallel minimum”) and pmax (“parallel maximum”) to put “floors” and “ceilings” on functions. pmin/pmax will either take two vectors, compare them element-by-element, and take the min/max of the two vectors at each position, or they can take a scalar (a single number) and a vector and return the min/max of the scalar and the vector at each element. For example:
Exercise 3 *: The critical vaccination threshold for eradicating an infectious disease with reproductive number is , if is ; otherwise the disease goes extinct by itself and the critical threshold is zero. Use curve and pmin to draw a graph of the critical vaccination threshold as a function of from to 10.
Exercise 4 *: Draw the hockey-stick function above using pmax instead of ifelse.
The last part of this exercise is an example of figuring out a function. The population-dynamics form of the logistic equation is
(1) |
where is carrying capacity, is intrinsic population growth rate, and is initial density.
At , and this reduces to (as it had better!)
Finding the derivative with respect to time is pretty ugly, but it will to reduce to something you may already know. Writing the equation as and using the chain rule we get (). The derivative is . At , , and . So this all comes out to
which should be reminiscent of intro. ecology: we have rediscovered, by working backwards from the time-dependent solution, that the logistic equation arises from a linearly decreasing per capita growth rate.
If is small we can do better than just getting the intercept and slope.
Exercise 5 *: show that if and are both very small, . (Start by showing that dominates all the other terms in the denominator.)
If is small, this reduces (because ) to , a linear increase with slope . Convince yourself that this matches the expression we got for the derivative when is small.
For large , convince yourself that the value of the function approaches and (by revisiting the expressions for the derivative above) that the slope approaches zero.
The half-maximum of the logistic curve occurs when the denominator (which I was calling “stuff” on the previous page, after eq. 1) is 2; we can solve for (getting to and taking logarithms on both sides) to get .
We have two options:
(note that we have to use x and not t in the expression for the logistic).
We can do more with this plot: let’s see if our conjectures are right. Using abline and curve to add horizontal lines to a plot to test our estimates of starting value and ending value, vertical and horizontal lines that intersect at the half-maximum, a line with the intercept and initial linear slope, and a curve corresponding to the initial exponential increase:
Exercise 6 *: The Holling type III functional response () is useful when (e.g.) the predation rate initially accelerates with prey density, but then saturates. However, the parameters (curvature at low prey density) and (the reciprocal of the half-maximum squared) aren’t easy to read off a graph. Reparameterize the Holling type III function in terms of its asymptote and half-maximum.