https://github.com/pakillo/bayesianwebs
Bayesian modelling of bipartite networks
https://github.com/pakillo/bayesianwebs
Last synced: 11 months ago
JSON representation
Bayesian modelling of bipartite networks
- Host: GitHub
- URL: https://github.com/pakillo/bayesianwebs
- Owner: Pakillo
- License: gpl-3.0
- Created: 2024-05-05T10:13:06.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2025-01-22T19:01:15.000Z (over 1 year ago)
- Last Synced: 2025-06-20T02:39:08.655Z (11 months ago)
- Language: R
- Homepage: https://pakillo.github.io/BayesianWebs/
- Size: 2.64 MB
- Stars: 12
- Watchers: 2
- Forks: 5
- Open Issues: 4
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE.md
Awesome Lists containing this project
README
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# BayesianWebs

[](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)
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")
```