R/EdgeRatio.R

Defines functions EdgeRatio.phylo EdgeRatio

Documented in EdgeRatio EdgeRatio.phylo

#' Ratio of external:internal edge length
#' 
#' Reports the ratio of tree length associated with external edges (i.e.
#' edges whose child is a leaf) and internal edges.
#' Where tree length is dominated by internal edges, variation between tips
#' is dominantly controlled by phylogenetic history.
#' 
#' @param x A tree of class \code{\link[ape:read.tree]{phylo}}.
#' @returns `EdgeRatio()` returns a numeric specifying the ratio of external
#' to internal edge length (> 1 means the length of a tree is predominantly
#' in external edges), with attributes `external`, `internal`, and `total`
#' specifying the total length associated with edges of that nature.
#' @template MRS
#' @family tree properties
#' @export
EdgeRatio <- function(x) UseMethod("EdgeRatio")
  
#' @rdname EdgeRatio
#' @export
EdgeRatio.phylo <- function(x) {
  el <- x[["edge.length"]]
  if (is.null(el)) {
    warning("Edge lengths not specified")
    return(NA_real_)
  }
  ed <- x[["edge"]]
  nTip <- NTip(x)
  external <- ed[, 2] <= nTip
  exLen <- sum(el[external])
  inLen <- sum(el[!external])
  structure(exLen / inLen,
            external = exLen,
            internal = inLen,
            total = sum(exLen, inLen))
}

Try the TreeTools package in your browser

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

TreeTools documentation built on April 23, 2026, 5:06 p.m.