dstGraph: Distance-Based Directed Graph

View source: R/dstGraph.R

dstGraphR Documentation

Distance-Based Directed Graph

Description

Calculates a distance-based directed graph from a dissimilarity matrix, a threshold value, and an origin (or root) vertex.

Usage

dstGraph(d, th, origin, stretch)

Arguments

d

A dissimilarity matrix such as the one obtained from dist or dist.dna.

th

Numeric. A threshold value for dissimilarity. Vertices are considered as connected whenever their pairwise dissimilarity value is smaller or equal to that value.

origin

Integer. Index of the origin vertex from which the edges are directed.

stretch

Numeric (optional). When a vertex is unreachable, stretch the threshold value for the shortest edge connecting it to the rest of the graph up to that value.

Details

The algorithm

Beginning on a user-defined origin vertex, the algorithm proceeds by connecting all vertices within a given dissimilarity value from the ones that have already been connected, until all the vertices that can be reached has been reached. Optionally, the dissimilarity threshold value can be stretched for the vertices that are unreachable. Vertices that cannot be reached in any way are reported by the function.

Value

A graph-class object.

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>

References

Guénard, G., Legendre, P., and Peres-Neto, P. 2013. Phylogenetic eigenvector maps: a framework to model and predict species traits. Methods in Ecology and Evolution 4: 1120-1131

Makarenkov, V., Legendre, L. & Desdevise, Y. 2004. Modelling phylogenetic relationships using reticulated networks. Zoologica Scripta 33: 89-96

Examples


## We set the seed to obtain a consistent example, but you are welcome to
## experiment with other graphs.
set.seed(7653401)

## Here, the dissimilarity matrix is generated from the Euclidean distance of
## a two-dimensional plot for the sake of simplicity. In practice, the matrix
## will come from DNA data using a dissimilarity method such as those
## implemented by  ape packages's function dist.dna().

N <- 100
coords <- cbind(x=runif(N,-1,1), y=runif(N,-1,1))
rownames(coords) <- sprintf("N%d",1:N)
dst <- dist(coords)

## Calculate the distance-based graph:
gr <- dstGraph(d=dst, th=0.25, origin=15)

## This graph have unconnected vertices.

## Plotting the graph with colors indicating the order of the edges:
plot(coords, type="n", asp=1)
col <- head(rainbow(max(gr$order) + 1), max(gr$order))
arrows(
  x0 = coords[edge(gr)[[1]],1],
  x1 = coords[edge(gr)[[2]],1],
  y0 = coords[edge(gr)[[1]],2],
  y1 = coords[edge(gr)[[2]],2],
  length = 0.05,
  col = col[gr$order[edge(gr)[[2]]]]
)
points(coords, pch=21, bg="black", cex=0.25)

## Try again raising the threshold to help in connecting all the vertices:
gr <- dstGraph(d=dst, th=0.28, origin=15)

## It helped, but does not entirely solve the matter.

plot(coords, type="n", asp=1)
col <- head(rainbow(max(gr$order) + 1), max(gr$order))
arrows(
  x0 = coords[edge(gr)[[1]],1],
  x1 = coords[edge(gr)[[2]],1],
  y0 = coords[edge(gr)[[1]],2],
  y1 = coords[edge(gr)[[2]],2],
  length = 0.05,
  col = col[gr$order[edge(gr)[[2]]]]
)
points(coords, pch=21, bg="black", cex=0.25)

## Try again while stretching the threshold for the unconnected vertices:
gr <- dstGraph(d=dst, th=0.28, origin=15, stretch=0.5)

## All the vertices are now connected.

plot(coords, type="n", asp=1)
col <- head(rainbow(max(gr$order) + 1), max(gr$order))
arrows(
  x0 = coords[edge(gr)[[1]],1],
  x1 = coords[edge(gr)[[2]],1],
  y0 = coords[edge(gr)[[1]],2],
  y1 = coords[edge(gr)[[2]],2],
  length = 0.05,
  col = col[gr$order[edge(gr)[[2]]]]
)
points(coords, pch=21, bg="black", cex=0.25)

## This is the influence matrix of that directed graph:
tmp <- InflMat(gr)

## An image plot of this influence matrix:
image(t(tmp[nrow(tmp):1L,]), col=gray(c(1,0)), asp=1)



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