|
| 1 | +--- |
| 2 | +title: "Extinctions Unit" |
| 3 | +author: "Your name, partner name" |
| 4 | +maketitle: true |
| 5 | +output: github_document |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +```{r include=FALSE} |
| 11 | +library("tidyverse") |
| 12 | +library("httr") |
| 13 | +library("jsonlite") |
| 14 | +#library("printr") |
| 15 | +knitr::opts_chunk$set(comment=NA) |
| 16 | +``` |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +## Extinctions Module |
| 21 | + |
| 22 | +_Are we experiencing the sixth great extinction?_ |
| 23 | + |
| 24 | +What is the current pace of extinction? Is it accelerating? How does it compare to background extinction rates? |
| 25 | + |
| 26 | +## Background |
| 27 | + |
| 28 | +- [Section Intro Video](https://youtu.be/QsH6ytm89GI) |
| 29 | +- [Ceballos et al (2015)](http://doi.org/10.1126/sciadv.1400253) |
| 30 | + |
| 31 | +## Computational Topics |
| 32 | + |
| 33 | +- Accessing data from a RESTful API |
| 34 | +- Error handling |
| 35 | +- JSON data format |
| 36 | +- Regular expressions |
| 37 | +- Working with missing values |
| 38 | + |
| 39 | +## Additional references: |
| 40 | + |
| 41 | +- http://www.hhmi.org/biointeractive/biodiversity-age-humans (Video) |
| 42 | +- [Barnosky et al. (2011)](http://doi.org/10.1038/nature09678) |
| 43 | +- [Pimm et al (2014)](http://doi.org/10.1126/science.1246752) |
| 44 | +- [Sandom et al (2014)](http://dx.doi.org/10.1098/rspb.2013.3254) |
| 45 | + |
| 46 | + |
| 47 | +# Getting started (based on live code session) |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +## CURL and REST |
| 53 | + |
| 54 | +Use the `httr` package to make a single API query against the following endpoint: `http://api.iucnredlist.org/index/species/Acaena-exigua.json` |
| 55 | + |
| 56 | +```{r} |
| 57 | +
|
| 58 | +``` |
| 59 | + |
| 60 | +Examine the response and the content of the response. Can you tell if the call was successful? What was the return type object? Can you parse the return into an R object? Can you represent the return data as a data.frame? |
| 61 | + |
| 62 | +```{r} |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +# Working with Regular Expressions |
| 68 | + |
| 69 | +- [Self-guided Tutorial](http://regexone.com/) |
| 70 | +- [Cheetsheet](http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/) |
| 71 | +- [stringr RegEx Vignette](https://cran.r-project.org/web/packages/stringr/vignettes/regular-expressions.html) |
| 72 | + |
| 73 | + |
| 74 | +One of the entries in the response contains a field that may contain some information on when the species went extinct. Identify the appropriate column and extract this information using a *regular expression*, as discussed in the live code exercise. |
| 75 | + |
| 76 | + |
| 77 | +```{r} |
| 78 | +``` |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +# Calculating Extinction Rates: Putting it all together |
| 84 | + |
| 85 | +First, to know what queries to make to the IUCN REST API, we need a list of extinct species names. This information can be downloaded from the IUCN website, but unfortunately this is not easily automated. Thus we'll download the data file using a copy already prepared for the course: |
| 86 | + |
| 87 | + |
| 88 | +```{r} |
| 89 | +extinct = read_csv("http://berkeley.carlboettiger.info/espm-88b/extinction/data/extinct.csv") |
| 90 | +extinct |
| 91 | +``` |
| 92 | + |
| 93 | + |
| 94 | +Write a function to extract the rationale for the extinction for all extinct species in the data set (see above file) |
| 95 | + |
| 96 | +```{r} |
| 97 | +
|
| 98 | +``` |
| 99 | + |
| 100 | +Test your function on a subset of the data before attempting the full data set. Use our `dplyr` pipe syntax to iterate over your function. |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +```{r} |
| 105 | +
|
| 106 | +
|
| 107 | +``` |
| 108 | + |
| 109 | +Now create a function that can extract the date from the rationale, and include this function in your data analysis pipeline. |
| 110 | + |
| 111 | + |
| 112 | +```{r} |
| 113 | +
|
| 114 | +``` |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | +## Histogram of Extinction Dates |
| 120 | + |
| 121 | +We can get a sense for the tempo of extinctions by plotting extinctions since 1500 in 25-year interval bins. |
| 122 | + |
| 123 | +```{r} |
| 124 | +``` |
| 125 | + |
| 126 | +# Exercises |
| 127 | + |
| 128 | + |
| 129 | +# Question 1: Extinctions by group |
| 130 | + |
| 131 | +A. Compute the number of extinctions from 1500 - 1900 and from 1900 to present of each of the following taxonomic groups: |
| 132 | + |
| 133 | + - Vertebrates |
| 134 | + - Mammals |
| 135 | + - Birds |
| 136 | + - Fish |
| 137 | + - Amphibians |
| 138 | + - Reptiles |
| 139 | + - Insects |
| 140 | + - Plants |
| 141 | + |
| 142 | +Compare your estimates to Table 1 of [Ceballos et al (2015)](http://doi.org/10.1126/sciadv.1400253). |
| 143 | + |
| 144 | + |
| 145 | +## Question 2: Weighing by number of species |
| 146 | + |
| 147 | + |
| 148 | +The number of species going extinct per century in a given taxonomic group will be influenced by how many species are present in the group to begin with. (For an obvious example, the number of vertebrate extinctions is always going to be higher than the number of mammal extinctions, since mammals are vertebrates). Overall, these numbers do not change greatly over a period of a few hundred years, so we were able to make the relative comparisons between the roughly pre-industrial and post-industrial periods above. |
| 149 | + |
| 150 | +As discussed by Tony Barnosky in the introductory video (or in [Ceballos et al (2015)](http://doi.org/10.1126/sciadv.1400253) paper), if we want to compare these extinction rates against the long-term palentological record, it is necessary to weigh the rates by the total number of species. That is, to compute the number of extinctions per million species per year (MSY; equivalently, the number extinctions per 10,000 species per 100 years). |
| 151 | + |
| 152 | +A) First, we will compute how many species are present in each of the taxonomic groups. To do so, we need a table that has not only extinct species, but all assessed species. We will once again query this information from the IUCN API. |
| 153 | + |
| 154 | + |
| 155 | +This is going to involve a lot of data -- more than the API can serve in a single chunk. Instead, the API breaks the returns up into groups of 10,000 species per page (see API docs: http://apiv3.iucnredlist.org/api/v3/docs#species). Luckily, the API also tells us the total number of species: |
| 156 | + |
| 157 | +http://apiv3.iucnredlist.org/api/v3/speciescount?token=9bb4facb6d23f48efbf424bb05c0c1ef1cf6f468393bc745d42179ac4aca5fee |
| 158 | + |
| 159 | +The code below queries the first page. How many pages will we need to get all the data? Modify the example below to collect all of the data into a single DataFrame. Note the use of `append` to add data to an existing data.frame with matching column labels. |
| 160 | + |
| 161 | + |
| 162 | +```{r} |
| 163 | +
|
| 164 | +``` |
| 165 | + |
| 166 | + |
| 167 | +B) Based on the complete data, write queries that count the number of species in each group. Then use these numbers to compute MSY, the number extinctions per 10,000 species per 100 years, for each of the groups listed in Question 1. How do your estimates compare to the overall historical average of about 2 MSY? |
| 168 | + |
| 169 | +## Question 3: Improving our algorithm |
| 170 | + |
| 171 | + |
| 172 | +In parsing the data with regular expressions, we encountered certain data that resulted in missing values. Identify and investigate the strings for which we were not able to extract a date value. |
| 173 | + |
| 174 | +- Why did the date extraction fail? |
| 175 | +- Can you deduce an approximate date by examining the text? |
| 176 | +- Can you modify the regular expression to reduce the number of missing values? |
| 177 | +- How do these missing values impact our overall estimate of the extinction rate? (In which direction, and by approximately what amount?) |
| 178 | + |
| 179 | + |
| 180 | +## Question 4: Looking forward (bonus) |
| 181 | + |
| 182 | +Plot the MSY rates in intervals of 50 years for each of the groups as a line plot (compare to Figure 1a of [Ceballos et al (2015)](http://doi.org/10.1126/sciadv.1400253) paper). Compute the slope of these curves to forecast the extinction rate in 2100. |
0 commit comments