India
15,930,774
2,291,237
184,657
2021-04-21
---
title: "Static Covid Dashboard"
output:
flexdashboard::flex_dashboard:
storyboard: true
social: menu
source: embed
---
`r my_country <- "India"; my_country`
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(tidyverse)
library(ggplot2)
library(plotly)
library(lubridate)
library(flexdashboard)
country_selection <- c("US", "Poland", "India", "Mexico", "China", "United Kingdom", "France", "Canada")
get_data <- function() {
lnk_conf <- 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'
lnk_death <- 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv'
lnk_rec <- 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv'
copy_data <- function (theLink, theLabel) {
d1 <- read_csv(theLink)
d1 <- d1 %>% gather(key=`Date`,value=theLabel,-"Province/State", -"Country/Region", -"Lat", -"Long")
d1$Date <- ymd(as.Date(d1$Date, format="%m/%d/%Y"))
d1$`Province/State` <- ifelse(is.na(d1$`Province/State`), d1$`Country/Region`, d1$`Province/State`) # replace empty state names by country names
d1[complete.cases(d1),]
colnames(d1) <- c('Province', 'Country', 'Lat', 'Long', 'Date', theLabel)
d1
}
d_conf <- copy_data(lnk_conf, 'Confirmed')
d_recv <- copy_data(lnk_rec, 'Recovered')
d_deat <- copy_data(lnk_death, 'Deaths')
#max(d_conf$Date)
d1 <- left_join(d_conf, d_recv, by = c('Province', 'Country', 'Lat', 'Long', 'Date'))
d1 <- left_join(d1, d_deat, by = c('Province', 'Country', 'Lat', 'Long', 'Date'))
d1 %>% replace_na(list(Confirmed = 0, Recovered = 0, Deaths = 0)) %>%
mutate(Sick = Confirmed - Recovered - Deaths)
}
d <- get_data() %>%
filter(Country == my_country)
```
# `r d$Country[1]` Summary
```{r summary}
N <- nrow(d)
```
Column
------------------
### Confirmed
```{r}
d$Confirmed[N] %>%
formatC(format="f", big.mark=",", digits=0) %>%
valueBox(caption = "Confirmed", icon = "fa-ambulance", color = "#CC3333", href = NULL)
```
### Sick
```{r}
d$Sick[N] %>%
formatC(format="f", big.mark=",", digits=0) %>%
valueBox(caption = "Sick", icon = "fa-thumbs-down", color = "#009999", href = NULL)
```
Column
------------------
### Death
```{r}
d$Deaths[N] %>%
formatC(format="f", big.mark=",", digits=0) %>%
valueBox(caption = "Deaths", icon = "fa-skull-crossbones", color = "#660099", href = NULL)
```
### Last Update
```{r}
valueBox(d$Date[N], caption = "Date", icon = "calendar", color = "#CECECE", href = NULL)
```
Column
-------------------
### Sick Rate
```{r}
rate <- round(d$Sick[N] / d$Confirmed[N], 2)
gauge(rate, min = 0, max = 1, symbol = '%', gaugeSectors(
success = c(0, .39), warning = c(.40, .79), danger = c(.80, .100)
))
```
### Recovered Rate
```{r}
rate <- round(d$Recovered[N] / d$Confirmed[N], 2)
gauge(rate, min = 0, max = 1, symbol = '%', gaugeSectors(
success = c(.80, .100), warning = c(.40, .79), danger = c(0, .39)
))
```
# Sick {.storyboard}
### The daily increase in sick people is at an all time high and shows no signs of waning
```{r ggplotDaily}
ggplot(d, aes(x=Date, y=Sick)) +
geom_bar(stat = "Identity")
```
```{r expmod}
# Selection parameters:
dte_start <- as_date("2021-01-01")
f_nbrDays <- 14
# prepare the data
dd <- d %>% filter(Date > dte_start)
x <- dd$Date
y <- dd$Sick
n <- 1:nrow(dd)
N <- max(n)
dd <- cbind(dd, n)
# find good starting values
theta.0 <- min(y) * 0.5
model.0 <- lm (log(y - theta.0) ~ n)
alpha.0 <- exp(coef(model.0)[1])
beta.0 <- coef(model.0)[2]
starts <- list(alpha = alpha.0, beta = beta.0, theta = theta.0)
model <- NULL
try({
model <- nls(y ~ alpha * exp(beta * n) + theta,
data = dd, start = starts,
#algorithm = 'port',
nls.control(maxiter = 100, minFactor = 0.000002, tol = 1e-03))
}) #try
if(f_nbrDays > 0) {
df_frcst <- tibble(
Date = as_date((max(dd$Date) + days(1)):(max(dd$Date) + days(f_nbrDays - 1))),
Sick = NA,
n = (N + 1):(N + f_nbrDays - 1)
)
dd <- bind_rows(dd, df_frcst)
}
#df <- df %>% dplyr::mutate(Forecast = predict(model, newdata = df)) ## fails ... Error: Column `Forecast` must be length 1 (the group size), not 74
dd$Forecast <- predict(model, newdata = dd)
```
### Prediction of Sick people: we fit an exponential model for `r f_nbrDays` days and note that the number of sick people can increase to `r formatC(max(dd$Forecast), format="f", big.mark=",", digits=0)` people PER DAY
#### We use an exponential model, and forecast `r f_nbrDays` days and use data starting from `r dte_start`.
```{r plotdd}
ggplot(dd, aes(x=Date)) +
geom_line(aes(y=Sick), col="blue") +
geom_point(aes(y=Sick), col="blue") +
geom_line(aes(y=Forecast), col="red") +
scale_y_continuous(labels = function(x) formatC(x, format="f", big.mark=",", digits=0))
```
### The same with plotly
```{r plotly}
plot_ly(dd, x = ~Date, y = ~Sick, type = 'scatter', name = "Daily people sick",
mode = 'markers', marker = list(size=8)) %>%
add_trace(x = ~Date, y = ~Forecast,
mode = 'lines', type = 'scatter', name ='Model Estimate', line = list(width = 3), marker = list(size=1)) %>%
layout(
title = "Daily new cases forecast",
xaxis = list(title = "Date"),
yaxis = list(title = "Number of People"),
#autosize = TRUE,
showlegend = TRUE)
```