The aim of this tutorial is to perform scenario analyses with an individual-based model in R based on the random walk dynamics from the previous tutorial.
In this session, you will
The best thing to do is to read each section and type (or copy and
paste) the R commands (in grey boxes) into your own R session to check
that you understand the code.
The first step when creating a new R file is to specify a working
directory. You need to tell the R session where you want it to work. To
do this you use the command to “set the working directory” or
setwd()
.
You can label this working directory as ‘home’, and have other locations stored for plotting or for getting data. This location should be an existing folder on your computer. For example:
<- "~/Documents/Modelling_intro/" ## on a OS X
home <- "C:\\Documents\\Modelling_intro\\" ## on windows
home setwd(home)
If the curl
package is not installed yet, you can do so
by
install.packages(curl) # install the curl library (to import data)
Please load the IBM functions we prepared on the GitHub repository. Using your skills from the previous tutorial(s), you should be able to code this model with random walks and vaccination yourselves.
# load package 'curl'
library('curl')
# download R file with IBM function
curl_download("https://github.com/lwillem/modelling-intro/raw/master/lib/ibm_functions.R", destfile = "ibm_functions.R")
# load the IBM functions
source('ibm_functions.R')
First, let’s run the model with the default parameters:
# you can run the IBM with default parameters (see console)
run_ibm_random_walk()
## [1] "MODEL PARAMETERS"
## [1] "pop_size: 1000"
## [1] "num_days: 50"
## [1] "num_infected_seeds: 3"
## [1] "vaccine_coverage: 0.1"
## [1] "apply_spatial_vaccine_refusal: FALSE"
## [1] "rng_seed: 36"
## [1] "area_size: 20"
## [1] "max_velocity: 1"
## [1] "target_num_contacts_day: 10"
## [1] "max_contact_distance: 2"
## [1] "num_days_infected: 7"
## [1] "transmission_prob: 0.1"
## [1] "plot_time_delay: 0"
## [1] "-------------"
## [1] "MODEL RESULTS"
## [1] "total incidence: 90%"
## [1] "Peak prevalence: 35%"
## [1] "Peak day: 18"
## [1] "Total run time: 3s"
This IBM is able to show the location and health states of each
individual over time. Therefore, you have to increase the parameter
plot_time_delay
to set a delay between two time steps (=
default this is 0 = no time between 2 time steps = no plots)
# you can run the IBM with default parameters (see console)
run_ibm_random_walk(plot_time_delay = 0.2)
This IBM is able to account for vaccination. Therefore, you have to
specify the parameter vaccine_coverage
. By default,
vaccinated individuals are randomly distributed over the area. To make
this clear, we suggest to exclude the random movement, and use a static
population (with velocity = 0).
# set vaccination coverage to 60% with a population that does not move
run_ibm_random_walk(max_velocity = 0,
vaccine_coverage = 0.6)
## [1] "MODEL PARAMETERS"
## [1] "pop_size: 1000"
## [1] "num_days: 50"
## [1] "num_infected_seeds: 3"
## [1] "vaccine_coverage: 0.6"
## [1] "apply_spatial_vaccine_refusal: FALSE"
## [1] "rng_seed: 40"
## [1] "area_size: 20"
## [1] "max_velocity: 0"
## [1] "target_num_contacts_day: 10"
## [1] "max_contact_distance: 2"
## [1] "num_days_infected: 7"
## [1] "transmission_prob: 0.1"
## [1] "plot_time_delay: 0"
## [1] "-------------"
## [1] "MODEL RESULTS"
## [1] "total incidence: 9%"
## [1] "Peak prevalence: 2%"
## [1] "Peak day: 40"
## [1] "Total run time: 2s"
# is vaccination clustered?
run_ibm_random_walk(max_velocity = 0,
vaccine_coverage = 0.6,
apply_spatial_vaccine_refusal = TRUE)
## [1] "MODEL PARAMETERS"
## [1] "pop_size: 1000"
## [1] "num_days: 50"
## [1] "num_infected_seeds: 3"
## [1] "vaccine_coverage: 0.6"
## [1] "apply_spatial_vaccine_refusal: TRUE"
## [1] "rng_seed: 42"
## [1] "area_size: 20"
## [1] "max_velocity: 0"
## [1] "target_num_contacts_day: 10"
## [1] "max_contact_distance: 2"
## [1] "num_days_infected: 7"
## [1] "transmission_prob: 0.1"
## [1] "plot_time_delay: 0"
## [1] "-------------"
## [1] "MODEL RESULTS"
## [1] "total incidence: 17%"
## [1] "Peak prevalence: 5%"
## [1] "Peak day: 30"
## [1] "Total run time: 2s"
(TIP: the run_ibm_random_walk
function has an option to
add_baseline = TRUE
to add model results with the default
parameters. However, this increases the run time since the model is
executed twice.)