3 \title{Discrete Character Simulation}
5 rTraitDisc(phy, model = "ER", k = if (is.matrix(model)) ncol(model) else 2,
6 rate = 0.1, states = LETTERS[1:k], freq = rep(1/k, k),
7 ancestor = FALSE, root.value = 1, ...)
10 \item{phy}{an object of class \code{"phylo"}.}
11 \item{model}{a character, a square numeric matrix, or a function
12 specifying the model (see details).}
13 \item{k}{the number of states of the character.}
14 \item{rate}{the rate of change used if \code{model} is a character; it
15 is \emph{not} recycled if \code{model = "ARD"} of \code{model =
17 \item{states}{the labels used for the states; by default ``A'', ``B'',
19 \item{freq}{a numeric vector giving the equilibrium relative
20 frequencies of each state; by default the frequencies are equal.}
21 \item{ancestor}{a logical value specifying whether to return the
22 values at the nodes as well (by default, only the values at the tips
24 \item{root.value}{an integer giving the value at the root (by default,
25 it's the first state). To have a random value, use \code{root.value
27 \item{\dots}{further arguments passed to \code{model} if it is a
31 This function simulates the evolution of a discrete character along a
32 phylogeny. If \code{model} is a character or a matrix, evolution is
33 simulated with a Markovian model; the transition probabilities are
34 calculated for each branch with \eqn{P = e^{Qt}} where \eqn{Q} is the
35 rate matrix given by \code{model} and \eqn{t} is the branch length.
36 The calculation is done recursively from the root. See Paradis (2006,
37 p. 101) for a general introduction applied to evolution.
40 There are three possibilities to specify \code{model}:
43 \item{A matrix:}{it must be a numeric square matrix; the diagonal is
44 always ignored. The arguments \code{k} and \code{rate} are ignored.}
46 \item{A character:}{these are the same short-cuts than in the function
47 \code{\link{ace}}: \code{"ER"} is an equal-rates model, \code{"ARD"}
48 is an all-rates-different model, and \code{"SYM"} is a symmetrical
49 model. Note that the argument \code{rate} must be of the appropriate
50 length, i.e., 1, \eqn{k(k - 1)}, or \eqn{k(k - 1)/2} for the three models,
51 respectively. The rate matrix \eqn{Q} is then filled column-wise.}
53 \item{A function:}{it must be of the form \code{foo(x, l)} where
54 \code{x} is the trait of the ancestor and \code{l} is the branch
55 length. It must return the value of the descendant as an integer.}
58 A factor with names taken from the tip labels of \code{phy}. If
59 \code{ancestor = TRUE}, the node labels are used if present,
60 otherwise, ``Node1'', ``Node2'', etc.
63 Paradis, E. (2006) \emph{Analyses of Phylogenetics and Evolution with
64 R.} New York: Springer.
66 \author{Emmanuel Paradis}
68 \code{\link{rTraitCont}}, \code{\link{ace}}
72 ### the two followings are the same:
73 rTraitDisc(bird.orders)
74 rTraitDisc(bird.orders, model = matrix(c(0, 0.1, 0.1, 0), 2))
75 ### two-state model with irreversibility:
76 rTraitDisc(bird.orders, model = matrix(c(0, 0, 0.1, 0), 2))
77 ### an imaginary model with stasis 0.5 time unit after a node, then
79 foo <- function(x, l) {
80 if (l < 0.5) return(x)
83 tr <- rcoal(20, br = runif)
84 x <- rTraitDisc(tr, foo, ancestor = TRUE)
85 plot(tr, show.tip.label = FALSE)
86 co <- c("blue", "yellow")
87 cot <- c("white", "black")
90 nodelabels(A, bg = co[A], col = cot[A])
91 tiplabels(Y, bg = co[Y], col = cot[Y])