-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructure-stability.Rmd
365 lines (238 loc) · 10.1 KB
/
structure-stability.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
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
---
title: "structure-stability"
author: "Iara Diamela Rodriguez"
editor_options:
chunk_output_type: console
---
## Setup ##
```{r setup, eval=T, echo=FALSE}
knitr::opts_chunk$set(echo=TRUE)
load(".RData")
require(igraph)
require(NetIndices)
require(multiweb)
require(devtools)
require(dplyr)
require(future.apply)
require(knitr)
require(purrr)
require(stringr)
require(cowplot)
require(ggplot2)
require(RColorBrewer)
```
### Read Potter Cove and Beagle Channel food webs ###
```{r fw, echo=FALSE, message=FALSE, results='asis'}
# Read food webs
# .txt format: predator -> prey
p <- readNetwork("Data/Potter_FW.txt", edgeListFormat=1)
b <- readNetwork("Data/Beagle_FW.txt", edgeListFormat=1)
# Remove loops and repeated interactions
p <- igraph::simplify(p)
b <- igraph::simplify(b)
# Check that loops and repeated interactions were removed.
# If Cannib = 0 loops were removed.
calc_topological_indices(p)
calc_topological_indices(b)
```
### Calculate empirical structure and stability metrics and perfome Strona Curveball randomizations ###
```{r ti, eval=T, echo=F, message=T, warning=T}
# Calculate empirical structure and stability metrics
if(!file.exists("Data/ti.rds")){
nc <- detectCores()
tip <- bind_cols(calc_topological_indices(p), calc_modularity(p), calc_QSS(p, nsim=1000, ncores=nc)) %>% mutate(Name="Potter")
tib <- bind_cols(calc_topological_indices(b), calc_modularity(b), calc_QSS(b, nsim=1000, ncores=nc)) %>% mutate(Name="Beagle")
ti <- bind_rows(tip, tib) %>% rename(Network=Name)
saveRDS(ti, "Data/ti.rds")
write.csv(ti, file="Results/ti.csv")
rm(tip, tib)
} else {
ti <- readRDS("Data/ti.rds")
}
# Calculate species trophic level and omnivory index
adj_matrix <- get.adjacency(p, sparse=FALSE)
p_tl <- TrophInd(adj_matrix)
adj_matrix1 <- get.adjacency(b, sparse=FALSE)
b_tl <- TrophInd(adj_matrix1)
# Calculate Strona Curveball randomizations structure and stability metrics
pCB <- curveBall(p, nsim=1000)
bCB <- curveBall(b, nsim=1000)
if(!file.exists("Data/tiIC95.rds")){
require(parallel)
nc <- detectCores()
cl <- makeCluster(rep("localhost", nc))
ptiIC <- bind_cols(calc_topological_indices(pCB)) %>% mutate(Name="Potter")
btiIC <- bind_cols(calc_topological_indices(bCB)) %>% mutate(Name="Beagle")
tiIC <- bind_rows(ptiIC, btiIC) %>% rename(Network=Name)
ptiQSS <- calc_QSS(pCB, nsim=10000, ncores=nc) %>% mutate(Name="Potter")
btiQSS <- calc_QSS(bCB, nsim=10000, ncores=nc) %>% mutate(Name="Beagle")
QSSIC <- bind_rows(ptiQSS, btiQSS) %>% rename(Network=Name)
pM <- calc_modularity(pCB) %>% mutate(Name="Potter")
bM <- calc_modularity(bCB) %>% mutate(Name="Beagle")
ModularityIC <- bind_rows(pM, bM) %>% rename(Network=Name)
tiMIC <- cbind(tiIC, QSSIC, ModularityIC)
stop2Cluster(cl)
saveRDS(tiMIC,"Data/tiIC95.rds")
rm(ptiIC,btiIC,tiIC,ptiQSS,btiQSS,QSSIC,pM,bM,ModularityIC,nc,cl)
} else {
tiMIC <- readRDS("Data/tiIC95.rds")
}
```
### Statistical analysis of structure and stability metrics ###
```{r stat, eval=T, echo=F, message=T, warning=T}
# Two-sided Kolmogorov-Smirnov test
#(change metric name wanting to test: TLmean, Omnivory, Modularity, MEing (QSS))
tiMIC_pc <- filter((tiMIC %>% select(TLmean, Network)), Network=="Potter")
tiMIC_bc <- filter((tiMIC %>% select(TLmean, Network)), Network=="Beagle")
ks.test(tiMIC_pc$TLmean, tiMIC_bc$TLmean)
#rm(tiMIC_pc, tiMIC_bc)
```
### Calculate and classify species by topological roles ###
```{r topoRol, echo=FALSE, message=FALSE, results='asis'}
source("R/network_fun.r")
mod_by_red <- vector(mode="list", length=2)
set.seed(123)
modulos <- cluster_spinglass(p)
mti <- data_frame(Name="Potter", Groups=length(modulos$csize), Modularity=modulos$modularity)
mod_by_red[[1]] <- modulos
modulos <- cluster_spinglass(b)
mti <- mti %>% add_row(Name="Beagle", Groups=length(modulos$csize), Modularity=modulos$modularity)
mod_by_red[[2]] <- modulos
# Topological roles
if(!file.exists("Data/rolesTopologicos.rds")){
nc <- detectCores()
# POTTER
tR1 <- calc_topological_roles(p, nsim=100, ncores=nc)
tR <- tR1 %>% group_by(node) %>% summarize(wtmLowCI=quantile(within_module_degree, 0.005, na.rm=TRUE),
wtmHiCI=quantile(within_module_degree, 0.995, na.rm=TRUE),
amcLowCI=quantile(among_module_conn, 0.005, na.rm=TRUE),
amcHiCI=quantile(among_module_conn, 0.995, na.rm=TRUE),
within_module_degree=mean(within_module_degree, na.rm=TRUE),
among_module_conn=mean(among_module_conn, na.rm=TRUE))
topoRoles <- tR %>% mutate(Name="Potter")
# BEAGLE
tR1 <- calc_topological_roles(b, nsim=100, ncores=nc)
tR <- tR1 %>% group_by(node) %>% summarize(wtmLowCI=quantile(within_module_degree, 0.005, na.rm=TRUE),
wtmHiCI=quantile(within_module_degree, 0.995, na.rm=TRUE),
amcLowCI=quantile(among_module_conn, 0.005, na.rm=TRUE),
amcHiCI=quantile(among_module_conn, 0.995, na.rm=TRUE),
within_module_degree=mean(within_module_degree, na.rm=TRUE),
among_module_conn=mean(among_module_conn, na.rm=TRUE))
topoRoles <- bind_rows(topoRoles, tR %>% mutate(Name="Beagle"))
saveRDS(topoRoles,"Data/rolesTopologicos.rds")
rm(nc)
} else {
topoRoles <- readRDS("Data/rolesTopologicos.rds")
}
# Classify species by topological roles
hub_conn <- data.frame()
hc <- plot_topological_roles(filter(topoRoles, Name=="Potter"), p, mod_by_red[[1]])
hub_connp <- hc %>% mutate(Name="Potter", modulo=mod_by_red[[1]]$membership[node])
hc <- hc %>% mutate(Name="Potter", modulo=mod_by_red[[1]]$membership[node])
hub_conn <- bind_rows(hub_conn, hc)
hc <- plot_topological_roles(filter(topoRoles, Name=="Beagle"), b, mod_by_red[[2]])
hub_connb <- hc %>% mutate(Name="Beagle", modulo=mod_by_red[[2]]$membership[node])
hc <- hc %>% mutate(Name="Beagle", modulo=mod_by_red[[2]]$membership[node])
hub_conn <- bind_rows(hub_conn, hc)
hub_conn <- rename(hub_conn, Network=Name)
write.csv(hub_conn, file="Results/rolestop.csv")
#rm(hc,hub_connb,hub_connp,mod_by_red,plot_topological_roles,mti,modulos,topoRoles)
```
### Habitat and functional group vs modules ###
```{r traits, echo=FALSE, message=FALSE, results='asis'}
# Read habitat and functional group data in each module
# format .txt
cp <- read.delim(file="Data/Potter_ModHabFG.txt")
cb <- read.delim(file="Data/Beagle_ModHabFG.txt")
cp <- lapply(cp, factor)
cb <- lapply(cb, factor)
# Calculate trait levels frequency in each module
cpH <- as.data.frame(table(cp$Mod, cp$Hab))
colnames(cpH) <- c("Mod", "Habitat", "Freq")
cpFG <- as.data.frame(table(cp$Mod, cp$FG))
colnames(cpFG) <- c("Mod", "FG", "Freq")
cbH <- as.data.frame(table(cb$Mod, cb$Hab))
colnames(cbH) <- c("Mod", "Habitat", "Freq")
cbFG <- as.data.frame(table(cb$Mod, cb$FG))
colnames(cbFG) <- c("Mod", "FG", "Freq")
# Chi-square test
require(tidyr)
#--Chi-square test: Trait's levels vs FW
# Potter-Beagle Habitat
cpH_t <- cpH %>% group_by(Habitat) %>% summarise(Total=(sum(Freq)/110)*100) %>% mutate(Name="Potter")
cbH_t <- cbH %>% group_by(Habitat) %>% summarise(Total=(sum(Freq)/145)*100) %>% mutate(Name="Beagle")
H_t <- bind_rows(cpH_t, cbH_t) %>% rename(Network=Name)
H_t <- spread(H_t, Habitat, Total)
H_t <- data.matrix(H_t)
chisq.test(H_t[,3]) #(change habitat level of interest to test: [,2]=benthic, [,3]=benthopelagic, [,4]=pelagic)
# Potter-Beagle Functional Group
cpFG_t <- cpFG %>% group_by(FG) %>% summarise(Total=(sum(Freq)/110)*100) %>% mutate(Name="Potter")
cbFG_t <- cbFG %>% group_by(FG) %>% summarise(Total=(sum(Freq)/145)*100) %>% mutate(Name="Beagle")
FG_t <- bind_rows(cpFG_t, cbFG_t) %>% rename(Network=Name)
FG_t <- spread(FG_t, FG, Total)
chisq.test(FG_t$fish) #(change function group of interest to test: basal taxa, zooplankton, non-living, benthos, fish)
#--Chi-square test: Modules vs traits
# Potter-Habitat
cpH <- spread(cpH, Habitat, Freq)
cpH$Mod <- as.integer(cpH$Mod)
cpH <- data.matrix(cpH)
chisq.test(cpH)
# Potter-Functional group
cpFG <- spread(cpFG, FG, Freq)
cpFG$Mod <- as.integer(cpFG$Mod)
cpFG <- data.matrix(cpFG)
chisq.test(cpFG)
# Beagle-Habitat
cbH <- spread(cbH, Habitat, Freq)
cbH$Mod <- as.integer(cbH$Mod)
cbH <- data.matrix(cbH)
chisq.test(cbH)
# Beagle-Functional group
cbFG <- spread(cbFG, FG, Freq)
cbFG$Mod <- as.integer(cbFG$Mod)
cbFG <- data.matrix(cbFG)
chisq.test(cbFG)
rm(cp,cb,cpH_t,cbH_t,cpFG_t,cbFG_t,cbFG,cbH,cpFG,cpH,H_t,FG_t)
```
### Setup degree distribution analysis ###
```{r setupDegree, eval=T, echo=F, message=T, warning=T}
require(pander)
require(plyr)
require(dplyr)
panderOptions('table.split.table', Inf)
panderOptions('table.style', 'multiline')
options("scipen"=6, "digits"=4)
library(poweRlaw)
source("R/dist_fun.r")
# Upload continuous distributions functions
source("R/powerlaw/discpowerexp.R")
source("R/powerlaw/discexp.R")
source("R/powerlaw/zeta.R")
source("R/powerlaw/powerexp.R")
source("R/powerlaw/exp.R")
source("R/powerlaw/pareto.R")
```
### Fit degree distributions ###
```{r fitDegree, eval=T, echo=F, message=T, warning=T}
# Total-degree
dgp <- data.frame(degree(p)) %>% mutate(Name="Potter")
colnames(dgp) <- c("Degree", "Network")
dgb <- data.frame(degree(b)) %>% mutate(Name="Beagle")
colnames(dgb) <- c("Degree", "Network")
dg <- bind_rows(dgp, dgb)
# Test for differences in degree distribution
ks.test(dgp$Degree, dgb$Degree)
kSamples::ad.test(dgp$Degree, dgb$Degree, method="simulated", Nsim=10000)
#rm(dgp,dgb)
# Fit distributions
opt.output <- vector("list", length=0)
opt.output$GOF <- 0
opt.output$ploting <- 1
fit <- dg %>% group_by(Network) %>% do(fit_ht_dplyr_helper(.))
fitML <- fit
# Correct negative uniform parameter
fitML <- fitML %>% mutate(par1=ifelse(ModelNames=="Uniform", abs(par1), par1))
fitML1 <- filter(fitML, Delta_AICc==0) %>% mutate_if(is.numeric, round, digits=4)
write.csv(fitML, file="Results/NetworksFit.csv")
write.csv(fitML1, file="Results/NetworksLowerAICc.csv")
```