library(learnr) tutorial_options(exercise.reveal_solution = FALSE) gradethis::gradethis_setup() knitr::opts_chunk$set(error = TRUE) set.seed(123)
At the end of the last practical we looked at the diversity of a population using a variety of different diversity measures. We now want to be able to do that more easily so that we can examine the diversity profile of a population.
Instead of looking at individual diversity measures, we can instead look at the diversity profile of a population. This is a plot of the effective number of species against $q$, the parameter of this family of diversity measures. You should have observed in the previous exercise that the effective number of species drops as $q$ increases, and you may remember that I said that this is because $q$ is a measure of how much we care about evenness of the species abundances. At $q = 0$, we don't care at all, and the rarest species counts as much as the most common; at $q = \infty$ we care only about evenness, and not about the actual number of species present. When we plot $D_q$ against $q$, profiles which drop away quickly are indicative of uneven distributions of species, whereas profiles which are less sensitive to $q$ show more evenly distributed species (none much more abundant than any other).
We want to plot $D_q$ against $q$ for a variety of values of $q$ -- say from
0 to 10 in small steps. First call your general diversity function with a vector
of values for $q$ instead of a single value. You will probably get a warning
message, and in any event you will only get a single result, whereas you wanted
a result for each value of $q$. This is because the raise-to-the-power function
^
does not understand that you want to raise each species proportion to the
first power, then each to the second power, and so on in turn and instead raises
the first proportion to the first power, the second to the second, and so on.
Instead we need to write a loop to iterate over the different values of $q$.
First, you probably want to copy your existing functions again. You will edit these functions (renaming them as you go along) to create final, definitive diversity functions that do everything we want.
q
Add a test in the general diversity function that checks that there is only one
value of $q$ and stop()
s if there is more than one -- now we have identified a
problem in our code, we want to avoid it happening again! We will stop worrying
about the specific (e.g. species richness) functions now, as it's boring to keep
on rewriting them!
q
sNow write a completely new function to calculate a series of diversity measures
for a vector of values of $q$ given as a second argument (e.g. c(0, 0.1, 3.4,
7, 15)
or 0:20
, etc.). In the function, you should just loop over all of the
values of $q$ given to it as an argument (just like your original diversity
function took one value of $q$ as an argument) and then just call your
single-q-value function with each value of $q$ (and the population) in turn. We
want to reuse our code from the last exercise as much as possible to avoid
making new mistakes! It should then combine all of the results and return a
vector of diversity measures that corresponds to the vector of values of $q$
that it has been given. Remember that if we have a vector of values (called
Dqs.so.far
), and want to add a new value Dq
to the end, then we can do that
with just c(Dqs.so.far, Dq)
.
Check that the first diversity function now correctly rejects more than one
value of $q$. Then calculate the diversity profiles of the datasets, and plot
them all on a graph. You can display however you like, but in the simplest case
you can use plot(qs, Dqs)
for the first graph, and lines(qs, Dqs)
for
subsequent graphs while changing the colours (col=
) or line types (lty=
),
etc.
Write a demo that calculates diversity profiles for each dataset, plots them against each other, and comments on the differences between the results.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.