Writining math in RMarkdown

  1. 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. To write inline, use two dollar signs and write the math in the middle; e.g., $C = \tau r$ gives you what I just typed. In display mode, you will create a line break, which you use for more complex and 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. Tell me about your three favorite equations in-line and make sure that you include them.
    2. Using the \(\LaTeX\) guide as a reference, write the three logistic models from the quiz. 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} \frac{\mathrm{d}N}{\mathrm{d}t} & = N\left(r - \alpha N\right) \\ \frac{\mathrm{d}N}{\mathrm{d}t} & = rN\left(\frac{K - N}{K}\right) \\ \frac{\mathrm{d}N}{\mathrm{d}t} & = rN\left(1 - \alpha N\right) \end{aligned} \] So now you have it—you can write math using RMarkdown!

Making graphs in R

  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(), etc. to make graphs in this course because (1) what you learn in base R is transferable but specialized packages like ggplot() 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 ggplot() and the rest of the tidy universe.
    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. If I 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.
  2. I want you to make a couple more plots. First, choose one of your favorite equations from above and plot it in this way. 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 your favorite equation. For each of the three curves, vary a 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")

Visualizing density dependence

  1. Since the beginning of this course includes learning a programming language (R), then I hope you will excuse this problem set for being very R/skills forward. For this last question, it will be doing things you’ve done above, but with logistic equations.
    1. First, plot a time series of the logistic equation. Use any parameter values that you like and use either the \(r\)-\(K\) or \(r\)-\(\alpha\). In lab this week you have already created and plotted a time series, so modify your code (hint).
    2. Second, make two plots of your same logistic equation (see last week’s key on how to make plots side-by-side), but where you have the population growth rate on a vertical axis and the per-capita growth rate on the other axis, with both having density on the horizontal axis. This is just like the quiz. But also remember you are not integrating and just plotting some function like what you did in the previous section.
    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 numerical solver you used in a. above, but in the ode() function, write the argument method = "iteration".