Skip to content

Commit

Permalink
Adding mixing models (need examples) (#16)
Browse files Browse the repository at this point in the history
* Adding mixing models (need examples)

* Updating readme with figures and list of models

* Adding entity to the model

* Adding new entity wrappers

* Starting off with entity vignette

* Working on the vignette

* Updating version of epiworld

* Updating README

* Refactoring how virus and tools are added

* Updating calling methods

* Finally passing

* Adding a note on the news file

* Adding defunct message

* Updating roxygen

* Updating news and minor updates to README.md

* Working on entity

* Correcting mixing vignette

* Adding the dist functions (starting to). Expected to fail [skip ci]

* Updating interface

* Updating version of acions

* Updating epiworld c++

* Updating epiworld (warnings)

* Removing dangling calls

* Final check errors

* Missed Rd file

* Updating reference

* Adding examples and cleaning docs.

* Updating
  • Loading branch information
gvegayon authored Aug 1, 2024
1 parent b4eb34d commit b1fd9a9
Show file tree
Hide file tree
Showing 108 changed files with 4,641 additions and 1,016 deletions.
15 changes: 15 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM rocker/r-base:latest

RUN \
echo 'options(repos=c(CRAN="https://cloud.r-project.org"))' >> ~/.Rprofile && \
Rscript --vanilla -e 'getOption("repos")'

# Adding Git
RUN apt-get update && apt-get install -y --no-install-recommends git

# Adding R packages
RUN \
wget https://github.com/jgm/pandoc/releases/download/3.2.1/pandoc-3.2.1-1-amd64.deb && \
dpkg -i pandoc-3.2.1-1-amd64.deb

RUN install2.r cpp11 rmarkdown roxygen2 tinytest data.table netplot
23 changes: 23 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/cpp
{
"name": "epiworldR",
"build": {
"dockerfile": "Dockerfile"
},

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "gcc -v",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
"remoteUser": "root"
}
4 changes: 2 additions & 2 deletions .github/workflows/r.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

Expand Down Expand Up @@ -69,7 +69,7 @@ jobs:
error-on: '"warning"'

# Upload the built package as an artifact
- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
if: ${{ matrix.config.os == 'ubuntu-latest' && matrix.config.r == 'release' }}
with:
name: ${{ matrix.config.os }}-pkg
Expand Down
3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"/usr/local/include",
"/usr/lib/R/site-library/cpp11/include",
"/usr/lib/R/site-library/Rcpp/include",
"/usr/share/R/include"
"/usr/share/R/include",
"inst/include/epiworld"
],
"intelliSenseMode": "linux-gcc-x64",
"compilerPath": "/usr/bin/gcc",
Expand Down
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: epiworldR
Type: Package
Title: Fast Agent-Based Epi Models
Version: 0.1-1
Date: 2024-04-07
Version: 0.3-2
Date: 2024-06-12
Authors@R: c(
person("Derek", "Meyer", role=c("aut","cre"),
email="[email protected]", comment = c(ORCID = "0009-0005-1350-6988")),
Expand All @@ -21,7 +21,7 @@ URL: https://github.com/UofUEpiBio/epiworldR,
https://uofuepibio.github.io/epiworldR-workshop/
BugReports: https://github.com/UofUEpiBio/epiworldR/issues
License: MIT + file LICENSE
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
LinkingTo: cpp11
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Capture the current value of the version of the package in DESCRIPTION
VERSION := $(shell grep Version DESCRIPTION | sed -e 's/Version: //')


build:
cd .. && R CMD build epiworldR

Expand Down
35 changes: 29 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Generated by roxygen2: do not edit by hand

S3method("[",epiworld_agents)
S3method("[",epiworld_entities)
S3method(add_tool,epiworld_model)
S3method(add_tool_n,epiworld_model)
S3method(add_virus,epiworld_model)
S3method(add_virus,epiworld_seir)
S3method(add_virus,epiworld_seirconn)
Expand All @@ -12,14 +12,10 @@ S3method(add_virus,epiworld_sir)
S3method(add_virus,epiworld_sirconn)
S3method(add_virus,epiworld_sird)
S3method(add_virus,epiworld_sirdconn)
S3method(add_virus_n,epiworld_model)
S3method(add_virus_n,epiworld_seir)
S3method(add_virus_n,epiworld_seirconn)
S3method(add_virus_n,epiworld_sir)
S3method(add_virus_n,epiworld_sirconn)
S3method(agents_from_edgelist,epiworld_model)
S3method(agents_smallworld,epiworld_model)
S3method(as.array,epiworld_hist_transition)
S3method(get_agents,epiworld_model)
S3method(get_hist_tool,epiworld_model)
S3method(get_hist_total,epiworld_model)
S3method(get_hist_transition_matrix,epiworld_model)
Expand Down Expand Up @@ -48,16 +44,23 @@ S3method(plot,epiworld_seir)
S3method(plot,epiworld_seirconn)
S3method(plot,epiworld_seird)
S3method(plot,epiworld_seirdconn)
S3method(plot,epiworld_seirmixing)
S3method(plot,epiworld_sir)
S3method(plot,epiworld_sirconn)
S3method(plot,epiworld_sird)
S3method(plot,epiworld_sirdconn)
S3method(plot,epiworld_sirmixing)
S3method(plot,epiworld_sis)
S3method(plot,epiworld_sisd)
S3method(plot,epiworld_surv)
S3method(plot_epi,epiworld_hist)
S3method(plot_epi,epiworld_hist_virus)
S3method(plot_epi,epiworld_model)
S3method(print,epiworld_agent)
S3method(print,epiworld_agents)
S3method(print,epiworld_agents_tools)
S3method(print,epiworld_entities)
S3method(print,epiworld_entity)
S3method(print,epiworld_globalevent)
S3method(print,epiworld_model)
S3method(print,epiworld_saver)
Expand All @@ -82,14 +85,17 @@ export(ModelSEIR)
export(ModelSEIRCONN)
export(ModelSEIRD)
export(ModelSEIRDCONN)
export(ModelSEIRMixing)
export(ModelSIR)
export(ModelSIRCONN)
export(ModelSIRD)
export(ModelSIRDCONN)
export(ModelSIRLogit)
export(ModelSIRMixing)
export(ModelSIS)
export(ModelSISD)
export(ModelSURV)
export(add_entity)
export(add_globalevent)
export(add_tool)
export(add_tool_agent)
Expand All @@ -101,10 +107,22 @@ export(agents_from_edgelist)
export(agents_smallworld)
export(change_state)
export(clone_model)
export(distribute_entity_randomly)
export(distribute_entity_to_set)
export(distribute_tool_randomly)
export(distribute_tool_to_set)
export(distribute_virus_randomly)
export(distribute_virus_set)
export(entity)
export(entity_add_agent)
export(entity_get_agents)
export(get_agents)
export(get_agents_data_ncols)
export(get_agents_states)
export(get_agents_tools)
export(get_entities)
export(get_entity_name)
export(get_entity_size)
export(get_generation_time)
export(get_hist_tool)
export(get_hist_total)
Expand Down Expand Up @@ -138,12 +156,14 @@ export(globalevent_tool_logit)
export(has_tool)
export(has_virus)
export(initial_states)
export(load_agents_entities_ties)
export(make_saver)
export(plot_generation_time)
export(plot_incidence)
export(plot_reproductive_number)
export(queuing_off)
export(queuing_on)
export(rm_entity)
export(rm_tool)
export(rm_virus)
export(run)
Expand All @@ -153,6 +173,9 @@ export(set_agents_data)
export(set_death_reduction)
export(set_death_reduction_fun)
export(set_death_reduction_ptr)
export(set_distribution_entity)
export(set_distribution_tool)
export(set_distribution_virus)
export(set_incubation)
export(set_incubation_fun)
export(set_incubation_ptr)
Expand Down
19 changes: 19 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# epiworldR 0.3-2 (dev)

* Starting version 0.3-0, `epiworldR` is versioned using the same version as the C++ library, `epiworld`.

* Adds the new mixing models `ModelSIRMixing` and `ModelSEIRMixing`.

* Ports the `Entity` class. Entities are used to group agents within a model.

* Refactors `add_tool`, `add_virus`, and `add_entity` simplifying syntax. Now,
these functions only receive the model and object. Prevalence is
specified in the object itself. `add_tool_n` and `add_virus_n` are now
deprecated.

* `globalaction_*` are now defunct. Use `globalevent_*` instead.

* New functions to specify how viruses, tools, and entities are distributed
among agents: `distribute_viruses`, `distribute_tools`, and `distribute_entities`.


# epiworldR 0.1-0`

* Force model to update agents' states when running a simulation.
Expand Down
4 changes: 2 additions & 2 deletions R/ModelSEIRCONN.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
#' plot(model_seirconn)
#'
#' # Adding the flu
#' flu <- virus("Flu", .9, 1/7)
#' add_virus(model_seirconn, flu, .001)
#' flu <- virus("Flu", .9, 1/7, prevalence = 0.001, as_proportion = TRUE)
#' add_virus(model_seirconn, flu)
#'
#' #' # Running and printing
#' run(model_seirconn, ndays = 100, seed = 1912)
Expand Down
8 changes: 6 additions & 2 deletions R/ModelSEIRDCONN.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@
#' plot(model_seirdconn)
#'
#' # Adding the flu
#' flu <- virus("Flu", prob_infecting = .3, recovery_rate = 1/7, prob_death = 0.001)
#' add_virus(model = model_seirdconn, virus = flu, proportion = .001)
#' flu <- virus(
#' "Flu", prob_infecting = .3, recovery_rate = 1/7,
#' prob_death = 0.001,
#' prevalence = 0.001, as_proportion = TRUE
#' )
#' add_virus(model = model_seirdconn, virus = flu)
#'
#' #' # Running and printing
#' run(model_seirdconn, ndays = 100, seed = 1912)
Expand Down
91 changes: 91 additions & 0 deletions R/ModelSEIRMixing.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#' Susceptible Exposed Infected Removed model (SEIR) with mixing
#' @param name String. Name of the virus
#' @param prevalence Double. Initial proportion of individuals with the virus.
#' @param contact_rate Numeric scalar. Average number of contacts per step.
#' @param transmission_rate Numeric scalar between 0 and 1. Probability of
#' transmission.
#' @param incubation_days Numeric scalar. Average number of days in the
#' incubation period.
#' @param recovery_rate Numeric scalar between 0 and 1. Probability of recovery.
#' @param x Object of class SIRCONN.
#' @param ... Currently ignore.
#' @param n Number of individuals in the population.
#' @param contact_matrix Matrix of contact rates between individuals.
#' @export
#' @family Models
#' @details
#' The `contact_matrix` is a matrix of contact rates between entities. The
#' matrix should be of size `n x n`, where `n` is the number of entities.
#' This is a row-stochastic matrix, i.e., the sum of each row should be 1.
#'
#' The [initial_states] function allows the user to set the initial state of the
#' model. In particular, the user can specify how many of the non-infected
#' agents have been removed at the beginning of the simulation.
#' @returns
#' - The `ModelSEIRMixing`function returns a model of class [epiworld_model].
#' @aliases epiworld_seirmixing
#'
#' @examples
#'
#' # Start off creating three entities.
#' # Individuals will be distribured randomly between the three.
#' e1 <- entity("Population 1", 3e3, as_proportion = FALSE)
#' e2 <- entity("Population 2", 3e3, as_proportion = FALSE)
#' e3 <- entity("Population 3", 3e3, as_proportion = FALSE)
#'
#' # Row-stochastic matrix (rowsums 1)
#' cmatrix <- c(
#' c(0.9, 0.05, 0.05),
#' c(0.1, 0.8, 0.1),
#' c(0.1, 0.2, 0.7)
#' ) |> matrix(byrow = TRUE, nrow = 3)
#'
#' N <- 9e3
#'
#' flu_model <- ModelSEIRMixing(
#' name = "Flu",
#' n = N,
#' prevalence = 1 / N,
#' contact_rate = 20,
#' transmission_rate = 0.1,
#' recovery_rate = 1 / 7,
#' incubation_days = 7,
#' contact_matrix = cmatrix
#' )
#'
#' # Adding the entities to the model
#' flu_model |>
#' add_entity(e1) |>
#' add_entity(e2) |>
#' add_entity(e3)
#'
#' set.seed(331)
#' run(flu_model, ndays = 100)
#' summary(flu_model)
#' plot_incidence(flu_model)
#'
#' @seealso epiworld-methods
ModelSEIRMixing <- function(
name, n, prevalence, contact_rate, transmission_rate,
incubation_days, recovery_rate, contact_matrix
) {

structure(
ModelSEIRMixing_cpp(
name, n, prevalence, contact_rate,
transmission_rate, incubation_days,
recovery_rate, as.vector(contact_matrix)
),
class = c("epiworld_seirmixing", "epiworld_model")
)

}

#' @rdname ModelSEIRMixing
#' @export
#' @returns The `plot` function returns a plot of the SEIRMixing model of class
#' [epiworld_model].
#' @param main Title of the plot
plot.epiworld_seirmixing <- function(x, main = get_name(x), ...) { # col = NULL
plot_epi(x, main = main, ...)
}
Loading

0 comments on commit b1fd9a9

Please sign in to comment.