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.15 v expsmooth 2.3
## Warning: package 'ggplot2' was built under R version 4.0.5
## Warning: package 'forecast' 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 Third wave infection cases in chelyabinsk" # input name of data
Actual_date_interval <- c("2020/03/12","2021/07/31")
Forecast_date_interval <- c("2021/08/01","2021/08/31")
validation_data_days <-3
frequency<-"days"
Number_Neural<-5 # 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 85 122 145 214 338
# calculate standard deviation
data.frame(kurtosis=kurtosis(original_data)) # calculate Cofficient of kurtosis
## kurtosis
## 1 2.193685
data.frame(skewness=skewness(original_data)) # calculate Cofficient of skewness
## skewness
## 1 0.488203
data.frame(Standard.deviation =sd(original_data))
## Standard.deviation
## 1 91.90665
#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(2,5)
## Call: nnetar(y = data_series, size = Number_Neural)
##
## Average of 20 networks, each of which is
## a 2-5-1 network with 21 weights
## options were - linear output units
##
## sigma^2 estimated as 89.03
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 3 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 3 days in NNAR Model for ==> Forecast Third wave infection cases in chelyabinsk"
paste(MAPE_Mean_All,"%")
## [1] "3.018 % MAPE 3 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 3 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-07-29 | Thursday | 331.00 | 344.21 | 3.991 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 2 | 2021-07-30 | Friday | 335.00 | 351.82 | 5.021 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 3 | 2021-07-31 | Saturday | 338.00 | 338.14 | 0.041 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
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-08-01 | Sunday | 356.74 |
## +----+------------+-----------------+---------------------+
## | 2 | 2021-08-02 | Monday | 337.81 |
## +----+------------+-----------------+---------------------+
## | 3 | 2021-08-03 | Tuesday | 348.79 |
## +----+------------+-----------------+---------------------+
## | 4 | 2021-08-04 | Wednesday | 349.92 |
## +----+------------+-----------------+---------------------+
## | 5 | 2021-08-05 | Thursday | 341.53 |
## +----+------------+-----------------+---------------------+
## | 6 | 2021-08-06 | Friday | 362.13 |
## +----+------------+-----------------+---------------------+
## | 7 | 2021-08-07 | Saturday | 341.00 |
## +----+------------+-----------------+---------------------+
## | 8 | 2021-08-08 | Sunday | 361.00 |
## +----+------------+-----------------+---------------------+
## | 9 | 2021-08-09 | Monday | 347.49 |
## +----+------------+-----------------+---------------------+
## | 10 | 2021-08-10 | Tuesday | 352.91 |
## +----+------------+-----------------+---------------------+
## | 11 | 2021-08-11 | Wednesday | 367.68 |
## +----+------------+-----------------+---------------------+
## | 12 | 2021-08-12 | Thursday | 348.43 |
## +----+------------+-----------------+---------------------+
## | 13 | 2021-08-13 | Friday | 374.03 |
## +----+------------+-----------------+---------------------+
## | 14 | 2021-08-14 | Saturday | 349.34 |
## +----+------------+-----------------+---------------------+
## | 15 | 2021-08-15 | Sunday | 369.52 |
## +----+------------+-----------------+---------------------+
## | 16 | 2021-08-16 | Monday | 361.53 |
## +----+------------+-----------------+---------------------+
## | 17 | 2021-08-17 | Tuesday | 357.96 |
## +----+------------+-----------------+---------------------+
## | 18 | 2021-08-18 | Wednesday | 380.78 |
## +----+------------+-----------------+---------------------+
## | 19 | 2021-08-19 | Thursday | 353.21 |
## +----+------------+-----------------+---------------------+
## | 20 | 2021-08-20 | Friday | 378.82 |
## +----+------------+-----------------+---------------------+
## | 21 | 2021-08-21 | Saturday | 359.56 |
## +----+------------+-----------------+---------------------+
## | 22 | 2021-08-22 | Sunday | 370.57 |
## +----+------------+-----------------+---------------------+
## | 23 | 2021-08-23 | Monday | 377.13 |
## +----+------------+-----------------+---------------------+
## | 24 | 2021-08-24 | Tuesday | 362.51 |
## +----+------------+-----------------+---------------------+
## | 25 | 2021-08-25 | Wednesday | 386.04 |
## +----+------------+-----------------+---------------------+
## | 26 | 2021-08-26 | Thursday | 363.32 |
## +----+------------+-----------------+---------------------+
## | 27 | 2021-08-27 | Friday | 381.47 |
## +----+------------+-----------------+---------------------+
## | 28 | 2021-08-28 | Saturday | 375.65 |
## +----+------------+-----------------+---------------------+
## | 29 | 2021-08-29 | Sunday | 370.74 |
## +----+------------+-----------------+---------------------+
## | 30 | 2021-08-30 | Monday | 390.40 |
## +----+------------+-----------------+---------------------+
## | 31 | 2021-08-31 | Tuesday | 366.68 |
## +----+------------+-----------------+---------------------+
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"))
