Разработка алгоритмов выбора лучших моделей временных рядов и нейронных сетей для прогнозирования случаев COVID-19

Daily Covid 19 Infections cases In Chelyabinsk (forecasting Third-wave) after update data till 17 June 2021
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$infection
y_lab <- "Forecast Third wave infection cases in Chelyabinsk"   # input name of data
Actual_date_interval <- c("2020/03/12","2021/06/17")
Forecast_date_interval <- c("2021/06/18","2021/07/30")
validation_data_days <-7
frequency<-"days"
Number_Neural<-15 # Number of Neural For model NNAR Model
NNAR_Model<- 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.0    79.0   116.0   136.7   185.0   317.0
# calculate standard deviation 
data.frame(kurtosis=kurtosis(original_data))   # calculate Cofficient of kurtosis
##   kurtosis
## 1  2.41796
data.frame(skewness=skewness(original_data))  # calculate Cofficient of skewness
##    skewness
## 1 0.5932153
data.frame(Standard.deviation =sd(original_data))
##   Standard.deviation
## 1           88.75785
#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
}
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
}
## Series: data_series 
## Model:  NNAR(19,15) 
## Call:   nnetar(y = data_series, size = Number_Neural)
## 
## Average of 20 networks, each of which is
## a 19-15-1 network with 316 weights
## options were - linear output units 
## 
## sigma^2 estimated as 12.48
# 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  7 days by using NNAR Model for  ==>  Forecast Third 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  7  days in NNAR Model for  ==>  Forecast Third wave infection cases in Chelyabinsk"
paste(MAPE_Mean_All,"%")
## [1] "0.955 % MAPE  7 days Forecast Third 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  7  days in NNAR Model for  ==>  Forecast Third 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 | 2021-06-11 | Friday                  | 86.00       | 86.82            | 0.958 %         |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 2 | 2021-06-12 | Saturday                | 87.00       | 87.27            | 0.31 %          |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 3 | 2021-06-13 | Sunday                  | 87.00       | 87.71            | 0.812 %         |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 4 | 2021-06-14 | Monday                  | 88.00       | 88.86            | 0.978 %         |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 5 | 2021-06-15 | Tuesday                 | 88.00       | 89.57            | 1.786 %         |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 6 | 2021-06-16 | Wednesday               | 89.00       | 90.26            | 1.414 %         |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 7 | 2021-06-17 | Thursday                | 92.00       | 91.61            | 0.424 %         |
## +---+------------+-------------------------+-------------+------------------+-----------------+
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  | 2021-06-18 | Friday          | 93.04               |
## +----+------------+-----------------+---------------------+
## | 2  | 2021-06-19 | Saturday        | 94.09               |
## +----+------------+-----------------+---------------------+
## | 3  | 2021-06-20 | Sunday          | 95.70               |
## +----+------------+-----------------+---------------------+
## | 4  | 2021-06-21 | Monday          | 97.53               |
## +----+------------+-----------------+---------------------+
## | 5  | 2021-06-22 | Tuesday         | 99.16               |
## +----+------------+-----------------+---------------------+
## | 6  | 2021-06-23 | Wednesday       | 100.86              |
## +----+------------+-----------------+---------------------+
## | 7  | 2021-06-24 | Thursday        | 103.40              |
## +----+------------+-----------------+---------------------+
## | 8  | 2021-06-25 | Friday          | 105.78              |
## +----+------------+-----------------+---------------------+
## | 9  | 2021-06-26 | Saturday        | 107.87              |
## +----+------------+-----------------+---------------------+
## | 10 | 2021-06-27 | Sunday          | 110.62              |
## +----+------------+-----------------+---------------------+
## | 11 | 2021-06-28 | Monday          | 113.95              |
## +----+------------+-----------------+---------------------+
## | 12 | 2021-06-29 | Tuesday         | 117.17              |
## +----+------------+-----------------+---------------------+
## | 13 | 2021-06-30 | Wednesday       | 120.53              |
## +----+------------+-----------------+---------------------+
## | 14 | 2021-07-01 | Thursday        | 124.46              |
## +----+------------+-----------------+---------------------+
## | 15 | 2021-07-02 | Friday          | 128.74              |
## +----+------------+-----------------+---------------------+
## | 16 | 2021-07-03 | Saturday        | 133.02              |
## +----+------------+-----------------+---------------------+
## | 17 | 2021-07-04 | Sunday          | 137.81              |
## +----+------------+-----------------+---------------------+
## | 18 | 2021-07-05 | Monday          | 143.08              |
## +----+------------+-----------------+---------------------+
## | 19 | 2021-07-06 | Tuesday         | 148.49              |
## +----+------------+-----------------+---------------------+
## | 20 | 2021-07-07 | Wednesday       | 154.26              |
## +----+------------+-----------------+---------------------+
## | 21 | 2021-07-08 | Thursday        | 160.50              |
## +----+------------+-----------------+---------------------+
## | 22 | 2021-07-09 | Friday          | 166.84              |
## +----+------------+-----------------+---------------------+
## | 23 | 2021-07-10 | Saturday        | 173.07              |
## +----+------------+-----------------+---------------------+
## | 24 | 2021-07-11 | Sunday          | 179.65              |
## +----+------------+-----------------+---------------------+
## | 25 | 2021-07-12 | Monday          | 186.08              |
## +----+------------+-----------------+---------------------+
## | 26 | 2021-07-13 | Tuesday         | 191.69              |
## +----+------------+-----------------+---------------------+
## | 27 | 2021-07-14 | Wednesday       | 196.78              |
## +----+------------+-----------------+---------------------+
## | 28 | 2021-07-15 | Thursday        | 201.37              |
## +----+------------+-----------------+---------------------+
## | 29 | 2021-07-16 | Friday          | 204.84              |
## +----+------------+-----------------+---------------------+
## | 30 | 2021-07-17 | Saturday        | 206.60              |
## +----+------------+-----------------+---------------------+
## | 31 | 2021-07-18 | Sunday          | 207.16              |
## +----+------------+-----------------+---------------------+
## | 32 | 2021-07-19 | Monday          | 206.94              |
## +----+------------+-----------------+---------------------+
## | 33 | 2021-07-20 | Tuesday         | 205.65              |
## +----+------------+-----------------+---------------------+
## | 34 | 2021-07-21 | Wednesday       | 203.14              |
## +----+------------+-----------------+---------------------+
## | 35 | 2021-07-22 | Thursday        | 199.49              |
## +----+------------+-----------------+---------------------+
## | 36 | 2021-07-23 | Friday          | 194.75              |
## +----+------------+-----------------+---------------------+
## | 37 | 2021-07-24 | Saturday        | 188.58              |
## +----+------------+-----------------+---------------------+
## | 38 | 2021-07-25 | Sunday          | 180.69              |
## +----+------------+-----------------+---------------------+
## | 39 | 2021-07-26 | Monday          | 170.84              |
## +----+------------+-----------------+---------------------+
## | 40 | 2021-07-27 | Tuesday         | 159.00              |
## +----+------------+-----------------+---------------------+
## | 41 | 2021-07-28 | Wednesday       | 146.31              |
## +----+------------+-----------------+---------------------+
## | 42 | 2021-07-29 | Thursday        | 135.11              |
## +----+------------+-----------------+---------------------+
## | 43 | 2021-07-30 | Friday          | 127.19              |
## +----+------------+-----------------+---------------------+
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

message("System finished Modelling and Forecasting  by using BATS, TBATS, Holt's Linear Trend,ARIMA Model, and SIR Model ==>",y_lab, sep=" ")
## System finished Modelling and Forecasting  by using BATS, TBATS, Holt's Linear Trend,ARIMA Model, and SIR Model ==>Forecast Third wave infection cases in Chelyabinsk
message(" Thank you for using our System For Modelling and Forecasting ==> ",y_lab, sep=" ")
##  Thank you for using our System For Modelling and Forecasting ==> Forecast Third wave infection cases in Chelyabinsk