An open API service indexing awesome lists of open source software.

https://github.com/viralemergence/epizootic

An R package for spatially explicit population models of disease transmission in wildlife
https://github.com/viralemergence/epizootic

software

Last synced: 4 months ago
JSON representation

An R package for spatially explicit population models of disease transmission in wildlife

Awesome Lists containing this project

README

          

---
output: github_document
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# epizootic

[![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)

`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")
```