library(rgl) knitr::knit_hooks$set(webgl = hook_webgl)
Here we are going to look at an example on how to simulate complex evolutionary histories with treats
.
Let say we want to simulate the following story:
Three events then happen during the evolutionary history of this lineage:
We are going to run each step one by one in a cumulative manner to see how they pile up:
First let's set the overall birth death parameters (a speciation of 1.2 and an extinction of 0.3):
## The birth death parameters bd_params <- list(speciation = 1.5, extinction = 0.3) ## The stopping rule stop_rule <- list(max.time = 4)
And let's quickly test if the simulation works by running a birth death tree without traits and anything:
set.seed(0) # 123 also works ## Running a birth death tree results <- treats(bd.params = bd_params, stop.rule = stop_rule) ## And displaying the results plot(results) axisPhylo()
Then let's create the traits:
## The trait object two_traits <- make.traits(process = c(BM.process, OU.process), n = c(1, 2), trait.names = c("Distance", "Variables"))
## Visualise the 1D trait (BM.process) plot(two_traits, trait = 1)
## Visualise the 2D trait (uncorrelated OU.process) plot(two_traits, trait = 2, use.3D = TRUE)
Note that specifying the correlation matrix is not necessary here per se (since no correlation is the default) but we will modify it through an event later on so it's good practice to have it properly stated before hand (to avoid any confusion).
We can then run the tree with the traits:
set.seed(0) ## Running a birth death tree results <- treats(bd.params = bd_params, stop.rule = stop_rule, traits = two_traits) ## And displaying the results par(mfrow = c(1,2)) plot(results, trait = 1, main = "Trait 1: Distance") plot(results, trait = c(2,3), main = "Variables correlation")
## Visualising the tree in 3D plot(results, trait = c(2,3), use.3D = TRUE)
## Speciation event is more likely if lineage's ancestor is further away from the mean trait value distance.modify <- function(x, trait.values, lineage) return(x + x * abs(parent.traits(trait.values, lineage)[1] - mean(trait.values[,1]))) ## Make a distance modifier distance.speciation <- make.modifiers(speciation = speciation, modify = distance.modify)
set.seed(13) ## Running a birth death tree results <- treats(bd.params = bd_params, stop.rule = stop_rule, traits = two_traits, modifiers = distance.speciation) ## And displaying the results par(mfrow = c(1,2)) plot(results, trait = 1, main = "Trait 1: Distance") plot(results, trait = c(2,3), main = "Variables correlation")
## Visualising the tree in 3D plot(results, trait = c(2,3), use.3D = TRUE)
## The correlation change event update.correlation <- traits.update(process.args =list(Sigma = matrix(c(10,3,3,2),2,2))) events_list <- make.events(event.name = "Correlation", target = "traits", condition = age.condition(2), modification = traits.update( process.args =list(Sigma = matrix(c(10,3,3,2),2,2)), trait.names = "Variables"))
results <- NULL set.seed(1) ## Running a birth death tree results <- treats(bd.params = bd_params, stop.rule = stop_rule, traits = two_traits, modifiers = distance.speciation, events = events_list) ## And displaying the results par(mfrow = c(1,2)) plot(results, trait = 1, main = "Trait 1: Distance") plot(results, trait = c(2,3), main = "Variables correlation")
## Visualising the tree in 3D plot(results, trait = c(2,3), use.3D = TRUE)
BUGGED FROM HERE!
## Adding the selective event events_list <- make.events(event.name = "Selection", add = events_list, target = "taxa", condition = age.condition(4), modification = trait.extinction(x = 0, condition = `<`, trait = 2))
Problem with modification
argument here
## Running a birth death tree results <- treats(bd.params = bd_params, stop.rule = stop_rule, traits = two_traits, modifiers = distance.speciation, events = events_list) ## And displaying the results par(mfrow = c(1,2)) plot(results, trait = 1, main = "Trait 1: Distance") plot(results, trait = c(2,3), main = "Variables correlation")
## The new trait for the founding event new_traits <- make.traits(process = c(BM.process, BM.process), n = c(1, 2), trait.names = c("Distance", "Variables")) ## The mass extinction event after reaching 50 taxa mass_extinction <- make.events(target = "taxa", condition = taxa.condition(50), modification = random.extinction(0.8))
## The selective event events_list <- make.events(event.name = "Founding", add = events_list, target = "founding", condition = trait.condition(3), modification = founding.event(traits = new_traits, events = mass_extinction, bd.params = list( speciation = 2, extinction = 0)), additional.args = list(prefix = "found_"))
## Running a birth death tree results <- treats(bd.params = bd_params, stop.rule = stop_rule, traits = two_traits, modifiers = distance.speciation, events = events_list, null.error = 100) ## And displaying the results par(mfrow = c(1,2)) plot(results, trait = 1, main = "Trait 1: Distance") plot(results, trait = c(2,3), main = "Variables correlation")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.