There are two ways of writing math in R, either inline or displayed.
In-line is used when you want to write math that is simple and short,
like if I wanted to write, \(C = \tau
r\), as the circumference of a circle in relation to its radius.
(See the Tau Manifesto.)
; For example, $C = \tau r$
gives you what I just typed.
This is what you did on last week’s problem set. To write displayed, use
two dollar signs and write the math in the middle. Displayed math create
a line break, which you use for more complex or important math; e.g.,
\[a^2 + b^2 = c^2.\] To write math in
display mode, you use two dollar signs; e.g.,
$$a^2 + b^2 = c^2$$
. For different symbols, letters, etc.
see the Resources page on
the course website and follow the link to \(\LaTeX\).
1.1 Using the \(\LaTeX\) guide as a
reference, take the solution from the continuous-time
density-independent growth mode, \(N_t =
N_0e^{rt}\), and algebraically solve for \(t\). For every algebraic step, please use a
new line within \(\LaTeX\) display
mode, like if I wanted to express \(E =
mc^2\) in terms of the speed of light, \(c\). Separate lines using \\
and, to align the equations, use \begin{aligned}
after the
first pair of dollar signed and \end{aligned}
before the
second pair of dollar signed and put an &
before each
equals sign to align them, so they look something like this: \[
\begin{aligned}
E &= mc^2 \\
\frac{E}{m} &= c^2 \\
c^2 &= \frac{E}{m} \\
c &= \sqrt[2]{\frac{E}{m}}.
\end{aligned}
\] 1.2. Do the same thing as above, but with the discrete-time
density-independent solution, \(N_t =
N_0\lambda ^ t\). Also, as a \(\LaTeX\) hint, use $\frac{}{}$
to write a fraction, where what you want in the numerator goes into the
first set of braces, and the denominator in the second.
1.3. Finally, we’ll cover the concept of equilibrium later in the semester. But it’s when the system (e.g., population) is not changing; i.e., where the rate of change is 0, or \(0 = dN/dt\). For the logistic equation, \(dN/dt = rN(K - N)/K\), turn it into a per-capita rate of change by dividing both sides by 0, then find the equilibrium; i.e., set the per-capita rate of change to 0 and solve for \(N\). For every algebraic step, please use a new line within \(\LaTeX\) display mode, like above.
2.1. Before we proceed in making graphs, I want to note that you are
free to use any method that you choose, but I would prefer that you use
plot()
in base R to make graphs in this course because (1)
what you learn in base R is transferable but specialized packages like
ggplot2
are not and (2) although there is no plotting in
base R that I cannot do (feel free to test this out) I cannot say the
same about ggplot2
and the rest of the tidy universe. (If
you don’t know what any of this means, feel free to ignore it.)
Onward: making plots in R is fundamentally simple. The three steps you
need for 2-dimensional graphs (i.e., have a horizontal and vertical
axis) are: (1) create a vector of numbers representing your \(x\) variable, (2) plug \(x\) into an equation, and (3) plot it using
plot(x = , y = )
where you plug in the names of your \(x\) and \(y\) variables. That’s really it.
Let’s try this by plotting a quadratic function, \(y = 0.5x^2 + 0.1x - 2\). To do this, I
would create a sequence of values of a variable \(x\), like
x_vec <- seq(from = -5, to = 5, length.out = 10)
and
make \(y\) a function of \(x\), like
y_vec <- 0.5*x_vec^2 + 0.1*x_vec - 2
. When I plot,
plot(x = x_vec, y = y_vec, type = "l", las = 1)
, I
get
Make this plot, but label the horizontal and vertical axes \(x\) and \(y\), respectively.
2.2. I want you to make a couple more plots. First, choose one to
plot either of the equations from 1.1. or 1.2., with the \(x\) variable being \(t\) and the \(y\) variable being \(N_t\). Make sure you make the
length.out
argument within the sequence of \(x\) values longer than just 10: try
whatever makes it sufficiently smooth like 100, 1,000, or 10,000.
Second, I want you to add 3 additional curves onto a new graph of which
ever equation you chose to plor. For each of the three curves, vary
either the \(r\) or \(\lambda\) parameter. For instance, if I use
the quadratic function from above, I would plot it and change, say, the
coefficient of \(x^2\). To add this to
your plot, use the lines()
function. Change the color of
the curve just to tell the difference between them; e.g.,
x_vec <- seq(from = -5, to = 5, length.out = 100)
y_vec <- 0.5*x_vec^2 + 0.1*x_vec - 2
y_vec1 <- 1*x_vec^2 + 0.1*x_vec - 2
y_vec2 <- 2*x_vec^2 + 0.1*x_vec - 2
y_vec3 <- 4*x_vec^2 + 0.1*x_vec - 2
par(mar = c(4.5, 4.5, 0.1, 0.1)) # sets the whitespace on the bottom, left, top, right
plot(x = x_vec, y = y_vec, type = "l", las = 1, xlab = "x", ylab = "y")
lines(x = x_vec, y = y_vec1, col = "red")
lines(x = x_vec, y = y_vec2, col = "#0000FF") # You can use hex codes
lines(x = x_vec, y = y_vec3, col = "gold")
ode()
and some density-dependent modelsLast lab I showed you the numerical solver you will be using for just
about everything in the class. It is the function called
ode()
in the deSolve
package.
3.1. First, I want you to go to ode()
’s documentation (use
?ode
in the console) and copy the the code from example 1
through the line when out
is created. Plot the predator and
prey time series. Two ways to plot the columns of the ode()
output saves as out
could be, using the time variable as an
example, out[,"time"]
or out[,1]
. Make sure to
adjust the vertical axis in plot to show all the data.
3.2. Take the code above and modify it so that way you have the logistic equation, \(dN/dt = rN(K - N)/K\). Give \(r\) and \(K\) whatever values you want and start at some low value of \(N\), say 0.1 Please plot the time series. Then, again numerically ingrate across a few different initial population sizes, say where \(N\) starts at 1/2 of the \(K\) you chose,where \(N\) starts just slightly about the \(K\) you chose, and where \(N\) starts at a value that’s 25% higher than the \(K\) you chose. Plot these curves all on the same graph.
3.3. Last, recreate parts of the Figure 5.12 from Stevens link.
Choose a value of \(r_d\) that gives
you asymptotic stability, oscillations (damped or perpetual), and chaos.
Use the logisitic equation he uses that is found 3 chunks above where he
creates dlogistic
. Use the numerical solver you used above,
but in the ode()
function, write the argument
method = "iteration"
. Please create your own plots using
base plot()
. To make a 2-row-by-3-column figure, in a line
above plot()
add a line for a function that adjusts
plotting parameters, par()
. If you use the argument
mfrow
(stands for matrix of
figures by rows; there’s also an
mfcol
) equal to the rows and columns, c(2, 3)
,
then the next 6 plots you make will be loaded in with the first 3 in the
top row and the last 3 in the bottom row.
At the end of chapter 1 you’re introduces to additional commonly-used discrete-time density-dependent models, the Beverton-Holt model (eqn. 28), the Hassell model (eqn. 29), and the Ricker model (eqn. 30). I’d like for you to visually/graphically analyze these models by plotting them and modifying their parameters.
4.1. First, let’s compare among the models. Let’s set all the growth
terms the same, by setting \(\lambda = r =
1\), and do the same to the crowding terms, \(\alpha = b~\text{in Ricker}\). The last
remaining parameter is \(b\) in the
Hassell model, and let’s just set it to 1. Create a time sequence, but
remember that we are in discrete-time, so we are taking steps by 1. The
easiest way to write this in R is 0:10
if you wanted to
iterate/solve across 10 time steps. Last, start with \(N_0\) at 0.1. Plot all three using points
on the same graph and make sure your axes are showing all the data.
4.2. Please look over the graphs you just made and comment on what you observe given what we did with the parameters.
4.3. Next, let’s vary the density-dependent terms in the model. On three graphs, please vary \(\alpha\) in the Beverton-Holt model in one, vary \(b\) in the Hassell model (even though \(\alpha\) is the density-dependent term, we vary it with the Beverton-Holt model that is analogous to Hassell), and \(b\) in the Ricker model. For each of those parameters, chose 3 values and plot them on each graph. Try to use a paramter value that gives you at least one different type of qualitative behavior (e.g., oscillations, choas).