Install the release version from CRAN:

`install.packages("spectre")`

To install the developmental version of `spectre`

, use:

```
install.packages("devtools")
::install_github("r-spatialecology/spectre") devtools
```

`library("spectre")`

For this example, we created a random species composition (15 sites, gamma diversity = 20) and calculated \(\alpha\)-diversity and Bray-Curtis dissimilarity to be used as input for the `spectre`

algorithm (please see `R/generate_minimal_example_data.R`

for details).

```
# load "observed" alpha-, beta- and gamma-diversity values of the random species composition
<- minimal_example_data$alpha_list # richness
alpha_list <- minimal_example_data$beta_list # Bray-Curtis dissimilarity
beta_list <- dim(minimal_example_data$species_list)[1] # 20 species total_gamma
```

`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")
<- spectre::generate_commonness_matrix_from_gdm(gdm_predictions = beta_list,
objective_matrix alpha_list = alpha_list)
# Solve composition
<- spectre::run_optimization_min_conf(alpha_list = alpha_list,
res total_gamma = total_gamma,
target = objective_matrix,
max_iterations = 1000, # n iterations
seed = 123) # use a random seed for reproducibility
#>
#> > Optimization finished with lowest absolute error = 8 (highest absolute error was: 115 improved by: 107)
```

`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.08. The solution matrix had an relative commonness error (RCE) of 4.3 %.

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
```