---
title: Stochastic SEIR Model
output:
  html_document:
    theme: null
    highlight: null
    fig_retina: null
    fig_caption: true
    mathjax: default 
    keep_md: false
bibliography: references.bib
---



```{r, echo = FALSE}
#this code loads the settings file for the current app so we can automatically 
#list the functions in the further information section
currentrmdfile = knitr::current_input() 
currentappinfo = gsub("_documentation.Rmd" ,"_settings.R",currentrmdfile)
source(currentappinfo)
```


## Overview {#shinytab1}
This app lets you explore a stochastic SEIR model. Read about the model in the "Model" tab. Then do the tasks described in the "What to do" tab.

### Learning Objectives
* Obtain further familiarity with stochastic compartmental models

## The Model {#shinytab2}

### Model Overview

This model tracks susceptibles, exposed/pre-symptomatic, infected/symptomatic and recovered hosts. 
The following compartments are included:  

* **S** - uninfected and susceptible individuals 
* **E** - individuals who are infected and do not yet show symptoms. Those individuals can be infectious.
* **I** - individuals who are infected and show symptoms. Those individuals are likely infectious, but the model allows to adjust this, including no infectiousness.
* **R** - recovered/removed individuals. Those individuals have recovered and are immune. They can lose their immunity.


The included processes/mechanisms are the following:

* Susceptible individuals (S) can become infected by pre-symptomatic/exposed (E) or symptomatic (I) hosts at rates _b~E~_ and _b~I~_. 
* All infected hosts first enter the exposed/presymptomatic stage and leave the stage at rate _g~E~_, the inverse of which is the average time spent in this stage. 
* All infected hosts go from the presymptomatic to the symptomatic stage and leave the stage at rate _g~I~_. 
* Recovered individuals are initially immune to reinfection. They can lose their immunity at rate _w_ and return to the susceptible compartment. 
* New susceptible hosts enter the system (are born) at fixed rate _m_. Natural death occurs at rate _n_.



### Model Implementation
The flow diagram for the model implemented in this app is:


```{r stochasticseirmodel,  fig.cap='Flow diagram for this model. ',  echo=FALSE}
knitr::include_graphics("../media/stochasticSEIRmodel.png")
```



Note that this model is _not_ an ordinary differential equation model. It is instead its stochastic equivalent.
We can specify the model by writing down every possible transition/event/reaction that can occur and their propensities (the propensity multiplied with the
time step gives the probability that a given event/transition occurs). For our model these are the following:

Event type              |  Transitions        |   Propensity      |
----------              |  -----------        |   ----------      |
Infection               |  S => S-1, E => E+1 |   S(b~E~E+b~I~I)  |
Progression to Symptoms |  E => E-1, I => I+1 |   g~E~E           |
Recovery                |  I => I-1, R => R+1 |   g~I~I           |
Waning of Immunity      |  R => R-1, S => S+1 |   wR          |
Births                  |  S => S+1           |   m          |
Death of susceptible    |  S => S-1           |   nS          | 
Death of exposed        |  E => E-1           |   nE          |
Death of symptomatic    |  I => I-1           |   nI          |
Death of recovered      |  R => R-1           |   nR          |




## What to do {#shinytab3}

**The tasks below are described in a way that assumes everything is in units of MONTHS (rate parameters, therefore, have units of inverse months). If any quantity is not given in those units, you need to convert it first (e.g. if it says a year, you need to convert it to 12 months).**

_Some of the simulations might take a few seconds to run. Be patient._

### Task 1: 
* Set the model parameters such that it corresponds to the following setting: 
    * 1000 susceptible and 1 initially symptomatic host.
    * Simulation duration approximately 5 years.
    * Assume that only symptomatic individuals transmit, at rate 0.002. 
    * Assume that the duration of the symptomatic period is 1 month long, the duration of the presymptomatic period is half a month long. 
    * Assume immunity does not wane and that there are no births and deaths.
    * Set the number of simulations to 1.
    
* With parameters set to correspond to the scenario just described, run the simulation.
* If you rerun the simulation without changing anything, what do you expect to see? Test your expectations by running the simulation several times.
* To speed things up, set the number of simulations to 20, run all 20.

### Task 2: 
* In the previous task, you should have seen that occasionally, no outbreak occurs (because the infected person recovers/dies before they can infect others). Explore how the initial number of infected influences the chance of (not) getting outbreaks. Set the initial number of infected to 5, everything as above. 
* Run 20 outbreaks at a time, repeat a few times. Observe the number of times you do (not) get an outbreak.
* Repeat with the initial number of infected at 2,3, and 10.


### Task 3: 
* Set everything as in task 1, with 1 initial infected. Now explore how the rate of transmission (and with it the reproductive number) affects outbreaks.
* Try values for the transmission rate of 0.001, 0.0015 and 0.004. Compare the results.


### Task 4: 
* Set susceptibles to 400, infected to 100, the rate of immunity loss to 0.2, the rate of symptomatic transmission to 0.005, and no presymptomatic transmission. 
* Set the duration of the symptomatic period to 1 month and the duration of the presymptomatic period to half a month. 
* Simulation time about 5 years.
* No births and deaths.
* Set the number of simulations to 1. Run the model several times.
* You should see the stochastic version of a steady state, namely S-E-I-R values that fluctuate around some level.
    
### Task 5: 
* Record the fraction of susceptibles at the end of the simulation for the setting you just had. They will change.
* Now change the number of simulations to 5, 10, 20 (be patient). See how the average number of susceptibles at the end of the simulation settles down to some number. It will still fluctuate a bit, but as you take the average of more simulations, results become more stable (deterministic). 

### Task 6: 
* Set simulation number to 5, rest as before.
* Run the model a few times for around 50 years (be patient). Observe if/when you get extinctions.
* Now change the rate of transmission to 0.003, again run multiple simulations, then repeat with transmission at 0.0025.
* Contemplate why you see what you see.
  
  
### Task 7: 
* Keep exploring. Turn on births and deaths. Play around with anything else you can think of.  
  
  

## Further Information {#shinytab4}
* This app (and all others) are structured such that the Shiny part (the graphical interface you see and the server-side function that goes with it) calls an underlying R script (or several) which runs the simulation for the model of interest and returns the results.
* For this app, the underlying function(s) running the simulation are called ``r appsettings$simfunction``. You can call them directly, without going through the shiny app. Use the `help()` command for more information on how to use the functions directly. If you go that route, you need to use the results returned from this function and produce useful output (such as a plot) yourself. 
* You can also download all simulator functions and modify them for your own purposes.  Of course to modify these functions, you'll need to do some coding.
* For examples on using the simulators directly and how to modify them, read the package vignette by typing `vignette('DSAIDE')` into the R console.
* For some further reading concerning stochastic dynamics of ID, see e.g. [@lloyd-smith05a; @black66; @keeling97].

### References

