---
title: Direct Transmission Modes - 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 allows you to explore a model with 2 different modes of direct transmission. Read about the model in the "Model" tab. Then do the tasks described in the "What to do" tab.


## The Model {#shinytab2}


### Model Overview

This is a simple SIR model with births and deaths and 2 types of direct transmission, density-dependent and frequency-dependent (based on the terminology in [@begon02] - see _Further Information_ tab for references).

This model has the following compartments:  

* **S** - uninfected and susceptible individuals 
* **I** - individuals who are infected and infectious.
* **R** - recovered/removed individuals.

The processes being modeled are:

* Susceptible individuals can become infected by two types of direct transmission from infected hosts, further described below.
* Infected hosts recover after some time (specified by the rate _g_). 
* Recovered individuals are initially immune to reinfection. They can lose their immunity at rate _w_ and return to the susceptible compartment. 
* New susceptibles enter (are born) at a rate _m_. From each compartment, hosts "leave" after some time (i.e. they die) at rate _n_. The inverse of this rate is the average lifespan of a host. 



The force of infection, denoted by _f_ here, depends on the transmission model. For density-dependent transmission, it is given by _f = b~d~ I / A_, where _A_ is the area of the scenario under consideration (e.g. a city). For frequency-dependent transmission, we have _f = b~f~ I / N_, where _N_ is the size of the population, _N=S+I+R_. 



### Model Implementation
The flow diagram and the set of ordinary differential equations (ODE) 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/directtransmissionmodel.png")
```



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

with _f = b~d~ I / A_ for density-dependent transmission and _f = b~f~ I / N_ for frequency-dependent transmission.


### Model Comments
Note that quite often, we assume that the area is constant and do not include it in the model or even talk about it. Often, the population size is also assumed to be constant. In that case, the 2 types of transmission models behave the same. But as soon as area or population size change, the model results differ. You can explore the effect of this relationship using this simulation.

## 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: 
* Set the model parameters such that it corresponds to the following setting: 
    * A starting population of 1000 individuals, 999 susceptible and 1 initially infected host.
    * Simulation duration, _tmax_ approximately 10 years.
    * Consider density-dependent transmission, with a transmission rate of _b~d~_ = 0.005, and an area of size 2.
    * Assume that the duration of the infectious period is 15 days long (and that there are 30 days in a month). 
    * Turn off births, deaths and waning immunity for now.
* With parameters set to correspond to the scenario just described, run the simulation.
* Record the number and fraction of susceptible/infected/recovered remaining at the end of the outbreak. You should be left with 625 susceptibles.

### Task 2: 
* Now switch to frequency-dependent transmission, set _b~f~_ = 2.5. Keep everything else as before.
* Think about your expectations, run the simulation and compare your expectations with the results.
* Anything surprising happening? Do you understand why you see what you see?


### Task 3: 
* Let's assume we are now in a location with twice the number of people as before (_S~0~_ = 1999) , living in roughly the same area.
* Implement that in the simulation by changing population size, keep all other settings as above. 
* What do you expect to see for the frequency and density dependent scenarios? Run simulations and check. Pay attention to both the numbers and fractions of S/I/R individuals at the end of the outbreak.  

### Task 4: 
* If you double the population size as you just did, how do you need to adjust the area to obtain the same sized outbreak with regard to the fraction of people getting infected/remaining susceptible for density-dependent transmission?
* Try with the simulation and see if your expectation is correct.

### Task 5: 
* Keep exploring by trying different parameters and transmission settings and see how they influence results.
* You can also go beyond a single outbreak and turn on births/deaths (which can impact population size) or waning immunity.
* As you continue your exploration, think about real infectious diseases that might be approximated by either one of the transmission types, and what approximate choices for the model parameters would describe those IDs.




## 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 more on this topic, see e.g. [@begon02]. 

### References

