###
# first a simple BiSSE simulation, with
# binary state-dependent fossil sampling
# initial number of species
n0 <- 1
# maximum simulation time
tMax <- 20
# speciation, higher for state 1
lambda <- c(0.1, 0.2)
# extinction, highe for state 0
mu <- c(0.06, 0.03)
# number of traits and states (1 binary trait)
nTraits <- 1
nStates <- 2
# initial value of the trait
X0 <- 0
# transition matrix, with symmetrical transition rates
Q <- list(matrix(c(0, 0.1,
0.1, 0), ncol = 2, nrow = 2))
# set seed
set.seed(1)
# run the simulation
sim <- bd.sim.traits(n0, lambda, mu, tMax = tMax, nTraits = nTraits,
nStates = nStates, X0 = X0, Q = Q, nFinal = c(2, Inf))
# now a fossil sampling rate, with higher rate for state 1
rho <- c(0.5, 1)
# run fossil sampling
fossils <- sample.clade.traits(sim$SIM, rho, tMax, sim$TRAITS)
# draw simulation with fossil occurrences as time points
draw.sim(sim$SIM, traits = sim$TRAITS,
fossils = fossils$FOSSILS, traitLegendPlacement = "bottomleft")
###
# can also run a HiSSE model, with hidden traits having an effect on rates
# initial number of species
n0 <- 1
# maximum simulation time
tMax <- 20
# speciation, higher for state 1A, highest for 1B
lambda <- c(0.1, 0.2, 0.1, 0.3)
# extinction, lowest for 0B
mu <- c(0.03, 0.03, 0.01, 0.03)
# number of traits and states--in this case, we just run with 4 observed
# states, so that our traits data frames will include that info for sampling
nTraits <- 1
nStates <- 4
# initial value of the trait
X0 <- 0
# transition matrix, with symmetrical transition rates. Only one transition
# is allowed at a time, i.e. 0A can go to 0B and 1A,
# but not to 1B, and similarly for others
Q <- list(matrix(c(0, 0.1, 0.1, 0,
0.1, 0, 0, 0.1,
0.1, 0, 0, 0.1,
0, 0.1, 0.1, 0), ncol = 4, nrow = 4))
# 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, Inf))
# note how sim$TRAITS contains states 2 and 3, even though this
# is a HiSSE model, because we need that information to run hidden states
# on fossil sampling as well (see below)
# now a fossil sampling rate, with higher rate for state 1A,
# and highest yet for state 1B
rho <- c(0.5, 1, 0.5, 2)
# bins for fossil occurrences
bins <- seq(tMax, 0, -1)
# run fossil sampling, with returnAll = TRUE to include ranges
fossils <- sample.clade.traits(sim$SIM, rho, tMax, sim$TRAITS,
nStates = 2, nHidden = 2,
returnAll = TRUE, bins = bins)
# note how fossils$TRAITS only contains trait values 0 and 1, similar to
# if we had ran bd.sim.traits with nHidden = 2, since sample.clade.traits
# did the job of transforming observed into hidden states
# draw simulation with fossil occurrences as time points AND ranges
draw.sim(sim$SIM, traits = sim$TRAITS, fossils = fossils$FOSSILS,
fossilsFormat = "all", traitLegendPlacement = "bottomleft")
# note that there are a lot of fossils, so the ranges are difficult to see
# see ?sample.clade for further examples using different return types
# (fossil ranges etc.), and ?bd.sim.traits for more examples using
# higher numbers of states (hidden or observed), though in
# sample.clade.traits we always have only one trait of focus
Run the code above in your browser using DataLab