rexp.var: General rate exponential and Weibull waiting times

Description Usage Arguments Value Author(s) Examples

View source: R/rexp.var.R

Description

Generates a waiting time following an exponential or Weibull distribution with constant or varying rates. Allows for an optional shape parameter, in which case lambda will be taken as a Weibull scale. Allows for information on the current time, to consider only rates starting from then, and the speciation time, optionally, in case shape is provided, as the process is age-dependent. Allows for customization on the possibility of throwing away waiting times higher than tMax, and therefore takes that time as a parameter.

Usage

1
rexp.var(n, lambda, now = 0, tMax = Inf, shape = NULL, TS = 0, fast = FALSE)

Arguments

n

The number of waiting times to return. Usually 1, but we allow for a higher n to be consistent with the rexp function.

lambda

The rate parameter for the exponential distribution. If shape is not NULL, lambda is a scale for a Weibull distribution. In both cases we allow for any time-varying function. Note lambda can be constant.

now

The current time. Needed if one wants to consider only the interval between the current time and the maximum time for the time-varying rate. Note this does means the waiting time is >= now, so we also subtract now from the result before returning. The default is 0.

tMax

The simulation ending time. If the waiting time would be too high, we return tMax + 0.01 to signify the event never happens, if fast == TRUE. Otherwise we return the true waiting time. By default, tMax will be Inf, but if FAST == TRUE one must supply a finite value.

shape

Shape of the Weibull distribution. This will be when smaller than one, rate will decrease along species' age (negative age-dependency). When larger than one, rate will increase along each species's age (positive age-dependency). Default is NULL, equivalent to an exponential distribution. For pShape != NULL (including when equal to one), pp will be considered a scale (= 1/rate), and rexp.var will draw a Weibull distribution instead of an exponential. This means Weibull(rate, 1) = Exponential(1/rate). Notice even when pShape != NULL, pp may still be time-dependent.

Note: Time-varying shape is implemented, so one could have shape be a function of time. It is not thoroughly tested, however, so it may be prudent to wait for a future release where this feature is well established.

TS

Speciation time, used to account for the scaling between simulation and species time. The default is 0. Supplying a TS > now will return an error.

fast

If set to FALSE, waiting times larger than tMax will not be thrown away. This argument is needed so one can test the function without bias.

Value

A vector of waiting times for the exponential or weibull distribution with the given parameters.

Author(s)

Bruno do Rosario Petrucci.

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
###
# let us start by checking a simple exponential variable

# rate
lambda <- 0.1

# current time
now <- 0

# maximum time to check
tMax <- 40

# get waiting time
t <- rexp.var(n = 1, lambda, now, tMax)
t

###
# another simple exponential

# rate
lambda <- 0.5

# current time
now <- 0

# maximum time to check
tMax <- 40

# find the waiting time
t <- rexp.var(n = 1, lambda, now, tMax)
t

###
# now let us try a linear function for the rate

# rate
lambda <- function(t) {
  return(0.01*t + 0.1)
}

# current time
now <- 0

# maximum time to check
tMax <- 40

# find the waiting time
t <- rexp.var(n = 1, lambda, now, tMax)
t

###
# what if lambda is exponential?

# rate
lambda <- function(t) {
  return(0.01 * exp(0.1*t) + 0.02)
}

# current time
now <- 0

# maximum time to check
tMax <- 40

# find the waiting time
t <- rexp.var(n = 1, lambda, now, tMax)
t

###
# rexp.var also works for a weibull

# scale
lambda <- 2

# shape
shape <- 1

# current time
now <- 0

# maximum time to check
tMax <- 40

# speciation time
TS <- 0

# find the vector of waiting time
t <- rexp.var(n = 1, lambda, now, tMax,
              shape = shape, TS = TS)
t

###
# when shape = 1, the Weibull is an exponential, we could do better

# scale
lambda <- 10

# shape
shape <- 2

# current time
now <- 0

# maximum time to check
tMax <- 40

# speciation time
TS <- 0

# find the vector of waiting times - it doesn't need to be just one
t <- rexp.var(n = 5, lambda, now, tMax,
              shape = shape, TS = TS)
t

###
# shape can be less than one, of course

# scale
lambda <- 10

# shape
shape <- 0.5

# current time
now <- 3

# maximum time to check
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

# find the vector of waiting times
t <- rexp.var(n = 3, lambda, now, tMax,
              shape = shape, TS = TS)
t

###
# both lambda and shape can be time varying for a Weibull, but since we have
# only been able to test time-varying lambda effectively, we do not recommend
# using shape as a function

# scale
lambda <- function(t) {
  return(0.25*t + 5)
}

# shape
shape <- 3

# current time
now <- 0

# maximum time to check
tMax <- 40

# speciation time
TS <- 0

# find the vector of waiting times - it doesn't need to be just one
t <- rexp.var(n = 5, lambda, now, tMax,
              shape = shape, TS = TS)
t

###
# lambda can be any function of time, remember

# scale
lambda <- function(t) {
  return(0.2*exp(0.1*t) + 5)
}

# shape
shape <- 3

# current time - remember lambda will only be considered starting from time now
now <- 2.5

# maximum time to check
tMax <- 40

# speciation time
TS <- 1.2

# find the vector of waiting times - it doesn't need to be just one
t <- rexp.var(n = 2, lambda, now, tMax,
              shape = shape, TS = TS)
t

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