Development of the Advanced Algorithm for Forecasting Covid-19 Cases Based on Time Series and Neural Network Models

Daily Covid 19 Infections cases In Russian federation (forecasting First-wave) by using NNAR Bootstrap model
Makarovskikh Tatyana Anatolyevna “Макаровских Татьяна Анатольевна”
Abotaleb mostafa“Аботалеб Мостафа”
Faculty of Electrical Engineering and Computer Science
Department of system programming
South ural state university, Chelyabinsk, Russian federation
#Import
library(fpp2)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## -- Attaching packages ---------------------------------------------- fpp2 2.4 --
## v ggplot2   3.3.2     v fma       2.4  
## v forecast  8.13      v expsmooth 2.3
## 
library(forecast)
library(ggplot2)
library("readxl")
library(moments)
library(forecast)
require(forecast)  
require(tseries)
## Loading required package: tseries
require(markovchain)
## Loading required package: markovchain
## Package:  markovchain
## Version:  0.8.5-3
## Date:     2020-12-03
## BugReport: https://github.com/spedygiorgio/markovchain/issues
require(data.table)
## Loading required package: data.table
library(Hmisc)
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
library(ascii)
##Global vriable##
Full_original_data <- read.csv("data.csv") # path of your data ( time series data)
original_data<-Full_original_data$cases
y_lab <- "Forecast First wave infection cases in Russia"   # input name of data
Actual_date_interval <- c("2020/03/01","2020/04/30")
Forecast_date_interval <- c("2020/05/01","2020/05/30")
validation_data_days <-3
frequency<-"days"
Number_Neural<-20 # Number of Neural For model NNAR Model
NNAR_Model<- TRUE     #create new model (TRUE/FALSE)
frequency<-"days"
number_bootstrapped<-100
# Data Preparation & calculate some of statistics measures
summary(original_data) # Summary your time series
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0     0.0     0.0   894.9   371.5  7099.0
# calculate standard deviation 
data.frame(kurtosis=kurtosis(original_data))   # calculate Cofficient of kurtosis
##   kurtosis
## 1 5.777923
data.frame(skewness=skewness(original_data))  # calculate Cofficient of skewness
##   skewness
## 1 2.058073
data.frame(Standard.deviation =sd(original_data))
##   Standard.deviation
## 1           1876.437
#processing on data (input data)
rows <- NROW(original_data) # calculate number of rows in time series (number of days)
training_data<-original_data[1:(rows-validation_data_days)] # Training data
testing_data<-original_data[(rows-validation_data_days+1):rows] #testing data
AD<-fulldate<-seq(as.Date(Actual_date_interval[1]),as.Date(Actual_date_interval[2]), frequency)  #input range for actual date
FD<-seq(as.Date(Forecast_date_interval[1]),as.Date(Forecast_date_interval[2]), frequency)  #input range forecasting date
N_forecasting_days<-nrow(data.frame(FD))  #calculate number of days that you want to forecasting
validation_dates<-tail(AD,validation_data_days) # select validation_dates
validation_data_by_name<-weekdays(validation_dates) # put names of validation dates
forecasting_data_by_name<-weekdays(FD)  # put names of Forecasting dates
## Bootstrapping and bagging NNAR model
model_NNAR <- baggedModel(original_data,bootstrapped_series= bld.mbb.bootstrap(original_data,number_bootstrapped), fn = nnetar,size = Number_Neural)
forecasting_NNAR <- forecast(model_NNAR,N_forecasting_days+validation_data_days)
validation_forecast<-head(forecasting_NNAR$mean,validation_data_days)
MAPE_Per_Day<-round(  abs(((testing_data-validation_forecast)/testing_data)*100)  ,3)
paste ("MAPE % For ",validation_data_days,frequency,"by using NNAR Model for  ==> ",y_lab, sep=" ")
## [1] "MAPE % For  3 days by using NNAR Model for  ==>  Forecast First wave infection cases in Russia"
MAPE_Mean_All<-paste(round(mean(MAPE_Per_Day),3),"% MAPE ",validation_data_days,frequency,y_lab,sep=" ")
MAPE_Mean_All_NNAR<-round(mean(MAPE_Per_Day),3)
MAPE_NNAR<-paste(round(MAPE_Per_Day,3),"%")
MAPE_NNAR_Model<-paste(MAPE_Per_Day ,"%")
paste (" MAPE that's Error of Forecasting for ",validation_data_days," days in NNAR Model for  ==> ",y_lab, sep=" ")
## [1] " MAPE that's Error of Forecasting for  3  days in NNAR Model for  ==>  Forecast First wave infection cases in Russia"
paste(MAPE_Mean_All,"%")
## [1] "13.405 % MAPE  3 days Forecast First wave infection cases in Russia %"
paste ("MAPE that's Error of Forecasting day by day for ",validation_data_days," days in NNAR Model for  ==> ",y_lab, sep=" ")
## [1] "MAPE that's Error of Forecasting day by day for  3  days in NNAR Model for  ==>  Forecast First wave infection cases in Russia"
print(ascii(data.frame(date_NNAR=validation_dates,validation_data_by_name,actual_data=testing_data,forecasting_NNAR=validation_forecast,MAPE_NNAR_Model)), type = "rest")
## 
## +---+------------+-------------------------+-------------+------------------+-----------------+
## |   | date_NNAR  | validation_data_by_name | actual_data | forecasting_NNAR | MAPE_NNAR_Model |
## +===+============+=========================+=============+==================+=================+
## | 1 | 2020-04-28 | Tuesday                 | 6411.00     | 6819.06          | 6.365 %         |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 2 | 2020-04-29 | Wednesday               | 5841.00     | 7244.83          | 24.034 %        |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 3 | 2020-04-30 | Thursday                | 7099.00     | 7795.82          | 9.816 %         |
## +---+------------+-------------------------+-------------+------------------+-----------------+
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting_by_NNAR=tail(forecasting_NNAR$mean,N_forecasting_days))), type = "rest")
## 
## +----+------------+-----------------+---------------------+
## |    | FD         | forecating_date | forecasting_by_NNAR |
## +====+============+=================+=====================+
## | 1  | 2020-05-01 | Friday          | 8824.73             |
## +----+------------+-----------------+---------------------+
## | 2  | 2020-05-02 | Saturday        | 9437.16             |
## +----+------------+-----------------+---------------------+
## | 3  | 2020-05-03 | Sunday          | 10259.02            |
## +----+------------+-----------------+---------------------+
## | 4  | 2020-05-04 | Monday          | 10312.53            |
## +----+------------+-----------------+---------------------+
## | 5  | 2020-05-05 | Tuesday         | 10749.78            |
## +----+------------+-----------------+---------------------+
## | 6  | 2020-05-06 | Wednesday       | 10589.55            |
## +----+------------+-----------------+---------------------+
## | 7  | 2020-05-07 | Thursday        | 10845.57            |
## +----+------------+-----------------+---------------------+
## | 8  | 2020-05-08 | Friday          | 10681.41            |
## +----+------------+-----------------+---------------------+
## | 9  | 2020-05-09 | Saturday        | 10954.61            |
## +----+------------+-----------------+---------------------+
## | 10 | 2020-05-10 | Sunday          | 10726.74            |
## +----+------------+-----------------+---------------------+
## | 11 | 2020-05-11 | Monday          | 10885.70            |
## +----+------------+-----------------+---------------------+
## | 12 | 2020-05-12 | Tuesday         | 10691.76            |
## +----+------------+-----------------+---------------------+
## | 13 | 2020-05-13 | Wednesday       | 10991.29            |
## +----+------------+-----------------+---------------------+
## | 14 | 2020-05-14 | Thursday        | 10704.10            |
## +----+------------+-----------------+---------------------+
## | 15 | 2020-05-15 | Friday          | 10982.71            |
## +----+------------+-----------------+---------------------+
## | 16 | 2020-05-16 | Saturday        | 10742.38            |
## +----+------------+-----------------+---------------------+
## | 17 | 2020-05-17 | Sunday          | 11052.68            |
## +----+------------+-----------------+---------------------+
## | 18 | 2020-05-18 | Monday          | 10705.73            |
## +----+------------+-----------------+---------------------+
## | 19 | 2020-05-19 | Tuesday         | 11056.24            |
## +----+------------+-----------------+---------------------+
## | 20 | 2020-05-20 | Wednesday       | 10747.85            |
## +----+------------+-----------------+---------------------+
## | 21 | 2020-05-21 | Thursday        | 11096.80            |
## +----+------------+-----------------+---------------------+
## | 22 | 2020-05-22 | Friday          | 10825.17            |
## +----+------------+-----------------+---------------------+
## | 23 | 2020-05-23 | Saturday        | 11063.99            |
## +----+------------+-----------------+---------------------+
## | 24 | 2020-05-24 | Sunday          | 10751.53            |
## +----+------------+-----------------+---------------------+
## | 25 | 2020-05-25 | Monday          | 11068.67            |
## +----+------------+-----------------+---------------------+
## | 26 | 2020-05-26 | Tuesday         | 10736.73            |
## +----+------------+-----------------+---------------------+
## | 27 | 2020-05-27 | Wednesday       | 11030.32            |
## +----+------------+-----------------+---------------------+
## | 28 | 2020-05-28 | Thursday        | 10738.12            |
## +----+------------+-----------------+---------------------+
## | 29 | 2020-05-29 | Friday          | 11056.69            |
## +----+------------+-----------------+---------------------+
## | 30 | 2020-05-30 | Saturday        | 10738.56            |
## +----+------------+-----------------+---------------------+
plot(forecasting_NNAR,xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab)
x1_test <- ts(testing_data, start =(rows-validation_data_days+1) )
lines(x1_test, col='red',lwd=2)

graph1<-autoplot(forecasting_NNAR,xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab)
graph1+scale_y_continuous(labels = scales::comma)+
  forecast::autolayer(forecasting_NNAR$mean, series="NNAR Model",size = 0.7) +
  guides(colour=guide_legend(title="Forecasts"),fill = "black")+
  theme(legend.position="bottom")+
  theme(legend.background = element_rect(fill="white",
                                         size=0.7, linetype="solid", 
                                         colour ="gray"))