make.rate {paleobuddy} | R Documentation |
Create a flexible rate for birth-death or sampling simulations
Description
Generates a function determining the variation of a rate (speciation, extinction, sampling) with respect to time. To be used on birth-death or sampling functions, it takes as the base rate (1) a constant, (2) a function of time, (3) a function of time and a time-series (usually an environmental variable), or (4) a vector of numbers describing rates as a step function. Requires information regarding the maximum simulation time, and allows for optional extra parameters to tweak the baseline rate.
Usage
make.rate(rate, tMax = NULL, envRate = NULL, rateShifts = NULL)
Arguments
rate |
The baseline function with which to make the rate. It can be a
|
tMax |
Ending time of simulation, in million years after the clade's
origin. Needed to ensure |
envRate |
A Note that, since simulation functions are run in forward-time (i.e. with
Acknowledgements: The strategy to transform a function of |
rateShifts |
A vector indicating the time of rate shifts in a step
function. The first element must be the first or last time point for the
simulation, i.e. |
Value
A constant or time-varying function (depending on input) that can
then be used as a rate in the other paleobuddy
functions.
Author(s)
Bruno do Rosario Petrucci
References
Morlon H. et al (2016) RPANDA: an R package for macroevolutionary analyses on phylogenetic trees. Methods in Ecology and Evolution 7: 589-597.
Examples
# first we need a time vector to use on plots
time <- seq(0, 50, 0.1)
###
# we can have a step function rate
# vector of rates
rate <- c(0.1, 0.2, 0.3, 0.2)
# vector of rate shifts
rateShifts <- c(0, 10, 20, 35)
# this could be c(50, 40, 30, 15) for equivalent results
# make the rate
r <- make.rate(rate, tMax = 50, rateShifts = rateShifts)
# plot it
plot(time, rev(r(time)),type = 'l', xlim = c(max(time), min(time)))
# note that this method of generating a step function rate is slower to
# numerically integrate
# it is also not possible a rate and a shifts vector and a time-series
# dependency, so in cases where one looks to run many simulations, or have a
# step function modified by an environmental variable, consider
# using ifelse() (see below)
###
# we can have an environmental variable (or any time-series)
# temperature data
data(temp)
# function
rate <- function(t, env) {
return(0.05*env)
}
# make the rate
r <- make.rate(rate, envRate = temp)
# plot it
plot(time, rev(r(time)), type = 'l', xlim = c(max(time), min(time)))
###
# we can have a rate that depends on time AND temperature
# temperature data
data(temp)
# function
rate <- function(t, env) {
return(0.001*exp(0.1*t) + 0.05*env)
}
# make a rate
r <- make.rate(rate, envRate = temp)
# plot it
plot(time, rev(r(time)), type = 'l', xlim = c(max(time), min(time)))
###
# as mentioned above, we could also use ifelse() to
# construct a step function that is modulated by temperature
# temperature data
data(temp)
# function
rate <- function(t, env) {
return(ifelse(t < 10, 0.1 + 0.01*env,
ifelse(t < 30, 0.2 - 0.005*env,
ifelse(t <= 50, 0.1 + 0.005*env, 0))))
}
# rate
r <- make.rate(rate, envRate = temp)
# plot it
plot(time, rev(r(time)), type = 'l', xlim = c(max(time), min(time)))
# while using ifelse() to construct a step function is more
# cumbersome, it leads to much faster numerical integration,
# so in cases where the method above is proving too slow,
# consider using ifelse() even if there is no time-series dependence
###
# make.rate will leave some types of functions unaltered
# constant rates
r <- make.rate(0.5)
# plot it
plot(time, rep(r, length(time)), type = 'l',
xlim = c(max(time), min(time)))
###
# linear rates
# function
rate <- function(t) {
return(0.01*t)
}
# create rate
r <- make.rate(rate)
# plot it
plot(time, rev(r(time)), type = 'l', xlim = c(max(time), min(time)))
###
# any time-varying function, really
# function
rate <- function(t) {
return(abs(sin(t))*0.1 + 0.05)
}
# create rate
r <- make.rate(rate)
# plot it
plot(time, r(time), type = 'l')