R/asc_baseline.R

Defines functions asc_baseline

Documented in asc_baseline

#' Functional Metrics of Each Community
#'
#' @description
#' Calculates, for each community separately and without contrasts, the
#' abundance-weighted centroid on each axis of the functional space (CWM),
#' functional dispersion (FDis) and convex hull volume (FRic). Useful to
#' describe the communities before comparing them.
#'
#' @param traits A data frame or matrix of functional traits, one row per
#'   species (row names = species). Categorical traits should be factors.
#' @param abund A data frame or matrix of abundances, one row per community
#'   and one column per species (matched to \code{traits} by name).
#' @param dist_method Distance metric for the trait matrix. Default is "gower".
#' @param dim_retention Character. How many PCoA axes to keep: \code{"msd"}
#'   (default since 0.5.1: the number of axes, 2 to 10, whose Euclidean
#'   distances best reproduce the trait distances, i.e. the minimum mean
#'   squared deviation of Maire et al. 2015), \code{"variance"} (axes needed
#'   to reach \code{var_tol} of the positive variance; the default up to
#'   0.5.0, use it to reproduce earlier results) or \code{"broken_stick"}
#'   (axes explaining more than the broken-stick expectation).
#'   \code{\link{assess_functional_space}} shows the fidelity for each number
#'   of axes.
#' @param var_tol Numeric. Proportion of cumulative variance to retain when
#'   \code{dim_retention = "variance"}. Default is 0.80.
#' @param na.rm Logical. Remove missing values? Default is TRUE.
#' @param correction Character. \code{"none"} (default) builds the PCoA on the
#'   Gower dissimilarity, as in 0.1.1. \code{"sqrt"} uses its square root, which
#'   Gower (1971) showed to be Euclidean for complete data with constant
#'   weights (no negative eigenvalues). It changes the distances
#'   between species (and the shape of the space), so results with and without
#'   it are not comparable; its effect on the tests has not been evaluated by
#'   simulation. An option when the space quality is low, not a default
#'   recommendation.
#' @param type,weights Optional; passed unchanged to \code{\link[cluster]{daisy}}
#'   (e.g. to declare asymmetric binary traits or to down-weight traits coded
#'   in several columns). Default: not used.
#'
#' @return An S3 object of class \code{ascent_baseline} (it also carries the
#'   class \code{ascfcd_base} of ascent 0.5.0, which will be dropped in 0.6.0), a
#'   list whose main element is \code{entities_results} (one row per community:
#'   \code{Entity}, the CWM on each axis, \code{FDis} and \code{FRic}). Here
#'   "Entity" means community (row of \code{abund}), not a functional entity
#'   of \code{\link{asc_entities}}.
#'
#' @examples
#' # Absolute metrics of each community (no contrasts): community-weighted
#' # position on each axis (CWM), dispersion (FDis) and volume (FRic)
#' base <- asc_baseline(birds_sim$traits, birds_sim$landscape)
#' summary(base)
#'
#' # Compare FDis and FRic along the gradient from primary forest to farmland
#' base$entities_results[, c("Entity", "FDis", "FRic")]
#'
#' @export
asc_baseline <- function(traits, abund, dist_method = "gower",
                         dim_retention = c("msd", "variance", "broken_stick"), var_tol = 0.80, na.rm = TRUE,
                         correction = c("none", "sqrt"), type = NULL, weights = NULL) {

  dim_retention <- match.arg(dim_retention)
  correction <- match.arg(correction)

  abund <- .align_species(traits, abund)
  abund_rel <- as.matrix(sweep(abund, 1, rowSums(abund, na.rm = na.rm), "/"))

  fs <- .build_functional_space(traits, dist_method, dim_retention, var_tol,
                                correction, type, weights)
  .check_fric_richness(abund, fs$k_retained)
  F_mat <- fs$F_mat
  k_valid <- fs$k_retained

  cwm_global <- abund_rel %*% F_mat
  entities <- rownames(abund_rel)

  res_df <- data.frame(Entity = entities)
  for(j in 1:k_valid) {
    res_df[[paste0("CWM_Axis_", j)]] <- cwm_global[, j]
  }

  fdis_vals <- numeric(length(entities))
  fric_vals <- numeric(length(entities))

  for(i in seq_along(entities)) {
    topo <- .calc_core_topology(abund_rel[i, ], F_mat)
    fdis_vals[i] <- topo$FDis
    fric_vals[i] <- topo$FRic
  }

  res_df$FDis <- fdis_vals
  res_df$FRic <- fric_vals

  output <- list(
    entities_results = res_df,
    cwm_global = cwm_global,
    F_space = F_mat,
    k_retained = k_valid,
    dim_retention = dim_retention,
    var_retained = fs$var_retained,
    axis_var = fs$axis_var,
    quality = fs$quality,
    original_traits = traits,
    original_abund = abund_rel,
    correction = correction
  )
  class(output) <- c("ascent_baseline", "ascfcd_base")
  return(output)
}

Try the ascent package in your browser

Any scripts or data that you put into this service are public.

ascent documentation built on Nov. 30, 2026, 1:07 a.m.