Learn R Programming

mrangr

The mrangr package is designed to simulate metacommunities within a spatially explicit, mechanistic framework. It extends the functionality of the rangr package by allowing for the simulation of multiple interacting species via an asymmetric interaction matrix.

This tool mimics the essential processes shaping metacommunity dynamics: local population growth, dispersal, and interspecific interactions. Simulations take place in dynamic environments, facilitating projections of community shifts in response to environmental changes.

Installation

You can install mrangr with:

install.packages("mrangr")

Basic Workflow

The mrangr workflow involves initialising a community with spatial data and interaction parameters, running the simulation, and analysing the results.

1. Input Maps and Interactions

You must provide carrying capacity maps (K_map) and initial abundance maps (n1_map) as SpatRaster objects. For a community of $N$ species, the rasters must contain $N$ layers.

# Load example maps
K_map <- rast(system.file("input_maps/K_map_eg.tif", package = "mrangr"))
K_map <- subset(K_map, 1:2)

Interspecific interactions are defined using an interaction matrix ($a$), where values represent the per-capita interaction strength of the species in the column on the species in the row.

# Example for 2 species with symmetric competition
nspec <- 2
a <- matrix(c(NA, -0.8, -0.8, NA), nrow = nspec, ncol = nspec)

2. Community Initialisation

Use initialise_com() to create a sim_com_data object. This stores all parameters, including the intrinsic growth rate ($r$) and the dispersal rate.

first_com <- initialise_com(
  n1_map = round(K_map / 2), 
  K_map = K_map, 
  r = 1.1, 
  a = a, 
  rate = 1 / 500
)

3. Running the Simulation

The sim_com() function executes the simulation over a specified number of time steps.

first_sim <- sim_com(first_com, time = 100)

4. Visualisation

You can visualise the final spatial distributions or the change in mean abundance over time.

# Visualise spatial niches at specific time steps
plot(first_sim, time = c(1, 10, 100))
# Plot abundance time series for all species
plot_series(first_sim)

Virtual Ecologist

The package includes a virtual_ecologist() function to simulate real-world observation processes. This allows users to sample the simulated community at defined points in space and time, incorporating sampling effort and detection probability into the simulation.

Citation

To cite mrangr, please use the citation() function:

library(mrangr)
citation("mrangr")

Copy Link

Version

Install

install.packages('mrangr')

Version

1.0.1

License

MIT + file LICENSE

Maintainer

Katarzyna Markowska

Last Published

January 25th, 2026

Functions in mrangr (1.0.1)

to_rast.sim_com_results

Convert sim_com_results to SpatRaster(s)
sim_com

Simulate Community Dynamics Over Time
update.sim_com_data

Update sim_com_data Object
K_map_eg.tif

Example Of Carrying Capacity Map
community_eg

Example Community Data
get_simulated_com

Load Example Simulated Community Results
a_eg

Example Of Interaction Coefficients Matrix
K_sim

Carrying Capacity Map Simulator
get_community

Load Example Community Object
diagonal

Compute Maximum Possible Distance for a Raster Object
initialise_com

Initialise Community Simulation Data
grf

Generate a Gaussian Random Field
initialise_inv

Initialise Invasion Parameters
print.summary.sim_com_data

Print summary.sim_com_data Object
plot.sim_com_results

Plot sim_com_results Object
print.sim_com_data

Print sim_com_data Object
set_zero

Set Non-Missing Values to Zero
n1_map_eg.tif

Example Of Abundance Map At First Time Step Of The Simulation
plot_series

Community Time-Series Plot
summary.sim_com_data

Summary Of sim_com_data Object
reexports

Objects exported from other packages
mrangr-package

mrangr: Mechanistic Metacommunity Simulator
print.sim_com_results

Print sim_com_results Object
summary.sim_com_results

Summary Of sim_com_results Object
simulated_com_eg

Example Simulated Community Output
print.summary.sim_com_results

Print summary.sim_results Object
virtual_ecologist

Virtual Ecologist