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
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).
library("gdm")
# create random species composition
set.seed(42)
<- 20
nspecies <- 15
nsites <- 0.3 # probability for each species to be present at each site
presence_prob
<- function(nspecies, nsites, presence_prob)
get_species_list
{# generate a siteXspecies list with a random set of species presences/absences
# fill list with random species presences
<- matrix(nrow = nspecies, ncol = nsites, data = 0)
m
for (row in 1:ncol(m)) {
for (col in 1:nrow(m)) {
if (runif(1) < presence_prob) {
<- 1
m[col, row]
}
}
}
mode(m) <- "integer"
return(m)
}
# random species composition
<- get_species_list(nspecies = nspecies, nsites = nsites,
species_list 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
<- data.frame(site_id = 1:nsites, x_coords = rep(13, nsites),
bioData y_coords = rep(10, nsites))
<- cbind(bioData, t(species_list))
bioData
<- data.frame(site_id = 1:nsites, preds = runif(nsites))
predData
<- gdm::formatsitepair(bioData = bioData, bioFormat = 1, abundance = FALSE,
sitepairs siteColumn = "site_id",
XColumn = "x_coords", YColumn = "y_coords",
predData = predData)
<- gdm::gdm(sitepairs, geo = TRUE) gdm_result
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
<- colSums(species_list) # alpha-diversity of random species community
alpha_list
<- spectre::generate_commonness_matrix_from_gdm(gdm_predictions = gdm_result$observed,
objective_matrix alpha_list = alpha_list)
# Solve composition
<- spectre::run_optimization_min_conf(alpha_list = alpha_list,
res 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)
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.
<- spectre::calc_commonness_error(x = res, objective_matrix = objective_matrix) error_c
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
::plot_error(x = res) spectre
# Plot commonness error between objective matrix and solution matrix
::plot_commonness(x = res, target = objective_matrix) spectre
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