Skip to content

Commit 0919a96

Browse files
committed
initial commit
0 parents  commit 0919a96

File tree

6 files changed

+251
-0
lines changed

6 files changed

+251
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata
5+
.Rbuildignore

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r
2+
3+
language: R
4+
sudo: false
5+
cache: packages
6+
script:
7+
# - R -e "devtools::install(dep=TRUE)"
8+
- R -e 'lapply(list.files(pattern=".*.Rmd", recursive=TRUE), rmarkdown::render)'
9+
10+

DESCRIPTION

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Package: compendium
2+
Version: 0.1.0
3+
Depends: rmarkdown, tidyverse, httr, jsonlite

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
*add travis-ci badge here*
3+
4+
## Team Members:
5+
6+
- full name, github handle
7+
- full name, github handle
8+
9+
This repository is a template for your team's repository.
10+
11+
## assignment
12+
13+
All work for this assignment should be in the `assignment` directory. You will work in the `.Rmd` notebook, and commit your rendered output files (`.md` and associated files) in the `assignment` directory as well.
14+
15+
## Special files
16+
17+
All team repositories will also include most of the special files found here:
18+
19+
### Common files
20+
21+
- `README.md` this file, a general overview of the repository in markdown format.
22+
- `.gitignore` Optional file, ignore common file types we don't want to accidentally commit to GitHub. Most projects should use this.
23+
- `<REPO-NAME>.Rproj` Optional, an R-Project file created by RStudio for it's own configuration. Some people prefer to `.gitignore` this file.
24+
25+
26+
### Infrastructure for Testing
27+
28+
- `.travis.yml`: A configuration file for automatically running [continuous integration](https://travis-ci.com) checks to verify reproducibility of all `.Rmd` notebooks in the repo. If all `.Rmd` notebooks can render successfully, the "Build Status" badge above will be green (`build success`), otherwise it will be red (`build failure`).
29+
- `DESCRIPTION` a metadata file for the repository, based on the R package standard. It's main purpose here is as a place to list any additional R packages/libraries needed for any of the `.Rmd` files to run.
30+
- `tests/render_rmds.R` an R script that is run to execute the above described tests, rendering all `.Rmd` notebooks.
31+
32+
33+
34+

assignment/extinction-assignment.Rmd

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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.

extinction-template.Rproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: knitr
13+
LaTeX: pdfLaTeX
14+
15+
BuildType: Package
16+
PackageUseDevtools: Yes
17+
PackageInstallArgs: --no-multiarch --with-keep.source

0 commit comments

Comments
 (0)