knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
The goal of package sirstochastic is to run simulations of single and multiple iterations of the SIR stochastic model, compartmental and individual.
You can install the released version of sirstochastic from CRAN with:
install.packages("sirstochastic") install.packages("ggplot2")
And the development version from GitHub with:
# install.packages("devtools") devtools::install_github("reside-ic/sirstochastic")
The equations defining the model are:
and these are converted to the discrete stochastic model by imagining that in a small period of time dt
the chance of an event happening follows a Bernoulli trial. In a discrete time step of size dt
we model the number of infections (i.e., the number of individuals who move from S
to I
) as a binomial draw with n = S
and p = beta * I / N * dt
) and the number of recoveries (movements from I
to R
) as a binomial draw with n = I
and p = sigma * dt
. Note, N = S + I + R.
This is a basic example which shows you how to solve a common problem. The model is run once for 10,000 iterations with a time step of 0.01.
options(warn = - 1) pars <- sirstochastic::get_parameters() end_time <- 100 df <- sirstochastic::compartmental_sirmodel(end_time, pars) sirstochastic:::displaythemodel(df)
This time the model is run a 100 times for 10,000 iterations with a time step of 0.01 using the same code.
options(warn = - 1) pars <- sirstochastic::get_parameters() end_time <- 100 df <- lapply(seq_len(100), function(simulation) sirstochastic::compartmental_sirmodel(end_time, pars)) sirstochastic:::displaythemodel(df)
library('remotes') install_github('mrc-ide/individual') library(individual) library(ggplot2) library(reshape2) options(warn = - 1) pars <- sirstochastic::get_parameters() population <- pars$N NI <- pars$I0 NR <- 2 pops <- population - NI - NR timestep <- pars$num/pars$dt S <- State$new('S', pops) I <- State$new('I', NI) R <- State$new('R', NR) human <- Individual$new('human', list(S, I, R)) processes <- list( sirstochastic::individual_S_to_I(S, I, human, pars), sirstochastic::individual_I_to_R(I, R, human, pars), sirstochastic::individual_R_to_S(S, R, human, pars), sirstochastic::render_state_sizes(S, I, R, human) ) output <- simulate(human, processes, timestep) df <- data.frame(S = output$susceptable_counts, I = output$infected_counts, R = output$recovered_counts, time = output$time, type = "Individual", legend = "Individual", stringsAsFactors = FALSE) sirstochastic:::displaythemodel(df)
MIT © Imperial College of Science, Technology and Medicine
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.