This function calculates an adjusted value over a time interval with optional discounting. This is useful for instances when adding cycles may not be desirable, so one can perform "cycle-like" calculations without needing cycles, offering performance speeds. See the vignette on avoiding cycles for an example in a model.
Arguments
- curtime
Numeric. The current time point.
- nexttime
Numeric. The next time point. Must be greater than or equal to
curtime
.- by
Numeric. The step size for evaluation within the interval.
- expression
An expression evaluated at each step. Use
time
as the variable within the expression.- discount
Numeric or NULL. The discount rate to apply, or NULL for no discounting.
Details
The user can use the .time
variable to select the corresponding time of the sequence being evaluated.
For example, in curtime = 0, nexttime = 4, by = 1
, time
would correspond to 0, 1, 2, 3
.
If using nexttime = 4.2
, 0, 1, 2, 3, 4
Examples
# Define a function or vector to evaluate
bs_age <- 1
vec <- 1:8/10
# Calculate adjusted value without discounting
adj_val(0, 4, by = 1, expression = vec[floor(.time + bs_age)])
#> [1] 0.25
adj_val(0, 4, by = 1, expression = .time * 1.1)
#> [1] 1.65
# Calculate adjusted value with discounting
adj_val(0, 4, by = 1, expression = vec[floor(.time + bs_age)], discount = 0.03)
#> [1] 0.2463061