-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0105-run-birth-death.R
44 lines (34 loc) · 1.17 KB
/
0105-run-birth-death.R
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
#' ---
#' title: "Simple birth-death difference equation model"
#' author: "Richard Reeve"
#' date: '`r format(Sys.Date(), "%B %d %Y")`'
#' output: html_document
#' ---
#' Load in the functions that do the work
library(RPiR)
source("0105-step-birth-death.R")
#' Set up the simulation parameters
# Set the birth and death rates
birth.rate <- 0.2
death.rate <- 0.1
# Starting population size
initial.count <- 1
# And setting times
start.time <- 0
end.time <- 100
#' Run the simulation
## Set up the population starting size (at the first timestep)
population.df <- data.frame(count = initial.count)
## the timesteps that the simulation will run through
timesteps <- seq(from = start.time + 1, to = end.time)
## Now we loop through the time itself (starting at the second timestep)
for (new.time in timesteps) {
updated.population <-
step_deterministic_birth_death(latest = tail(population.df, 1),
birth.rate = birth.rate,
death.rate = death.rate)
population.df <- rbind(population.df, updated.population)
}
#' And plot the results
population.df$time <- c(start.time, timesteps)
plot_populations(population.df)