]> git.donarmstrong.com Git - ape.git/blob - man/rTraitDisc.Rd
ed1919d710de03821f7989b852c0f1bca6e72408
[ape.git] / man / rTraitDisc.Rd
1 \name{rTraitDisc}
2 \alias{rTraitDisc}
3 \title{Discrete Character Simulation}
4 \usage{
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)
8 }
9 \arguments{
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 =
16       "SYM"}.}
17   \item{states}{the labels used for the states; by default ``A'', ``B'',
18     \dots}
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
23     are returned).}
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
26       = sample(k)}.}
27 }
28 \description{
29   This function simulates the evolution of a discrete character along a
30   phylogeny. If \code{model} is a character or a matrix, evolution is
31   simulated with a Markovian model; the transition probabilities are
32   calculated for each branch with \eqn{P = e^{Qt}} where \eqn{Q} is the
33   rate matrix given by \code{model} and \eqn{t} is the branch length.
34   The calculation is done recursively from the root. See Paradis (2006,
35   p. 101) for a general introduction applied to evolution.
36 }
37 \details{
38   There are three possibilities to specify \code{model}:
39
40 \itemize{
41   \item{A matrix:}{it must be a numeric square matrix; the diagonal is
42     always ignored. The arguments \code{k} and \code{rate} are ignored.}
43
44   \item{A character:}{these are the same short-cuts than in the function
45   \code{\link{ace}}: \code{"ER"} is an equal-rates model, \code{"ARD"}
46   is an all-rates-different model, and \code{"SYM"} is a symmetrical
47   model. Note that the argument \code{rate} must be of the appropriate
48   length, i.e., 1, \eqn{k(k - 1)}, or \eqn{k(k - 1)/2} for the three models,
49   respectively. The rate matrix \eqn{Q} is then filled column-wise.}
50
51   \item{A function:}{it must be of the form \code{foo(x, l)} where
52     \code{x} is the trait of the ancestor and \code{l} is the branch
53     length. It must return the value of the descendant as an integer.}
54 }}
55 \value{
56   A factor with names taken from the tip labels of \code{phy}. If
57   \code{ancestor = TRUE}, the node labels are used if present,
58   otherwise, ``Node1'', ``Node2'', etc.
59 }
60 \references{
61   Paradis, E. (2006) \emph{Analyses of Phylogenetics and Evolution with
62     R.} New York: Springer.
63 }
64 \author{Emmanuel Paradis}
65 \seealso{
66   \code{\link{rTraitCont}}, \code{\link{ace}}
67 }
68 \examples{
69 data(bird.orders)
70 ### the two followings are the same:
71 rTraitDisc(bird.orders)
72 rTraitDisc(bird.orders, model = matrix(c(0, 0.1, 0.1, 0), 2))
73 ### two-state model with irreversibility:
74 rTraitDisc(bird.orders, model = matrix(c(0, 0, 0.1, 0), 2))
75 ### an imaginary model with stasis 0.5 time unit after a node, then
76 ### random evolution:
77 foo <- function(x, l) {
78     if (l < 0.5) return(x)
79     sample(2, size = 1)
80 }
81 tr <- rcoal(20, br = runif)
82 x <- rTraitDisc(tr, foo, ancestor = TRUE)
83 plot(tr, show.tip.label = FALSE)
84 co <- c("blue", "yellow")
85 cot <- c("white", "black")
86 Y <- x[1:20]
87 A <- x[-(1:20)]
88 nodelabels(A, bg = co[A], col = cot[A])
89 tiplabels(Y, bg = co[Y], col = cot[Y])
90 }
91 \keyword{datagen}