-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiversityMeasures.R
324 lines (241 loc) · 16.7 KB
/
DiversityMeasures.R
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
## Created 13 / 3 / 2014
## Isabel Fenton
##
## Code to calculate diversity for Paleogene data
##
## Input files:
##
##
## Output files:
##
## To work on:
##
setwd("C:/Documents/Science/PhD/Project/Eocene/")
## 1. Load in the data -----------------------------------------------------
load("C:/Documents/Science/PhD/Project/Eocene/Outputs/OlComb.RData")
load("C:/Documents/Science/PhD/Project/Eocene/Outputs/EocComb.RData")
# dataset for sites with all species
EO_abun <- merge(EocComb, OlComb, all = TRUE)
nrow(EO_abun) == nrow(EocComb) + nrow(OlComb)
head(EO_abun)
# remove sites which don't have whole species counts
EO_abun <- EO_abun[EO_abun$All != "No", ]
dim(EO_abun)
## 2. Calculate species richness -------------------------------------------
## 2a. Generate a dataframe with each row as a site at a given age ---------
# create a dataframe containing rows for each unique site / age
# add column of site ID / age for sorting
EO_abun$site.age <- NA
for (i in 1:nrow(EO_abun)) {
# split the ID
tmp.split <- unlist(strsplit(EO_abun$ID[i], "_"))
# paste the ID and the age
# if it is not neptune
if (length(tmp.split) == 3) {
EO_abun$site.age[i] <- paste(tmp.split[2], "_", EO_abun$Age[i], sep = "")
} else {
EO_abun$site.age[i] <- paste(tmp.split[2], ".", tmp.split[3], "_", EO_abun$Age[i], sep = "")
}
}
# one row for each unique combination
EO_abun_div <- EO_abun[!duplicated(EO_abun$site.age), ]
# remove rows for species and abundance
EO_abun_div <- EO_abun_div[, -c(which(names(EO_abun_div) == "Species"), which(names(EO_abun_div) == "Abundance"))]
## 2b. Calculate the number of species at each site for each age -----------
EO_abun_div$SR <- NA
for(i in 1:nrow(EO_abun_div)) {
EO_abun_div$SR[i] <- sum(EO_abun$site.age == EO_abun_div$site.age[i])
}
head(EO_abun_div)
with(EO_abun_div, plot(Latitude, SR, pch = 16, col = as.factor(Period)))
par(ask = TRUE)
for (i in unique(EO_abun_div$Period)) {
with(EO_abun_div[EO_abun_div$Period == i, ], plot(Latitude, SR, pch = 16, col = as.factor(Period), main = i))
}
## 2c. Generate a dataframe with sites by period ---------------------------
# create a dataframe of average richness by period
EO_abun_div$site.period <- NA
for (i in 1:nrow(EO_abun_div)) {
tmp.split <- unlist(strsplit(EO_abun_div$site.age[i], "_"))
EO_abun_div$site.period[i] <- paste(tmp.split[1], "_", EO_abun_div$Period[i], sep = "")
}
EO_abun_age <- EO_abun_div[!duplicated(EO_abun_div$site.period), ]
# remove sprich column
EO_abun_age <- EO_abun_age[, -21]
## 2d. Calculate average species richness for each site --------------------
EO_abun_age$SR <- NA
for(i in 1:nrow(EO_abun_age)) {
EO_abun_age$SR[i] <- mean(EO_abun_div$SR[which(EO_abun_div$site.period == EO_abun_age$site.period[i])])
}
## 2e. Plot this up --------------------------------------------------------
with(EO_abun_age, plot(Latitude, SR, pch = 16, col = as.factor(Period)))
par(ask = TRUE)
for (i in unique(EO_abun_age$Period)) {
with(EO_abun_age[EO_abun_age$Period == i, ], plot(pal.lat.m, SR, pch = 16, col = as.factor(Period), main = i))
}
par(ask = FALSE)
## species richness by latitude split by period -------------
par(mfrow = c(2, 3))
with(EO_abun_age[EO_abun_age$Period == "Lower Eocene", ], plot(pal.lat.m, SR, pch = 16, col = as.factor(Period), main = "Lower Eocene"))
with(EO_abun_age[EO_abun_age$Period == "Middle Eocene", ], plot(pal.lat.m, SR, pch = 16, col = as.factor(Period), main = "Middle Eocene"))
with(EO_abun_age[EO_abun_age$Period == "Upper Eocene", ], plot(pal.lat.m, SR, pch = 16, col = as.factor(Period), main = "Upper Eocene"))
with(EO_abun_age[EO_abun_age$Period == "Lower Oligocene", ], plot(pal.lat.m, SR, pch = 16, col = as.factor(Period), main = "Lower Oligocene"))
with(EO_abun_age[EO_abun_age$Period == "Upper Oligocene", ], plot(pal.lat.m, SR, pch = 16, col = as.factor(Period), main = "Upper Oligocene"))
## species richness by latitude split by hemisphere and period -------------
par(mfrow = c(2, 3))
with(EO_abun_age[EO_abun_age$Period == "Lower Eocene", ], plot(abs(pal.lat.m), SR, pch = 16, col = (abs(pal.lat.m) == pal.lat.m) + 1, main = "Lower Eocene"))
with(EO_abun_age[EO_abun_age$Period == "Middle Eocene", ], plot(abs(pal.lat.m), SR, pch = 16, col = (abs(pal.lat.m) == pal.lat.m) + 1, main = "Middle Eocene"))
with(EO_abun_age[EO_abun_age$Period == "Upper Eocene", ], plot(abs(pal.lat.m), SR, pch = 16, col = (abs(pal.lat.m) == pal.lat.m) + 1, main = "Upper Eocene"))
with(EO_abun_age[EO_abun_age$Period == "Lower Oligocene", ], plot(abs(pal.lat.m), SR, pch = 16, col = (abs(pal.lat.m) == pal.lat.m) + 1, main = "Lower Oligocene"))
with(EO_abun_age[EO_abun_age$Period == "Upper Oligocene", ], plot(abs(pal.lat.m), SR, pch = 16, col = (abs(pal.lat.m) == pal.lat.m) + 1, main = "Upper Oligocene"))
## species richness with regression fitted by period -----------------------
for (i in unique(EO_abun_age$Period)) {
with(EO_abun_age[EO_abun_age$Period == i, ], plot(abs(pal.lat.m), SR, pch = 16, col = as.factor(Period), main = i))
abline(lm(SR ~ pal.lat.m, data = EO_abun_age[EO_abun_age$Period == i, ]))
}
head(EO_abun_age$site.period)
## species richness by latitude plotted by period --------------------------
par(mfrow = c(2, 3))
with(EO_abun_age[EO_abun_age$Period == "Lower Eocene" & EO_abun_age$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = All))
with(EO_abun_age[EO_abun_age$Period == "Middle Eocene" & EO_abun_age$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = All))
with(EO_abun_age[EO_abun_age$Period == "Upper Eocene" & EO_abun_age$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = All))
with(EO_abun_age[EO_abun_age$Period == "Lower Oligocene" & EO_abun_age$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = All))
with(EO_abun_age[EO_abun_age$Period == "Upper Oligocene" & EO_abun_age$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = All))
## species richness against latitude by period, coloured by hemisphere --------------
par(mfrow = c(2, 3))
with(EO_abun_age[EO_abun_age$Period == "Lower Eocene" & EO_abun_age$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
with(EO_abun_age[EO_abun_age$Period == "Middle Eocene" & EO_abun_age$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
with(EO_abun_age[EO_abun_age$Period == "Upper Eocene" & EO_abun_age$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
with(EO_abun_age[EO_abun_age$Period == "Lower Oligocene" & EO_abun_age$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
with(EO_abun_age[EO_abun_age$Period == "Upper Oligocene" & EO_abun_age$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
## distribution maps of species richness by period -------------------------
with(EO_abun_age[EO_abun_age$Period == "Lower Eocene", ], distrib.map(mod_long51, mod_lat51, SR))
with(EO_abun_age[EO_abun_age$Period == "Middle Eocene", ], distrib.map(mod_long42, mod_lat42, SR))
with(EO_abun_age[EO_abun_age$Period == "Middle Eocene", ], distrib.map(mod_long39, mod_lat39, SR))
with(EO_abun_age[EO_abun_age$Period == "Upper Eocene", ], distrib.map(mod_long35, mod_lat35, SR))
## plot map of points at 39Ma ----------------------------------------------
PTR.39Mya <- readPNG("Output\\39Mya.png")
PTR.39Mya.img <- pixmapGrey(PTR.39Mya)
plot(PTR.39Mya.img)
# use locator() to work out these coordinates
points((-180 + 180) + 135.8, (-90 + 90) + 202, pch = 16)
points((-180 + 180) + 135.8, (90 + 90) /180 * 903.1 + 202, pch = 16)
points((180 + 180) / 360 * 1802.6 + 135.8, (-90 + 90) /180 * 903.1 + 202, pch = 16)
points((180 + 180) / 360 * 1802.6 + 135.8, (90 + 90) /180 * 903.1 + 202, pch = 16)
with(EO_abun_age[EO_abun_age$Period == "Middle Eocene",], points((mod_long39 + 180) / 360 * 1802.6 + 135.8, (mod_lat39 + 90) / 180 * 903.1 + 202, pch = 16, col = "black"))
## create a smaller dataset -----------------------------------------------
# remove all almost and those with poor preservation
EO_abun_sub <- EO_abun_age[EO_abun_age$All != "Yes (almost)" &EO_abun_age$Preservation != 3 & EO_abun_age$Preservation != 4, ]
table(EO_abun_age$Preservation) # remove 3 and 4
## species richness in eocene ages with average maximum SR --------------------------------
par(mfrow = c(1, 3))
with(EO_abun_sub[EO_abun_sub$Period == "Lower Eocene" & EO_abun_sub$All != "Yes (almost)" , ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
mn.low <- rep(NA, 7)
for (i in 1:7) {
mn.low[i] <- max(EO_abun_sub$SR[abs(EO_abun_sub$pal.lat.m) < (i * 10) & abs(EO_abun_sub$pal.lat.m) > ((i - 1) * 10) & EO_abun_sub$Period == "Lower Eocene"])
}
points(seq(5, 65, 10), mn.low, type = "l")
with(EO_abun_sub[EO_abun_sub$Period == "Middle Eocene" & EO_abun_sub$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
mn.mid <- rep(NA, 7)
for (i in 1:7) {
mn.mid[i] <- max(EO_abun_sub$SR[abs(EO_abun_sub$pal.lat.m) < (i * 10) & abs(EO_abun_sub$pal.lat.m) > ((i - 1) * 10) & EO_abun_sub$Period == "Middle Eocene"])
}
points(seq(5, 65, 10), mn.mid, type = "l")
with(EO_abun_sub[EO_abun_sub$Period == "Upper Eocene" & EO_abun_sub$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
mn.up <- rep(NA, 7)
for (i in 1:7) {
mn.up[i] <- max(EO_abun_sub$SR[abs(EO_abun_sub$pal.lat.m) < (i * 10) & abs(EO_abun_sub$pal.lat.m) > ((i - 1) * 10) & EO_abun_sub$Period == "Upper Eocene"])
}
points(seq(5, 65, 10), mn.up, type = "l")
## species richness in eocene ages with regression fitted ----------------------------
par(mfrow = c(1, 3))
with(EO_abun_sub[EO_abun_sub$Period == "Lower Eocene" & EO_abun_sub$All != "Yes (almost)" , ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
modL <- with(EO_abun_sub[EO_abun_sub$Period == "Lower Eocene", ], lm(SR ~ poly(abs(pal.lat.m), 2)))
points(0:70, predict(modL, data.frame(pal.lat.m = 0:70)), type = "l")
with(EO_abun_sub[EO_abun_sub$Period == "Middle Eocene" & EO_abun_sub$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
modM <- with(EO_abun_sub[EO_abun_sub$Period == "Middle Eocene", ], lm(SR ~ poly(abs(pal.lat.m), 2)))
points(0:70, predict(modM, data.frame(pal.lat.m = 0:70)), type = "l")
with(EO_abun_sub[EO_abun_sub$Period == "Upper Eocene" & EO_abun_sub$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
modU <- with(EO_abun_sub[EO_abun_sub$Period == "Upper Eocene", ], lm(SR ~ poly(abs(pal.lat.m), 2)))
points(0:70, predict(modU, data.frame(pal.lat.m = 0:70)), type = "l")
## CBEP --------------------------------------------------------------------
## plot only those that are yes and preservation of 0 or n/a
## species richness by lat coloured by All? --------------------------------
par(mfrow = c(1, 3))
with(EO_abun_sub[EO_abun_sub$Period == "Lower Eocene" & EO_abun_sub$All != "Yes (almost)" , ], plot(abs(pal.lat.m), SR, pch = 16, col = All))
with(EO_abun_sub[EO_abun_sub$Period == "Middle Eocene" & EO_abun_sub$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = All))
with(EO_abun_sub[EO_abun_sub$Period == "Upper Eocene" & EO_abun_sub$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = All))
## species richness by lat coloured by Preservation? --------------------------------
par(mfrow = c(1, 3))
with(EO_abun_sub[EO_abun_sub$Period == "Lower Eocene" & EO_abun_sub$All != "Yes (almost)" , ], plot(abs(pal.lat.m), SR, pch = 16, col = Preservation))
with(EO_abun_sub[EO_abun_sub$Period == "Middle Eocene" & EO_abun_sub$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = Preservation))
with(EO_abun_sub[EO_abun_sub$Period == "Upper Eocene" & EO_abun_sub$All != "Yes (almost)", ], plot(abs(pal.lat.m), SR, pch = 16, col = Preservation))
## genus level richness ----------------------------------------------------
summary(EO_abun_div$Preservation)
# remove all poor preservation
EO_abun_CBEP <- EO_abun_div[EO_abun_div$Preservation != 2 & EO_abun_div$Preservation != 3 & EO_abun_div$Preservation != 4 & EO_abun_div$Preservation != 5, ]
summary(EO_abun_CBEP$Preservation)
dim(EO_abun_div)
dim(EO_abun_CBEP)
# remove all incomplete data
summary(EO_abun_CBEP$All)
EO_abun_CBEP <- EO_abun_CBEP[EO_abun_CBEP$All != "Yes (almost)" & EO_abun_CBEP$All != "Yes (maybe)", ]
summary(EO_abun_CBEP$All)
# calculate SR
EO_abun_CBEP$SR <- NA
for(i in 1:nrow(EO_abun_CBEP)) {
EO_abun_CBEP$SR[i] <- length(unique(EO_abun$Corr.species[EO_abun$site.age == EO_abun_CBEP$site.age[i]]))
}
# coloured by hemisphere
par(mfrow = c(1, 3))
with(EO_abun_CBEP[EO_abun_CBEP$Period == "Lower Eocene", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
modL <- with(EO_abun_CBEP[EO_abun_CBEP$Period == "Lower Eocene", ], lm(SR ~ abs(pal.lat.m)))
points(0:70, predict(modL, data.frame(pal.lat.m = 0:70)), type = "l")
with(EO_abun_CBEP[EO_abun_CBEP$Period == "Lower Eocene", ], anova(modL, lm(SR ~ 1)))
with(EO_abun_CBEP[EO_abun_CBEP$Period == "Middle Eocene", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
modM <- with(EO_abun_CBEP[EO_abun_CBEP$Period == "Middle Eocene", ], lm(SR ~ abs(pal.lat.m)))
points(0:70, predict(modM, data.frame(pal.lat.m = 0:70)), type = "l")
with(EO_abun_CBEP[EO_abun_CBEP$Period == "Middle Eocene", ], anova(modM, lm(SR ~ 1)))
with(EO_abun_CBEP[EO_abun_CBEP$Period == "Upper Eocene", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
modU <- with(EO_abun_CBEP[EO_abun_CBEP$Period == "Upper Eocene", ], lm(SR ~ abs(pal.lat.m)))
points(0:70, predict(modU, data.frame(pal.lat.m = 0:70)), type = "l")
with(EO_abun_CBEP[EO_abun_CBEP$Period == "Upper Eocene", ], anova(modU, lm(SR ~ 1)))
# coloured by source
par(mfrow = c(1, 3))
with(EO_abun_CBEP[EO_abun_CBEP$Period == "Lower Eocene", ], plot(abs(pal.lat.m), SR, pch = 16, col = sapply(ID, function(x) length(grep("^M", x, value = TRUE)) + 1)))
with(EO_abun_CBEP[EO_abun_CBEP$Period == "Middle Eocene", ], plot(abs(pal.lat.m), SR, pch = 16, col = sapply(ID, function(x) length(grep("^M", x, value = TRUE)) + 1)))
with(EO_abun_CBEP[EO_abun_CBEP$Period == "Upper Eocene", ], plot(abs(pal.lat.m), SR, pch = 16, col = sapply(ID, function(x) length(grep("^M", x, value = TRUE)) + 1)))
# add genus level richness
EO_abun_CBEP$GR <- NA
for(i in 1:nrow(EO_abun_CBEP)) {
EO_abun_CBEP$GR[i] <- length(unique(grep("^[A-Z]", unlist(strsplit(EO_abun$Corr.species[EO_abun$site.age == EO_abun_CBEP$site.age[i]], " ")), value = TRUE)))
}
with(EO_abun_CBEP[EO_abun_CBEP$Period == "Lower Eocene", ], plot(abs(pal.lat.m), GR, pch = 16, col = sapply(ID, function(x) length(grep("^M", x, value = TRUE)) + 1)))
with(EO_abun_CBEP[EO_abun_CBEP$Period == "Middle Eocene", ], plot(abs(pal.lat.m), GR, pch = 16, col = sapply(ID, function(x) length(grep("^M", x, value = TRUE)) + 1)))
with(EO_abun_CBEP[EO_abun_CBEP$Period == "Upper Eocene", ], plot(abs(pal.lat.m), GR, pch = 16, col = sapply(ID, function(x) length(grep("^M", x, value = TRUE)) + 1)))
## calculating maximum diversity for a given time period -------------------
# get a dataset which is the unique values of age & lat & long
names(EO_abun_CBEP)
max.abun <- EO_abun_CBEP[!duplicated(EO_abun_CBEP[, c(3:4, 7)]), ]
# calculate max SR within these
for (i in 1:nrow(max.abun)) {
max.abun$SR[i] <- max(EO_abun_CBEP$SR[which(EO_abun_CBEP$Latitude == max.abun$Latitude[i] & EO_abun_CBEP$Longitude == max.abun$Longitude[i] & EO_abun_CBEP$Period == max.abun$Period[i])])
}
head(max.abun$SR)
EO_abun_CBEP[1:10,]
par(mfrow = c(1, 3))
with(max.abun[max.abun$Period == "Lower Eocene", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
modL <- with(max.abun[max.abun$Period == "Lower Eocene", ], lm(SR ~ abs(pal.lat.m)))
#points(0:70, predict(modL, data.frame(pal.lat.m = 0:70)), type = "l")
with(max.abun[max.abun$Period == "Lower Eocene", ], anova(modL, lm(SR ~ 1)))
with(max.abun[max.abun$Period == "Middle Eocene", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
modM <- with(max.abun[max.abun$Period == "Middle Eocene", ], lm(SR ~ abs(pal.lat.m)))
#points(0:70, predict(modM, data.frame(pal.lat.m = 0:70)), type = "l")
with(max.abun[max.abun$Period == "Middle Eocene", ], anova(modM, lm(SR ~ 1)))
with(max.abun[max.abun$Period == "Upper Eocene", ], plot(abs(pal.lat.m), SR, pch = 16, col = (pal.lat.m > 0) + 1))
modU <- with(max.abun[max.abun$Period == "Upper Eocene", ], lm(SR ~ abs(pal.lat.m)))
#points(0:70, predict(modU, data.frame(pal.lat.m = 0:70)), type = "l")
with(max.abun[max.abun$Period == "Upper Eocene", ], anova(modU, lm(SR ~ 1)))
## 3. Calculate evenness ---------------------------------------------------
## 4. Calculate lineage ages --------------------------------------------------
ldg.data$simpsonEve <- sapply(1:nrow(ldg.data), function (i) diversity(ldg.data[i, which(colnames(ldg.data) %in% bfd.species)], "invsimpson") / ldg.data$sp.rich[i])