---
title: "Week 2: Covid Testing"
author: " "
date: "16/09/2020"
output:
  pdf_document: default
  html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
```

## R Markdown

These calculations illustrate the role of Covid prevalance in determining how useful population-wide testing is. The definition of conditional probability gives:
$$
\text{Pr}(C+ \mid T+) = \frac{\text{Pr}(T+ \mid C+)\text{Pr}(C+)}{\text{Pr}(T+ \mid C+)\text{Pr}(C+) + \text{Pr}(T+ \mid C-)\text{Pr}(C-)}
$$
```{r Conditional Probability 1}
C19plus <- function(tp,fp,prev){
tp*prev/(tp*prev + fp*(1-prev))
}

```
While we're at it,
```{r Conditional Probability 2}
C19minus <- function(tn,fn,prev){
tn*(1-prev)/(tn*(1-prev) + fn*prev)}
```

Prevalence is hard to pin down, but the following shows the probabilty that one has covid, given a positive test, for a range of prevalence values.

```{r calculations}
options(scipen=100) # disables scientific notation for prevvals
prevvals <- c(.0001, .001, .005, .01, .05, .1)
ppv <- C19plus(.8,.01,prevvals)
mydf <- data.frame(cbind(ppv,prevvals))
mydf


```
The probability of having a disease, given a positive test, is called the positive predictive value of the test. It depends quite strongly on the prevalence of the disease. Estimates of Covid prevalence in the UK are hard to come by, but [this web site](https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/conditionsanddiseases/bulletins/coronaviruscovid19infectionsurveypilot/11september2020#:~:text=An%20estimated%2039%2C700%20people%20(95,1%2C900%20to%201%20in%201%2C000).)  estimates that during the most recent week (Aug 30 - Sep 5), the prevalence was estimated at around 1 in 1,400 people, i.e. 0.0007. The World Health Organization's COVID-19 Dashboard reports 374,000 confirmed cases in the UK since the beginning of the epidemic; the population of the UK is 66.6 million, so this gives an estimated of 0.005  (of current or resolved covid, which is a bit different). There would have been many asymptomatic cases, so perhaps the prevalence is as large as 1 or 2\%, but it seems unlikely at this time to be as large as 10\%. A widely criticized study published early in the epidemic estimated population prevalence in a county in California at $2.5 - 4$\%.
```{r plot, fig.width = 5,fig.height = 5}
prevvals2 <- seq(.0007,.1,length=50)
ppv2 <- C19plus(0.8, 0.01, prevvals2)
plot(prevvals2,ppv2,xlab="prevalence", main = "Prob(CV |  positive test)", ylab = "PPV", type="l", yaxp=c(0,1,10), ylim=c(0,1))
points(x=c(0.0007, 0.005, 0.01, 0.02), y=c(.053,.287,.447,.620), ,pch="X",col="red")
```
