graph-purge: MPSEM Graph Purging Functions

graph-purgeR Documentation

MPSEM Graph Purging Functions

Description

A set of functions for purging possibly uninformative edges from MPSEM graphs.

Usage

purge.terminal(x, ...)

purge.median(x, combine = function(x, ...) 1/sum(1/x), ...)

Arguments

x

A graph-class object.

...

Further argument to be internally passed to the function given as argument combine or function graphDist.

combine

A function for combining the distances of redundant edges, should they occur. The default function calculates the inverse of the sum of the inverse distances.

Details

Function purge.terminal or purge.median will only purge the vertices that are not marked as species. Removal follows certain constraints. Function purge.terminal removes the terminal vertices, irrespective of the number of incoming edges. Function purge.median removes the median vertices (vertices with a single incoming edge and a single outgoing edge) by joining the incoming and outgoing edges into a single edge (having the name of the incoming edge). When joining is done, care is taken that any edge having the same origin vertex and destination vertex be consolidated. The default function for consolidating the distances is the inverse of the sum of the inverse distances. For all the other edge characteristics, values of the incoming edge involved in the last removal is taken.

The latter are vertices that have only a single incoming edge and a single outgoing edge, whereas the former are vertices that have no outgoing edges. These vertices are generally uninformative for phylogenetic modelling and do not carry known trait values; thus making them safe for removal.

Value

The purged graph-class object, possibly with attributes removedVertex (whenever vertices has to be removed) and/or removedEdge (whenever edges had to be removed).

Functions

  • purge.terminal(): Purge Terminal Vertices

    Attempts to purge the terminal vertices of a graph that are not marked as species.

  • purge.median(): Purge Median Vertices

    Attempts to purge the median vertices of a graph that are not marked as species, connecting the two end vertices.

Author(s)

Guillaume Guénard [aut, cre] (<https://orcid.org/0000-0003-0761-3072>), Pierre Legendre [ctb] (<https://orcid.org/0000-0002-3838-3305>) Maintainer: Guillaume Guénard <guillaume.guenard@umontreal.ca>

Examples

## A 16-vertex graph with 24 edges:
data.frame(
  species = as.logical(c(1,0,0,0,1,0,0,0,1,1,1,1,1,0,0,0)),
  x = c(1,3,4,0,1.67,4,1,1.33,2.33,3.33,4.33,4,5,5,5,2.33),
  y = c(1,1,1,0,0.5,0,-1,0,0,-0.5,-1,-0.5,-0.5,1,0.5,-1),
  row.names = sprintf("V%d",1:16)
) %>%
  st_as_sf(
    coords=c("x","y"),
    crs = NA
  ) %>%
  graph %>%
  add.edge(
    from = c(1,2,1,5,4,4,5,9,4,8,9,4,7,7,3,6 ,9 ,10,10,3 ,3 ,7 ,9, 10),
    to =   c(2,3,5,2,1,5,9,2,8,9,6,7,8,9,6,13,10,12,11,14,15,16,16,16),
    data = data.frame(
      distance = c(4.2,4.7,3.9,3.0,3.6,2.7,4.4,3.4,3.6,3.3,4.8,3.2,3.5,
                   4.4,2.5,3.4,4.3,3.1,2.2,2.1,0.9,1.0,2.1,0.9),
      row.names = sprintf("E%d",1:24)
    )
  ) -> gr1

## Plotting the exemplary graph:
plot(gr1)

## Purging the terminal vertices:
tmp <- purge.terminal(gr1)
plot(tmp)
attr(tmp,"removedVertex")
attr(tmp,"removedEdge")

## Purging the median vertices:
tmp2 <- purge.median(tmp)
plot(tmp2)
attr(tmp2,"removedVertex")
attr(tmp2,"removedEdge")


guenardg/MPSEM documentation built on April 14, 2025, 3:53 p.m.