---
title: "Shiny R0"
output: 
  html_document:
    keep_md: yes
runtime: shiny
---

[![urhere.png](https://omega0.xyz/urhere.png)](https://omega0.xyz/omega8008/)


```{r libs, echo=FALSE, results='hide', message=FALSE}
library(tidyverse)
library(zoo)
library(shiny)

#"https://covid.ourworldindata.org/data/owid-covid-data.csv"
```
Data from:  

"https://covid.ourworldindata.org/data/owid-covid-data.csv"

```{r owid_countries, echo=FALSE, results='hide', message=FALSE}
df <- read_csv("https://covid.ourworldindata.org/data/owid-covid-data.csv")

sum_ahead <- function(v,lead=1) {
    if (lead > 0) {
        df <- tibble(V1=v)
        df$V2 <- rollsum(lead(df$V1), lead, align="left", fill = NA) + df$V1
        V3 <- df[['V2']]
        df1 <- df %>% filter(is.na(V2)) %>% mutate(V2=rev(cumsum(rev(V1))))
        V3[is.na(V3)] <- df1$V2
    }
    else V3 <- v
    V3
}

get_sir <- function(tib,newI,newD,lead=0) {
   nI <- tib[[newI]]
   nD <- tib[[newD]]
   R <- cumsum(nI+nD)
   n_days <- length(R)
   I <- sum_ahead(nI,lead)
   N_tot <- R[n_days] + I[n_days]
   S <- N_tot - I - R
   R0 <- 1 + diff(c(0,I))/diff(c(1,R))
   sir <- tibble(t=as.Date(tib[[1]],format="%Y-%m-%d"),
                 S=S, I=I, R=R, R0=R0)
   sir
}

pR0 <- function(lead=0,dof=15,tib=rt,country) {
  sir <- get_sir(tib,'newI','newD',lead)
  R0 <- sir$R0
  R0[R0==-Inf | R0==Inf | is.nan(R0)] <- 1   # regularize the Inf
  n_days <- length(R0)
  first_date <- sir$t[1]
  plot(R0,type="l",xlab=paste("days after ",first_date),
        main=paste(country,": R0, lead=",lead," smoothed with df=",dof))
  abline(h=1,col="red")
  lines(smooth.spline(x=1:n_days,y=R0,df=dof),col="blue")
}

plot_R0_country <- function(country='Chile', lead=5, dof=15) {
    df1 <- df %>% filter(location==country)
    rt <- tibble(t=df1[['date']], newI=df1[['new_cases']],
                 newD=df1[['new_deaths']])
    pR0(lead,dof,rt,country)
}
```
Get R0 by country,

```{r plotcountry, echo=FALSE}
area <- function(x,y){
    n <- length(x)
    yp <- (y+lead(y))[1:(n-1)]
    sum(diff(x)*yp)/2
}

mean_area <- function(x,y) area(x,y)/(length(x)-1)

area_of <- function(country,lead=5,dof=15) {
    df1 <- df %>% filter(location==country)
    rt <- tibble(t=df1[['date']], newI=df1[['new_cases']],
                 newD=df1[['new_deaths']])
	sir <- get_sir(rt,'newI','newD',lead)
	R0 <- sir$R0
	R0[R0==-Inf | R0==Inf | is.nan(R0)] <- 1   # regularize the Inf
	n_days <- length(R0)
#	ss <- smooth.spline(x=1:n_days,y=R0,df=dof)
	ss <- smooth.spline(x=R0,df=dof,tol=1e-6)
	mean_area(ss$x,ss$y)
}	

textInput(
	'country', label = 'Country', value="Chile"
)

renderPlot({
	plot_R0_country(input$country)
	}
)

renderText({
   paste(input$country,": Mean Area Under blue Curve= ",
     round(area_of(input$country),2))
   }
)
# to run:   rmarkdown::run("owid.Rmd")
```


## Mean AUC of smooth R0

This is just a robust overall average of R0.  
Here is the list of all the countries ranked by it:

```{r areas, echo=FALSE}
area <- function(x,y){
    n <- length(x)
    yp <- (y+lead(y))[1:(n-1)]
    sum(diff(x)*yp)/2
}

mean_area <- function(x,y) area(x,y)/(length(x)-1)

area_of <- function(country,lead=5,dof=15) {
    df1 <- df %>% filter(location==country)
    rt <- tibble(t=df1[['date']], newI=df1[['new_cases']],
                 newD=df1[['new_deaths']])
	sir <- get_sir(rt,'newI','newD',lead)
	R0 <- sir$R0
	R0[R0==-Inf | R0==Inf | is.nan(R0)] <- 1   # regularize the Inf
	n_days <- length(R0)
	ss <- smooth.spline(x=1:n_days,y=R0,df=dof)
	mean_area(ss$x,ss$y)
}	

countries <- df[['location']]
unique_countries <- unique(countries)
AUCall <- map(unique_countries,area_of)
auc <- unlist(AUCall)
auc_sorted_with_index  <- sort(auc,index.return=TRUE)
aucix <- auc_sorted_with_index$ix
auc_ordered <- auc_sorted_with_index$x
countries_ordered <- unique_countries[aucix]
n_countries <- length(unique_countries)
# worst 40
#countries_ordered[(n_countries-40):n_countries]

# best 40
#countries_ordered[1:40]

areas <- tibble(rev_rank=rev(1:length(unique_countries)),
                country=countries_ordered,
                mean_AUR0=auc_ordered)
options(tibble.print_min=Inf)  # print it all not just 10 rows
areas
```
## Here is the code

(https://omega0.xyz/owid1.Rmd)

