---
title: Reproductive Number - Practice
output:
  html_document:
    theme: null
    highlight: null
    css: ../styles/handelshiny.css
    fig_caption: true
    mathjax: default 
    keep_md: false
    includes:
      #in_header: in_header.txt
      before_body: ../styles/before_shiny_body.txt
      after_body: ../styles/after_shiny_body.txt 
bibliography: ../media/references.bib
---


##Overview {#shinytab1}
This app lets you explore the reproductive number in some detail. Read about the model in the "Model" tab. Then do the tasks described in the "What to do" tab.


##The Model {#shinytab2}

###Model Overview
For this app, we'll use the basic compartmental SIR model. We allow for 3 different stages/compartments: 

* **S** - uninfected and susceptible individuals 
* **I** - infected and infectious individuals (note that these terms are often used interchangably, 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, there are processes that increase the number of individuals in a given compartment/stage, and processes that lead to a reduction. Those processes are sometimes called in-flows and out-flows. 

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

* Susceptible individuals (S) can become infected by infectious individuals (I) at some rate (which is usually labeled b or $\beta$). 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 (often labeled as $\gamma$).
* Recovered individuals can loose immunity at rate $w$ and return back to the susceptible compartment. 
* Natural births (into the S compartment at rate $\lambda$) and deaths (from all compartments at rate $n$) are possible.
* The model allows a fraction of individuals to be vaccinated before the outbreak. Those individuals are moved into the R compartment prior to the start of the outbreak.



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

![Model Figure](../media/R0modelfigure.png)

$$S_{v} = (1-ef)S_0$$
$$R_0 = efS_{v}$$
$$\dot S =\lambda -\beta SI - nS + wR$$ 
$$\dot I = \beta S I - \gamma I - nI$$
$$\dot R = \gamma I - nR - wR$$

Here, $S_0$ is the initial population of susceptibles, and $S_{v}$ is the susceptible population after vaccination. Vaccinated individuals are moved to the $R$ compartment prior to the start of the outbreak.


##What to do {#shinytab3}

_The following tasks ask you to use various equations to compute the reproductive number. If you are not familiar with those, you need to do some background reading, e.g. the books listed in the ID introduction app or some of the papers listed in the _Further Resources_ section of this app._

###Task 1: 
* Set the simulation with a population size of 1000, 1 infected, simulation time 60 months, gamma=5, beta=0.01. Everything else at 0.
* Run the simulation, you should get an outbreak.
* Use the final size equation linking R~0~ and the fraction of susceptible hosts left at the end of the outbreak to compute the reproductive number. 

###Task 2: 
* Use the equation that expresses R~0~ as function of model parameters for the simple SIR model. Using the values of the model parameters, compute R~0~ that way. Check that it agrees with what you found in task #1.

###Task 3: 
* Double the value of beta. Before you run the simulation, what do you expect to see and what do you expect to get for R~0~?
* Run the simulation and compute R~0~ using the final outbreak size to test your expectations. 

###Task 4: 
* Now, double the rate of recovery. Think about your expectations for R~0~ and the dynamics.
* Run the simulation to check your expectations. Use the final outbreak size to compute R~0~.

###Task 5: 
* Set the model parameters back to those given in task #1.
* Another way to estimate R~0~ is to determine the rate of increase in infected hosts at the beginning of the outbreak. During the initial phase, new infections increase exponentially according to $I(t)=I_0 e^{gt}$, with _g_ being the rate of growth. Usually, for any real outbreak, you do not know the number of infected at the start, I~0~, or the exact time the outbreak starts. It is still possible to estimate _g_ by obtaining two values of _I_ at two time points during that initial growth rate, i.e. _I~1~_ at time _t~1~_ and _I~2~_ at time _t~2~_. With those quantities, one can compute the exponential growth rate, _g_. Figure out how to determine those values and use them to compute _g_. Hint: play with the maximum simulation duration such that the simulation stops at 2 different times during the initial exponential growth phase.
* For this model, the growth rate and R~0~ are related through _R~0~ = 1+gD_, where _D_ is the average duration of the infectious period (i.e. the inverse of the recovery rate). Use this to determine R~0~. You should get essentially the same answer (up to some rounding errors) as for task #1.
* Note that the choice of _t~1~_ and _t~2~_ can influence the results. Earlier times are better since once the number of susceptibles starts to drop markedly, the growth of infected slows down and is not truly exponential anymore.


###Task 6: 
* What is the value of R at the time the outbreak peaks? (It's only called R~0~ at the beginning for a fully susceptible population). Explain how you can find that value for R, both using intuitive reasoning and using the equation for R~0~ given above (R~0~ = 1+rD). Note that at this R value, the outbreak wanes, but people still get infected. What R value would you need to completely halt any further infections?

###Task 7: 
* What would happen if a new ID came along that had an R~0~ value that was the same as the one you just determined in the previous question, namely the value of R at the peak of an outbreak? Test this with the simulation. Choose parameter values for which you get the right R~0~ and observe what the model produces.


###Task 8: 
* R~0~ quantifies the level of transmissibility of an ID, which determines how many people will become infected or what level of intervention is needed to stop/prevent an outbreak. However, it is important to be aware that R~0~ says nothing about the timing/dynamics of the outbreak. Set parameter values as in #1. Run an outbreak, record the time of peak and duration of the outbreak (the latter is somewhat ill defined, so just come up with a rough number). Then increase the infectious duration by a factor of 4 (rate reduced by factor of 4) and adjust the infectiousness-level such that you get the same R~0~ as before. Run again and compare the results with respect to total outbreak size and timing of outbreak.    


###Task 9: 
* One usually obtains R~0~ from the data/literature and uses it to pick values for the transmission parameter. Let's try that. Go online and find (approximate) values for the duration of the infectious period and for R~0~ for SARS. Keep the population size at 1000. Use those values to compute the transmission parameter, $\beta$. Run an outbreak, 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 10: 
* Knowing R is important for control strategies, e.g. for vaccine campaigns. You learned at what value of R an outbreak switches from growth to decline (often called the threshold value) and that for this R value, outbreaks don't take off. Let's say you have an ID that enters a new population where everyone is susceptible. That ID has R~0~=4. Would you expect to see an outbreak? Why? Now let's assume that we protected half the population through a (100% effective) vaccine. What is the new value for R, i.e. how many people are being infected on average by an infected person after we vaccinated? Is that new value of R low enough to prevent the outbreak? How low does R need to be to prevent an outbreak? What fraction of the population would you need to protect/vaccinate to achieve such an R? 



###Task 11: 
* Let's test the vaccination idea with the computer simulation. Set model parameters such that you get an R~0~=4. Given what you learned above, figuring out how to do that should not be hard. If you are not sure how to do it, review the first few tasks above.
* Run the simulation for 0% vaccination coverage to confirm things happen as you expect. Check if the final size of the susceptible/infected population is what it should be and returns (approximately) an R~0~=4 when you use the final size equation to compute it. 
* Now repeat for 50% vaccination coverage (at 100% vaccine efficacy). What is the value of the effective _R_ after vaccination? Run a simulation and check to make sure that is what you get. 
* Finally, run the simulation at the vaccination level you determined above to be enough to prevent an outbreak. Make sure your expectations and simulation results agree.


###Task 12: 
* Most vaccines are not perfect. What fraction of the population would you need to vaccinate for the scenario above if the vaccine efficacy/effectiveness was 75%? What if it was 60%? Confirm with the simulations.


###Task 13: 
* So far, we looked at an individual outbreak. Now we introduce births and deaths. For our model, we use a constant birth rate $\lambda$, and a fixed per-capita death rate $\mu$.  It is possible to have population dynamics (births, deaths) in the absence of an ID. It is useful to first figure out what's going on there before you introduce the disease dynamics. 
* Choose various values for the birthrate and lifespan, run the model and see what the population does in the absence of an infectious disease. Can you figure out the equation that relates births, deaths and steady state level for our model? Test it with the simulation.

###Task 14: 
* 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 an average life-span of 41 years, 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, gamma=5, beta=0.015. No vaccine.
* Run the simulation, make sure you reach a steady state. Compare the steady state values for S, I and R from the simulation with those predicted from the SIR steady state equations.

###Task 15: 
* Use the fraction of susceptibles remaining at steady state to compute R~0~ as described in the lecture notes. 
* Double the value of $\gamma$, 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 16:
* Keep exploring. 





##Answers 
They are currently not shown anywhere. The answers below are also not updated and are likely not in sync with the questions/tasks above.

__Those answers correspond to an old set of tasks and might not be up-to-date!__

###Answer 1: 
Fraction of susceptibles left at end of outbreak are about Sf=0.2, which leads to an R0=log(Sf)/(Sf-1)=2, same (up to some rounding errors) one gets from the R0=beta*S0/gamma equation.

###Answer 2: 
Double beta gives double R0, bigger outbreak.

###Answer 3: 
Double rate of recovery brings R0 back down. Faster recovery means fewer chances for an infected to infect other, therefore lower R0.

###Answer 4: 
Growth rate can be read off from values of I(t) and given as r=(log(It2)-log(It1))/(t2-t1), where It1 and It2 are number of infected at times t2 and t1. 

###Answer 5: 
The peak is when the outbreak goes from growth (R>1) to decline (R<1), just at the peak we have R=1. At the peak, there is no growth, so r=0, and the equation R=1+rD becomes R=1+0D=1. To prevent any further infections (not only have the outbreak wane, but stop instantaneously) would require an R=0.

###Answer 6: 
R~0~=1 means no outbreak, we need R0>1 for an outbreak. If R0 is only slightly above 1, you can see a very slow and small outbreak.

###Answer 7: 
In the model, we describe births and deaths by a constant birth rate lambda and a per-capita death rate $\mu=1/Y$, where Y is the average lifespan. To get a steady state in the absence of the disease, we need births and deaths to balance each other, i.e. $\lambda=\mu*S$, or $S=\lambda/ \mu= \lambda*Y$. In the simulation, no matter what number of susceptibles you start with, they will eventually settle down at a level given by lambda/mu.

###Answer 8: 
At steady state, we have no changes in the sizes of the S, I, R compartments. Therefore our equations become 0=lambda-mu*S-beta*S*I, 0=beta*S*I-gamma*I-mu*I, 0=gamma*I-mu*R. Those equations can be solved for  the steady state values Ss, Is and Rs as functions of the model parameters. One finds Ss=(gamma+mu)/beta, Is= lambda/(gamma+mu)- mu/beta, Rs=gamma/mu*Is . For given choices of the parameters, one can compute the values for these steady states. They agree with the values one gets from the simulation at steady state.

###Answer 9: 
R0=1/Ss=1/(0.5)=2, as expected. Double gamma gives R0=1, no SS with disease. Changes in birth and death don't make a difference.

###Answer 10: 
Same R0, so outbreak size is the same (same total number of people become infected), but the dynamics is different. With a longer infectious duration and a lower level of infectiousness, we get a slower outbreak. Could for instance be SARS versus HIV: Similar R0, but SARS is shorter and more infectious, HIV longer and less infectious. 

###Answer 11: 
Estimates for R0 for SARS are around 3-5. If you have R0, you can use the equation for R0, solve for beta to get beta=R0*gamma/S0. So if you know R0, duration of infectious period and size of your population, you can get the rate of transmission.

###Answer 12: 
R=4 is >1 so we get an outbreak. If we protected half the population, we would cut down R=R0*(1-p)=4*(1-0.5)=2. Would still give an outbreak, though a smaller one. We need to get R<=1 to not get an outbreak. For that, we need to protect 75% (or a bit more) of the population to get R=4*(1-0.75)=1.

###Answer 13: 
As it should be, simulations agree with theory (up to small rounding/numerical differences).


##Further Information {#shinytab4}
* For this app, the underlying function running the simulation is called `simulate_reproductivenumber.R`. You can call this function directly, without going through the shiny app. Check the help file for the function for more information. 
* If you call the function directly, you need to process the results returned from this function and produce useful output (such as a plot) yourself. 
* You could also modify this R function for your own purposes - though that requires R coding knowledge.
* For some information on the reproductive number, see e.g. [@fine11, @heffernan05a, @roberts07, @wallinga07]

### References



