-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommunity_analyses.R
More file actions
411 lines (271 loc) · 15.4 KB
/
Community_analyses.R
File metadata and controls
411 lines (271 loc) · 15.4 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
### Load packages ####
library(tidyverse)
library(lubridate)
library(vegan)
library(ade4)
library(mvabund)
library(usdm)
### Load data ####
# CKS monitoring dataset
list.files()
cav <- read.csv("Monitoring_data.csv", head=TRUE, sep=";", dec = ".")
cav$Date <- mdy(cav$Date)
cav<-as_tibble(cav)
head(cav)
str(cav)
cav$Location <- as.factor(cav$Location)
cav$Year <- as.factor(cav$Year)
cav$Abundance <- as.numeric(as.character(cav$Abundance))
cav$Area <- as.numeric(as.character(cav$Area))
names(cav)
## Exclude selected species - Presence/absence records or taxonomically problematic species
exclude <- c("aff Xyccarph sp.1", "Ochyrocera sp.1", "Pipa arrabali", "Scydmaenus sp.1", "Zelurus sp.",
"Carajas paraua", "Circoniscus buckupi", "Coarazuphium amazonicus", "Copelatus cessaima",
"Pseudonannolene sp.", "Turbellaria")
cav <- cav %>%
dplyr::filter(! Species %in% exclude)
# Landscape data
landscape_metrics <- read.csv("Landscape_metrics.csv", head=TRUE, sep=",", dec = ".")
landscape_metrics <- as_tibble(landscape_metrics)
landscape_metrics2 <- landscape_metrics %>%
dplyr::select("Cave","B500_Forest_pland_2015":"B1000_Allvegetation_pland_2019") %>%
pivot_longer(!Cave, names_to = "metric", values_to = "value") %>%
separate(metric, c("Scale", "Class", "metric", "Year"))
topodist_mine2 <- landscape_metrics %>%
dplyr::select("Cave","tdm_2015":"tdm_2019") %>%
pivot_longer(!Cave, names_to = "metric", values_to = "value") %>%
separate(metric, c("metric", "Year"))
### Inspecting data ####
## Evaluate sampling tables
cav_sampling <- cav %>%
dplyr::select(Cave, Date, Year, Season) %>%
group_by(Cave, Year, Season) %>%
distinct() %>%
summarise(Ncamp=n()) %>%
pivot_wider(names_from = c(Year,Season), values_from = Ncamp)%>%
dplyr::select( "Cave","2015_Chuvosa","2015_Seca","2016_Chuvosa":"2019_Seca")
# Note there is no sampling in whole Serra Sul during 2015, only SN
# We can also check individual species records by year/season
cav_sampling2 <- cav %>%
dplyr::select(Cave, Month, Year, Season, Species) %>%
group_by(Cave, Year, Season, Month) %>%
count(N=Species)
## Nest data by cave
cav_nested <- cav %>% dplyr::select(Cave, Date, Month, Year, Season, Abundance, Species) %>%
group_by(Cave) %>%
nest()
## Calculate number of field surveys per cave and plot histogram
cav_nested <- cav_nested %>% mutate(Ncamp = map_dbl(data, function(.x){
.x %>% distinct(Date, .keep_all= TRUE) %>% nrow()}))
cav_nested %>% ggplot(aes(Ncamp)) + geom_histogram(binwidth=1)
# Note by comparing this histogram with the cave_sampling table that cave with 5 or less
# survey occasions were those from SSul not sampled in 2015 and 2019
# SSul has no surveys in 2015
### Arrange data matrices for analysis (Decisions taken) ####
# Considered samples from 2015, but then filtered only caves sampled in all remaining years
cav_comm <- as.data.frame(cav %>%
dplyr::select(Cave, Location, Date,Year, Season, Abundance, Species)%>%
group_by(Cave) %>%
nest() %>%
mutate(Ncamp = map_dbl(data, function(.x){ .x %>%
distinct(Date, .keep_all= TRUE) %>%
nrow()})) %>%
filter(Ncamp >= 8) %>%
unnest(data) %>%
dplyr::select(-Ncamp, -Date) %>%
arrange(Cave, Year, Season) %>%
dplyr::group_by(Cave,Year, Season) %>%
tidyr::pivot_wider(names_from = Species, values_from =Abundance, values_fn = list(Abundance = mean)) %>%
mutate(across(c("Mesabolivar spp.":"Chelodesmidae sp."), as.integer)) %>%
mutate_all(replace_na, 0))
str(cav_comm)
# Generate Taxonomic abundance-matrix
cav_comm_df <- cav_comm[,-c(1:4)]
str(cav_comm_df)
cav_comm_df %>%
rowSums() # All caves have records in all years
# Landscape predictors matrix
cav_land <- as.data.frame(cav_comm %>%
dplyr::select(Cave, Location, Year, Season) %>%
group_by(Cave, Location, Year, Season) %>%
distinct() %>%
inner_join(y=landscape_metrics2) %>%
pivot_wider(names_from = c("Scale", "Class", "metric"), values_from = "value") %>%
dplyr::select(Cave, Location, Year, Season, B500_Forest_pland, B1000_Forest_pland, B500_Minning_pland, B1000_Minning_pland,
B500_Canga_pland, B1000_Canga_pland) %>%
inner_join(y=topodist_mine2) %>%
pivot_wider(names_from = "metric", values_from = "value") %>%
arrange(Cave, Location, Year, Season))
cav_land_df <- cav_land[,-c(1:4)]
# Environmental predictors matrix
names(cav)
cav_env <- cav %>%
dplyr::select(Cave, Location, Year, Season, Precipitation, Temperature, Area) %>%
dplyr::group_by(Cave,Location, Year, Season) %>%
summarise(Precipitation=mean(Precipitation),
Temperature=mean(Temperature), Area = mean(Area)) %>%
arrange(Cave, Location, Year, Season) %>%
drop_na()
# Some environmental data is missing in some caves, which where dropped. We have to
# adjust the other data matrix
# First let´s arrange the environmental matrix to match community
cav_DF<- as.data.frame(cav_env %>%
inner_join(y=cav_comm) %>%
arrange(Cave, Location, Year, Season))
# And now add and correct landscape data
cav_DF <- as.data.frame(cav_land %>%
inner_join(y=cav_DF) %>%
arrange(Cave, Location, Year, Season))
# Finally, order Year data and transform it, together with season, in a factor
cav_DF$Year <- factor(cav_DF$Year, order = TRUE, levels = c("2015", "2016", "2017", "2018", "2019"))
cav_DF$Season <- factor(cav_DF$Season, order = F, levels = c("Chuvosa", "Seca"))
str(cav_DF)
# Remove species with < 10 records
cav_DF_pa <- cav_DF[,c(15:47)]
cav_DF_pa[cav_DF_pa > 0] <- 1
cav_DF_pa <- bind_cols(tibble(Cave=cav_DF$Cave),tibble(cav_DF_pa))
Cave_registers <- cav_DF_pa %>%
group_by(Cave) %>%
summarise_all(funs(sum))
Cave_registers_pa <- Cave_registers[,c(2:34)]
Cave_registers_pa[Cave_registers_pa > 0] <- 1
Cave_registers_pa <- bind_cols(tibble(Cave=Cave_registers$Cave),tibble(Cave_registers_pa))
caves <- as.data.frame(Cave_registers_pa %>%
dplyr::select("Mesabolivar spp.":"Chelodesmidae sp.") %>%
colSums)
retirar <- c("Dytiscidae sp.1", "Glomeridesmus spelaeus", "Circoniscus carajasensis", "Chelodesmidae sp.",
"Pseudonannolenidae sp.")
cav_DF2 <- cav_DF %>%
dplyr::select(-(retirar))
### Overall constrained analysis (RDA)- Redundancy Analysis ####
# What contributes most to the patterns in species composition found: space, time or environment?
# Generate time variable to be used in the following analysis
# First, creating a variable that nests Season in Years
cav_DF3 <- as.data.frame(cav_DF2 %>%
unite("time", c("Year","Season"), sep = "_"))
cav_DF3$time <- factor(cav_DF3$time, order = TRUE,
levels = c("2015_Chuvosa","2015_Seca","2016_Chuvosa","2016_Seca","2017_Chuvosa","2017_Seca","2018_Chuvosa","2018_Seca","2019_Chuvosa","2019_Seca"))
Season <- dudi.mix(cav_DF2$Season, scannf=F, nf=2)$tab
Year <- dudi.mix(cav_DF2$Year, scannf=F, nf=2)$tab
time <- dudi.mix(cav_DF3$time, scannf=F, nf=1)$tab #interaction of Year and Season
## Pre-RDA
# Hellinger transformation for abundance data (some outliers in abundance)
cav_comm_df.hel <- decostand(cav_DF3[14:41], "hellinger")
## VIFS - checking for colinearity
cor(cav_DF3[,c(4:13)])
vif(cav_DF3[,c(4:10)]) # We´ll retain only the 1000 scale variables
cav_land_df <- cav_DF3[,c(5,7,9,10)]
vif(cav_land_df) #ok
vif(cav_DF3[,c(11:13)]) # ok
cav_env_df <- cav_DF3[,c(11:13)]
## Final preparation of predictor datasets to run RDA
Location <- cav_DF3$Location
alldata <- -cbind(Year, Season,cav_land_df,cav_env_df)
#or
alldata2 <- -cbind(time,cav_land_df,cav_env_df) #One variable nesting Year:Season
# Full RDA
# check between sets of variables
rda.comp.full<-rda(cav_comm_df.hel~.,data=alldata2,add=TRUE)
var<-varpart(cav_comm_df.hel,time, cav_land_df,cav_env_df)
plot(var,
digits = 2,
bg = c("red", "blue", "green"),
Xnames = c("Temporal", "Landscape", "Environmental"),
id.size = 0.7
)
# pRDA - Partial Redundancy analysis
rda.comp.p <- rda(cav_comm_df.hel~time$df.L+cav_land_df$B1000_Canga_pland+cav_land_df$B1000_Forest_pland+cav_land_df$B1000_Minning_pland+cav_land_df$tdm+
cav_env_df$Area+cav_env_df$Temperature+cav_env_df$Precipitation+Condition(Location),add=TRUE)
anova(rda.comp.p,permutations = how(nperm = 999), by="margin") # acess p-values
RsquareAdj(rda.comp.p) # acess adjusted R-squared
### Modelling multivariate composition data using GLMs (mvAbund approach) ####
# generate multivariate response variable data for mvAbund package
cav_spp2 <- mvabund(cav_DF3[,14:41])
meanvar.plot(cav_spp2) # clearly not a good sign for traditional ordination techniques
# Does disturbance variables influence multi-species abundance patterns?
# Multi-model selection using AIC, evaluating between time and area variables in
# a model in which disturbance variables are fixed (selected from the results of the RDA)
N.Model_ta <- manyglm(cav_spp2~cav_DF3$time+cav_DF3$Area+cav_DF3$B1000_Minning_pland+cav_DF3$tdm, family="negative_binomial")
N.Model_a <-manyglm(cav_spp2~cav_DF3$Area+cav_DF3$B1000_Minning_pland+cav_DF3$tdm, family="negative_binomial")
N.Model_t <- manyglm(cav_spp2~cav_DF3$time+cav_DF3$B1000_Minning_pland+cav_DF3$tdm, family="negative_binomial")
N.Model_ta$AICsum # best model, including time and area
N.Model_a$AICsum
N.Model_t$AICsum
# extract values
output_coef <- N.Model_ta$coefficients
output_coef2 <- N.Model_ta$stderr.coefficients
term <- dimnames(N.Model_ta$coefficients)[[1]]
# wrangle them
# coefficient estimate
output_coef <- cbind(term,output_coef)
output_coef <- as_tibble(output_coef)
output_coef <- output_coef %>%
pivot_longer(-term,names_to = "sp.name", values_to = "estimate")
# standard error
output_coef2 <- cbind(term,output_coef2)
output_coef2 <- as_tibble(output_coef2)
output_coef2 <- output_coef2 %>%
pivot_longer(-term,names_to = "sp.name", values_to = "std.error")
# join both
output_coefficients <- as.data.frame(output_coef %>%
inner_join(y=output_coef2))
str(output_coefficients)
output_coefficients$estimate <- as.numeric(output_coefficients$estimate)
output_coefficients$std.error <- as.numeric(output_coefficients$std.error)
output_coefficients_f <- output_coefficients %>%
filter(term=="cav_DF3$tdm" | term=="cav_DF3$B1000_Minning_pland") %>%
mutate(term = case_when(term == "cav_DF3$B1000_Minning_pland" ~ "Mining cover",
term == "cav_DF3$tdm" ~ "Distance to mine",
TRUE ~ "Other")) %>%
filter(sp.name=="Charinus.ferreus" & term == "Mining cover"| sp.name=="Pyrgodesmidae.sp.1" & term == "Mining cover" |
sp.name=="Escadabiidae.sp.1" & term == "Mining cover" | sp.name=="Sphendononema.guildingii" & term == "Mining cover" |
sp.name=="Stygnidae.sp.1" & term == "Mining cover" | sp.name=="Thecadactylus.rapicauda" & term == "Mining cover" |
sp.name=="Pristimantis.fenestratus" & term == "Mining cover"|
sp.name=="Prodidomidae.sp." & term == "Mining cover" | sp.name=="Phalangopsis.sp.1" & term == "Mining cover" |
sp.name=="Theraphosidae" & term == "Mining cover" |
sp.name=="Astieae.sp.1" & term == "Distance to mine" |
sp.name=="Spirostreptida.sp." & term == "Distance to mine"| sp.name=="Pristimantis.fenestratus" & term == "Distance to mine"|
sp.name=="Plato.spp." & term == "Distance to mine"| sp.name=="Latebraria.sp." & term == "Distance to mine"|
sp.name=="Leptodactylus.pentadactylus" & term == "Distance to mine"| sp.name=="Prodidomidae.sp." & term == "Distance to mine" |
sp.name=="Prionostemma.sp." & term == "Distance to mine"| sp.name=="Protimesius.sp." & term == "Distance to mine"|
sp.name=="Theraphosidae" & term == "Distance to mine" | sp.name=="Cydninae.sp.1" & term == "Distance to mine" |
sp.name=="Escadabiidae.sp.1" & term == "Distance to mine" | sp.name=="Escadabiidae.sp.2" & term == "Distance to mine" |
sp.name=="Uvaroviella.sp." & term == "Distance to mine") %>%
mutate(sp.name = case_when(sp.name=="Charinus.ferreus" ~ "Charinus ferreus",
sp.name=="Astieae.sp.1" ~ "Astieae sp.1",
sp.name=="Spirostreptida.sp." ~ "Spirostreptida sp.",
sp.name=="Escadabiidae.sp.1" ~ "Escadabiidae sp.1",
sp.name=="Escadabiidae.sp.2" ~ "Escadabiidae sp.2",
sp.name=="Cydninae.sp.1" ~ "Cydninae sp.1",
sp.name=="Stygnidae.sp.1" ~ "Stygnidae sp.1",
sp.name=="Uvaroviella.sp." ~ "Uvaroviella sp.",
sp.name=="Pristimantis.fenestratus" ~ "Pristimantis fenestratus",
sp.name=="Prionostemma.sp." ~ "Prionostemma sp.",
sp.name=="Protimesius.sp." ~ "Protimesius sp.",
sp.name=="Prodidomidae.sp." ~ "Prodidomidae sp.",
sp.name=="Phalangopsis.sp.1" ~ "Phalangopsis sp.1",
sp.name=="Latebraria.sp." ~ "Latebraria sp.",
sp.name=="Plato.spp." ~ "Plato sp.",
sp.name=="Sphendononema.guildingii" ~ "Sphendononema guildingii",
sp.name=="Heterophrynus.longicornis" ~ "Heterophrynus longicornis", sp.name=="Theraphosidae" ~ "Theraphosidae",
sp.name=="Leptodactylus.pentadactylus" ~ "Leptodactylus pentadactylus",
sp.name=="Pyrgodesmidae.sp.1" ~ "Pyrgodesmidae sp.1", sp.name=="Spirostreptida.sp." ~ "Spirostreptida sp.",
sp.name=="Thecadactylus.rapicauda" ~ "Thecadactylus rapicauda")) %>%
mutate(sp.name = fct_reorder(sp.name,desc(sp.name)))
Coef_plot <- ggplot(output_coefficients_f,aes(x = estimate, y = sp.name,
xmin = estimate - 1.96*std.error,
xmax = estimate + 1.96*std.error)) +
geom_pointrange() +
facet_wrap( ~ term, scales = "free") +
geom_vline(xintercept=0, col="black", linetype = "dashed", size=0.5) +
labs(x = "Estimate", y = "Taxa") +
theme_bw() + theme(
strip.text = element_text(size = 15, face="bold"),
legend.position="bottom",
legend.title = element_text(size=10, face="bold"),
legend.text = element_text(size=10),
axis.title.x = element_text(size=15, face="bold"),
axis.title.y = element_text(size=15, face="bold"))
png("Fig4_Final.png",width =1800, height = 900, res=200, bg = "white")
Coef_plot
dev.off()