# Current time:
TODAY <- today()
# Data links (source: https://github.com/CSSEGISandData/COVID-19):
link1 <- "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Recovered.csv"
## Confrimed
link2 <- "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv"
## DEATh
link3 <- "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Deaths.csv"link2 %>%
read_csv() %>%
rename(province = `Province/State`, country = `Country/Region`) %>%
gather(Date, Confirmed, -province, - country, - Lat, -Long) %>%
mutate(Date = mdy(Date)) -> df_confirmedlink3 %>%
read_csv() %>%
rename(province = `Province/State`, country = `Country/Region`) %>%
gather(Date, Confirmed, -province, - country, - Lat, -Long) %>%
mutate(Date = mdy(Date)) -> df_deathdf_confirmed %>%
group_by(country, Date) %>%
summarise(confirmed = sum(Confirmed)) %>%
ungroup() -> Con
df_death %>%
group_by(country, Date) %>%
summarise(death = sum(Confirmed)) %>%
ungroup() ->Death
full_join(Con, Death, by =c("Date" ,"country")) -> ConDeath# Train Test 나눠주기
train <- italy$death[1:50] %>% ts()
test <- italy$death[51:56] %>% ts()
h<- length(test)
my_arima <- auto.arima(train)
# Model
predicted_arima <- forecast(my_arima, h=h)$mean %>% as.vector()
tibble(Date = italy$Date[51:56],
Actual = test %>% as.vector(),
Predicted = predicted_arima %>% round(0),
Error = Predicted - Actual,
Error_Percent = round(Error/ Actual,2)) -> df_arima
datatable(df_arima)train_full <- italy$death %>% ts()
my_arima_full <- auto.arima(train_full)
# Use the model for forecasting for next 20 days from 2020-03-17:
predicted_arima <- forecast(my_arima_full, h=20)$mean %>% as.vector()
arima_next20 <- data_frame(Date = italy$Date %>% max() + 1:20,
Predicted = predicted_arima %>% round(0))
arima_next20 %>%
knitr::kable(caption = "Table:Deaths by Coronavirus Predicted for Next 20 Days ")| Date | Predicted |
|---|---|
| 2020-03-23 | 6332 |
| 2020-03-24 | 7092 |
| 2020-03-25 | 7864 |
| 2020-03-26 | 8637 |
| 2020-03-27 | 9409 |
| 2020-03-28 | 10182 |
| 2020-03-29 | 10954 |
| 2020-03-30 | 11727 |
| 2020-03-31 | 12499 |
| 2020-04-01 | 13272 |
| 2020-04-02 | 14044 |
| 2020-04-03 | 14817 |
| 2020-04-04 | 15589 |
| 2020-04-05 | 16362 |
| 2020-04-06 | 17134 |
| 2020-04-07 | 17906 |
| 2020-04-08 | 18679 |
| 2020-04-09 | 19451 |
| 2020-04-10 | 20224 |
| 2020-04-11 | 20996 |
arima_next20 %>%
rename(death = Predicted) %>%
mutate(Type ="Predicted") -> predict_table
italy %>%
mutate(Type ="Actual") -> actual_table
ggplot()+
geom_point(data=predict_table, aes(Date, death, color= Type), size=2 )+
geom_point(data=actual_table, aes(Date, death, color= Type ), size=2)+
labs(title = "Death by Corona Predicted for next 20 days in Italy")+
scale_color_manual(values = c("cyan", "red")) +
theme_modern_rc()+
theme(axis.text.y = element_text(color = "white", size = 12)) +
theme(axis.text.x = element_text(color = "white", size = 12)) +
theme(axis.title.x = element_blank()) +
theme(axis.title.y = element_blank()) -> plot_italy
plotly:: ggplotly(plot_italy)ConDeath %>%
filter(country == "Korea, South") -> KOREA
# train test 나누기
train <- KOREA$confirmed[1:50] %>% ts()
test <- KOREA$confirmed[51:56] %>% ts()
# h 정해주기
h <- length(test)
# model auto <- train 으로 학습
korea_arima <- auto.arima(train)
# 예측 모델링
predict_korea_confrim <- forecast(korea_arima, h=h)$mean %>% as.vector()
# 데이터 프레임화 시켜주기
tibble(Date = KOREA$Date[51:56],
Actual = test %>% as.vector(),
Predict = predict_korea_confrim %>% round(0),
Error = Predict- Actual,
Error_Percent = round(Error/ Actual,2)) -> df_Korea
knitr::kable(df_Korea)| Date | Actual | Predict | Error | Error_Percent |
|---|---|---|---|---|
| 2020-03-12 | 7869 | 7951 | 82 | 0.01 |
| 2020-03-13 | 7979 | 8157 | 178 | 0.02 |
| 2020-03-14 | 8086 | 8360 | 274 | 0.03 |
| 2020-03-15 | 8162 | 8565 | 403 | 0.05 |
| 2020-03-16 | 8236 | 8769 | 533 | 0.06 |
| 2020-03-17 | 8320 | 8973 | 653 | 0.08 |
30 일 뒤에 확진자 예측하기
train_all <- KOREA$confirmed %>% ts()
Arima_full <- auto.arima(train_all)
pred_arima <- forecast(Arima_full, h=20)$mean %>% as.vector()
next_20_Korea_confirm <- data_frame(Date= KOREA$Date %>% max()+1:20,
Predict = pred_arima %>% round(0))
knitr::kable(next_20_Korea_confirm)| Date | Predict |
|---|---|
| 2020-03-23 | 9008 |
| 2020-03-24 | 9115 |
| 2020-03-25 | 9223 |
| 2020-03-26 | 9331 |
| 2020-03-27 | 9439 |
| 2020-03-28 | 9547 |
| 2020-03-29 | 9655 |
| 2020-03-30 | 9763 |
| 2020-03-31 | 9871 |
| 2020-04-01 | 9979 |
| 2020-04-02 | 10087 |
| 2020-04-03 | 10195 |
| 2020-04-04 | 10303 |
| 2020-04-05 | 10411 |
| 2020-04-06 | 10519 |
| 2020-04-07 | 10627 |
| 2020-04-08 | 10735 |
| 2020-04-09 | 10844 |
| 2020-04-10 | 10952 |
| 2020-04-11 | 11060 |
next_20_Korea_confirm %>%
rename(Confirmed = Predict) %>%
mutate(Type = "Predicted") -> KOREA_predict
KOREA %>%
mutate(Type ="Actual") -> KOREA_actual
KOREA_actual| country | Date | confirmed | death | Type |
|---|---|---|---|---|
| Korea, South | 2020-01-22 | 1 | 0 | Actual |
| Korea, South | 2020-01-23 | 1 | 0 | Actual |
| Korea, South | 2020-01-24 | 2 | 0 | Actual |
| Korea, South | 2020-01-25 | 2 | 0 | Actual |
| Korea, South | 2020-01-26 | 3 | 0 | Actual |
| Korea, South | 2020-01-27 | 4 | 0 | Actual |
| Korea, South | 2020-01-28 | 4 | 0 | Actual |
| Korea, South | 2020-01-29 | 4 | 0 | Actual |
| Korea, South | 2020-01-30 | 4 | 0 | Actual |
| Korea, South | 2020-01-31 | 11 | 0 | Actual |
| Korea, South | 2020-02-01 | 12 | 0 | Actual |
| Korea, South | 2020-02-02 | 15 | 0 | Actual |
| Korea, South | 2020-02-03 | 15 | 0 | Actual |
| Korea, South | 2020-02-04 | 16 | 0 | Actual |
| Korea, South | 2020-02-05 | 19 | 0 | Actual |
| Korea, South | 2020-02-06 | 23 | 0 | Actual |
| Korea, South | 2020-02-07 | 24 | 0 | Actual |
| Korea, South | 2020-02-08 | 24 | 0 | Actual |
| Korea, South | 2020-02-09 | 25 | 0 | Actual |
| Korea, South | 2020-02-10 | 27 | 0 | Actual |
| Korea, South | 2020-02-11 | 28 | 0 | Actual |
| Korea, South | 2020-02-12 | 28 | 0 | Actual |
| Korea, South | 2020-02-13 | 28 | 0 | Actual |
| Korea, South | 2020-02-14 | 28 | 0 | Actual |
| Korea, South | 2020-02-15 | 28 | 0 | Actual |
| Korea, South | 2020-02-16 | 29 | 0 | Actual |
| Korea, South | 2020-02-17 | 30 | 0 | Actual |
| Korea, South | 2020-02-18 | 31 | 0 | Actual |
| Korea, South | 2020-02-19 | 31 | 0 | Actual |
| Korea, South | 2020-02-20 | 104 | 1 | Actual |
| Korea, South | 2020-02-21 | 204 | 2 | Actual |
| Korea, South | 2020-02-22 | 433 | 2 | Actual |
| Korea, South | 2020-02-23 | 602 | 6 | Actual |
| Korea, South | 2020-02-24 | 833 | 8 | Actual |
| Korea, South | 2020-02-25 | 977 | 10 | Actual |
| Korea, South | 2020-02-26 | 1261 | 12 | Actual |
| Korea, South | 2020-02-27 | 1766 | 13 | Actual |
| Korea, South | 2020-02-28 | 2337 | 13 | Actual |
| Korea, South | 2020-02-29 | 3150 | 16 | Actual |
| Korea, South | 2020-03-01 | 3736 | 17 | Actual |
| Korea, South | 2020-03-02 | 4335 | 28 | Actual |
| Korea, South | 2020-03-03 | 5186 | 28 | Actual |
| Korea, South | 2020-03-04 | 5621 | 35 | Actual |
| Korea, South | 2020-03-05 | 6088 | 35 | Actual |
| Korea, South | 2020-03-06 | 6593 | 42 | Actual |
| Korea, South | 2020-03-07 | 7041 | 44 | Actual |
| Korea, South | 2020-03-08 | 7314 | 50 | Actual |
| Korea, South | 2020-03-09 | 7478 | 53 | Actual |
| Korea, South | 2020-03-10 | 7513 | 54 | Actual |
| Korea, South | 2020-03-11 | 7755 | 60 | Actual |
| Korea, South | 2020-03-12 | 7869 | 66 | Actual |
| Korea, South | 2020-03-13 | 7979 | 66 | Actual |
| Korea, South | 2020-03-14 | 8086 | 72 | Actual |
| Korea, South | 2020-03-15 | 8162 | 75 | Actual |
| Korea, South | 2020-03-16 | 8236 | 75 | Actual |
| Korea, South | 2020-03-17 | 8320 | 81 | Actual |
| Korea, South | 2020-03-18 | 8413 | 84 | Actual |
| Korea, South | 2020-03-19 | 8565 | 91 | Actual |
| Korea, South | 2020-03-20 | 8652 | 94 | Actual |
| Korea, South | 2020-03-21 | 8799 | 102 | Actual |
| Korea, South | 2020-03-22 | 8897 | 104 | Actual |
ggplot()+
geom_point(data= KOREA_predict, aes(Date,Confirmed, color=Type ), size=2)+
geom_point(data= KOREA_actual, aes(Date, confirmed, color=Type), size=2)+
theme_modern_rc()+
scale_y_continuous(limits = c(0,10000),
breaks = seq(0,10000,1000))+
scale_color_manual(values = c("cyan", "red")) +
labs(title ="Predicting confrimed cases for next 20 days in South Korea")+
theme(plot.title = element_text(size = 15)) -> KOREA_plot
plotly::ggplotly(KOREA_plot)