-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathREADME.Rmd
More file actions
132 lines (90 loc) · 3.65 KB
/
Copy pathREADME.Rmd
File metadata and controls
132 lines (90 loc) · 3.65 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# BayesianWebs
<!-- badges: start -->

[](https://github.com/Pakillo/BayesianWebs/actions/workflows/R-CMD-check.yaml)
`r badger::badge_lifecycle("stable")`
`r badger::badge_repostatus("Active")`
[](https://hits.dwyl.com/Pakillo/BayesianWebs)
[](https://hits.dwyl.com/Pakillo/BayesianWebs)
<!-- badges: end -->
The BayesianWebs R package facilitates modelling bipartite networks (like pollination, frugivory, or herbivory networks) using the Bayesian framework developed by [Young et al. (2021)](https://doi.org/10.1038/s41467-021-24149-x).
Inferring the structure of bipartite networks from field (observational) data is a challenging task. Interaction data are hard to collect and require typically large sampling efforts, particularly to characterize infrequent interactions. Inferred network structure is highly sensitive to sampling design, effort, and completeness. Comparing networks from different studies without accounting for these sampling effects may lead to mistaken inferences.
This package uses Bayesian modelling to infer the posterior probability of each pairwise interaction in bipartite networks, accounting for sampling completeness and the inherent stochasticity of field observation data.
## Installation
You can install the development version of BayesianWebs from [GitHub](https://github.com/) with:
``` r
# install.packages("remotes")
remotes::install_github("Pakillo/BayesianWebs")
```
The package requires a working installation of [Stan](https://mc-stan.org/). If you don't have `CmdStan` installed, after installing `BayesianWebs` run:
```r
cmdstanr::install_cmdstan()
```
## Example
```{r example}
library(BayesianWebs)
```
Let's infer the structure of an example dataset from [Kaiser-Bunbury et al. 2017](https://doi.org/10.1038/nature21071) as in Young et al. 2021:
```{r}
data(web)
```
This is an adjacency matrix collecting the number of visits of 21 animals on 8 plants:
```{r}
web
```
```{r}
plot_counts_obs(web, sort = FALSE)
```
First, prepare the data for modelling. Here we assume constant sampling effort among plants:
```{r}
dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web)))
```
Now fit the model. There are several models available: the original model used in Young et al. (2021), a model that takes into account varying sampling effort among plants, and a model that allows for varying preferences among animals. The user can also provide a customised Stan model. See `fit_model()`.
Here we run 4 parallel chains:
```{r}
set.seed(1)
options(mc.cores = 4)
fit <- fit_model(dt, refresh = 0)
```
Check model:
```{r}
check_model(fit, data = dt)
```
Get the posteriors:
```{r}
post <- get_posterior(fit, data = dt)
head(post)
```
```{r}
plot_interaction_prob(post)
```
Predict interaction counts:
```{r}
pred.df <- predict_counts(fit, data = dt)
plot_counts_pred(pred.df, sort = FALSE)
```
Compare observed and predicted counts:
```{r}
plot_counts_pred_obs(pred.df, data = dt)
```
Plot residuals:
```{r}
plot_residuals(pred.df, data = dt, sort = FALSE)
```
## Citation
If you use `BayesianWebs` please cite it:
```{r comment=NA}
citation("BayesianWebs")
```