Iterate a population model forward in time from an initial population vector. Parameters of the model can vary with time.
iterate(parms = NULL, N0 = NULL, popfun = NULL)
parms | a data frame containing the parameters of the model. Must have one row for each time step. |
---|---|
N0 | a numeric vector of the initial population size. If named, these names will be used in the returned tibble. |
popfun | a function that steps the model by one time step. Must take at least N0 as an argument. |
A data frame containing the input parameters and a column of projected population size. The return value will have the same class as parms.
Will fail if any of the three input arguments is NULL. If `parms` only has a single row, just column binds the initial population size. If `parms` has zero rows, the result also has zero rows.
# a simple exponential growth model anexppop <- function(N0, b, d) { N1 <- N0 * (1 + b - d) return(N1) } # make the input dataframe inputs <- data.frame(Year = 2018:2025, b = 0.2, d = 0.15) iterate(inputs, 23, anexppop)#> Year b d N #> 1 2018 0.2 0.15 23.00000 #> 2 2019 0.2 0.15 24.15000 #> 3 2020 0.2 0.15 25.35750 #> 4 2021 0.2 0.15 26.62538 #> 5 2022 0.2 0.15 27.95664 #> 6 2023 0.2 0.15 29.35448 #> 7 2024 0.2 0.15 30.82220 #> 8 2025 0.2 0.15 32.36331# time dependent model inputs <- data.frame(Year = 2018:2025, b = seq(0.2, 0.1, length = 8), d = 0.15) iterate(inputs, 23, anexppop)#> Year b d N #> 1 2018 0.2000000 0.15 23.00000 #> 2 2019 0.1857143 0.15 24.15000 #> 3 2020 0.1714286 0.15 25.01250 #> 4 2021 0.1571429 0.15 25.54848 #> 5 2022 0.1428571 0.15 25.73097 #> 6 2023 0.1285714 0.15 25.54718 #> 7 2024 0.1142857 0.15 24.99974 #> 8 2025 0.1000000 0.15 24.10689