fitContinuousMCMC: Fit models of continuous trait evolution to comparative data...

Description Usage Arguments Details Value Author(s) References Examples

View source: R/traits-fossil.R

Description

This function is essentially an MCMC version of the fitContinuous function that fits models using Maximum Likelihood. The main advantage of using MCMC is that informative prior distributions may be placed on node values when such information is available, for example from the fossil record. In such cases, model selection may be improved upon and differ from results using extant taxa only.

Usage

1
2
3
4
fitContinuousMCMC(phy, d, model = c("BM", "Trend", "SSP", "ACDC.exp","ACDC.lin"),
	Ngens = 1e+06, sampleFreq = 1000, printFreq = 1000,
	propwidth = rep(0.1, 5), node.priors = NULL, root.prior = NULL,
	acdc.prior = NULL, sample.node.states = T, outputName = "mcmc.output")

Arguments

phy

A phylogenetic tree in phylo format

d

A named numeric vector of tip values

model

The type of model to be fitted. Options are: "BM" - Brownian motion, "Trend" = BM with a directional trend (this option can only be used with non ultrametric trees or when node priors are available for an ultrametric tree), "SSP" - the single stationary peak, or single-optimum OU model, "ACDC.exp" - Accelerating-Decelerating evolution where the change in rate is exponential with respect to time. The DC portion of this model corresponds to the EB model in Harmon et al. (2010), "ACDC.lin" - Accelerating-Decelerating evolution where the change in rate is linear with respect to time (see the supplementary information for Harmon et al. 2010 for details).

Ngens

The number of generations that the MCMC should be run for.

sampleFreq

The frequency with which likelihoods and parameters should be sampled from the chain. For large trees and/or if ancestral states are being sampled, this value should be large (at least equal to the number of internal nodes in the tree).

printFreq

The frequency with which progress should be printed to the screen.

propwidth

A numeric vector containing five values corresponding to proposal widths. The order is: 1 - root state proposal width, 2 - rate proposal width, 3 - scaler (alpha, rate change, trend parameter) proposal width, 4 - Theta (OU optimum) proposal width (not yet implemented), and 5- node state proposal width. 5 Values should be provided regardless of the model being fitted. Dummy or random values can be used for non-applicable parameters in such cases.

node.priors

A data frame of informative node priors. The default is node.priors=NULL, in which case uniformative priors will be assumed for all nodes. Alternatively, the user may provide a dataframe with five columns. The first two columns should specify the tips spanning the clade descended from the node on which an informative prior is to be placed. Columns 3 and 4 give parameters related to the prior while column 5 specifies the type of prior distribution. Current options, along with the associated parameters required in columns 3 and 4, are "normal" (mean and sd), "uniform" (min and max), and "exp" (offset and rate).

root.prior

A prior for the root value. The default is root.prior = NULL, in which case an uniformative root prior is assumed. Alternatively, the user may provide a vector containing 3 values: 1 & 2 are the parameters for the root prior parameter values, while the 3rd is the type of prior. Current prior types are the same as for node priors.

acdc.prior

For the ACDC models, it is wise to bound the values of the rate change parameter as unreasonable proposed values can result in rounding issues and negatively infinite log-likelihoods. The default for this option is NULL. If an ACDC model is selected and no prior is given, a bounded, informative uniform prior will be assigned based on the root height of the tree. Alternatively, the user may provide bounds of their own. This may be particularly useful if the user is only interested in a subset of the parameter space area (eg., EB).

sample.node.states

Logical. If sample.node.states = TRUE, node states (ancestral state values) will be logged to a separate output file. If sample.node.states = FALSE, ancestral states will be treated as nuisance parameters and discarded after computation of likelihoods.

outputName

stem name for output file(s)

Details

Unlike fitContinuous, which returns output as a list, fitContinuousMCMC outputs directly to text file(s). The model parameter file is designed to be compatatible with the popular Tracer software used in conjunction with BEAST. The output can easily be read back into R for further analysis. A particular advantage of writing directly to text file is that crashes etc do not result in the complete loss of long runs, and memory is saved.

Value

If sample.node.states = FALSE, one text file is written to the current working directory.

outputName_model_params.txt

a text file containing sampled prior, posterior, likelihood, and model parameter values from the MCMC run

If sample.node.states = TRUE, a second text file is written

outputName_nodestates.txt

contains the posterior sample of ancestral states for internal nodes

Author(s)

Graham Slater

References

Slater GJ, LJ Harmon, and ME Alfaro. 2012. Integrating fossils with molecular phylogenies improves inference of trait evolution. Evolution 66:3931-3944.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
## Not run: 
data(caniformia)
phy <- caniformia$phy
d <- caniformia$dat
node.priors <- caniformia$node.priors
root.prior <- caniformia$root.prior

## as an example, we will run a very short (too short!) analysis,
##fitting BM and Trend to the caniform data

fitContinuousMCMC(phy, d, model = "BM", Ngens = 1000, sampleFreq =100,
printFreq = 100, node.priors = node.priors, root.prior = root.prior,
outputName ="BM_caniforms")

fitContinuousMCMC(phy, d, model = "Trend", Ngens = 1000, sampleFreq=100,
printFreq = 100, node.priors = node.priors, root.prior = root.prior,
outputName ="Trend_caniforms")

bm.res <- read.table("BM_caniforms_model_params.txt", header= TRUE)
head(bm.res)

trend.res <- read.table("Trend_caniforms_model_params.txt", header= TRUE)
head(trend.res)

### produce trace plots of logLk scores 

plot(bm.res$generation, bm.res$logLk, type = "l",
ylim = c(min(bm.res$logLk), max = max(trend.res$logLk)))

lines(trend.res$generation, trend.res$logLk, col = "red")
legend("bottomleft", c("bm", "trend"), lwd = 3, col = c("black", "red"))

## End(Not run)

Example output

Loading required package: ape
Generation: 100   1.02 0.84 0.99 

Generation: 200   1.005 0.93 0.96 

Generation: 300   0.99 0.92 0.97 

Generation: 400   0.9975 0.93 0.9675 

Generation: 500   0.996 0.924 0.966 

Generation: 600   0.99 0.92 0.97 

Generation: 700   0.9942857 0.9128571 0.9728571 

Generation: 800   0.99375 0.91875 0.975 

Generation: 900   0.99 0.91 0.9766667 

Generation: 1000   0.993 0.888 0.975 

Generation: 100   1 0.96 0.36 0.96 

Generation: 200   1 0.94 0.5 0.92 

Generation: 300   0.9866667 0.9333333 0.5333333 0.9333333 

Generation: 400   0.99 0.93 0.51 0.95 

Generation: 500   0.992 0.928 0.472 0.944 

Generation: 600   0.9866667 0.9266667 0.46 0.9533333 

Generation: 700   0.9828571 0.9257143 0.4457143 0.96 

Generation: 800   0.985 0.9 0.435 0.955 

Generation: 900   0.9866667 0.8755556 0.4177778 0.96 

Generation: 1000   0.984 0.868 0.416 0.96 

  generation Posterior       Prior     logLk   sigmasq      root
1          0 -180.9717  0.27831000 -181.2500 0.1200515 0.8681560
2        100 -181.6479  0.09107660 -181.7389 0.1173220 0.8538203
3        200 -180.6069  0.61722840 -181.2241 0.1218434 0.8690827
4        300 -181.5492  0.51630970 -182.0655 0.1077417 1.0118580
5        400 -180.6110 -0.02301018 -180.5880 0.1200141 1.1793950
6        500 -179.4040  0.01289550 -179.4169 0.1167175 1.0883460
  generation Posterior     Prior     logLk   sigmasq          mu      root
1          0 -187.3683 0.3055495 -187.6739 0.1259469 0.000000000 1.1840640
2        100 -186.4399 0.5232899 -186.9632 0.1192290 0.007023351 1.0255840
3        200 -185.1662 0.7142659 -185.8804 0.1287784 0.025891450 1.0107890
4        300 -184.9419 0.8543958 -185.7963 0.1313075 0.003697099 0.6979164
5        400 -185.4675 0.9001299 -186.3677 0.1359567 0.020669660 0.9179412
6        500 -185.9358 0.9116333 -186.8475 0.1325280 0.009878006 0.9010834

geiger documentation built on July 8, 2020, 7:12 p.m.