-
Notifications
You must be signed in to change notification settings - Fork 16
/
creating_infiles.Rmd
185 lines (134 loc) · 9.22 KB
/
creating_infiles.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
---
title: "creating_infiles, rlpi package"
author: "Louise McRae, Stefanie Deinet, Robin Freeman, IoZ, Zoological Society of London"
output:
html_document:
keep_md: true
toc: true
theme: united
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(cache=TRUE)
```
```{r, echo=FALSE, message=FALSE}
require(rlpi)
```
The code also provides a means to create infiles from tabular population data such as provided in the comma separated output of Living Planet Database **[here](https://dx.doi.org/10.6084/m9.figshare.4300022.v1)**. NB: The **create_infile** method currently expects particular columns that define where the abundance data resides, using the **convert_to_rows** function that assumes abundance data is in columns between the *X1950* column and a column called *Managed*.
> Note: This [Comma Separated Value version of the Living Planet Database ](https://dx.doi.org/10.6084/m9.figshare.4300022.v1) excludes around 3000 populations which are confidential and cannot therefore be shared. Therefore, results produced with this data set may differ slightly from those presented in the manuscript and elsewhere.
```{r rlpi_data, eval=FALSE}
# Get example data from package
# Copy zipped data to local directory
file.copy(from=system.file("extdata", "example_data.zip", package = "rlpi"), to=getwd())
# Extract data, this will create a directory of terrestrial LPI data to construct a terrestrial index from.
unzip("example_data.zip")
```
```{r making_infiles, eval=TRUE, message=FALSE}
# Constructing infiles from a populations table...
# First read the population table (this is the Living Planet Database excluding confidential records)
lpi_data <- read.csv("example_data/LPI_LPR2016data_public.csv", na.strings = "NULL")
# Create an infile from all the data. All the population data in the 'lpi_data' table will be converted and stored in a file called 'example_data_pops.txt' and a file called 'example_data_infile.txt' will be created that references the first file (the infile name will also be stored in the returned variable 'example_infile_name')
# Here we select the first 100 populations by creating an index vector that's FALSE for all rows, then setting the first 100 rows to TRUE
index_vector = rep(FALSE, nrow(lpi_data))
index_vector[1:100] = TRUE
example_infile_name <- create_infile(lpi_data, index_vector=index_vector, name="example_data")
# An index can be created using this infile, for the period 1970 to 2014 with 100 bootstraps.
example_lpi <- LPIMain(example_infile_name, REF_YEAR = 1970, PLOT_MAX = 2014, BOOT_STRAP_SIZE = 100, VERBOSE=FALSE, show_progress=FALSE)
# Remove NAs (trailing years with no data)
example_lpi <- example_lpi[complete.cases(example_lpi), ]
# Plot the resulting index
ggplot_lpi(example_lpi, title = "example_lpi", xlims=c(1970, 2012), ylim=c(0, 2))
# Plot the resulting index with logged y-axis (note the use of a +ive ymin)
ggplot_lpi(example_lpi, title = "example_lpi", xlims=c(1970, 2012), ylim=c(0.3, 2), trans="log")
# Here we limit the data to 'Strigiformes' simply by creating a boolean (true/false) vector which is true for populations (rows) where the Order is "Strigiformes"
Strigiformes = lpi_data$Order == "Strigiformes"
# Passing this vector into the create_infile function will select just those populations and create an infile for them
s_infile_name <- create_infile(lpi_data, index_vector=Strigiformes, name="example_data_strig")
# Again, create and index
s_lpi <- LPIMain(s_infile_name, REF_YEAR = 1970, PLOT_MAX = 2014, BOOT_STRAP_SIZE = 100, VERBOSE=FALSE, show_progress=FALSE)
# And plot
# Remove NAs (trailing years with no data)
s_lpi <- s_lpi[complete.cases(s_lpi), ]
ggplot_lpi(s_lpi, title = "s_lpi", xlims=c(1970, 2012))
# Similarly, this will create an index just for those populations of the Order 'Passeriformes'
Passeriformes = lpi_data$Order == "Passeriformes"
p_infile_name <- create_infile(lpi_data, index_vector=Passeriformes, name="example_data_pass")
p_lpi <- LPIMain(s_infile_name, REF_YEAR = 1970, PLOT_MAX = 2014, BOOT_STRAP_SIZE = 100, VERBOSE=FALSE, show_progress=FALSE)
# Remove NAs (trailing years with no data)
p_lpi <- p_lpi[complete.cases(p_lpi), ]
ggplot_lpi(p_lpi, title = "p_lpi", xlims=c(1970, 2012))
# Nearctic mammals
Nearctic_mammals = lpi_data$Class == "Mammalia" & lpi_data$T_realm == "Nearctic"
nm_infile_name <- create_infile(lpi_data, index_vector=Nearctic_mammals, name="terrestrial_Nearctic_Mammalia")
# How many pops...
sum(Nearctic_mammals, na.rm = T)
nm_lpi <- LPIMain(nm_infile_name, REF_YEAR = 1970, PLOT_MAX = 2014, BOOT_STRAP_SIZE = 100, VERBOSE=FALSE, show_progress=FALSE)
# Remove NAs (trailing years with no data)
nm_lpi <- nm_lpi[complete.cases(nm_lpi), ]
ggplot_lpi(nm_lpi, title = "nm_lpi", xlims=c(1970, 2012))
# Nearctic birds
Nearctic_birds = lpi_data$Class == "Aves" & lpi_data$T_realm == "Nearctic"
nb_infile_name <- create_infile(lpi_data, index_vector=Nearctic_birds, name="terrestrial_Nearctic_Aves")
# How many pops...
sum(Nearctic_birds, na.rm = T)
nb_lpi <- LPIMain(nb_infile_name, REF_YEAR = 1970, PLOT_MAX = 2014, BOOT_STRAP_SIZE = 100, VERBOSE=FALSE, show_progress=FALSE)
# Remove NAs (trailing years with no data)
nb_lpi <- nb_lpi[complete.cases(nb_lpi), ]
ggplot_lpi(nb_lpi, title = "nb_lpi", xlims=c(1970, 2012))
# Nearctic herps
Nearctic_herps = (lpi_data$Class == "Reptilia" | lpi_data$Class == "Amphibia") & lpi_data$T_realm == "Nearctic"
nh_infile_name <- create_infile(lpi_data, index_vector=Nearctic_herps, name="terrestrial_Nearctic_Herps")
# How many pops...
sum(Nearctic_herps, na.rm = T)
nh_lpi <- LPIMain(nh_infile_name, REF_YEAR = 1970, PLOT_MAX = 2014, BOOT_STRAP_SIZE = 100, VERBOSE=FALSE, show_progress=FALSE)
# Remove NAs (trailing years with no data)
nh_lpi <- nh_lpi[complete.cases(nh_lpi), ]
ggplot_lpi(nh_lpi, title = "nh_lpi", xlims=c(1970, 2012))
```
Constructing infiles
```{r making_infiles_for_terr, echo=FALSE, eval=FALSE}
selected_pops = lpi_data$Class == "Aves" & lpi_data$T_realm == "Afrotropical"
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_Afrotropical_aves")
selected_pops = lpi_data$Class == "Aves" & (lpi_data$T_realm == "Indo-Malayan" | lpi_data$T_realm == "Australasia" | lpi_data$T_realm == "Oceania")
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_IndoPacific_aves")
selected_pops = lpi_data$Class == "Aves" & lpi_data$T_realm == "Palearctic"
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_Palearctic_aves")
selected_pops = lpi_data$Class == "Aves" & lpi_data$T_realm == "Neotropical"
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_Neotropical_aves")
selected_pops = lpi_data$Class == "Aves" & lpi_data$T_realm == "Nearctic"
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_Nearctic_aves")
selected_pops = lpi_data$Class == "Mammalia" & lpi_data$T_realm == "Afrotropical"
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_Afrotropical_mammalia")
selected_pops = lpi_data$Class == "Mammalia" & (lpi_data$T_realm == "Indo-Malayan" | lpi_data$T_realm == "Australasia" | lpi_data$T_realm == "Oceania")
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_IndoPacific_mammalia")
selected_pops = lpi_data$Class == "Mammalia" & lpi_data$T_realm == "Palearctic"
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_Palearctic_mammalia")
selected_pops = lpi_data$Class == "Mammalia" & lpi_data$T_realm == "Neotropical"
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_Neotropical_mammalia")
selected_pops = lpi_data$Class == "Mammalia" & lpi_data$T_realm == "Nearctic"
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_Nearctic_mammalia")
selected_pops = (lpi_data$Class == "Reptilia" | lpi_data$Class == "Amphibia") & lpi_data$T_realm == "Afrotropical"
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_Afrotropical_herps")
selected_pops = (lpi_data$Class == "Reptilia" | lpi_data$Class == "Amphibia") & (lpi_data$T_realm == "Indo-Malayan" | lpi_data$T_realm == "Australasia" | lpi_data$T_realm == "Oceania")
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_IndoPacific_herps")
selected_pops = (lpi_data$Class == "Reptilia" | lpi_data$Class == "Amphibia") & lpi_data$T_realm == "Palearctic"
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_Palearctic_herps")
selected_pops = (lpi_data$Class == "Reptilia" | lpi_data$Class == "Amphibia") & lpi_data$T_realm == "Neotropical"
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_Neotropical_herps")
selected_pops = (lpi_data$Class == "Reptilia" | lpi_data$Class == "Amphibia") & lpi_data$T_realm == "Nearctic"
sum(selected_pops, na.rm = T)
infile_name <- create_infile(lpi_data, index_vector=selected_pops, name="T_Nearctic_herps")
```