PageRenderTime 26ms CodeModel.GetById 20ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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{
26In 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{
30If \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 
31the (calibrated) proposal width used for chain sampling. 
32
33Posterior results are written to several files within a base directory, the contents of which are as follows:
34  \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"); 
35  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 
36  begin to appear in the \code{logfile}: samples are not saved until after the calibration period has terminated }
37  \item{errors}{a logfile to which errors are recorded, if any are generated in the Markov chain}
38  \item{posteriorsamples}{a compressed .rda file, storing estimates from the Markov chain for relevant parameters; contents of this file can be loaded 
39  with \code{load("posteriorsamples.rda")}. This list object, whose referent is also \code{posteriorsamples} when in R-memory, includes two dataframes: 
40  \code{optima} comprises the inferred selective regime (trait optimum) for each branch in the phylogeny; \code{shifts} stores the branches that were chosen for 
41 shifts in selective regime for each retained sample of the Markov chain (\code{1} signifies a shift; 
42  \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 
43  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 
44  \bold{Examples} below. }
45}
46\author{AA King and JM Eastman}
47\note{This function is largely based on code from \code{\link[ouch]{ouch-package}}}
48
49\examples{
50#############
51## generate tree
52n=24
53while(1) {
54	phy=prunelastsplit(birthdeath.tree(b=1,d=0,taxa.stop=n+1))
55	phy$tip.label=paste("sp",1:n,sep="")
56	rphy=reorder(phy,"pruningwise")
57
58	# find an internal edge
59	anc=get.desc.of.node(Ntip(phy)+1,phy)
60	branches=phy$edge[,2]
61	branches=branches[branches>Ntip(phy) & branches!=anc]
62	branch=branches[sample(1:length(branches),1)]
63	desc=get.descendants.of.node(branch,phy)
64	if(length(desc)>=4) break()
65}
66rphy=phy
67
68## build array of optima
69t=numeric(nrow(phy$edge))
70t[match(c(branch,desc),phy$edge[,2])]=8
71cols=c("red","gray")
72dev.new()
73plot(phy,edge.col=ifelse(t==8,cols[1],cols[2]), edge.width=2) 
74mtext("expected pattern of optima")
75
76#############
77## simulate data on the 'optimum-shifted' tree: shift from optimum 0.0 to optimum 2.0
78dat=rTraitCont(phy=rphy, model="OU", sigma=sqrt(0.01), alpha=0.1, theta=t)
79
80## run two short reversible-jump Markov chains
81r=paste(sample(letters,9,replace=TRUE),collapse="")
82lapply(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=".")))
83
84 # collect directories
85dirs=dir("./",pattern=paste("OU",r,sep="."))
86pool.rjmcmcsamples(base.dirs=dirs, lab=r)
87
88## plot Markov sampled optima
89dev.new()
90shifts.plot(phy=phy, base=paste(r,"combined.rjmcmc",sep="."), burnin=0.5, legend=TRUE, pdf=FALSE, edge.width=2) 
91
92## PASTE UNCOMMENTED FOLLOWING LINE TO DROP DIRECTORIES CREATED BY RJMCMC
93 # unlink(dir(pattern=paste(r)),recursive=TRUE)
94 }