# 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')
Run the code above in your browser using DataLab