An alternative to ape::rtree. This function was written in C++ and is significantly faster than rtree.

sim_tree(n, edge.length = stats::runif)

Arguments

n

Integer scalar. Number of leaf nodes.

edge.length

A Function. Used to set the length of the edges.

Value

An object of class ape::phylo with the edgelist as a postorderd, node.label and edge.length.

Details

The algorithm was implemented as follows

  1. Initialize N = {1, ..., n}, E to be empty, k = 2*n - 1

  2. While length(N) != 1 do:

    1. Randomly choose a pair (i, j) from N

    2. Add the edges E = E U {(k, i), (k, j)},

    3. Redefine N = (N \ {i, j}) U {k}

    4. Set k = k - 1

    5. next

  3. Use edge.length(2*n - 1) (simulating branch lengths).

Examples

# A very simple example ----------------------------------------------------
set.seed(1223)
newtree <- sim_tree(50)

plot(newtree)



# A performance benchmark with ape::rtree ----------------------------------
if (FALSE) {
library(ape)
microbenchmark::microbenchmark(
  ape = rtree(1e3),
  phy = sim_tree(1e3),
 unit = "relative"
)
# This is what you would get.
# Unit: relative
#   expr     min       lq     mean  median       uq      max neval
#    ape 14.7598 14.30809 14.30013 16.7217 14.32843 4.754106   100
#    phy  1.0000  1.00000  1.00000  1.0000  1.00000 1.000000   100
}