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
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 First 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)  #create new model (TRUE/FALSE)
frequency<-"days"
# 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 119
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
}

# Testing Data Evaluation
forecasting_NNAR <- forecast(model_NNAR, h=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 First 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 First wave infection cases in chelyabinsk"
paste(MAPE_Mean_All,"%")
## [1] "5.473 % MAPE  25 days Forecast First 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 First 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      | 182.28           | 1.831 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 2  | 2020-11-07 | Saturday                | 181.00      | 189.32           | 4.598 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 3  | 2020-11-08 | Sunday                  | 185.00      | 194.06           | 4.897 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 4  | 2020-11-09 | Monday                  | 186.00      | 197.98           | 6.443 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 5  | 2020-11-10 | Tuesday                 | 189.00      | 200.72           | 6.2 %           |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 6  | 2020-11-11 | Wednesday               | 194.00      | 203.91           | 5.11 %          |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 7  | 2020-11-12 | Thursday                | 197.00      | 207.02           | 5.088 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 8  | 2020-11-13 | Friday                  | 201.00      | 209.61           | 4.284 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 9  | 2020-11-14 | Saturday                | 207.00      | 211.79           | 2.312 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 10 | 2020-11-15 | Sunday                  | 209.00      | 213.28           | 2.046 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 11 | 2020-11-16 | Monday                  | 211.00      | 214.51           | 1.661 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 12 | 2020-11-17 | Tuesday                 | 210.00      | 215.49           | 2.613 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 13 | 2020-11-18 | Wednesday               | 215.00      | 216.28           | 0.597 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 14 | 2020-11-19 | Thursday                | 218.00      | 216.95           | 0.483 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 15 | 2020-11-20 | Friday                  | 219.00      | 217.43           | 0.718 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 16 | 2020-11-21 | Saturday                | 213.00      | 217.80           | 2.252 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 17 | 2020-11-22 | Sunday                  | 218.00      | 218.06           | 0.025 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 18 | 2020-11-23 | Monday                  | 219.00      | 218.25           | 0.345 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 19 | 2020-11-24 | Tuesday                 | 223.00      | 218.39           | 2.067 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 20 | 2020-11-25 | Wednesday               | 228.00      | 218.50           | 4.169 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 21 | 2020-11-26 | Thursday                | 241.00      | 218.58           | 9.304 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 22 | 2020-11-27 | Friday                  | 252.00      | 218.64           | 13.239 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 23 | 2020-11-28 | Saturday                | 261.00      | 218.68           | 16.214 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 24 | 2020-11-29 | Sunday                  | 272.00      | 218.71           | 19.591 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 25 | 2020-11-30 | Monday                  | 276.00      | 218.73           | 20.748 %        |
## +----+------------+-------------------------+-------------+------------------+-----------------+
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         | 218.75              |
## +----+------------+-----------------+---------------------+
## | 2  | 2020-12-02 | Wednesday       | 218.76              |
## +----+------------+-----------------+---------------------+
## | 3  | 2020-12-03 | Thursday        | 218.77              |
## +----+------------+-----------------+---------------------+
## | 4  | 2020-12-04 | Friday          | 218.78              |
## +----+------------+-----------------+---------------------+
## | 5  | 2020-12-05 | Saturday        | 218.78              |
## +----+------------+-----------------+---------------------+
## | 6  | 2020-12-06 | Sunday          | 218.79              |
## +----+------------+-----------------+---------------------+
## | 7  | 2020-12-07 | Monday          | 218.79              |
## +----+------------+-----------------+---------------------+
## | 8  | 2020-12-08 | Tuesday         | 218.79              |
## +----+------------+-----------------+---------------------+
## | 9  | 2020-12-09 | Wednesday       | 218.79              |
## +----+------------+-----------------+---------------------+
## | 10 | 2020-12-10 | Thursday        | 218.79              |
## +----+------------+-----------------+---------------------+
## | 11 | 2020-12-11 | Friday          | 218.80              |
## +----+------------+-----------------+---------------------+
## | 12 | 2020-12-12 | Saturday        | 218.80              |
## +----+------------+-----------------+---------------------+
## | 13 | 2020-12-13 | Sunday          | 218.80              |
## +----+------------+-----------------+---------------------+
## | 14 | 2020-12-14 | Monday          | 218.80              |
## +----+------------+-----------------+---------------------+
## | 15 | 2020-12-15 | Tuesday         | 218.80              |
## +----+------------+-----------------+---------------------+
## | 16 | 2020-12-16 | Wednesday       | 218.80              |
## +----+------------+-----------------+---------------------+
## | 17 | 2020-12-17 | Thursday        | 218.80              |
## +----+------------+-----------------+---------------------+
## | 18 | 2020-12-18 | Friday          | 218.80              |
## +----+------------+-----------------+---------------------+
## | 19 | 2020-12-19 | Saturday        | 218.80              |
## +----+------------+-----------------+---------------------+
## | 20 | 2020-12-20 | Sunday          | 218.80              |
## +----+------------+-----------------+---------------------+
## | 21 | 2020-12-21 | Monday          | 218.80              |
## +----+------------+-----------------+---------------------+
## | 22 | 2020-12-22 | Tuesday         | 218.80              |
## +----+------------+-----------------+---------------------+
## | 23 | 2020-12-23 | Wednesday       | 218.80              |
## +----+------------+-----------------+---------------------+
## | 24 | 2020-12-24 | Thursday        | 218.80              |
## +----+------------+-----------------+---------------------+
## | 25 | 2020-12-25 | Friday          | 218.80              |
## +----+------------+-----------------+---------------------+
## | 26 | 2020-12-26 | Saturday        | 218.80              |
## +----+------------+-----------------+---------------------+
## | 27 | 2020-12-27 | Sunday          | 218.80              |
## +----+------------+-----------------+---------------------+
## | 28 | 2020-12-28 | Monday          | 218.80              |
## +----+------------+-----------------+---------------------+
## | 29 | 2020-12-29 | Tuesday         | 218.80              |
## +----+------------+-----------------+---------------------+
## | 30 | 2020-12-30 | Wednesday       | 218.80              |
## +----+------------+-----------------+---------------------+
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"))