make.rate: Create a flexible rate for birth-death or sampling...

Description Usage Arguments Value Author(s) References Examples

View source: R/make.rate.R

Description

Generates a rate to be used on birth-death or sampling functions. Takes as the base rate (1) a constant, (2) a function of time, (3) a function of time interacting with 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

1
make.rate(ff, tMax = NULL, envF = NULL, fShifts = NULL)

Arguments

ff

The baseline function with which to make the rate. It can be a

A number

For constant birth-death rates.

A function of time

For rates that vary with time. Note that this can be any function of time.

A function of time and an environmental variable

For rates varying with time and an environmental variable, such as temperature. Note that supplying a function on more than one variable without an accompanying envF will result in an error.

A numeric vector

To create step function rates. Note this must be accompanied by a corresponding vector of shifts fShifts.

tMax

Ending time of simulation, in million years after the clade's origin. Needed to ensure fShifts runs the correct way.

envF

A data.frame representing the variation of an environmental variable (e.g. CO2, temperature, available niches, etc) with time. The first column of this data.frame must be time, and the second column must be the values of the variable. The function will return an error if supplying envF without ff being a function of two variables. Note paleobuddy has two environmental data frames, temp and co2. One can check RPANDA for more examples.

Acknowledgements: The strategy to transform a function of t and env into a function of t only using envF was adapted from RPANDA (see below).

fShifts

A vector indicating the time placement of rate shifts in a step function. The first element must be the first time point for the simulation. This may be 0 or tMax. Since functions in paleobuddy run from 0 to tMax, if fShifts runs from past to present (fShifts[2] < fShifts[1]), we take tMax - fShifts as the shifts vector. Note that supplying fShifts when ff is not a numeric vector of the same length will result in an error.

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

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# first we need a time vector to use on plots
t <- seq(0, 50, 0.1)

# make.rate will leave some types of functions unaltered, like the following

###
# let us start simple: create a constant rate
r <- make.rate(0.5)

# plot it
plot(t, rep(r, length(t)), type = 'l')

###
# something a bit more complex: a linear rate

# function
ff <- function(t) {
  return(0.01*t)
}

# create rate
r <- make.rate(ff)

# plot it
plot(t, r(t), type = 'l')

###
# remember: this can be any time-varying function!

# function
ff <- function(t) {
  return(sin(t)*0.01)
}

# create rate
r <- make.rate(ff)

# plot it
plot(t, r(t), type = 'l')

###
# we can use ifelse() to make a step function like this
ff <- function(t) {
  return(ifelse(t < 10, 0.1,
                ifelse(t < 20, 0.3,
                       ifelse(t < 30, 0.2,
                              ifelse(t < 40, 0.05, 0)))))
}

# and make it into a rate - in this case, as the previous, it does not alter
# ff. We put it here as a contrast to the other way to make a step function
r <- make.rate(ff)

# plot it
plot(t, r(t), type = 'l')

# important note: this method of creating a step function might be annoying,
# but when running thousands of simulations it will provide a much faster
# integration than when using our method of transforming a rates and shifts
# vector into a function of time

# this is a good segway into the cases where make.rate actually makes a rate!
# note that while the previous ones seemed useless, we need that implementation
# so that the birth-death functions work

###
# now we can demonstrate the other way of making a step function

# vector of rates
ff <- c(0.1, 0.2, 0.3, 0.2)

# vector of rate shifts
fShifts <- c(0, 10, 20, 35)
# this could be c(50, 40, 30, 15) for equivalent results

# make the rate
r <- make.rate(ff, tMax = 50, fShifts = fShifts)

# plot it
plot(t, r(t),type = 'l')

# as mentioned above, while this works well it will be a pain to integrate.
# Furthermore, it is impractical to supply a rate and a shifts vector and
# have an environmental dependency, so in cases where one looks to run
# more than a couple dozen simulations, or when one is looking to have a
# step function modified by an environmental variable, consider using ifelse()

###
# finally let us see what we can do with environmental variables

# temperature data
data(temp)

# function
ff <- function(t, env) {
  return(0.05*env)
}

# make the rate
r <- make.rate(ff, envF = temp)

# plot it
plot(t, r(t), type = 'l')

###
# we can also have a function that depends on both time AND temperature

# function
ff <- function(t, env) {
  return(0.001*exp(0.1*t) + 0.05*env)
}

# make a rate
r <- make.rate(ff, envF = temp)

# plot it
plot(t, r(t), type = 'l')

###
# as mentioned above, we could also use ifelse() to construct a step function
# that is modulated by temperature

# function
ff <- 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(ff, envF = temp)

# plot it
plot(t, r(t), type = 'l')

brpetrucci/paleobuddy documentation built on Aug. 8, 2020, 2:03 a.m.