Minimal example

Getting started

Installation

Install the release version from CRAN:

# install.packages("spectre")

To install the developmental version of spectre, use:

# install.packages("devtools")
# devtools::install_github("r-spatialecology/spectre") #Uncomment when repo is public

Example

Before we illustrate how the objective matrix is derived from modelled \(\alpha\)-diversity and Bray-Curtis dissimilarity, we do a detour first. If you already have modelled \(\alpha\)- and Bray-Curtis dissimilarity estimates at hand, please skip ahead. In the detour, we just use a random species composition to i) get an \(\alpha\)- diversity estimate and to ii) generate a Bray-Curtis dissimilarity estimate in the output format of the gdm package (Fitzpatrick et al., 2020).

Get \(\alpha\)-diversity and Bray-Curtis dissimilarity from a random species composition (detour)

library("gdm")

# create random species composition
set.seed(42) 

nspecies <- 20
nsites <- 15
presence_prob <- 0.3 # probability for each species to be present at each site

get_species_list <- function(nspecies, nsites, presence_prob)
{
  # generate a siteXspecies list with a random set of species presences/absences
  # fill list with random species presences
  m <- matrix(nrow = nspecies, ncol = nsites, data = 0)
  
  for (row in 1:ncol(m)) {
    for (col in 1:nrow(m)) {
      if (runif(1) < presence_prob) {
        m[col, row] <- 1
      }
    }
  }
  
  mode(m) <- "integer"
  
  return(m)
}

# random species composition
species_list <- get_species_list(nspecies = nspecies, nsites = nsites, 
                                 presence_prob = presence_prob) 

# calculation of Bray-Curtis dissimilarity with the gdm package: 
# bioData is required by the gdm package, but does not affect 
# the observed Bray-Curtis dissimilarity we will use later
bioData <- data.frame(site_id = 1:nsites, x_coords = rep(13, nsites), 
                      y_coords = rep(10, nsites)) 

bioData <- cbind(bioData, t(species_list)) 

predData <- data.frame(site_id = 1:nsites, preds = runif(nsites))

sitepairs <- gdm::formatsitepair(bioData = bioData, bioFormat = 1, abundance = FALSE, 
                                 siteColumn = "site_id",
                                 XColumn = "x_coords", YColumn = "y_coords", 
                                 predData = predData)

gdm_result <- gdm::gdm(sitepairs, geo = TRUE) 

Solve species composition using spectre

Running the optimization is straightforward in spectre and only includes one function call. However, first we need to generate to commonness matrix from the gdm predictions to generate the object matrix. Then, we simply run the optimization using the alpha list, the \(gamma\)-diversity, the objective matrix (or target), and finally the number of maximum iterations.

library("spectre")

# Calculate objective_matrix from (modelled) alpha-diversity and Bray-Curtis dissimilarity
alpha_list <- colSums(species_list) # alpha-diversity of random species community

objective_matrix <- spectre::generate_commonness_matrix_from_gdm(gdm_predictions = gdm_result$observed, 
                                                                 alpha_list = alpha_list)

# Solve composition 
res <- spectre::run_optimization_min_conf(alpha_list = alpha_list,
                                          total_gamma = nspecies,
                                          target = objective_matrix,
                                          max_iterations = 1000) # n iterations
## 
##  > Optimization finished with lowest absolute error = 5 (highest absolute error was: 101 improved by: 96)

Result evaluation

spectre allows to easily calculate some error measures, namely the mean absolute commonness error (\(MAE_c\)) and the relative commonness error [%] (RCE). The error is calculated between the solved species composition and the objective matrix.

error_c <- spectre::calc_commonness_error(x = res, objective_matrix = objective_matrix)

The objective matrix had a mean commonness of 1.75. The mean absolute error between the objective matrix and the solved solution matrix was 0.05. The solution matrix had an relative commonness error (RCE) of 2.7%.

There are also two functions to visualize the optimization. First you can plot the error of the solution matrix over time. Second, you can plot the commonness error between the solution matrix and the objective matrix.

# With an increasing number of iterations, the solution matrix improved
spectre::plot_error(x = res)

# Plot commonness error between objective matrix and solution matrix
spectre::plot_commonness(x = res, target = objective_matrix)

References

Matthew C. Fitzpatrick, Karel Mokany, Glenn Manion, Matthew Lisk, Simon Ferrier and Diego Nieto-Lugilde (2021). gdm: Generalized Dissimilarity Modeling. R package version 1.4.2.2. https://CRAN.R-project.org/package=gdm