To simulate, we first need our instantaneous transition matrix, Q. Let's make Q symmetric, but with uneven rates among states. For fun, let's also call our three states "blue", "green", and "red".
> source("sim.history.R")
> Q<-matrix(c(-1,0.8,0.2,0.8,-1.2,0.4,0.2,0.4,-0.6),3,3, dimnames=list(c("red","green","blue"),c("red","green","blue")))
> Q
red green blue
red -1.0 0.8 0.2
green 0.8 -1.2 0.4
blue 0.2 0.4 -0.6
Now let's get a stochastic birth-death tree:
> require(ape)
> tree<-rbdtree(birth=1.0,death=0.0,Tmax=log(25/2))
Finally, let's simulate on the tree & plot:
> mtree<-sim.history(tree,Q)
> cols<-rownames(Q); names(cols)<-rownames(Q)
> plotSimmap(mtree,cols)
And we get the following (obviously individual results will vary):

Both the node & tip states are also stored in the modified "phylo" object: here as $node.states and $states. I include this mainly to facilitate some analyses that I have been working on.
This function should be added to the next version of "phytools".
Hi, Liam,
ReplyDeleteThanks so much for your post!
I have been trying to use an ultrametric tree resulting from Phylobayes (I made sure all tips have the same distance from the root) as input for make.simmap and sim.history. However, for some reason I get an error caused by a wrong Q-matrix. force.ultrametric() does not work on my tree, only chronos(), however this last function changes the branches length dramatically, and I am interested in the timing of the stochastic mapping results.
Any advice?
Thanks so much in advance!
Carolina.
Just to follow up, this is the error I keep getting:
ReplyDeleteError in if (dt[2] < tree$edge.length[j]) new.state <- rstate(Q[, state][-match(state, :
missing value where TRUE/FALSE needed
In addition: Warning message:
In rexp(n = 1, rate = -Q[state, state]) : NAs produced