Develop Algorithm For the Advanced software system for Forecasting Covid-19 Cases Dependent on Time Series and Neural Network Models

Daily Covid 19 Infections cases In chelyabinsk (forecasting Second-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.5     v fma       2.4  
## v forecast  8.13      v expsmooth 2.3
## Warning: package 'ggplot2' was built under R version 4.0.5
## 
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
## Warning: package 'data.table' was built under R version 4.0.5
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 Second wave infection cases in chelyabinsk"   # input name of data
Actual_date_interval <- c("2020/03/12","2020/11/30")
Forecast_date_interval <- c("2020/12/01","2020/12/30")
validation_data_days <-25
frequency<-"days"
Number_Neural<-3 # Number of Neural For model NNAR Model
NNAR_Model<- TRUE     #create new model (TRUE/FALSE) 
frequency<-"days"
number_bootstrapped<-3 # Number of bootstrabbing
# Data Preparation & calculate some of statistics measures
summary(original_data) # Summary your time series
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00   62.00   89.00   95.64  132.00  276.00
# calculate standard deviation 
data.frame(kurtosis=kurtosis(original_data))   # calculate Cofficient of kurtosis
##   kurtosis
## 1  2.86046
data.frame(skewness=skewness(original_data))  # calculate Cofficient of skewness
##    skewness
## 1 0.4027947
data.frame(Standard.deviation =sd(original_data))
##   Standard.deviation
## 1            62.9977
#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

#NNAR Model 
if(NNAR_Model==TRUE){
  data_series<-ts(training_data)
  model_NNAR<-nnetar(data_series, size = Number_Neural)
  saveRDS(model_NNAR, file = "model_NNAR.RDS")
  my_model <- readRDS("model_NNAR.RDS")
  accuracy(model_NNAR)  # accuracy on training data #Print Model Parameters
  model_NNAR
}
## Series: data_series 
## Model:  NNAR(5,3) 
## Call:   nnetar(y = data_series, size = Number_Neural)
## 
## Average of 20 networks, each of which is
## a 5-3-1 network with 22 weights
## options were - linear output units 
## 
## sigma^2 estimated as 120
if(NNAR_Model==FALSE){
  data_series<-ts(training_data)
  #model_NNAR<-nnetar(data_series, size = Number_Numeral)
  model_NNAR <- readRDS("model_NNAR.RDS")
  accuracy(model_NNAR)  # accuracy on training data #Print Model Parameters
  model_NNAR
}

## 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  25 days by using NNAR Model for  ==>  Forecast Second wave infection cases in chelyabinsk"
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  25  days in NNAR Model for  ==>  Forecast Second wave infection cases in chelyabinsk"
paste(MAPE_Mean_All,"%")
## [1] "44.024 % MAPE  25 days Forecast Second wave infection cases in chelyabinsk %"
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  25  days in NNAR Model for  ==>  Forecast Second wave infection cases in chelyabinsk"
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-11-06 | Friday                  | 179.00      | 278.10           | 55.366 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 2  | 2020-11-07 | Saturday                | 181.00      | 284.78           | 57.336 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 3  | 2020-11-08 | Sunday                  | 185.00      | 288.67           | 56.036 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 4  | 2020-11-09 | Monday                  | 186.00      | 293.13           | 57.598 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 5  | 2020-11-10 | Tuesday                 | 189.00      | 296.74           | 57.007 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 6  | 2020-11-11 | Wednesday               | 194.00      | 300.09           | 54.687 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 7  | 2020-11-12 | Thursday                | 197.00      | 302.94           | 53.779 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 8  | 2020-11-13 | Friday                  | 201.00      | 305.44           | 51.961 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 9  | 2020-11-14 | Saturday                | 207.00      | 307.57           | 48.584 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 10 | 2020-11-15 | Sunday                  | 209.00      | 309.39           | 48.032 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 11 | 2020-11-16 | Monday                  | 211.00      | 310.92           | 47.357 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 12 | 2020-11-17 | Tuesday                 | 210.00      | 312.22           | 48.675 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 13 | 2020-11-18 | Wednesday               | 215.00      | 313.30           | 45.721 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 14 | 2020-11-19 | Thursday                | 218.00      | 314.21           | 44.131 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 15 | 2020-11-20 | Friday                  | 219.00      | 314.96           | 43.817 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 16 | 2020-11-21 | Saturday                | 213.00      | 315.59           | 48.162 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 17 | 2020-11-22 | Sunday                  | 218.00      | 316.10           | 45.001 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 18 | 2020-11-23 | Monday                  | 219.00      | 316.53           | 44.535 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 19 | 2020-11-24 | Tuesday                 | 223.00      | 316.88           | 42.101 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 20 | 2020-11-25 | Wednesday               | 228.00      | 317.18           | 39.112 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 21 | 2020-11-26 | Thursday                | 241.00      | 317.42           | 31.708 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 22 | 2020-11-27 | Friday                  | 252.00      | 317.61           | 26.037 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 23 | 2020-11-28 | Saturday                | 261.00      | 317.78           | 21.753 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 24 | 2020-11-29 | Sunday                  | 272.00      | 317.91           | 16.878 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 25 | 2020-11-30 | Monday                  | 276.00      | 318.02           | 15.224 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
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-12-01 | Tuesday         | 318.11              |
## +----+------------+-----------------+---------------------+
## | 2  | 2020-12-02 | Wednesday       | 318.18              |
## +----+------------+-----------------+---------------------+
## | 3  | 2020-12-03 | Thursday        | 318.24              |
## +----+------------+-----------------+---------------------+
## | 4  | 2020-12-04 | Friday          | 318.29              |
## +----+------------+-----------------+---------------------+
## | 5  | 2020-12-05 | Saturday        | 318.33              |
## +----+------------+-----------------+---------------------+
## | 6  | 2020-12-06 | Sunday          | 318.37              |
## +----+------------+-----------------+---------------------+
## | 7  | 2020-12-07 | Monday          | 318.40              |
## +----+------------+-----------------+---------------------+
## | 8  | 2020-12-08 | Tuesday         | 318.42              |
## +----+------------+-----------------+---------------------+
## | 9  | 2020-12-09 | Wednesday       | 318.44              |
## +----+------------+-----------------+---------------------+
## | 10 | 2020-12-10 | Thursday        | 318.45              |
## +----+------------+-----------------+---------------------+
## | 11 | 2020-12-11 | Friday          | 318.46              |
## +----+------------+-----------------+---------------------+
## | 12 | 2020-12-12 | Saturday        | 318.48              |
## +----+------------+-----------------+---------------------+
## | 13 | 2020-12-13 | Sunday          | 318.48              |
## +----+------------+-----------------+---------------------+
## | 14 | 2020-12-14 | Monday          | 318.49              |
## +----+------------+-----------------+---------------------+
## | 15 | 2020-12-15 | Tuesday         | 318.50              |
## +----+------------+-----------------+---------------------+
## | 16 | 2020-12-16 | Wednesday       | 318.50              |
## +----+------------+-----------------+---------------------+
## | 17 | 2020-12-17 | Thursday        | 318.51              |
## +----+------------+-----------------+---------------------+
## | 18 | 2020-12-18 | Friday          | 318.51              |
## +----+------------+-----------------+---------------------+
## | 19 | 2020-12-19 | Saturday        | 318.51              |
## +----+------------+-----------------+---------------------+
## | 20 | 2020-12-20 | Sunday          | 318.51              |
## +----+------------+-----------------+---------------------+
## | 21 | 2020-12-21 | Monday          | 318.52              |
## +----+------------+-----------------+---------------------+
## | 22 | 2020-12-22 | Tuesday         | 318.52              |
## +----+------------+-----------------+---------------------+
## | 23 | 2020-12-23 | Wednesday       | 318.52              |
## +----+------------+-----------------+---------------------+
## | 24 | 2020-12-24 | Thursday        | 318.52              |
## +----+------------+-----------------+---------------------+
## | 25 | 2020-12-25 | Friday          | 318.52              |
## +----+------------+-----------------+---------------------+
## | 26 | 2020-12-26 | Saturday        | 318.52              |
## +----+------------+-----------------+---------------------+
## | 27 | 2020-12-27 | Sunday          | 318.52              |
## +----+------------+-----------------+---------------------+
## | 28 | 2020-12-28 | Monday          | 318.52              |
## +----+------------+-----------------+---------------------+
## | 29 | 2020-12-29 | Tuesday         | 318.52              |
## +----+------------+-----------------+---------------------+
## | 30 | 2020-12-30 | Wednesday       | 318.52              |
## +----+------------+-----------------+---------------------+
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"))