Title: | Bayesian Modelling of Bipartite Networks |
---|---|
Description: | Bayesian modelling of bipartite network structure, following the approach of Young et al. <doi:10.1038/s41467-021-24149-x>. |
Authors: | Francisco Rodriguez-Sanchez [aut, cre] |
Maintainer: | Francisco Rodriguez-Sanchez <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.0.7 |
Built: | 2024-11-13 05:15:10 UTC |
Source: | https://github.com/Pakillo/BayesianNetworks |
Check fitted model
check_model(fit = NULL, data = NULL)
check_model(fit = NULL, data = NULL)
fit |
A fitted model, as obtained from |
data |
Data list (from |
Model checks on console and graphical window.
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) check_model(fit, data = data)
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) check_model(fit, data = data)
Fit model
fit_model( data = NULL, model = c("sampling_effort", "Young2021", "varying_preferences"), beta = 0.01, ... )
fit_model( data = NULL, model = c("sampling_effort", "Young2021", "varying_preferences"), beta = 0.01, ... )
data |
A named list containing the required data, as obtained from
|
model |
character. One of "Young2021", "sampling_effort", or "varying_preferences", or a path to a file describing the Stan model in case you want to use a modified Stan model. |
beta |
Rate of exponential prior on |
... |
Further arguments for |
A fitted model (cmdstanr::CmdStanMCMC()
object).
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt)
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt)
Get posterior values
get_posterior( fit = NULL, data = NULL, param = c("all", "connectance", "preference", "plant.abund", "animal.abund", "int.prob", "link") )
get_posterior( fit = NULL, data = NULL, param = c("all", "connectance", "preference", "plant.abund", "animal.abund", "int.prob", "link") )
fit |
Fitted model (from |
data |
Data list (from |
param |
character. Name of the parameter to retrieve the posterior samples. |
A data frame
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) get_posterior(fit, dt, param = "connectance") int.prob <- get_posterior(fit, dt, param = "int.prob") int.prob int.prob |> tidybayes::mean_qi() # mean edge probability # all posteriors get_posterior(fit, dt, param = "all")
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) get_posterior(fit, dt, param = "connectance") int.prob <- get_posterior(fit, dt, param = "int.prob") int.prob int.prob |> tidybayes::mean_qi() # mean edge probability # all posteriors get_posterior(fit, dt, param = "all")
Get seed used to fit a model
get_seed(fit = NULL)
get_seed(fit = NULL)
fit |
A fitted model |
A number
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) get_seed(fit)
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) get_seed(fit)
Plot heatmap of observed counts
plot_counts_obs(mat = NULL, ...)
plot_counts_obs(mat = NULL, ...)
mat |
A matrix with count data reporting interaction frequency (e.g. visits to flowers, number of fruits consumed per plant or species). Plants must be in rows, Animals must be in columns. |
... |
Further arguments for |
A ggplot object
data(web) plot_counts_obs(web) plot_counts_obs(web, sort = FALSE) plot_counts_obs(web, zero.na = FALSE, sort = FALSE)
data(web) plot_counts_obs(web) plot_counts_obs(web, sort = FALSE) plot_counts_obs(web, zero.na = FALSE, sort = FALSE)
Plot heatmap of predicted counts
plot_counts_pred(pred.df = NULL, ...)
plot_counts_pred(pred.df = NULL, ...)
pred.df |
A data frame containing the predicted counts, as generated by
|
... |
Further arguments for |
A ggplot object
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) pred.df <- predict_counts(fit, dt) plot_counts_pred(pred.df) plot_counts_pred(pred.df, sort = FALSE)
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) pred.df <- predict_counts(fit, dt) plot_counts_pred(pred.df) plot_counts_pred(pred.df, sort = FALSE)
Plot predicted versus observed counts
plot_counts_pred_obs( pred.df = NULL, data = NULL, byplant = FALSE, width = 0.95, ... )
plot_counts_pred_obs( pred.df = NULL, data = NULL, byplant = FALSE, width = 0.95, ... )
pred.df |
A data frame containing the predicted counts, as generated by
|
data |
Data list (from |
byplant |
Logical. If TRUE, show predicted and observed counts per plant
(using |
width |
width of the credible interval (default = 0.95). |
... |
Further arguments to be passed to |
A ggplot object
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) pred.df <- predict_counts(fit, dt) plot_counts_pred_obs(pred.df, dt) plot_counts_pred_obs(pred.df, dt, fatten_point = 3) plot_counts_pred_obs(pred.df, dt, byplant = TRUE) plot_counts_pred_obs(pred.df, dt, byplant = TRUE, scale = "free")
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) pred.df <- predict_counts(fit, dt) plot_counts_pred_obs(pred.df, dt) plot_counts_pred_obs(pred.df, dt, fatten_point = 3) plot_counts_pred_obs(pred.df, dt, byplant = TRUE) plot_counts_pred_obs(pred.df, dt, byplant = TRUE, scale = "free")
Plot a heatmap of average interaction probabilities
plot_interaction_prob(post = NULL)
plot_interaction_prob(post = NULL)
post |
Data frame containing the posterior probabilities, as generated
from |
A ggplot object
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) post <- get_posterior(fit, dt) plot_interaction_prob(post)
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) post <- get_posterior(fit, dt) plot_interaction_prob(post)
The r
(preference) parameter in Young et al. model takes a prior
exponential distribution with rate = beta. Use this function to
visualise the prior distribution of r
given the chosen beta.
Alternatively, if providing the fitted model, a plot comparing the
prior versus posterior preference(s) will be produced.
plot_prior(beta = NULL, fit = NULL, data = NULL)
plot_prior(beta = NULL, fit = NULL, data = NULL)
beta |
A number > 0. Rate of the exponential distribution. |
fit |
A fitted model, as obtained from |
data |
Data list (from |
A plot
## Providing value for beta plot_prior(beta = 0.01) plot_prior(beta = 0.001) ## Providing fitted model data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) plot_prior(fit = fit, data = dt)
## Providing value for beta plot_prior(beta = 0.01) plot_prior(beta = 0.001) ## Providing fitted model data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) plot_prior(fit = fit, data = dt)
Plot heatmap of residuals (observed - predicted counts).
plot_residuals(pred.df = NULL, data = NULL, ...)
plot_residuals(pred.df = NULL, data = NULL, ...)
pred.df |
A data frame containing the predicted counts, as generated by
|
data |
Data list (from |
... |
Further arguments for |
A ggplot object
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) pred.df <- predict_counts(fit, dt) plot_residuals(pred.df, dt) plot_residuals(pred.df, dt, sort = FALSE)
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) pred.df <- predict_counts(fit, dt) plot_residuals(pred.df, dt) plot_residuals(pred.df, dt, sort = FALSE)
Generate the posterior predictive distribution of counts for every pairwise interaction.
predict_counts(fit = NULL, data = NULL)
predict_counts(fit = NULL, data = NULL)
fit |
Fitted model |
data |
Data list |
A data frame
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) predict_counts(fit, dt)
data(web) dt <- prepare_data(mat = web, sampl.eff = rep(20, nrow(web))) fit <- fit_model(dt, refresh = 0) predict_counts(fit, dt)
Prepare the data for modelling
prepare_data(mat = NULL, sampl.eff = NULL)
prepare_data(mat = NULL, sampl.eff = NULL)
mat |
An integer matrix containing quantitative (not qualitative or binary) count data con interaction frequency (e.g. visits to flowers, number of fruits consumed per plant or species). Plants must be in rows, Animals must be in columns. |
sampl.eff |
A numeric vector with the sampling effort (e.g. observation hours) spent on each plant. |
A named list with all the data required to run the model.
data(web) prepare_data(web, sampl.eff = rep(20, nrow(web)))
data(web) prepare_data(web, sampl.eff = rep(20, nrow(web)))
An example bipartite network of 8 plant species and 21 pollinators, from Kaiser-Bunbury et al. 2017.
web
web
web
A numeric (integer) matrix with 8 rows (representing plants) and 21 columns (representing animals)
Kaiser-Bunbury, C., Mougal, J., Whittington, A. et al. Ecosystem restoration strengthens pollination network resilience and function. Nature 542, 223–227 (2017). https://doi.org/10.1038/nature21071