# we start drawing a simple simulation
# maximum simulation time
tMax <- 10
# set seed
set.seed(1)
# run a simulation
sim <- bd.sim(n0 = 1, lambda = 0.6, mu = 0.55, tMax = tMax,
nFinal = c(10,20))
# draw it
draw.sim(sim)
###
# we can add fossils to the drawing
# maximum simulation time
tMax <- 10
# set seed
set.seed(1)
# run a simulation
sim <- bd.sim(n0 = 1, lambda = 0.6, mu = 0.55, tMax = tMax,
nFinal = c(10,20))
# set seed
set.seed(1)
# simulate data resulting from a fossilization process
# with exact occurrence times
fossils <- sample.clade(sim = sim, rho = 4, tMax = tMax, returnTrue = TRUE)
# draw it
draw.sim(sim, fossils = fossils)
# we can order the vertical drawing of species based on
# any element of sim
draw.sim(sim, fossils = fossils, sortBy = "PAR")
# here we cluster lineages with their daughters by
# sorting them by the "PAR" list of the sim object
draw.sim(sim, fossils = fossils, sortBy = "TE")
# here we sort lineages by their extinction times
###
# fossils can also be represented by ranges
# maximum simulation time
tMax <- 10
# set seed
set.seed(1)
# run birth-death simulation
sim <- bd.sim(n0 = 1, lambda = 0.6, mu = 0.55, tMax = tMax,
nFinal = c(10,20))
# simulate data resulting from a fossilization process
# with fossil occurrence time ranges
# set seed
set.seed(20)
# create time bins randomly
bins <- c(tMax, 0, runif(n = rpois(1, lambda = 6), min = 0, max = tMax))
# set seed
set.seed(1)
# simulate fossil sampling
fossils <- sample.clade(sim = sim, rho = 2, tMax = tMax,
returnTrue = FALSE, bins = bins)
# get old par
oldPar <- par(no.readonly = TRUE)
# draw it, sorting lineages by their parent
draw.sim(sim, fossils = fossils, sortBy = "PAR",
fossilsFormat = "ranges", restoreOldPar = FALSE)
# adding the bounds of the simulated bins
abline(v = bins, lty = 2, col = "blue", lwd = 0.5)
# alternatively, we can draw lineages varying colors and tip labels
# (note how they are sorted)
draw.sim(sim, fossils = fossils, fossilsFormat = "ranges",
tipLabels = paste0("spp_", 1:length(sim$TS)),
lineageColors = rep(c("red", "green", "blue"), times = 5))
# restore old par
par(oldPar)
###
# we can control how to sort displayed species exactly
# maximum simulation time
tMax <- 10
# set seed
set.seed(1)
# run birth-death simulations
sim <- bd.sim(n0 = 1, lambda = 0.6, mu = 0.55, tMax = tMax,
nFinal = c(10,20))
# set seed
set.seed(1)
# simulate fossil sampling
fossils <- sample.clade(sim = sim, rho = 4, tMax = tMax, returnTrue = TRUE)
# draw it with random sorting (in pratice this could be a trait
# value, for instance)
draw.sim(sim, fossils = fossils, sortBy = sample(1:length(sim$TS)))
###
# we can display trait values as well
# initial number of species
n0 <- 1
# maximum simulation time
tMax <- 20
# speciation, higher for state 1
lambda <- c(0.1, 0.2)
# extinction, lowest for state 0
mu <- c(0.01, 0.03)
# number of traits and states (2 binary traits)
nTraits <- 2
nStates <- 2
# initial value of both traits
X0 <- 0
# transition matrix, with symmetrical transition rates for trait 1,
# and asymmetrical (and higher) for trait 2
Q <- list(matrix(c(0, 0.1,
0.1, 0), ncol = 2, nrow = 2),
matrix(c(0, 1,
0.5, 0), ncol = 2, nrow = 2))
# set seed
set.seed(1)
# run the simulation
sim <- bd.sim.traits(n0, lambda, mu, tMax, nTraits = nTraits,
nStates = nStates, X0 = X0, Q = Q, nFinal = c(2, 10))
# maybe we want to take a look at the traits of fossil records too
fossils <- sample.clade(sim$SIM, rho = 0.5, tMax = max(sim$SIM$TS),
returnAll = TRUE, bins = seq(0, 20, by = 1))
draw.sim(sim$SIM, traits = sim$TRAITS, sortBy = "PAR",
fossils = fossils, fossilsFormat = "all",
traitLegendPlacement = "bottomleft")
# note how fossil ranges are displayed above and below the true
# occurrence times, but we could also draw only one or the other
# just ranges
draw.sim(sim$SIM, traits = sim$TRAITS, sortBy = "PAR",
fossils = fossils, fossilsFormat = "ranges",
traitLegendPlacement = "bottomleft")
# just true occurrence times
draw.sim(sim$SIM, traits = sim$TRAITS, sortBy = "PAR", traitID = 2,
fossils = fossils, fossilsFormat = "exact",
traitLegendPlacement = "bottomleft")
# note the different traitID, so that segments are colored
# following the value of the second trait
Run the code above in your browser using DataLab