compute_hypervolume(traits_pca, method)

Estimate functional hypervolume using the hypervolume package (Blonder 2018). Returns volume and overlap between groups.

functional diversityhypervolume
Args:traits_pca — species × PCA axesmethod='gaussian'bandwidth='silverman'
compute_hypervolume <- function(traits_pca, method = "gaussian",
                              bandwidth = "silverman") {
  if (!requireNamespace("hypervolume", quietly = TRUE))
    install.packages("hypervolume")
  library(hypervolume)
  
  hv <- hypervolume(traits_pca,
                    method    = method,
                    bandwidth = bandwidth,
                    verbose   = FALSE)
  list(volume     = hv@Volume,
       hypervolume = hv)
}

# Compare two groups
compare_hypervolumes <- function(hv1, hv2) {
  set_obj  <- hypervolume_set(hv1, hv2, check.memory = FALSE)
  list(
    overlap  = hypervolume_overlap_statistics(set_obj),
    vol_1    = hv1@Volume,
    vol_2    = hv2@Volume
  )
}