/in.progress/auteurExtended/man/rjmcmc.ou.Rd

http://github.com/eastman/auteur · Unknown · 94 lines · 85 code · 9 blank · 0 comment · 0 complexity · 7afd32481545d486fbceee5c9d50ea01 MD5 · raw file

  1. \name{rjmcmc.ou}
  2. \alias{rjmcmc.ou}
  3. \title{Bayesian sampling of shifts in trait optima: Ornstein-Uhlenbeck process}
  4. \description{Implements reversible-jump Markov chain Monte Carlo sampling to identify shifts in the process of continuous trait evolution across phylogenetic trees}
  5. \usage{rjmcmc.ou(phy, dat, SE = 0, ngen = 1000, sample.freq = 100, prob.mergesplit = 0.1, prob.theta = 0.4, prob.alpha = 0.2, upper.alpha = 100, lambdaK = log(2), constrainK = FALSE, prop.width = NULL, simplestart = FALSE, internal.only = FALSE, summary = TRUE, fileBase = "result")}
  6. %- maybe also 'usage' for other objects documented here.
  7. \arguments{
  8. \item{phy}{a phylogenetic tree of class 'phylo'}
  9. \item{dat}{a named vector of continuous trait values, associated with each species in \code{phy}; see \code{\link[geiger]{name.check}}}
  10. \item{SE}{a named vector of standard errors for each trait value; applied to all trait values if given a single value}
  11. \item{ngen}{number of sampling generations}
  12. \item{sample.freq}{frequency with which Markov samples are retained (e.g., \code{sample.freq=10} retains every tenth sample in the chain)}
  13. \item{prob.mergesplit}{proportion of proposals that split or merge optima, thereby altering complexity of the evolutionary model}
  14. \item{prob.theta}{proportion of proposals in which selective regimes for branches are updated}
  15. \item{prob.alpha}{proportion of proposals in which the evolutionary constraint parameter is updated}
  16. \item{upper.alpha}{an upper bound on the evolutionary constraint parameter}
  17. \item{lambdaK}{the shape parameter for the Poisson prior-distribution on number of distinct optima}
  18. \item{constrainK}{a constraint of model complexity (e.g., \code{constrainK=2} restricts sampling to models with two independent optima)}
  19. \item{prop.width}{if \code{NULL}, the proposal width is calibrated; otherwise, proposal width is constrained to the supplied value}
  20. \item{simplestart}{if \code{FALSE}, a random model complexity is chosen; if \code{TRUE}, Markov sampling begins under a global-optimum model}
  21. \item{internal.only}{dictates whether any branch in the tree can be chosen for a regime shift or tips only (\code{internal.only=FALSE})}
  22. \item{summary}{a logical switch that determines whether any output is printed to the console after the analysis has finished}
  23. \item{fileBase}{a string used to uniquely identify the base directory to which Markov samples are written}
  24. }
  25. \details{
  26. In the two primary objects that store posterior estimates of \code{shifts} and \code{optima} (see \bold{Value}), branches are labeled in accordance with the numeric identifiers found in \code{phy$edge}, which represents a table of ancestor-descendant
  27. relationship (see \code{\link[ape]{read.tree}}. In the \bold{Examples} below do \bold{not} expect convergence with such short chains!
  28. }
  29. \value{
  30. If \code{summary=TRUE} the global rate of acceptance and calibrated proposal width is returned as a list. After a run has completed, the rates of acceptance for various proposal mechanisms are printed, along with
  31. the (calibrated) proposal width used for chain sampling.
  32. Posterior results are written to several files within a base directory, the contents of which are as follows:
  33. \item{logfile}{a logfile including the following for each Markov sample; the generations at which samples were retained; the generalized model of trait evolution used for inference (e.g., Brownian motion or 'BM");
  34. the mean treewide optimum trait value in the sampled generation; the number of independent optima; and the likelihood of the model at each sample. Do not be alarmed if it seems to take some time before values
  35. begin to appear in the \code{logfile}: samples are not saved until after the calibration period has terminated }
  36. \item{errors}{a logfile to which errors are recorded, if any are generated in the Markov chain}
  37. \item{posteriorsamples}{a compressed .rda file, storing estimates from the Markov chain for relevant parameters; contents of this file can be loaded
  38. with \code{load("posteriorsamples.rda")}. This list object, whose referent is also \code{posteriorsamples} when in R-memory, includes two dataframes:
  39. \code{optima} comprises the inferred selective regime (trait optimum) for each branch in the phylogeny; \code{shifts} stores the branches that were chosen for
  40. shifts in selective regime for each retained sample of the Markov chain (\code{1} signifies a shift;
  41. \code{0} implies a branch that was not sampled for a regime shift). Column names of both components of \code{posteriorsamples} are from the numeric vector of
  42. branch labels (see \bold{Details}). For further information on .Rd and .rda files, see \code{\link[base]{save}} and \code{\link[base]{load}} as well as the
  43. \bold{Examples} below. }
  44. }
  45. \author{AA King and JM Eastman}
  46. \note{This function is largely based on code from \code{\link[ouch]{ouch-package}}}
  47. \examples{
  48. #############
  49. ## generate tree
  50. n=24
  51. while(1) {
  52. phy=prunelastsplit(birthdeath.tree(b=1,d=0,taxa.stop=n+1))
  53. phy$tip.label=paste("sp",1:n,sep="")
  54. rphy=reorder(phy,"pruningwise")
  55. # find an internal edge
  56. anc=get.desc.of.node(Ntip(phy)+1,phy)
  57. branches=phy$edge[,2]
  58. branches=branches[branches>Ntip(phy) & branches!=anc]
  59. branch=branches[sample(1:length(branches),1)]
  60. desc=get.descendants.of.node(branch,phy)
  61. if(length(desc)>=4) break()
  62. }
  63. rphy=phy
  64. ## build array of optima
  65. t=numeric(nrow(phy$edge))
  66. t[match(c(branch,desc),phy$edge[,2])]=8
  67. cols=c("red","gray")
  68. dev.new()
  69. plot(phy,edge.col=ifelse(t==8,cols[1],cols[2]), edge.width=2)
  70. mtext("expected pattern of optima")
  71. #############
  72. ## simulate data on the 'optimum-shifted' tree: shift from optimum 0.0 to optimum 2.0
  73. dat=rTraitCont(phy=rphy, model="OU", sigma=sqrt(0.01), alpha=0.1, theta=t)
  74. ## run two short reversible-jump Markov chains
  75. r=paste(sample(letters,9,replace=TRUE),collapse="")
  76. lapply(1:2, function(x) rjmcmc.ou(phy=phy, dat=dat, ngen=10000, sample.freq=10, prob.mergesplit=0.1, prob.theta=0.4, prob.alpha=0.2, simplestart=FALSE, prop.width=NULL, fileBase=paste(r,x,sep=".")))
  77. # collect directories
  78. dirs=dir("./",pattern=paste("OU",r,sep="."))
  79. pool.rjmcmcsamples(base.dirs=dirs, lab=r)
  80. ## plot Markov sampled optima
  81. dev.new()
  82. shifts.plot(phy=phy, base=paste(r,"combined.rjmcmc",sep="."), burnin=0.5, legend=TRUE, pdf=FALSE, edge.width=2)
  83. ## PASTE UNCOMMENTED FOLLOWING LINE TO DROP DIRECTORIES CREATED BY RJMCMC
  84. # unlink(dir(pattern=paste(r)),recursive=TRUE)
  85. }