babette demo

Richèl J.C. Bilderbeek

2020-11-06

Introduction

This vignette briefly demonstrates multiple features of babette, without going into much detail.

First, load the library:

library(babette)

This vignette shows how to:

In all cases, this is done for a short MCMC chain length of 10K:

inference_model <- create_test_inference_model()
inference_model$mcmc$tracelog$filename <- tempfile()
inference_model$mcmc$treelog$filename <- tempfile()
inference_model$mcmc$screenlog$filename <- tempfile()
mcmc <- inference_model$mcmc
sample_interval <- mcmc$tracelog$log_every

Also, in all cases, we use the same BEAST2 options:

beast2_options <- create_beast2_options(
  input_filename = tempfile(),
  output_state_filename = tempfile()
)

Let babette run ‘BEAST2’

For an alignment, we’ll use a babette example alignment.

if (is_beast2_installed()) {
  out <- bbt_run_from_model(
    fasta_filename = get_babette_path("anthus_aco_sub.fas"),
    inference_model = inference_model,
    beast2_options = beast2_options
  )
}

Plot the posterior estimates

if (is_beast2_installed()) {
  library(ggplot2)
  p <- ggplot(
    data = out$estimates,
    aes(x = Sample)
  )
  p + geom_line(aes(y = TreeHeight), color = "green")
  p + geom_line(aes(y = YuleModel), color = "red")
  p + geom_line(aes(y = birthRate), color = "blue")
}

Show the effective sample sizes (ESS)

Effective sample sizes, with 20% burn-in removed:

if (is_beast2_installed()) {
  traces <- remove_burn_ins(
    traces = out$estimates,
    burn_in_fraction = 0.2
  )
  esses <- t(calc_esses(traces, sample_interval = sample_interval))
  colnames(esses) <- "ESS"
  knitr::kable(esses)
}

For a reliable inference, use an ESS of at least 200.

Show the summary statistics

if (is_beast2_installed()) {
  sum_stats <- t(
    calc_summary_stats(
      traces$posterior,
      sample_interval = sample_interval
    )
  )
  colnames(sum_stats) <- "Statistic"
  knitr::kable(sum_stats)
}

Plot the posterior phylogenies

if (is_beast2_installed()) {
  plot_densitree(out$anthus_aco_sub_trees, width = 2)
}