---
title: Reproductive Number 2 - Practice
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 covers more aspects of the reproductive number concept. Read about the model in the "Model" tab. Then do the tasks described in the "What to do" tab. If you are not familiar with the reproductive number, you should go through the 'Reproductive Number 1' app first.

### Learning Objectives
* Know how to estimate the reproductive number from steady state levels of disease.
* Understand how the reproductive number can be used to parameterize models.
 

## The Model {#shinytab2}

### Model Overview
For this app, we'll use a basic compartmental SIR model that also includes births, deaths and waning immunity. We allow for 3 different stages/compartments: 

* **S** - uninfected and susceptible individuals 
* **I** - infected and infectious individuals (note that these terms are often used interchangeably, but technically we are talking about someone who is infected **and** is infectious, i.e. can infect others)
* **R** - recovered/removed individuals. Those are individuals that do not further participate, either because they are now immune or because they died.


In addition to specifying the *compartments* of a model, we need to specify the dynamics determining the changes for each compartment. Broadly speaking, some processes increase the number of individuals in a given compartment/stage and other processes that lead to a reduction. Those processes are sometimes called inflows and outflows. 

For our system, we specify the following processes/flows: 

* Susceptible individuals (S) can become infected by infectious individuals (I) at some rate, _b_. This leads to the susceptible individual leaving the S compartment and entering the I compartment.   
* Infected individuals recover and enter the recovered (R) compartment at some rate, _g_.
* Recovered individuals can lose immunity at rate _w_ and return to the susceptible compartment. 
* Natural births (into the S compartment at rate _m_) and deaths (from all compartments at rate _n_) are possible.



### Model Implementation
The flow diagram and the set of equations which are used to implement this model are as follows:



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


$$\dot S =m - b SI - nS + wR$$ 
$$\dot I = \ S I - g I - nI$$
$$\dot R = g I - nR - wR$$



## 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).**



### Task 1 
* In 'Reproductive Number 1', the focus was on a single outbreak. But the reproductive number concept is also useful in situations where a disease is endemic. 
* As you learned in the 'Patterns of ID' app (if not, go do it before this one), to achieve an endemic state, we need replenishment of susceptibles through for instance births or waning immunity. We'll start with natural births and deaths. 
* If births (and deaths) are present, you will be able to get multiple outbreaks and endemic states. Set the parameter values such that your hosts have approximately an average lifespan of 41 years. Make sure to convert to months and convert to a rate. Set birth rate such that the population is steady at 1000 in the absence of the ID. Set simulation time to around 500 years, 1 infected, _g_=5, _b_=0.015. 
* Run the simulation, make sure you reach a steady state. Compare the steady state values for S (and optional I and R) from the simulation with those predicted from the SIR steady state equations (see "Patterns of ID" app).


### Task 2 
* Figure out what the reproductive number is at the endemic steady state. To that end, recall its definition and determine what has to be true if the number of infected neither increases nor decreases. 
* You've learned that the equation for the reproductive number for this simple model is R=bS/g. If the reproductive number at steady state is _R~s~_ and we have _S~f~_ susceptibles, then with _S~0~_ susceptibles at the start, what is the basic reproductive number? First do it by simply reasoning from the values of _R~s~_ and _S~f~_ to the values of _R~0~_ and _S~0~_. You can then confirm this by sticking in all parameter values into the _R~0~_ equation.


### Task 3 
* Double the value of _g_, run the simulation and again compute R~0~ using the steady state result. 
* Next double the values for both natural birth and death rates and repeat. 



### Task 4 
* One usually obtains R~0~ from the data/literature and uses it to pick values for the transmission parameter, _b_, which is otherwise very hard to estimate. Let's try that. Go online and find (approximate) values for the duration of the infectious period and R~0~ for SARS. Keep the population size at 1000. Use those values to compute the transmission parameter, _b_. Run a single outbreak (no births and deaths), see if things work out right. Specifically, using the equation involving the final number of susceptibles left should give you an R~0~ that roughly agrees with the one you started out with.

### Task 5
* Let's repeat the same for a disease that causes an endemic state, tuberculosis (TB). Go online and find estimates for the basic reproductive number of TB. Note that TB is somewhat special: An infected and infectious person (someone with TB disease) might infect many others, but only some of those will go on to develop disease and become infectious. For R~0~, we always need to go from infectious individual to another infectious individual.
* Also find an estimate for the duration of the infectious period for TB. Then use these quantities to determine parameters _b_ and _g_ and run a simulation with the same birth and death rates as in task 1.
* Check that the steady state levels of infected/infectious individuals matches with the R~0~ value you chose.

Note: The SIR model is not a good model for TB since for TB, the stage where individuals are infected but not yet infectious is long and important. So to really model TB, one would need to include such details. We are only using TB here for illustrative purposes. 


## 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 the equations mentioned in the tasks, see e.g. [@heffernan05a; @keeling08; @vynnycky10].
* For some information on the reproductive number, see e.g. [@fine11; @heffernan05a; @roberts07; @wallinga07].

### References



