###
# let us start by checking a simple exponential variable
# rate
rate <- 0.1
# set seed
set.seed(1)
# find the waiting time
t <- rexp.var(n = 1, rate)
t
# this is the same as t <- rexp(1, rate)
###
# now let us try a linear function for the rate
# rate
rate <- function(t) {
return(0.01*t + 0.1)
}
# set seed
set.seed(1)
# find the waiting time
t <- rexp.var(n = 1, rate)
t
###
# what if rate is exponential?
# rate
rate <- function(t) {
return(0.01 * exp(0.1*t) + 0.02)
}
# set seed
set.seed(1)
# find the waiting time
t <- rexp.var(n = 1, rate)
t
###
# if we give a shape argument, we have a Weibull distribution
# scale - note that this is equivalent to 1/rate if shape were NULL
rate <- 2
# shape
shape <- 1
# speciation time
TS <- 0
# set seed
set.seed(1)
# find the vector of waiting time
t <- rexp.var(n = 1, rate, shape = shape, TS = TS)
t
###
# when shape = 1, the Weibull is an exponential, we could do better
# scale
rate <- 10
# shape
shape <- 2
# speciation time
TS <- 0
# set seed
set.seed(1)
# find the vector of waiting times - it doesn't need to be just one
t <- rexp.var(n = 5, rate, shape = shape, TS = TS)
t
###
# shape can be less than one, of course
# scale
rate <- 10
# shape
shape <- 0.5
# note we can supply now (default 0) and tMax (default Inf)
# now matters when we wish to consider only waiting times
# after that time, important when using time-varying rates
now <- 3
# tMax matters when fast = TRUE, so that higher times will be
# thrown out and returned as tMax + 0.01
tMax <- 40
# speciation time - it will be greater than 0 frequently during a
# simulation, as it is used to represent where in the species life we
# currently are and rescale accordingly
TS <- 2.5
# set seed
set.seed(1)
# find the vector of waiting times
t <- rexp.var(n = 10, rate, now, tMax,
shape = shape, TS = TS, fast = TRUE)
t
# note how some results are tMax + 0.01, since fast = TRUE
###
# both rate and shape can be time varying for a Weibull
# scale
rate <- function(t) {
return(0.25*t + 5)
}
# shape
shape <- 3
# current time
now <- 0
# maximum time to check
tMax <- 40
# speciation time
TS <- 0
# set seed
set.seed(1)
# find the vector of waiting times
t <- rexp.var(n = 5, rate, now, tMax,
shape = shape, TS = TS, fast = TRUE)
t
Run the code above in your browser using DataLab