forked from valentinitnelav/bootstrapnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.Rmd
174 lines (133 loc) · 6.28 KB
/
examples.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
---
title: "bootstrapnet - Examples"
author: "by [Valentin Stefan](https://github.com/valentinitnelav) - last update `r format(Sys.time(), '%d %B %Y')`"
---
```{r setup, include=FALSE}
# For avoiding long waiting time, read already saved/cached objects
lst_nest <- readRDS(file = "./man/cache/README-example-nestedness-1-lst.rds")
lst_niov <- readRDS(file = "./man/cache/README-example-niche-overlap-1-lst.rds")
lst_btw <- readRDS(file = "./man/cache/README-example-betweenness-1-lst.rds")
```
# Load packages
```{r load-packages, message=FALSE, warning=FALSE}
# Install bootstrapnet if not already done:
# install.packages("devtools")
# devtools::install_github("valentinitnelav/bootstrapnet")
library(bootstrapnet)
library(magrittr)
library(bipartite)
```
# Simulate data
Generate two fictive networks from `bipartite::Safariland` to compare with `bootstrapnet` functionality:
```{r}
data(Safariland)
set.seed(321)
Safariland_1 <- Safariland[, sort(sample.int(ncol(Safariland), 20))]
sum(Safariland_1) # number of interactions
set.seed(123)
Safariland_2 <- Safariland[, sort(sample.int(ncol(Safariland), 20))]
sum(Safariland_2)
```
Note that, `Safariland_1` has `r sum(Safariland_1)` interactions and `Safariland_2` has `r sum(Safariland_2)`. This can give us an idea about the values we can use in the `start` and `step` arguments below. Presumably 10% of the interactions of the smallest network should suffice for `start.` And for `step` use 5% or 10%.
Note that, very small `step` will require intensive CPU time and a very small `start` can result in many `NA` warnings because indices cannot be computed for the first small sampled networks.
```{r}
my_start <- 50 # chosen higher than 20 to gain some CPU time
my_step <- 20
my_n_boot <- 50
```
# Network-level indices/metrics
## nestedness
**Resample two networks with computing "nestedness".**
The two matrices (webs) are placed in a named list (`list(s1 = Safariland_1, s2 = Safariland_2)`), then the data is prepared with `web_matrix_to_df()`, which gives a suitable data format for the `boot_networklevel()` function, which further prepares the bootstrapped results for `ggplot`.
```{r nestedness, eval=FALSE}
lst_nest <- list(s1 = Safariland_1, s2 = Safariland_2) %>%
lapply(web_matrix_to_df) %>%
boot_networklevel(col_lower = "lower", # column name for plants
col_higher = "higher", # column name for insects
index = "nestedness",
level = "both", # here, nestedness is not affected by level
start = my_start,
step = my_step,
n_boot = my_n_boot,
n_cpu = 3)
```
<!--
saveRDS(lst_nest, file = "./man/cache/README-example-nestedness-1-lst.rds")
-->
Plot the bootstrap results for the two webs.
```{r}
gg_networklevel(lst_nest)
```
The dashed lines represent the quantile based 95% confidence intervals. The continuous thicker lines represent the mean values at different sample sizes. The final sample sizes are actually the entire webs (total number of interactions). Each bootstrap/iteration is represented by a thinner transparent line.
## niche overlap
**Resample two networks with computing "niche overlap".**
The computation here is carried for both species levels (lower and higher, so plants and insects). If you are interested only in one level, then specify that with the parameter `level` (`level = 'lower'` or `level = 'higher'`).
```{r, eval=FALSE}
lst_niov <- list(s1 = Safariland_1, s2 = Safariland_2) %>%
lapply(web_matrix_to_df) %>%
boot_networklevel(col_lower = "lower", # column name for plants
col_higher = "higher", # column name for insects
index = "niche overlap",
level = "both", # for both levels (lower & higher)
start = my_start,
step = my_step,
n_boot = my_n_boot,
n_cpu = 3)
```
<!--
saveRDS(lst_niov, file = "man/cache/README-example-niche-overlap-1-lst.rds")
-->
Plot the bootstrap results.
```{r}
niov_gg <- gg_networklevel(lst_niov)
niov_gg$niche.overlap.HL # for higher level species
niov_gg$niche.overlap.LL # for lower level species
```
# Species-level indices/metrics
## betweenness
**Resample two networks with computing "betweenness".**
Compare "betweenness" of 'Alstroemeria aurea' (lower level species) between the two networks.
Also compare the betweenes of 'Allograpta.Toxomerus' (higher level species).
If you are interested only in the lower or higher level species computations, then set the parameter `level` accordingly (`level = 'lower'` or `level = 'higher'`). In such cases, then specify only one of the `sp_lower` and `sp_higher` in the plotting functions `gg_specieslevel_compare_webs` or `gg_specieslevel_web_by_web`.
```{r, eval=FALSE}
lst_btw <- list(s1 = Safariland_1, s2 = Safariland_2) %>%
lapply(web_matrix_to_df) %>%
boot_specieslevel(col_lower = "lower", # column name for plants
col_higher = "higher", # column name for insects
index = "betweenness",
level = "both", # for both levels (lower & higher)
start = my_start,
step = my_step,
n_boot = my_n_boot,
n_cpu = 3)
```
<!--
saveRDS(lst_btw, file = "man/cache/README-example-betweenness-1-lst.rds")
-->
Plot the bootstrap results.
```{r}
btw_comp_Aa_At_gg <- lst_btw %>%
get_stats_multi() %>%
gg_specieslevel_compare_webs(sp_lower = "Alstroemeria aurea",
sp_higher = "Allograpta.Toxomerus")
btw_comp_Aa_At_gg # plot all
```
Plot resampled betweenness of all species at both levels of selected common species.
Selected common species:
```{r}
common_lower_sp <- intersect(rownames(Safariland_1), rownames(Safariland_2))
common_higher_sp <- intersect(colnames(Safariland_1), colnames(Safariland_2))
btw_sp_webs_gg <- lst_btw %>%
gg_specieslevel_web_by_web(sp_lower = common_lower_sp[1:5],
sp_higher = common_higher_sp[1:5])
btw_sp_webs_gg
```
To plot betweenness of all species at all levels for each web/network, just leave the parameters `sp_lower` and `sp_higher` to default to `NULL`.
```{r}
btw_sp_webs_gg_all <- lst_btw %>%
gg_specieslevel_web_by_web()
```
# R Session info
```{r r-session-info}
sessionInfo()
```