Saturday, June 1, 2024

Addendum to fitting an Mk model when some states are not observed

To share yesterday’s blog post, I published the following tweet:

The figure of the tweet (with the embeded Mk model visualization) does not actually appear in my post.

As an addendum for posteriority & for interest, here is how that figure was created.

library(phytools)

tree is a "phylo" object, x is a factor vector, anc_ordered is a marginal ancestral state reconstruction from phytools::ancr, and fit_ordered is a fitting model object from phytools::fitMk.

plotFanTree.wTraits(tree,data.frame(factor(x,levels=0:9)),
  part=0.5,colors=list(setNames(hcl.colors(n=10),0:9)),
  ftype="off",arc_height=2)
par(fg="transparent")
nodelabels(pie=anc_ordered$ace,piecol=hcl.colors(n=10),
  cex=apply(anc_ordered$ace,1,
    function(x) if(max(x)<0.9) 0.4 else 0.2))
par(fg="black")
obj<-plot(fit_ordered,add=TRUE,show.zeros=FALSE,
  xlim=c(-1,1),ylim=c(-1,2.5),text=FALSE)
points(obj$x,obj$y,pch=16,col=hcl.colors(10),cex=2.7)
text(obj$x,obj$y,0:9,col="white")

plot of chunk unnamed-chunk-3

That’s all!

1 comment:

  1. how about a fixed-rates continuous-time Markov model (Mk model)?

    ReplyDelete

Note: due to the very large amount of spam, all comments are now automatically submitted for moderation.