| Title: | Spatial Ecological Network Inference |
|---|---|
| Description: | A model-agnostic framework for reconstructing and analysing spatially explicit ecological networks from species distributions and ecological inference models. Supports arbitrary ecological groups and includes stochastic block and maximum-entropy inference backends, simulation helpers, network summaries, and spatial mapping utilities. |
| Authors: | Gabriel Munoz [aut, cre] |
| Maintainer: | Gabriel Munoz <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.4.0 |
| Built: | 2026-10-07 11:21:55 UTC |
| Source: | https://github.com/fgabriel1891/metaweave |
Add standard metrics to network collection
add_network_metrics(x, threshold = NULL)add_network_metrics(x, threshold = NULL)
x |
Network collection. |
threshold |
Optional probability threshold. |
Assemble local communities from aligned distribution rasters
assemble_communities(distributions, min_species = 1L)assemble_communities(distributions, min_species = 1L)
distributions |
Named list of SpatRaster or distribution_collection objects. |
min_species |
Minimum species per group. |
Construct a generic stochastic block model backend
block_model( row_lookup, column_lookup, theta, row_group, column_group, species_col = "species", guild_col = "guild" )block_model( row_lookup, column_lookup, theta, row_group, column_group, species_col = "species", guild_col = "guild" )
row_lookup |
Data frame mapping species in the matrix rows to blocks. |
column_lookup |
Data frame mapping species in the matrix columns to blocks. |
theta |
Guild-by-guild probability matrix. |
row_group |
Name of the assemblage group placed in matrix rows. |
column_group |
Name of the assemblage group placed in matrix columns. |
species_col |
Species column in lookup tables. |
guild_col |
Guild column in lookup tables. |
Build a species range stack
build_range_stack( source, template, species_col = NULL, species_keep = NULL, region = NULL, touches = TRUE )build_range_stack( source, template, species_col = NULL, species_keep = NULL, region = NULL, touches = TRUE )
source |
Directory, vector file, sf, or SpatVector. |
template |
Raster template. |
species_col |
Species column for multi-species data. |
species_keep |
Optional species subset. |
region |
Optional crop region. |
touches |
Count all touched cells. |
Create a standard raster grid
create_standard_grid( extent = c(-180, 180, -90, 90), resolution = 1, crs = "EPSG:4326" )create_standard_grid( extent = c(-180, 180, -90, 90), resolution = 1, crs = "EPSG:4326" )
extent |
Numeric vector |
resolution |
Cell resolution. |
crs |
Coordinate reference system. |
Crop a stack and remove species absent from the region
crop_filter_stack(stack, region, return_cropped = TRUE)crop_filter_stack(stack, region, return_cropped = TRUE)
stack |
Species raster stack. |
region |
Crop region. |
return_cropped |
Return cropped rather than original layers. |
Compatibility wrapper for spatial network downscaling
downscale_networks( palm_stack, mammal_stack, palm_lookup, mammal_lookup, theta, species_col = "species", guild_col = "guild", min_palms = 1L, min_mammals = 1L )downscale_networks( palm_stack, mammal_stack, palm_lookup, mammal_lookup, theta, species_col = "species", guild_col = "guild", min_palms = 1L, min_mammals = 1L )
palm_stack, mammal_stack
|
Aligned species distribution rasters. |
palm_lookup, mammal_lookup
|
Species-to-guild lookup tables. |
theta |
Guild-by-guild probability matrix. |
species_col |
Species column in lookup tables. |
guild_col |
Guild column in lookup tables. |
min_palms, min_mammals
|
Minimum local richness in each group. |
Infer a network for one assemblage
infer_network(assemblage, model)infer_network(assemblage, model)
assemblage |
An assemblage. |
model |
An inference model. |
Infer networks for many assemblages
infer_networks(assemblages, model, template = NULL)infer_networks(assemblages, model, template = NULL)
assemblages |
List of assemblages. |
model |
Inference model. |
template |
Optional spatial template. |
Convert an interaction table to a matrix
interaction_table_to_matrix(data, row_col, column_col, value_col, fill = 0)interaction_table_to_matrix(data, row_col, column_col, value_col, fill = 0)
data |
Data frame. |
row_col, column_col, value_col
|
Column names. |
fill |
Missing-pair value. |
List ESRI shapefiles
list_shapefiles(path, recursive = FALSE)list_shapefiles(path, recursive = FALSE)
path |
Directory. |
recursive |
Search recursively. |
Build one local generic block-model network directly
local_block_network( rows, columns, row_lookup, column_lookup, theta, row_group = "rows", column_group = "columns", species_col = "species", guild_col = "guild", cell_id = NA_integer_ )local_block_network( rows, columns, row_lookup, column_lookup, theta, row_group = "rows", column_group = "columns", species_col = "species", guild_col = "guild", cell_id = NA_integer_ )
rows, columns
|
Species names in each network group. |
row_lookup |
Data frame mapping species in the matrix rows to blocks. |
column_lookup |
Data frame mapping species in the matrix columns to blocks. |
theta |
Guild-by-guild probability matrix. |
row_group |
Name of the assemblage group placed in matrix rows. |
column_group |
Name of the assemblage group placed in matrix columns. |
species_col |
Species column in lookup tables. |
guild_col |
Guild column in lookup tables. |
cell_id |
Optional raster cell identifier. |
Compatibility wrapper around local_block_network().
local_probability_network( palms, mammals, palm_lookup, mammal_lookup, theta, species_col = "species", guild_col = "guild", cell_id = NA_integer_ )local_probability_network( palms, mammals, palm_lookup, mammal_lookup, theta, species_col = "species", guild_col = "guild", cell_id = NA_integer_ )
palms, mammals
|
Species names in each network group. |
palm_lookup, mammal_lookup
|
Species-to-guild lookup tables. |
theta |
Guild-by-guild probability matrix. |
species_col |
Species column in lookup tables. |
guild_col |
Guild column in lookup tables. |
cell_id |
Optional raster cell identifier. |
Create a bounding-box polygon
make_bbox(extent, crs = "EPSG:4326")make_bbox(extent, crs = "EPSG:4326")
extent |
Numeric vector |
crs |
Coordinate reference system. |
Map cell metrics back to rasters
map_metrics(x, metrics = NULL)map_metrics(x, metrics = NULL)
x |
Network collection with metrics in its index. |
metrics |
Metric columns; inferred when omitted. |
Constructs a directed network inference backend that generates binary
adjacency matrices under hard structural constraints and returns their
marginal species-pair probabilities. Rows are consumers, columns are
resources, and matrix[i, j] = 1 means consumer i uses resource j.
maxent_model( row_group, column_group = row_group, constraint = c("connectance", "links", "degree_sequence"), connectance = NULL, links = NULL, out_degree = NULL, in_degree = NULL, ensemble_size = 100L, self_links = FALSE, seed = 1L, burn_in = 1000L, thin = 100L, keep_ensemble = FALSE )maxent_model( row_group, column_group = row_group, constraint = c("connectance", "links", "degree_sequence"), connectance = NULL, links = NULL, out_degree = NULL, in_degree = NULL, ensemble_size = 100L, self_links = FALSE, seed = 1L, burn_in = 1000L, thin = 100L, keep_ensemble = FALSE )
row_group |
Assemblage group represented by matrix rows (consumers). |
column_group |
Assemblage group represented by matrix columns
(resources). May equal |
constraint |
One of |
connectance |
Desired connectance in |
links |
Exact number of interactions in every local ensemble member. |
out_degree |
Row sums (numbers of resources per consumer). |
in_degree |
Column sums (numbers of consumers per resource). Degree constraints may be named numeric vectors or functions accepting the locally present species names and returning a numeric vector. |
ensemble_size |
Number of adjacency matrices used for marginal probabilities. |
self_links |
Whether a species may interact with itself when its name appears in both groups. |
seed |
Integer seed. A cell identifier is added to this seed so spatial
cells receive distinct reproducible ensembles. The previous global random
state is restored after inference. Use |
burn_in |
Number of degree-preserving swap proposals before recording a degree-constrained ensemble. |
thin |
Number of swap proposals between recorded matrices. |
keep_ensemble |
Store binary ensemble members in network metadata. |
For constraint = "connectance" or "links", networks are sampled
uniformly from all allowed adjacency matrices with exactly the requested
number of links. This is the maximum-entropy distribution under a hard link
count. For constraint = "degree_sequence", a feasible matrix is found by
maximum flow and randomized with degree-preserving checkerboard swaps. This
is an approximate uniform MCMC ensemble conditional on the supplied margins.
The implementation follows the constrained, minimally biased ensemble rationale of Banville, Gravel & Poisot (2023), but does not yet implement their SVD-entropy objective or simulated-annealing optimizer.
An inference_model compatible with infer_network() and
infer_networks().
Banville F, Gravel D, Poisot T (2023). What constrains food webs? A maximum entropy framework for predicting their structure with minimal biases. PLOS Computational Biology 19: e1011458. doi:10.1371/journal.pcbi.1011458
assemblage <- new_assemblage(list(food_web = c("a", "b", "c"))) model <- maxent_model( row_group = "food_web", constraint = "connectance", connectance = 0.25, ensemble_size = 50, self_links = FALSE, seed = 42 ) network <- infer_network(assemblage, model) network$matrixassemblage <- new_assemblage(list(food_web = c("a", "b", "c"))) model <- maxent_model( row_group = "food_web", constraint = "connectance", connectance = 0.25, ensemble_size = 50, self_links = FALSE, seed = 42 ) network <- infer_network(assemblage, model) network$matrix
Compatibility alias returning metric rasters
network_metrics_raster(x, metrics = NULL)network_metrics_raster(x, metrics = NULL)
x |
Network collection with metrics in its index. |
metrics |
Metric columns; inferred when omitted. |
Construct a local assemblage
new_assemblage(species, cell_id = NA_integer_, coordinates = NULL)new_assemblage(species, cell_id = NA_integer_, coordinates = NULL)
species |
Named list of character vectors, one per ecological group. |
cell_id |
Optional raster cell identifier. |
coordinates |
Optional named numeric vector containing x and y. |
Construct a distribution collection
new_distribution_collection(data, guild = NULL, metadata = NULL)new_distribution_collection(data, guild = NULL, metadata = NULL)
data |
A named |
guild |
Optional guild label for the collection. |
metadata |
Optional species metadata data frame. |
Construct an ecological network
new_ecological_network( matrix, type = c("probability", "binary", "weighted", "observed"), metadata = list(), row_group = "rows", column_group = "columns", directed = FALSE, self_links = NA )new_ecological_network( matrix, type = c("probability", "binary", "weighted", "observed"), metadata = list(), row_group = "rows", column_group = "columns", directed = FALSE, self_links = NA )
matrix |
Numeric interaction matrix. |
type |
One of probability, binary, weighted, or observed. |
metadata |
Optional metadata. |
row_group, column_group
|
Ecological groups represented by rows and columns. |
directed |
Whether matrix orientation represents directed interactions. |
self_links |
Whether self-links are permitted; |
Construct a pluggable inference model
new_inference_model( predict, name = "custom", parameters = list(), subclass = NULL )new_inference_model( predict, name = "custom", parameters = list(), subclass = NULL )
predict |
A function accepting |
name |
Model name. |
parameters |
Model-specific parameters. |
subclass |
Optional additional class name. |
Construct a collection of spatial ecological networks
new_network_collection(networks, index, template = NULL, settings = list())new_network_collection(networks, index, template = NULL, settings = list())
networks |
List of ecological networks. |
index |
Cell-level data frame. |
template |
Spatial raster template. |
settings |
Optional workflow settings. |
Construct a spatial result
new_spatial_result(data, metrics = names(data))new_spatial_result(data, metrics = names(data))
data |
A spatial object. |
metrics |
Names of mapped metrics. |
Prepare palm and mammal range stacks
prepare_range_stacks( palm_source, mammal_source, mammal_species_col, mammal_species_keep = NULL, template = create_standard_grid(), region = NULL, output_dir = NULL, touches = TRUE )prepare_range_stacks( palm_source, mammal_source, mammal_species_col, mammal_species_keep = NULL, template = create_standard_grid(), region = NULL, output_dir = NULL, touches = TRUE )
palm_source, mammal_source
|
Range sources accepted by |
mammal_species_col |
Species-name column in the mammal source. |
mammal_species_keep |
Optional mammal species subset. |
template |
Raster template. |
region |
Optional crop region. |
output_dir |
Optional directory in which to save the range stacks. |
touches |
Count all touched cells. |
Prints a compact summary of an assemblage, distribution collection,
ecological network, inference model, or network collection. The object is
returned invisibly. These methods are normally called through print().
Use this backend when an external model returns probabilities for named species pairs directly, including ensemble-averaged predictions for which no single coherent block partition exists.
probability_matrix_model( probability_matrix, row_group, column_group, name = "species probability matrix" )probability_matrix_model( probability_matrix, row_group, column_group, name = "species probability matrix" )
probability_matrix |
Numeric species-by-species probability matrix. Row and column names are required and must be unique. |
row_group |
Assemblage group corresponding to matrix rows. |
column_group |
Assemblage group corresponding to matrix columns. |
name |
Descriptive model name stored in outputs. |
Simulates a standardized count matrix and evaluates a user-supplied metric function.
rarefied_network_metric( prob_mat, target_size = 100L, metric, n_per_level = 10L, seed = NULL )rarefied_network_metric( prob_mat, target_size = 100L, metric, n_per_level = 10L, seed = NULL )
prob_mat |
Probability matrix. |
target_size |
Standardized number of interactions. |
metric |
A function accepting a count matrix. |
n_per_level |
Number of replicates. |
seed |
Optional seed. |
Rasterize one range file
rasterize_range_file(shp_file, template, touches = TRUE)rasterize_range_file(shp_file, template, touches = TRUE)
shp_file |
Shapefile path. |
template |
Raster template. |
touches |
Count all touched cells. |
Rasterize rows grouped by species
rasterize_range_rows( x, template, species_col, species_keep = NULL, touches = TRUE )rasterize_range_rows( x, template, species_col, species_keep = NULL, touches = TRUE )
x |
An sf or SpatVector object. |
template |
Raster template. |
species_col |
Species-name column. |
species_keep |
Optional species subset. |
touches |
Count all touched cells. |
Reconstruct spatial networks from arbitrary distribution groups
reconstruct_networks(distributions, model, min_species = 1L)reconstruct_networks(distributions, model, min_species = 1L)
distributions |
Named list of aligned distribution rasters or collections. |
model |
Inference model whose group names match |
min_species |
Minimum number of locally present species required in every group. |
Complete compatibility workflow
run_network_downscaling(...)run_network_downscaling(...)
... |
Arguments passed to |
Run generic spatial ecological-network inference
run_spatial_inference(distributions, model, min_species = 1L, threshold = NULL)run_spatial_inference(distributions, model, min_species = 1L, threshold = NULL)
distributions |
Named distribution groups. |
model |
Inference model. |
min_species |
Minimum local richness in each group. |
threshold |
Optional probability threshold for network summaries. |
Compatibility wrapper around block_model().
sbm_model( palm_lookup, mammal_lookup, theta, species_col = "species", guild_col = "guild" )sbm_model( palm_lookup, mammal_lookup, theta, species_col = "species", guild_col = "guild" )
palm_lookup, mammal_lookup
|
Species-to-guild lookup tables. |
theta |
Guild-by-guild probability matrix. |
species_col |
Species column in lookup tables. |
guild_col |
Guild column in lookup tables. |
Simulate an interaction-count matrix
simulate_interaction_counts(network, size = 100L, seed = NULL)simulate_interaction_counts(network, size = 100L, seed = NULL)
network |
Ecological network or probability matrix. |
size |
Total number of interactions. |
seed |
Optional random seed. |
Simulate a network
simulate_network(network, size = 100L, seed = NULL)simulate_network(network, size = 100L, seed = NULL)
network |
Probability network. |
size |
Total interaction count. |
seed |
Optional seed. |
Summarize one ecological network
summarize_network(network, threshold = NULL)summarize_network(network, threshold = NULL)
network |
Ecological network or numeric matrix. |
threshold |
Optional threshold for realized links. |
Summarize a collection of networks
summarize_networks(x, threshold = NULL)summarize_networks(x, threshold = NULL)
x |
Network collection. |
threshold |
Optional probability threshold. |