-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
239 lines (204 loc) · 9.39 KB
/
README.Rmd
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# epizootic <img src='man/figures/hex.svg' align="right" height="125" />
<!-- badges: start -->
[![R-CMD-check](https://github.com/viralemergence/epizootic/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/viralemergence/epizootic/actions/workflows/R-CMD-check.yaml)
[![CRAN status](https://www.r-pkg.org/badges/version/epizootic)](https://CRAN.R-project.org/package=epizootic)
[![Download_count](https://cranlogs.r-pkg.org/badges/grand-total/epizootic)](https://CRAN.R-project.org/package=epizootic)
[![Last commit](https://img.shields.io/github/last-commit/viralemergence/epizootic.svg)](https://github.com/viralemergence/epizootic/commits/master)
<!-- badges: end -->
`epizootic` is an extension to `poems`, a spatially-explicit, process-explicit, pattern-oriented framework for modeling population dynamics. This extension adds functionality for modeling disease dynamics in wildlife. It also adds capability for seasonality and for unique dispersal dynamics for each life cycle stage.
## Installation
You can install the latest release on CRAN with:
``` r
install.packages("epizootic")
```
You can install the latest version of epizootic from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
install.packages("poems")
devtools::install_github("viralemergence/epizootic")
```
Because `epizootic` is an extension to `poems`, it is necessary to install `poems`
first.
## About R6 classes
`poems` and `epizootic` run on [R6](https://r6.r-lib.org/articles/Introduction.html) classes. R is primarily a *functional* programming language in which the primary units of programming are expressions and functions. Here we use R6 to create an *object-oriented* framework inside of R. R6 classes such as `DiseaseModel` and `SimulationHandler` are used to store model attributes, check them for consistency, pass them to parallel sessions for simulation, and gather results and errors.
## Example
Here is the initial state of an idealized theoretical disease scenario, following a SIR disease model with three life cycle stages: juvenile, yearling, and adult.
```{r initial_state}
library(poems)
library(purrr)
library(epizootic)
example_region <- Region$new(coordinates = data.frame(x = rep(seq(177.01, 177.05, 0.01), 5),
y = rep(seq(-18.01, -18.05, -0.01), each = 5)))
initial_abundance <- c(c(5000, 5000, 5000, 1, 0, 0, 0, 0, 0),
rep(c(5000, 5000, 5000, 0, 0, 0, 0, 0, 0), 24)) |>
matrix(nrow = 9)
example_region$raster_from_values(initial_abundance[3,]) |>
raster::plot(main = "Susceptible Adults")
example_region$raster_from_values(initial_abundance[4,]) |>
raster::plot(main = "Infected Juveniles")
```
```{r user defined functions, include = F}
sir_model_summer <- function(inputs) {
list2env(inputs, environment())
params1 <-
list(
recovery = recovery,
fecundity = fecundity,
mortality = mortality,
transmission = transmission,
breeding_season_length = breeding_season_length
) |>
map(\(x) x[c(1, 4, 7)])
params2 <-
list(
recovery = recovery,
fecundity = fecundity,
mortality = mortality,
transmission = transmission,
breeding_season_length = breeding_season_length
) |>
map(\(x) x[c(2, 5, 8)])
params3 <-
list(
recovery = recovery,
fecundity = fecundity,
mortality = mortality,
transmission = transmission,
breeding_season_length = breeding_season_length
) |>
map(\(x) x[c(3, 6, 9)])
init_list1 <- array_branch(segment_abundance[, occupied_indices], 2) |>
map(\(x) x[c(1, 4, 7)])
init_list2 <- array_branch(segment_abundance[, occupied_indices], 2) |>
map(\(x) x[c(2, 5, 8)])
init_list3 <- array_branch(segment_abundance[, occupied_indices], 2) |>
map(\(x) x[c(3, 6, 9)])
demographic_sir_model <- function(time, state, params, ...) {
# Unlist parameters from the params list, and convert as necessary
transmission <- params[["transmission"]]
recovery <- params[["recovery"]]
bsl <- params[["breeding_season_length"]][1]
death <- params[["mortality"]] / bsl
# Unpack parameters from the state vector using indices
S1 <- state[1] # Susceptible class 1
I1 <- state[2] # Infected class 1
R1 <- state[3] # Recovered class 1
dS1dt <- -transmission[1] * S1 * I1 - death[1] * S1
dI1dt <- (transmission[1] * S1 * I1) - (recovery[1] * I1) - death[1] * I1
dR1dt <- recovery[1] * I1 - death[1] * R1
# Output: instantaneous change in the nine disease states for three classes
return(list(c(dS1dt, dI1dt, dR1dt)))
}
# Solve the ordinary differential equations
sir_sol2 <- map2(list(init_list1, init_list2, init_list3),
list(params1, params2, params3),
\(x, p) {
map(x,
\(y) deSolve::ode(
y = y,
times = seq(1, breeding_season_length[1], 1),
func = demographic_sir_model,
parms = p
) |> _[breeding_season_length[1], 2:4] |> round())
})
# Assign populations to occupied indices in segment_abundance
for (i in 1:length(occupied_indices)) {
segment_abundance[c(1, 4, 7), occupied_indices[i]] <- sir_sol2[[1]][[i]]
segment_abundance[c(2, 5, 8), occupied_indices[i]] <- sir_sol2[[2]][[i]]
segment_abundance[c(3, 6, 9), occupied_indices[i]] <- sir_sol2[[3]][[i]]
}
return(segment_abundance)
}
disperser <- function(params) {
segment_abundance <- params$segment_abundance[, params$occupied_indices]
# Iterate over each column
for (col in 1:ncol(segment_abundance)) {
# Operate row by row to maintain compartment integrity
for (row in 1:nrow(segment_abundance)) {
# Only proceed if the current cell has a positive number
if (segment_abundance[row, col] > 0) {
# Generate a random integer number for dispersal
# This number should not exceed the current cell's population
random_number <- runif(1,
min = 1,
max = segment_abundance[row, col]) |> round()
# Subtract the random number from the current cell
segment_abundance[row, col] <- segment_abundance[row, col] - random_number
# Generate a random column index to add the random number to, excluding
# the current column
random_column <- sample(setdiff(1:ncol(segment_abundance), col), 1)
# Add the random number to the corresponding disease compartment in the
# random column
segment_abundance[row, random_column] <-
segment_abundance[row, random_column] + random_number
}
}
}
params$segment_abundance[, params$occupied_indices] <- segment_abundance
return(params$segment_abundance)
}
```
Here I create a `DiseaseModel` object, which stores inputs for disease simulations and checks them for consistency and completeness.
```{r disease_model}
model_inputs <- DiseaseModel$new(
time_steps = 10,
seasons = 2,
populations = 25,
stages = 3,
compartments = 3, # indicates disease compartments
region = example_region,
initial_abundance = initial_abundance,
# Dimensions of carrying_capacity are populations by timesteps
carrying_capacity = matrix(100000, nrow = 25, ncol = 10),
# Indicates length of breeding season in days for each population
breeding_season_length = rep(100, 25),
# One mortality value for each stage and compartment
mortality = c(0.4, 0.2, 0, 0.505, 0.25, 0.105, 0.4, 0.2, 0),
# Indicates that these are seasonal mortality values
mortality_unit = 1,
# No reproduction in this simple example
fecundity = 0,
fecundity_unit = 1,
fecundity_mask = rep(0, 9),
# Transmission rates from infected individuals, one for each stage
transmission = c(0.00002, 0.00001, 7.84e-06),
# Indicates that these are daily transmission rates
transmission_unit = 0,
# Indicates that all stages in the first compartment, S, can be infected
transmission_mask = c(1, 1, 1, 0, 0, 0, 0, 0, 0),
recovery = c(0.05714286, 0.06, 0.1),
recovery_unit = 0,
# Indicates that all stages in the second compartment, I, can recover
recovery_mask = c(0, 0, 0, 1, 1, 1, 0, 0, 0),
season_functions = list(sir_model_summer, NULL),
dispersal = list(disperser),
simulation_order = list(c("transition", "season_functions", "results"),
c("dispersal", "results")),
verbose = F
)
model_inputs$is_complete()
model_inputs$is_consistent()
```
The core simulation engine of `epizootic` is the function `disease_simulator`, which simulates spatially explicit disease dynamics in populations. Here I show the results
from the non-breeding season in the tenth year of the simulation.
```{r disease_simulator}
results <- disease_simulator(model_inputs)
results$abundance_segments$stage_3_compartment_1[,10,2] |>
example_region$raster_from_values() |>
raster::plot(main = "Susceptible Adults")
results$abundance_segments$stage_3_compartment_2[,10,2] |>
example_region$raster_from_values() |>
raster::plot(main = "Infected Adults")
```