-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path05_elevational-migration.Rmd
190 lines (150 loc) · 7.66 KB
/
05_elevational-migration.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
186
187
188
189
---
editor_options:
chunk_output_type: console
---
# Extent of elevational migration
In this script, we calculate the median migration extent, breeding and wintering elevation for all combinations of elevational limit and sampling effort separately for the eastern and western Himalayas, modified from Tsai et al 2020)
## Load necessary libraries
```{r}
library(ape)
library(geiger)
library(phytools)
library(coxme)
library(evobiR)
library(nlme)
library(tidyverse)
library(phangorn)
```
## Load previously generated .Rdata files for analyses
```{r}
load("results/eBird_elev.RData")
dat.1<-as.data.frame(dat.1)
dat.1<-dat.1[,-27]
#### Keep only full species data
dat.1<-dat.1 %>% filter(CATEGORY == "species" | CATEGORY == "issf")
##Separating eastern and western himalayas
dat1W<-dat.1 %>% filter (LONGITUDE < 83)
dat1E<-dat.1 %>% filter (LONGITUDE > 83)
rm(dat.1)
```
## Eastern Himalayas
```{r}
#### Creating a list of unique species
uniSpe_east <- dat1E %>% filter(CATEGORY == "species" | CATEGORY == "issf")
uniSpe_east <- unique(uniSpe_east[, "COMMON.NAME"]) %>% data.frame()
colnames(uniSpe_east) <- "Species"
for (minSample in c(30, 60)) {
for (ds in c("05.q1", "05.q2", "05.q3", "95.q1", "95.q2", "95.q3", "50.q1", "50.q2", "50.q3")) {
load(paste("results/eBird_woNepal_resampled_east_", ds, ".Rdata", sep = ""))
B.elev <- sapply(1:1000, function(x) rs[[x]][, 1])
B.n <- sapply(1:1000, function(x) rs[[x]][, 2])
W.elev <- sapply(1:1000, function(x) rs[[x]][, 3])
W.n <- sapply(1:1000, function(x) rs[[x]][, 4])
diff <- B.elev - W.elev
dimnames(diff)<-list(uniSpe_east[,1],1:1000)
# For each species exclude trials where it has been detected less than 30/60 times in winter or summer
if (minSample == 30) diff[W.n < 30 | B.n < 30] <- NA
if (minSample == 60) diff[W.n < 60 | B.n < 60] <- NA
diff.sel <- diff[apply(diff, 1, FUN = function(x) sum(is.na(x))) < 1000, ]
# Calculate medians of the percentiles over 1000 sets of resampled sampling events. Along with confidence intervals
med.CI <- apply(diff.sel, 1, FUN = function(x) quantile((x), c(0.025, .5, .975), na.rm = TRUE))
med.CI <- t(med.CI)
med.CI <- as.data.frame(med.CI)
med.CI$Group <- with(med.CI, ifelse(`2.5%` > 0 & `97.5%` > 0, 2, ifelse(`2.5%` < 0 & `97.5%` < 0, 1, 0)))
med.CI$LCI_diff <- as.numeric(med.CI$`2.5%`)
med.CI$Median_diff <- as.numeric(med.CI$`50%`)
med.CI$UCI_diff <- as.numeric(med.CI$`97.5%`)
med.CI$Species <- rownames(med.CI)
med.CI<-med.CI[,-c(1,2,3)]
## adding median breeding elevations to the same table
dimnames(B.elev)<-list(uniSpe_east[,1],1:1000)
if (minSample == 30) B.elev[W.n < 30 | B.n < 30] <- NA
if (minSample == 60) B.elev[W.n < 60 | B.n < 60] <- NA
B.sel <- B.elev[apply(B.elev, 1, FUN = function(x) sum(is.na(x))) < 1000, ]
med.S.CI <- apply(B.sel, 1, FUN = function(x) quantile((x), c(0.025, .5, .975), na.rm = TRUE))
med.S.CI <- t(med.S.CI)
med.S.CI <- as.data.frame(med.S.CI)
med.S.CI$LCI_S <- as.numeric(med.S.CI$`2.5%`)
med.S.CI$Median_S <- as.numeric(med.S.CI$`50%`)
med.S.CI$UCI_S <- as.numeric(med.S.CI$`97.5%`)
med.S.CI$Species <- rownames(med.S.CI)
med.S.CI<-med.S.CI[,-c(1,2,3)]
## adding median winter elevations to the same table
dimnames(W.elev)<-list(uniSpe_east[,1],1:1000)
if (minSample == 30) W.elev[W.n < 30 | B.n < 30] <- NA
if (minSample == 60) W.elev[W.n < 60 | B.n < 60] <- NA
W.sel <- W.elev[apply(W.elev, 1, FUN = function(x) sum(is.na(x))) < 1000, ]
med.W.CI <- apply(W.sel, 1, FUN = function(x) quantile((x), c(0.025, .5, .975), na.rm = TRUE))
med.W.CI <- t(med.W.CI)
med.W.CI <- as.data.frame(med.W.CI)
med.W.CI$LCI_W <- as.numeric(med.W.CI$`2.5%`)
med.W.CI$Median_W <- as.numeric(med.W.CI$`50%`)
med.W.CI$UCI_W <- as.numeric(med.W.CI$`97.5%`)
med.W.CI$Species <- rownames(med.W.CI)
med.W.CI<-med.W.CI[,-c(1,2,3)]
ts<-left_join(med.CI,med.S.CI, by = "Species") %>% left_join(., med.W.CI, by = "Species", )
ts <- ts[c("Species", "Group", "Median_S", "LCI_S", "UCI_S","Median_W","LCI_W","UCI_W","Median_diff","LCI_diff", "UCI_diff")]
write.csv(ts, file = paste("results/final_birdlist_east_",ds,".sam",minSample, ".csv", sep = ""), row.names = F)
}
}
```
## Western Himalayas
```{r}
uniSpe_west <- dat1W %>% filter(CATEGORY == "species" | CATEGORY == "issf")
uniSpe_west <- unique(uniSpe_west[, "COMMON.NAME"]) %>% data.frame()
colnames(uniSpe_west) <- "Species"
for (minSample in c(30, 60)) {
for (ds in c("05.q1", "05.q2", "05.q3", "95.q1", "95.q2", "95.q3", "50.q1", "50.q2", "50.q3")) {
load(paste("results/eBird_woNepal_resampled_west_", ds, ".Rdata", sep = ""))
B.elev <- sapply(1:1000, function(x) rs[[x]][, 1])
B.n <- sapply(1:1000, function(x) rs[[x]][, 2])
W.elev <- sapply(1:1000, function(x) rs[[x]][, 3])
W.n <- sapply(1:1000, function(x) rs[[x]][, 4])
diff <- B.elev - W.elev
dimnames(diff)<-list(uniSpe_west[,1],1:1000)
# For each species exclude trials where it has been detected less than 30/60 times in winter or summer
if (minSample == 30) diff[W.n < 30 | B.n < 30] <- NA
if (minSample == 60) diff[W.n < 60 | B.n < 60] <- NA
diff.sel <- diff[apply(diff, 1, FUN = function(x) sum(is.na(x))) < 1000, ]
# Calculate medians of the percentiles over 1000 sets of resampled sampling events. Along with confidence intervals
med.CI <- apply(diff.sel, 1, FUN = function(x) quantile((x), c(0.025, .5, .975), na.rm = TRUE))
med.CI <- t(med.CI)
med.CI <- as.data.frame(med.CI)
med.CI$Group <- with(med.CI, ifelse(`2.5%` > 0 & `97.5%` > 0, 2, ifelse(`2.5%` < 0 & `97.5%` < 0, 1, 0)))
med.CI$LCI_diff <- as.numeric(med.CI$`2.5%`)
med.CI$Median_diff <- as.numeric(med.CI$`50%`)
med.CI$UCI_diff <- as.numeric(med.CI$`97.5%`)
med.CI$Species <- rownames(med.CI)
med.CI<-med.CI[,-c(1,2,3)]
## adding median breeding elevations to the same table
dimnames(B.elev)<-list(uniSpe_west[,1],1:1000)
if (minSample == 30) B.elev[W.n < 30 | B.n < 30] <- NA
if (minSample == 60) B.elev[W.n < 60 | B.n < 60] <- NA
B.sel <- B.elev[apply(B.elev, 1, FUN = function(x) sum(is.na(x))) < 1000, ]
med.S.CI <- apply(B.sel, 1, FUN = function(x) quantile((x), c(0.025, .5, .975), na.rm = TRUE))
med.S.CI <- t(med.S.CI)
med.S.CI <- as.data.frame(med.S.CI)
med.S.CI$LCI_S <- as.numeric(med.S.CI$`2.5%`)
med.S.CI$Median_S <- as.numeric(med.S.CI$`50%`)
med.S.CI$UCI_S <- as.numeric(med.S.CI$`97.5%`)
med.S.CI$Species <- rownames(med.S.CI)
med.S.CI<-med.S.CI[,-c(1,2,3)]
## adding median winter elevations to the same table
dimnames(W.elev)<-list(uniSpe_west[,1],1:1000)
if (minSample == 30) W.elev[W.n < 30 | B.n < 30] <- NA
if (minSample == 60) W.elev[W.n < 60 | B.n < 60] <- NA
W.sel <- W.elev[apply(W.elev, 1, FUN = function(x) sum(is.na(x))) < 1000, ]
med.W.CI <- apply(W.sel, 1, FUN = function(x) quantile((x), c(0.025, .5, .975), na.rm = TRUE))
med.W.CI <- t(med.W.CI)
med.W.CI <- as.data.frame(med.W.CI)
med.W.CI$LCI_W <- as.numeric(med.W.CI$`2.5%`)
med.W.CI$Median_W <- as.numeric(med.W.CI$`50%`)
med.W.CI$UCI_W <- as.numeric(med.W.CI$`97.5%`)
med.W.CI$Species <- rownames(med.W.CI)
med.W.CI<-med.W.CI[,-c(1,2,3)]
ts<-left_join(med.CI,med.S.CI, by = "Species") %>% left_join(., med.W.CI, by = "Species", )
ts <- ts[c("Species", "Group", "Median_S", "LCI_S", "UCI_S","Median_W","LCI_W","UCI_W","Median_diff","LCI_diff", "UCI_diff")]
write.csv(ts, file = paste("results/final_birdlist_west",ds,".sam",minSample, ".csv", sep = ""), row.names = F)
}
}
```