Nothing
#' Functional Transitions: Species Leverage, Partition and Entities
#'
#' @description
#' Describes \emph{who} moved the functional centroid (no p-values; for tests
#' see \code{\link{asc_null}}):
#' \itemize{
#' \item \strong{Species leverage}: contribution of each species to the
#' centroid displacement (Layer 1). Leverages add up exactly to the
#' displacement \eqn{||\Delta C||} for any reference point.
#' \item \strong{Partition}: share of the displacement due to species shared
#' by both states, gained (only in the comparison) and lost (only in the
#' reference).
#' \item \strong{Entities} (optional, exploratory): share of the
#' displacement that occurs between functional entities (their total
#' abundances change) and within entities (which species dominates inside
#' each entity changes). The two parts add up exactly to the displacement.
#' See \code{\link{asc_entities}} for its limits.
#' }
#'
#' @details
#' Leverage of species \eqn{i} is \eqn{\Delta p_i \, (F_i - r) \cdot u}, where
#' \eqn{u} is the unit vector of the displacement and \eqn{r} the reference
#' point. The total does not depend on \eqn{r}, but how it is shared among
#' species (and among shared, gained and lost species) does. The default
#' \code{ref = "midpoint"} (half-way between the two centroids) is symmetric:
#' it gives the same partition whichever state is called the reference, which
#' matters for pairwise comparisons. The partition also reports its minimum
#' and maximum across the three reference points; a wide range means the
#' partition should be interpreted with caution.
#'
#' Leverage keeps only the part of each species' contribution that lies along
#' the displacement. A species can change a lot in a direction perpendicular
#' to the displacement (and be compensated by other species) and still have a
#' small leverage. \code{Orthogonal} reports the size of that perpendicular
#' part, \eqn{|| \Delta p_i (F_i - r) - Leverage_i \, u ||}, so that "small
#' leverage" is not read as "small change": a species with small leverage
#' and large \code{Orthogonal} changed in abundance, but along another
#' direction of the trait space.
#'
#' In version 0.1.1 the reference point was the reference centroid
#' (\code{ref = "reference"}); per-species values differ from 0.1.1 unless
#' that option is used.
#'
#' @param x An object from \code{asc_paired} (class \code{ascent_paired}) or
#' \code{asc_pairwise} (class \code{ascent_pairwise}).
#' @param ref Character. Reference point for the projections:
#' \code{"midpoint"} (default), \code{"reference"} or \code{"comparison"}.
#' @param entities Optional. An object from \code{\link{asc_entities}} or a
#' named vector (names = species) giving each species' entity.
#' @param ... Further arguments passed to or from other methods.
#'
#' @return An S3 object of class \code{ascent_transitions} (it also carries the
#' class \code{ascfcd_transitions} of ascent 0.5.0, which will be dropped in
#' 0.6.0): a list with one
#' element per contrast (or pair), each containing \code{species_leverage}
#' (columns \code{Species}, \code{Group}, \code{Delta_p}, \code{Projection},
#' \code{Leverage}, \code{Orthogonal}; species absent from both states are
#' omitted),
#' \code{partition} (columns \code{Group}, \code{Fraction},
#' \code{Min_across_refs}, \code{Max_across_refs}), \code{entities}
#' (\code{Shift}, \code{Between}, \code{Within}, \code{Within_fraction}, or
#' \code{NULL}; descriptive: see \code{\link{asc_entities}} for its
#' limits), \code{shift} and \code{ref}.
#'
#' @examples
#' res <- asc_paired(birds_sim$traits, birds_sim$counts,
#' sites = birds_sim$samples$site,
#' time = birds_sim$samples$time, ref_time = "Before")
#' tr <- asc_transitions(res)
#'
#' # Species that moved the centroid at "Logged": positive leverage pushes it in
#' # the direction of the change, negative leverage resists it
#' head(tr$Logged$species_leverage)
#'
#' # Leverages add up exactly to the shift
#' c(sum = sum(tr$Logged$species_leverage$Leverage), shift = tr$Logged$shift)
#'
#' # Share of the shift due to species shared, gained or lost
#' tr$Logged$partition
#'
#' @export
asc_transitions <- function(x, ref = c("midpoint", "reference", "comparison"), entities = NULL, ...) {
if (!.is_paired(x) && !.is_pairwise(x)) {
stop("Input must come from asc_paired() or asc_pairwise().")
}
ref <- match.arg(ref)
F_mat <- x$F_space
ent <- .entity_vector(entities, rownames(F_mat))
out <- lapply(names(x$directional_vectors), function(s) {
p_r <- as.numeric(x$p_ref[[s]]); p_c <- as.numeric(x$p_comp[[s]])
main <- .leverage_partition(p_r, p_c, F_mat, ref)
all_refs <- sapply(c("reference", "comparison", "midpoint"), function(rf) {
pl <- .leverage_partition(p_r, p_c, F_mat, rf)
pl$by_group / max(pl$shift, 1e-12)
})
lev <- main$by_species
lev <- lev[lev$Group != "absent", , drop = FALSE]
lev <- lev[order(abs(lev$Leverage), decreasing = TRUE), , drop = FALSE]
rownames(lev) <- NULL
list(species_leverage = lev,
partition = data.frame(Group = rownames(all_refs),
Fraction = main$by_group / max(main$shift, 1e-12),
Min_across_refs = apply(all_refs, 1, min),
Max_across_refs = apply(all_refs, 1, max),
row.names = NULL, stringsAsFactors = FALSE),
entities = if (!is.null(ent)) .entity_decomposition(p_r, p_c, F_mat, ent),
shift = main$shift, ref = ref)
})
names(out) <- names(x$directional_vectors)
class(out) <- c("ascent_transitions", "ascfcd_transitions")
out
}
# ------------------------------------------------------------------------------
# Leverage and partition (internal)
# ------------------------------------------------------------------------------
.leverage_partition <- function(p_r, p_c, F_mat, ref = c("reference", "comparison", "midpoint")) {
ref <- match.arg(ref)
p_r <- p_r / sum(p_r); p_c <- p_c / sum(p_c)
Cr <- as.numeric(p_r %*% F_mat); Cc <- as.numeric(p_c %*% F_mat)
v <- Cc - Cr; nv <- sqrt(sum(v^2))
group <- ifelse(p_r > 0 & p_c > 0, "shared",
ifelse(p_r == 0 & p_c > 0, "gained",
ifelse(p_r > 0 & p_c == 0, "lost", "absent")))
r0 <- switch(ref, reference = Cr, comparison = Cc, midpoint = (Cr + Cc) / 2)
dp <- p_c - p_r
# size of each species' contribution vector dp_i (F_i - r0)
size <- abs(dp) * sqrt(rowSums(sweep(F_mat, 2, r0)^2))
if (nv < 1e-12) {
proj <- rep(0, length(p_r)); lev <- rep(0, length(p_r)); orth <- size
} else {
proj <- as.numeric(sweep(F_mat, 2, r0) %*% (v / nv))
lev <- dp * proj
orth <- sqrt(pmax(0, size^2 - lev^2))
}
sums <- tapply(lev, factor(group, levels = c("shared", "gained", "lost", "absent")), sum)
sums[is.na(sums)] <- 0
list(by_species = data.frame(Species = rownames(F_mat), Group = group, Delta_p = p_c - p_r,
Projection = proj, Leverage = lev, Orthogonal = orth,
stringsAsFactors = FALSE),
by_group = sums[c("shared", "gained", "lost")],
shift = nv, ref = ref)
}
# Exact decomposition of the centroid displacement into a part between
# entities (changes in entity totals) and a part within entities (changes in
# which species dominate inside each entity), projected on the displacement.
.entity_decomposition <- function(p_r, p_c, F_mat, group) {
p_r <- p_r / sum(p_r); p_c <- p_c / sum(p_c)
v <- as.numeric((p_c - p_r) %*% F_mat); nv <- sqrt(sum(v^2))
if (nv < 1e-12) return(c(Shift = 0, Between = 0, Within = 0, Within_fraction = NA))
u <- v / nv
vb <- vw <- numeric(ncol(F_mat))
for (e in sort(unique(group))) {
i <- which(group == e)
Pr <- sum(p_r[i]); Pc <- sum(p_c[i])
mr <- if (Pr > 0) colSums(p_r[i] * F_mat[i, , drop = FALSE]) / Pr else NULL
mc <- if (Pc > 0) colSums(p_c[i] * F_mat[i, , drop = FALSE]) / Pc else NULL
if (is.null(mr)) mr <- mc
if (is.null(mc)) mc <- mr
if (is.null(mr)) next
vb <- vb + (Pc - Pr) * (mr + mc) / 2
vw <- vw + (Pr + Pc) / 2 * (mc - mr)
}
between <- sum(vb * u); within <- sum(vw * u)
c(Shift = nv, Between = between, Within = within, Within_fraction = within / nv)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.