-
Notifications
You must be signed in to change notification settings - Fork 0
/
covid.Rmd
185 lines (133 loc) · 5.72 KB
/
covid.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
---
title: "Covid-19"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
theme: lumen
---
```{r setup, include=FALSE}
library(dplyr)
library(prophet)
library(rBayesianOptimization)
library(ggplot2)
library(flexdashboard)
#options(error=recover)
azn = data.table::fread('https://covid.ourworldindata.org/data/owid-covid-data.csv') %>%
mutate(date = as.Date(date)) %>% filter(location %in% c('Azerbaijan'))
index_max = nrow(azn)
df= azn %>% select(date,new_cases) %>% rename(ds=date,y=new_cases)
#df <- zoo::na.locf(df)
#df$y <- df$y + 1
#df$y[df$y==0]=NA
max_date = max(df$ds)
min_date = max_date - 4
df_ = df %>% filter(ds < min_date)
prophet_fit = function(n.changepoints, changepoint.range , seasonality.prior.scale,
holidays.prior.scale , changepoint.prior.scale ,
mcmc.samples , interval.width , uncertainty.samples) {
m = prophet(df_,growth = "linear",
yearly.seasonality = TRUE,
weekly.seasonality = TRUE, daily.seasonality = TRUE,
seasonality.mode = "additive",
n.changepoints = n.changepoints, changepoint.range = changepoint.range, seasonality.prior.scale = seasonality.prior.scale,
holidays.prior.scale = holidays.prior.scale, changepoint.prior.scale = changepoint.prior.scale,
mcmc.samples = mcmc.samples, interval.width = interval.width,
uncertainty.samples = uncertainty.samples)
future <- make_future_dataframe(m, periods = 20)
forecast <- predict(m, future) %>% mutate(yhat=ifelse(yhat<0,
50,
yhat))
actual = df %>% filter(ds > min_date) %>% pull(y)
yhat = forecast %>% filter(ds > min_date, ds <= max_date) %>% pull(yhat)
score_ <- list(Score = -Metrics::mae(actual,yhat),
Pred = 0)
score_
}
search_bound_prophet <- list(n.changepoints= c(0L, 150L),
changepoint.range= c(0.6, 0.9) ,
seasonality.prior.scale= c(2, 20),
holidays.prior.scale= c(2, 20) , changepoint.prior.scale= c(2, 20) ,
mcmc.samples=c(10,50) , interval.width=c(0,1) , uncertainty.samples=c(1000,1e4)
)
search_grid_prophet <- data.frame(
n.changepoints= runif(5,0L, 150L),
changepoint.range= runif(5, 0.6, 0.9) ,
seasonality.prior.scale= runif(5,2,20),
holidays.prior.scale= runif(5,2,20) , changepoint.prior.scale= runif(5,2,20) ,
mcmc.samples=runif(5,10,50) , interval.width=runif(5,0,1) ,
uncertainty.samples=runif(5,1000,1e4)
)
#head(search_grid_prophet)
bayes_prophet <- BayesianOptimization(FUN = prophet_fit, bounds = search_bound_prophet,
init_points = 2, init_grid_dt = search_grid_prophet,
n_iter = 12, acq = "ucb")
print(bayes_prophet$Best_Par)
obj=bayes_prophet$Best_Par
m = prophet(df,growth = "linear",
yearly.seasonality = TRUE,
weekly.seasonality = TRUE, daily.seasonality = TRUE,
seasonality.mode = "additive",
n.changepoints = obj[1], changepoint.range = obj[2], seasonality.prior.scale = obj[3],
holidays.prior.scale = obj[4], changepoint.prior.scale = obj[5],
mcmc.samples = obj[6], interval.width = obj[7],
uncertainty.samples = obj[8])
future <- make_future_dataframe(m, periods = 60)
#tail(future)
forecast <- predict(m, future) %>% mutate(yhat=ifelse(yhat<0,
1,
yhat))
#tail(forecast[c('ds', 'yhat', 'yhat_lower', 'yhat_upper')])
actual = df %>% filter(ds > min_date) %>% pull(y)
yhat = forecast %>% mutate(ds=as.Date(ds)) %>% filter(ds > min_date, ds <= max_date) %>% pull(yhat)
Metrics::mae(actual,yhat)
data = forecast %>% select(ds,yhat) %>% left_join(df) %>% mutate(ds=as.Date(ds),
yhat=as.integer(yhat)) #%>%filter(ds>='2020-08-01')
names(data) = c('Date','Prediction','Actual')
p=ggplot() +
geom_line(data = data, aes(x = Date, y = Actual), colour = "darkgreen") +
geom_line(data = data, aes(x = Date, y = Prediction), colour = "red") +
scale_x_date(date_breaks = "1 month",# date_minor_breaks = "1 week",
date_labels = "%B %Y") +
xlab('Dates') +
ylab('Daily cases')+ggtitle("Green = Actual \nRed = Predicted")
```
Azerbaijan
=====================================
Row {data-height=130}
-----------------------------------------------------------------------
### Date
```{r}
calen <- azn$date[index_max]
valueBox(calen, icon = "fa-calendar-alt",color = "danger")
```
### Total Cases
```{r}
tot_cases <- azn$total_cases[index_max]
valueBox(prettyNum(tot_cases, big.mark = ','), icon = "fa-users")
```
### Total Deaths
```{r}
tot_deaths <- azn$total_deaths[index_max]
valueBox(prettyNum(tot_deaths, big.mark = ','), icon = "fa-exclamation-triangle")
```
### New cases
```{r}
new_cases <- azn$new_cases[index_max]
valueBox(prettyNum(new_cases, big.mark = ','),
icon = "fa-diagnoses",
color = "warning")
```
### New Deaths
```{r}
new_deaths <- azn$new_deaths[index_max]
valueBox(prettyNum(new_deaths, big.mark = ','),
icon = "fa-exclamation-circle",
color = "danger")
```
Row {data-height=400}
-------------------------------------
### Prediction for the next 2 months
```{r}
plotly::ggplotly(p)
```