South ural state university, Chelyabinsk, Russian federation
#Import library
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)
library(pander)
##
## Attaching package: 'pander'
## The following object is masked from 'package:ascii':
##
## Pandoc
library(tseries)
require(tseries) # need to install tseries tj test Stationarity in time series
library(forecast) # install library forecast
library(ascii) # for make tables
##Global vriable##
Full_original_data <- read_excel("data.xlsx") # path of your data ( time series data)
original_data<-Full_original_data$Cases # select colum from your data
y_lab <- "(Daily Covid 19 Infection cases in Russia)" # input name of data
Actual_date_interval <- c("2020/03/01","2021/07/31") # put actual range date of your data
Forecast_date_interval <- c("2021/08/01","2021/08/07") #put forecasting date range
validation_data_days <-7 # Number of testing data(#testing last 10 days)10
Number_Neural<-5# Number of Neural For model NNAR Model
NNAR_Model<- TRUE #create new NNAR model (TRUE/FALSE)
frequency<-"days" # type of you data( daily-weekly-month-years)
country.name <- "Russia" # name of area or country or cases
# Data Preparation & calculate some of statistics measures
summary(original_data) # Summary your time series
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0 5232 8838 10878 16694 29935
# calculate Cofficient of kurtosis
# calculate Cofficient of skewness
# calculate standard deviation
data.frame(kurtosis=kurtosis(original_data),skewness=skewness(original_data),Standard.deviation =sd(original_data))
## kurtosis skewness Standard.deviation
## 1 2.314857 0.6028612 8399.223
#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(9,5)
## Call: nnetar(y = data_series, size = Number_Neural)
##
## Average of 20 networks, each of which is
## a 9-5-1 network with 56 weights
## options were - linear output units
##
## sigma^2 estimated as 219212
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 7 days by using NNAR Model for ==> (Daily Covid 19 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 7 days in NNAR Model for ==> (Daily Covid 19 Infection cases in Russia)"
paste(MAPE_Mean_All,"%")
## [1] "1.371 % MAPE 7 days (Daily Covid 19 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 7 days in NNAR Model for ==> (Daily Covid 19 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 | 2021-07-25 | Sunday | 24072.00 | 23728.20 | 1.428 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 2 | 2021-07-26 | Monday | 23239.00 | 23340.52 | 0.437 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 3 | 2021-07-27 | Tuesday | 23032.00 | 22965.84 | 0.287 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 4 | 2021-07-28 | Wednesday | 22420.00 | 23027.32 | 2.709 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 5 | 2021-07-29 | Thursday | 23270.00 | 23257.77 | 0.053 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 6 | 2021-07-30 | Friday | 23564.00 | 23182.32 | 1.62 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 7 | 2021-07-31 | Saturday | 23807.00 | 23077.56 | 3.064 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
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 | 22987.53 |
## +---+------------+-----------------+---------------------+
## | 2 | 2021-08-02 | Monday | 22738.48 |
## +---+------------+-----------------+---------------------+
## | 3 | 2021-08-03 | Tuesday | 22591.08 |
## +---+------------+-----------------+---------------------+
## | 4 | 2021-08-04 | Wednesday | 22711.09 |
## +---+------------+-----------------+---------------------+
## | 5 | 2021-08-05 | Thursday | 22869.99 |
## +---+------------+-----------------+---------------------+
## | 6 | 2021-08-06 | Friday | 22905.29 |
## +---+------------+-----------------+---------------------+
## | 7 | 2021-08-07 | Saturday | 22879.74 |
## +---+------------+-----------------+---------------------+
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"))

#################
## bats model #
#################
# Data Modeling
data_series<-ts(training_data) # make your data to time series
autoplot(data_series ,xlab=paste ("Time in", frequency, sep=" "), ylab = y_lab, main=paste ("Actual Data :", y_lab, sep=" "))

model_bats<-bats(data_series)
accuracy(model_bats) # accuracy on training data
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -3.195278 537.9162 348.3379 NaN Inf 0.8776161 0.006188345
# Print Model Parameters
model_bats
## BATS(1, {2,2}, 1, -)
##
## Call: bats(y = data_series)
##
## Parameters
## Alpha: 0.5733282
## Beta: 0.09703013
## Damping Parameter: 1
## AR coefficients: 1.230858 -0.980296
## MA coefficients: -1.173657 0.86548
##
## Seed States:
## [,1]
## [1,] -15.907985
## [2,] 1.487672
## [3,] 0.000000
## [4,] 0.000000
## [5,] 0.000000
## [6,] 0.000000
##
## Sigma: 537.9162
## AIC: 10789.07
#ploting BATS Model
plot(model_bats,xlab = paste ("Time in", frequency ,y_lab , sep=" "))

# Testing Data Evaluation
forecasting_bats <- predict(model_bats, h=N_forecasting_days+validation_data_days)
validation_forecast<-head(forecasting_bats$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 BATS Model for ==> ",y_lab, sep=" ")
## [1] "MAPE % For 7 days by using BATS Model for ==> (Daily Covid 19 Infection cases in Russia)"
MAPE_Mean_All.bats_Model<-round(mean(MAPE_Per_Day),3)
MAPE_Mean_All.bats<-paste(round(mean(MAPE_Per_Day),3),"% MAPE ",validation_data_days,frequency,y_lab,sep=" ")
MAPE_bats<-paste(round(MAPE_Per_Day,3),"%")
MAPE_bats_Model<-paste(MAPE_Per_Day ,"%")
paste ("MAPE that's Error of Forecasting for ",validation_data_days," days in BATS Model for ==> ",y_lab, sep=" ")
## [1] "MAPE that's Error of Forecasting for 7 days in BATS Model for ==> (Daily Covid 19 Infection cases in Russia)"
paste(MAPE_Mean_All.bats,"%")
## [1] "2.046 % MAPE 7 days (Daily Covid 19 Infection cases in Russia) %"
paste ("MAPE that's Error of Forecasting day by day for ",validation_data_days," days in BATS Model for ==> ",y_lab, sep=" ")
## [1] "MAPE that's Error of Forecasting day by day for 7 days in BATS Model for ==> (Daily Covid 19 Infection cases in Russia)"
print(ascii(data.frame(date_bats=validation_dates,validation_data_by_name,actual_data=testing_data,forecasting_bats=validation_forecast,MAPE_bats_Model)), type = "rest")
##
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | | date_bats | validation_data_by_name | actual_data | forecasting_bats | MAPE_bats_Model |
## +===+============+=========================+=============+==================+=================+
## | 1 | 2021-07-25 | Sunday | 24072.00 | 23605.91 | 1.936 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 2 | 2021-07-26 | Monday | 23239.00 | 22990.05 | 1.071 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 3 | 2021-07-27 | Tuesday | 23032.00 | 22542.56 | 2.125 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 4 | 2021-07-28 | Wednesday | 22420.00 | 22474.54 | 0.243 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 5 | 2021-07-29 | Thursday | 23270.00 | 22708.57 | 2.413 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 6 | 2021-07-30 | Friday | 23564.00 | 22942.37 | 2.638 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 7 | 2021-07-31 | Saturday | 23807.00 | 22879.80 | 3.895 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting_by_bats=tail(forecasting_bats$mean,N_forecasting_days),lower=tail(forecasting_bats$lower,N_forecasting_days),Upper=tail(forecasting_bats$lower,N_forecasting_days))), type = "rest")
##
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | | FD | forecating_date | forecasting_by_bats | lower.80. | lower.95. | Upper.80. | Upper.95. |
## +===+============+=================+=====================+===========+===========+===========+===========+
## | 1 | 2021-08-01 | Sunday | 22452.66 | 20636.85 | 19675.62 | 20636.85 | 19675.62 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 2 | 2021-08-02 | Monday | 21867.31 | 19827.58 | 18747.82 | 19827.58 | 18747.82 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 3 | 2021-08-03 | Tuesday | 21444.63 | 19192.69 | 18000.59 | 19192.69 | 18000.59 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 4 | 2021-08-04 | Wednesday | 21377.25 | 18928.16 | 17631.69 | 18928.16 | 17631.69 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 5 | 2021-08-05 | Thursday | 21587.74 | 18944.39 | 17545.08 | 18944.39 | 17545.08 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 6 | 2021-08-06 | Friday | 21791.94 | 18939.50 | 17429.51 | 18939.50 | 17429.51 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 7 | 2021-08-07 | Saturday | 21716.01 | 18628.31 | 16993.79 | 18628.31 | 16993.79 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
plot(forecasting_bats)
x1_test <- ts(testing_data, start =(rows-validation_data_days+1) )
lines(x1_test, col='red',lwd=2)

graph2<-autoplot(forecasting_bats,xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab)
graph2+scale_y_continuous(labels = scales::comma)+
forecast::autolayer(forecasting_bats$mean, series="BATS 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"))

###############
## TBATS Model#
###############
# Data Modeling
data_series<-ts(training_data)
model_TBATS<-forecast:::fitSpecificTBATS(data_series,use.box.cox=FALSE, use.beta=TRUE, seasonal.periods=c(6),use.damping=FALSE,k.vector=c(2))
accuracy(model_TBATS) # accuracy on training data
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 3.985268 606.9272 396.4664 NaN Inf 0.9988729 0.0282245
# Print Model Parameters
model_TBATS
## TBATS(1, {0,0}, 1, {<6,2>})
##
## Call: NULL
##
## Parameters
## Alpha: 0.8493231
## Beta: 0.0460706
## Damping Parameter: 1
## Gamma-1 Values: -0.004694001
## Gamma-2 Values: 0.002288531
##
## Seed States:
## [,1]
## [1,] -14.034647
## [2,] 1.275852
## [3,] 28.691821
## [4,] -18.011770
## [5,] -1.650095
## [6,] -37.877659
##
## Sigma: 606.9272
## AIC: 10922.44
plot(model_TBATS,xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab)

# Testing Data Evaluation
forecasting_tbats <- predict(model_TBATS, h=N_forecasting_days+validation_data_days)
validation_forecast<-head(forecasting_tbats$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 TBATS Model for ==> ",y_lab, sep=" ")
## [1] "MAPE % For 7 days by using TBATS Model for ==> (Daily Covid 19 Infection cases in Russia)"
MAPE_Mean_All.TBATS_Model<-round(mean(MAPE_Per_Day),3)
MAPE_Mean_All.TBATS<-paste(round(mean(MAPE_Per_Day),3),"% MAPE ",validation_data_days,frequency,y_lab,sep=" ")
MAPE_TBATS<-paste(round(MAPE_Per_Day,3),"%")
MAPE_TBATS_Model<-paste(MAPE_Per_Day ,"%")
paste (" MAPE that's Error of Forecasting for ",validation_data_days," days in TBATS Model for ==> ",y_lab, sep=" ")
## [1] " MAPE that's Error of Forecasting for 7 days in TBATS Model for ==> (Daily Covid 19 Infection cases in Russia)"
paste(MAPE_Mean_All.TBATS,"%")
## [1] "4.42 % MAPE 7 days (Daily Covid 19 Infection cases in Russia) %"
paste ("MAPE that's Error of Forecasting day by day for ",validation_data_days," days in TBATS Model for ==> ",y_lab, sep=" ")
## [1] "MAPE that's Error of Forecasting day by day for 7 days in TBATS Model for ==> (Daily Covid 19 Infection cases in Russia)"
print(ascii(data.frame(date_TBATS=validation_dates,validation_data_by_name,actual_data=testing_data,forecasting_TBATS=validation_forecast,MAPE_TBATS_Model)), type = "rest")
##
## +---+------------+-------------------------+-------------+-------------------+------------------+
## | | date_TBATS | validation_data_by_name | actual_data | forecasting_TBATS | MAPE_TBATS_Model |
## +===+============+=========================+=============+===================+==================+
## | 1 | 2021-07-25 | Sunday | 24072.00 | 24047.70 | 0.101 % |
## +---+------------+-------------------------+-------------+-------------------+------------------+
## | 2 | 2021-07-26 | Monday | 23239.00 | 24111.86 | 3.756 % |
## +---+------------+-------------------------+-------------+-------------------+------------------+
## | 3 | 2021-07-27 | Tuesday | 23032.00 | 24291.29 | 5.468 % |
## +---+------------+-------------------------+-------------+-------------------+------------------+
## | 4 | 2021-07-28 | Wednesday | 22420.00 | 24348.50 | 8.602 % |
## +---+------------+-------------------------+-------------+-------------------+------------------+
## | 5 | 2021-07-29 | Thursday | 23270.00 | 24411.61 | 4.906 % |
## +---+------------+-------------------------+-------------+-------------------+------------------+
## | 6 | 2021-07-30 | Friday | 23564.00 | 24607.48 | 4.428 % |
## +---+------------+-------------------------+-------------+-------------------+------------------+
## | 7 | 2021-07-31 | Saturday | 23807.00 | 24682.17 | 3.676 % |
## +---+------------+-------------------------+-------------+-------------------+------------------+
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting_by_TBATS=tail(forecasting_tbats$mean,N_forecasting_days),Lower=tail(forecasting_tbats$lower,N_forecasting_days),Upper=tail(forecasting_tbats$upper,N_forecasting_days))), type = "rest")
##
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
## | | FD | forecating_date | forecasting_by_TBATS | Lower.80. | Lower.95. | Upper.80. | Upper.95. |
## +===+============+=================+======================+===========+===========+===========+===========+
## | 1 | 2021-08-01 | Sunday | 24746.34 | 22869.01 | 21875.22 | 26623.66 | 27617.45 |
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
## | 2 | 2021-08-02 | Monday | 24925.77 | 22945.49 | 21897.19 | 26906.06 | 27954.36 |
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
## | 3 | 2021-08-03 | Tuesday | 24982.98 | 22904.82 | 21804.72 | 27061.13 | 28161.24 |
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
## | 4 | 2021-08-04 | Wednesday | 25046.09 | 22874.68 | 21725.20 | 27217.50 | 28366.98 |
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
## | 5 | 2021-08-05 | Thursday | 25241.96 | 22983.81 | 21788.42 | 27500.11 | 28695.50 |
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
## | 6 | 2021-08-06 | Friday | 25316.65 | 22976.90 | 21738.31 | 27656.40 | 28894.99 |
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
## | 7 | 2021-08-07 | Saturday | 25380.81 | 22962.53 | 21682.37 | 27799.10 | 29079.26 |
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
plot(forecasting_tbats)
x1_test <- ts(testing_data, start =(rows-validation_data_days+1) )
lines(x1_test, col='red',lwd=2)

graph3<-autoplot(forecasting_tbats,xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab)
graph3+scale_y_continuous(labels = scales::comma)+
forecast::autolayer(forecasting_tbats$mean, series="TBATS 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"))

#######################
## Holt's linear trend#
#######################
# Data Modeling
data_series<-ts(training_data)
model_holt<-holt(data_series,h=N_forecasting_days+validation_data_days,lambda = "auto")
accuracy(model_holt) # accuracy on training data
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -16.53708 617.0761 397.7567 NaN Inf 1.002124 0.1159935
# Print Model Parameters
summary(model_holt$model)
## Holt's method
##
## Call:
## holt(y = data_series, h = N_forecasting_days + validation_data_days,
##
## Call:
## lambda = "auto")
##
## Box-Cox transformation: lambda= 0.6072
##
## Smoothing parameters:
## alpha = 0.7317
## beta = 0.0538
##
## Initial states:
## l = -1.6262
## b = 0.0204
##
## sigma: 15.1238
##
## AIC AICc BIC
## 6706.767 6706.873 6728.486
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -16.53708 617.0761 397.7567 NaN Inf 1.002124 0.1159935
# Testing Data Evaluation
forecasting_holt <- predict(model_holt, h=N_forecasting_days+validation_data_days,lambda = "auto")
validation_forecast<-head(forecasting_holt$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 holt's Linear trend Model for ==> ",y_lab, sep=" ")
## [1] "MAPE % For 7 days by using holt's Linear trend Model for ==> (Daily Covid 19 Infection cases in Russia)"
MAPE_Mean_All.Holt_Model<-round(mean(MAPE_Per_Day),3)
MAPE_Mean_All.Holt<-paste(round(mean(MAPE_Per_Day),3),"% MAPE ",validation_data_days,frequency,y_lab,sep=" ")
MAPE_holt<-paste(round(MAPE_Per_Day,3),"%")
MAPE_holt_Model<-paste(MAPE_Per_Day ,"%")
paste (" MAPE that's Error of Forecasting for ",validation_data_days," days in holt's Linear trend Model for ==> ",y_lab, sep=" ")
## [1] " MAPE that's Error of Forecasting for 7 days in holt's Linear trend Model for ==> (Daily Covid 19 Infection cases in Russia)"
paste(MAPE_Mean_All.Holt,"%")
## [1] "3.937 % MAPE 7 days (Daily Covid 19 Infection cases in Russia) %"
paste ("MAPE that's Error of Forecasting day by day for ",validation_data_days," days in holt's Linear trend Model for ==> ",y_lab, sep=" ")
## [1] "MAPE that's Error of Forecasting day by day for 7 days in holt's Linear trend Model for ==> (Daily Covid 19 Infection cases in Russia)"
print(ascii(data.frame(date_holt=validation_dates,validation_data_by_name,actual_data=testing_data,forecasting_holt=validation_forecast,MAPE_holt_Model)), type = "rest")
##
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | | date_holt | validation_data_by_name | actual_data | forecasting_holt | MAPE_holt_Model |
## +===+============+=========================+=============+==================+=================+
## | 1 | 2021-07-25 | Sunday | 24072.00 | 24041.11 | 0.128 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 2 | 2021-07-26 | Monday | 23239.00 | 24108.20 | 3.74 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 3 | 2021-07-27 | Tuesday | 23032.00 | 24175.37 | 4.964 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 4 | 2021-07-28 | Wednesday | 22420.00 | 24242.61 | 8.129 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 5 | 2021-07-29 | Thursday | 23270.00 | 24309.92 | 4.469 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 6 | 2021-07-30 | Friday | 23564.00 | 24377.31 | 3.451 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
## | 7 | 2021-07-31 | Saturday | 23807.00 | 24444.77 | 2.679 % |
## +---+------------+-------------------------+-------------+------------------+-----------------+
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting_by_holt=tail(forecasting_holt$mean,N_forecasting_days),Lower=tail(forecasting_holt$lower,N_forecasting_days),Upper=tail(forecasting_holt$upper,N_forecasting_days))), type = "rest")
##
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | | FD | forecating_date | forecasting_by_holt | Lower.80. | Lower.95. | Upper.80. | Upper.95. |
## +===+============+=================+=====================+===========+===========+===========+===========+
## | 1 | 2021-08-01 | Sunday | 24512.30 | 21790.52 | 20401.06 | 27358.34 | 28914.24 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 2 | 2021-08-02 | Monday | 24579.91 | 21621.56 | 20116.38 | 27685.23 | 29387.30 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 3 | 2021-08-03 | Tuesday | 24647.59 | 21450.96 | 19830.07 | 28016.06 | 29867.16 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 4 | 2021-08-04 | Wednesday | 24715.34 | 21278.35 | 19541.63 | 28351.25 | 30354.51 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 5 | 2021-08-05 | Thursday | 24783.17 | 21103.52 | 19250.75 | 28691.14 | 30849.88 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 6 | 2021-08-06 | Friday | 24851.07 | 20926.29 | 18957.23 | 29035.99 | 31353.70 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 7 | 2021-08-07 | Saturday | 24919.04 | 20746.56 | 18660.96 | 29386.00 | 31866.32 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
plot(forecasting_holt)
x1_test <- ts(testing_data, start =(rows-validation_data_days+1) )
lines(x1_test, col='red',lwd=2)

graph4<-autoplot(forecasting_holt,xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab)
graph4+scale_y_continuous(labels = scales::comma)+
forecast::autolayer(forecasting_holt$mean, series="Holt's Linear Trend 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"))

##################
#Auto arima model#
##################
paste ("tests For Check Stationarity in series ==> ",y_lab, sep=" ")
## [1] "tests For Check Stationarity in series ==> (Daily Covid 19 Infection cases in Russia)"
kpss.test(data_series) # applay kpss test
## Warning in kpss.test(data_series): p-value smaller than printed p-value
##
## KPSS Test for Level Stationarity
##
## data: data_series
## KPSS Level = 3.7624, Truncation lag parameter = 6, p-value = 0.01
pp.test(data_series) # applay pp test
##
## Phillips-Perron Unit Root Test
##
## data: data_series
## Dickey-Fuller Z(alpha) = -2.148, Truncation lag parameter = 6, p-value
## = 0.9644
## alternative hypothesis: stationary
adf.test(data_series) # applay adf test
##
## Augmented Dickey-Fuller Test
##
## data: data_series
## Dickey-Fuller = -2.4073, Lag order = 8, p-value = 0.4059
## alternative hypothesis: stationary
ndiffs(data_series) # Doing first diffrencing on data
## [1] 1
#Taking the first difference
diff1_x1<-diff(data_series)
autoplot(diff1_x1, xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab,main = "1nd differenced series")

##Testing the stationary of the first differenced series
paste ("tests For Check Stationarity in series after taking first differences in ==> ",y_lab, sep=" ")
## [1] "tests For Check Stationarity in series after taking first differences in ==> (Daily Covid 19 Infection cases in Russia)"
kpss.test(diff1_x1) # applay kpss test after taking first differences
## Warning in kpss.test(diff1_x1): p-value greater than printed p-value
##
## KPSS Test for Level Stationarity
##
## data: diff1_x1
## KPSS Level = 0.24153, Truncation lag parameter = 6, p-value = 0.1
pp.test(diff1_x1) # applay pp test after taking first differences
## Warning in pp.test(diff1_x1): p-value smaller than printed p-value
##
## Phillips-Perron Unit Root Test
##
## data: diff1_x1
## Dickey-Fuller Z(alpha) = -576.94, Truncation lag parameter = 6, p-value
## = 0.01
## alternative hypothesis: stationary
adf.test(diff1_x1) # applay adf test after taking first differences
##
## Augmented Dickey-Fuller Test
##
## data: diff1_x1
## Dickey-Fuller = -3.4991, Lag order = 8, p-value = 0.04239
## alternative hypothesis: stationary
#Taking the second difference
diff2_x1=diff(diff1_x1)
autoplot(diff2_x1, xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab ,main = "2nd differenced series")

##Testing the stationary of the first differenced series
paste ("tests For Check Stationarity in series after taking Second differences in",y_lab, sep=" ")
## [1] "tests For Check Stationarity in series after taking Second differences in (Daily Covid 19 Infection cases in Russia)"
kpss.test(diff2_x1) # applay kpss test after taking Second differences
## Warning in kpss.test(diff2_x1): p-value greater than printed p-value
##
## KPSS Test for Level Stationarity
##
## data: diff2_x1
## KPSS Level = 0.0095809, Truncation lag parameter = 6, p-value = 0.1
pp.test(diff2_x1) # applay pp test after taking Second differences
## Warning in pp.test(diff2_x1): p-value smaller than printed p-value
##
## Phillips-Perron Unit Root Test
##
## data: diff2_x1
## Dickey-Fuller Z(alpha) = -663.63, Truncation lag parameter = 6, p-value
## = 0.01
## alternative hypothesis: stationary
adf.test(diff2_x1) # applay adf test after taking Second differences
## Warning in adf.test(diff2_x1): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: diff2_x1
## Dickey-Fuller = -14.836, Lag order = 8, p-value = 0.01
## alternative hypothesis: stationary
####Fitting an ARIMA Model
#1. Using auto arima function
model1 <- auto.arima(data_series,stepwise=FALSE, approximation=FALSE, trace=T, test = c("kpss", "adf", "pp")) #applaying auto arima
##
## ARIMA(0,1,0) : 8922.85
## ARIMA(0,1,0) with drift : 8922.253
## ARIMA(0,1,1) : 8923.856
## ARIMA(0,1,1) with drift : 8923.002
## ARIMA(0,1,2) : 8925.281
## ARIMA(0,1,2) with drift : 8924.201
## ARIMA(0,1,3) : 8927.28
## ARIMA(0,1,3) with drift : 8926.165
## ARIMA(0,1,4) : 8929.112
## ARIMA(0,1,4) with drift : 8928.012
## ARIMA(0,1,5) : 8909.696
## ARIMA(0,1,5) with drift : 8908.962
## ARIMA(1,1,0) : 8923.92
## ARIMA(1,1,0) with drift : 8923.097
## ARIMA(1,1,1) : 8925.466
## ARIMA(1,1,1) with drift : 8924.427
## ARIMA(1,1,2) : 8927.317
## ARIMA(1,1,2) with drift : 8926.17
## ARIMA(1,1,3) : Inf
## ARIMA(1,1,3) with drift : 8928.211
## ARIMA(1,1,4) : 8931.157
## ARIMA(1,1,4) with drift : 8930.063
## ARIMA(2,1,0) : 8925.348
## ARIMA(2,1,0) with drift : 8924.321
## ARIMA(2,1,1) : Inf
## ARIMA(2,1,1) with drift : 8926.143
## ARIMA(2,1,2) : 8929.326
## ARIMA(2,1,2) with drift : 8928.244
## ARIMA(2,1,3) : Inf
## ARIMA(2,1,3) with drift : Inf
## ARIMA(3,1,0) : 8927.32
## ARIMA(3,1,0) with drift : 8926.221
## ARIMA(3,1,1) : 8929.265
## ARIMA(3,1,1) with drift : 8928.1
## ARIMA(3,1,2) : Inf
## ARIMA(3,1,2) with drift : Inf
## ARIMA(4,1,0) : 8928.551
## ARIMA(4,1,0) with drift : 8927.196
## ARIMA(4,1,1) : 8930.55
## ARIMA(4,1,1) with drift : 8929.174
## ARIMA(5,1,0) : 8929.947
## ARIMA(5,1,0) with drift : 8928.331
##
##
##
## Best model: ARIMA(0,1,5) with drift
model1 # show the result of autoarima
## Series: data_series
## ARIMA(0,1,5) with drift
##
## Coefficients:
## ma1 ma2 ma3 ma4 ma5 drift
## -0.2059 -0.2560 -0.0376 0.2183 0.2435 41.1185
## s.e. 0.0420 0.0433 0.0377 0.0497 0.0346 24.4918
##
## sigma^2 estimated as 373621: log likelihood=-4447.38
## AIC=8908.76 AICc=8908.96 BIC=8939.16
#Make changes in the source of auto arima to run the best model
arima.string <- function (object, padding = FALSE)
{
order <- object$arma[c(1, 6, 2, 3, 7, 4, 5)]
m <- order[7]
result <- paste("ARIMA(", order[1], ",", order[2], ",",
order[3], ")", sep = "")
if (m > 1 && sum(order[4:6]) > 0) {
result <- paste(result, "(", order[4], ",", order[5],
",", order[6], ")[", m, "]", sep = "")
}
if (padding && m > 1 && sum(order[4:6]) == 0) {
result <- paste(result, " ", sep = "")
if (m <= 9) {
result <- paste(result, " ", sep = "")
}
else if (m <= 99) {
result <- paste(result, " ", sep = "")
}
else {
result <- paste(result, " ", sep = "")
}
}
if (!is.null(object$xreg)) {
if (NCOL(object$xreg) == 1 && is.element("drift", names(object$coef))) {
result <- paste(result, "with drift ")
}
else {
result <- paste("Regression with", result, "errors")
}
}
else {
if (is.element("constant", names(object$coef)) || is.element("intercept",
names(object$coef))) {
result <- paste(result, "with non-zero mean")
}
else if (order[2] == 0 && order[5] == 0) {
result <- paste(result, "with zero mean ")
}
else {
result <- paste(result, " ")
}
}
if (!padding) {
result <- gsub("[ ]*$", "", result)
}
return(result)
}
bestmodel <- arima.string(model1, padding = TRUE)
bestmodel <- substring(bestmodel,7,11)
bestmodel <- gsub(" ", "", bestmodel)
bestmodel <- gsub(")", "", bestmodel)
bestmodel <- strsplit(bestmodel, ",")[[1]]
bestmodel <- c(strtoi(bestmodel[1]),strtoi(bestmodel[2]),strtoi(bestmodel[3]))
bestmodel
## [1] 0 1 5
strtoi(bestmodel[3])
## [1] 5
#2. Using ACF and PACF Function
#par(mfrow=c(1,2)) # Code for making two plot in one graph
acf(diff2_x1,xlab = paste ("Time in", frequency ,y_lab , sep=" ") , ylab=y_lab, main=paste("ACF-2nd differenced series ",y_lab, sep=" ",lag.max=20)) # plot ACF "auto correlation function after taking second diffrences

pacf(diff2_x1,xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab,main=paste("PACF-2nd differenced series ",y_lab, sep=" ",lag.max=20)) # plot PACF " Partial auto correlation function after taking second diffrences

x1_model1= arima(data_series, order=c(bestmodel)) # Run Best model of auto arima for forecasting
x1_model1 # Show result of best model of auto arima
##
## Call:
## arima(x = data_series, order = c(bestmodel))
##
## Coefficients:
## ma1 ma2 ma3 ma4 ma5
## -0.2031 -0.2554 -0.0368 0.2209 0.2461
## s.e. 0.0419 0.0432 0.0376 0.0496 0.0345
##
## sigma^2 estimated as 371482: log likelihood = -4448.77, aic = 8909.55
paste ("accuracy of autoarima Model For ==> ",y_lab, sep=" ")
## [1] "accuracy of autoarima Model For ==> (Daily Covid 19 Infection cases in Russia)"
accuracy(x1_model1) # aacuracy of best model from auto arima
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 42.43636 608.9573 399.002 NaN Inf 1.005261 0.05722281
x1_model1$x # show result of best model from auto arima
## NULL
checkresiduals(x1_model1,xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab) # checkresiduals from best model from using auto arima

##
## Ljung-Box test
##
## data: Residuals from ARIMA(0,1,5)
## Q* = 100.67, df = 5, p-value < 2.2e-16
##
## Model df: 5. Total lags used: 10
paste("Box-Ljung test , Ljung-Box test For Modelling for ==> ",y_lab, sep=" ")
## [1] "Box-Ljung test , Ljung-Box test For Modelling for ==> (Daily Covid 19 Infection cases in Russia)"
Box.test(x1_model1$residuals^2, lag=20, type="Ljung-Box") # Do test for resdulas by using Box-Ljung test , Ljung-Box test For Modelling
##
## Box-Ljung test
##
## data: x1_model1$residuals^2
## X-squared = 576.4, df = 20, p-value < 2.2e-16
jarque.bera.test(x1_model1$residuals) # Do test jarque.bera.test
##
## Jarque Bera Test
##
## data: x1_model1$residuals
## X-squared = 123.61, df = 2, p-value < 2.2e-16
#Actual Vs Fitted
plot(data_series, col='red',lwd=2, main="Actual vs Fitted Plot", xlab='Time in (days)', ylab=y_lab) # plot actual and Fitted model
lines(fitted(x1_model1), col='black')

#Test data
x1_test <- ts(testing_data, start =(rows-validation_data_days+1) ) # make testing data in time series
forecasting_auto_arima <- forecast(x1_model1, h=N_forecasting_days+validation_data_days)
validation_forecast<-head(forecasting_auto_arima$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 bats Model for ==> ",y_lab, sep=" ")
## [1] "MAPE % For 7 days by using bats Model for ==> (Daily Covid 19 Infection cases in Russia)"
MAPE_Mean_All.ARIMA_Model<-round(mean(MAPE_Per_Day),3)
MAPE_Mean_All.ARIMA<-paste(round(mean(MAPE_Per_Day),3),"% MAPE ",validation_data_days,frequency,y_lab,sep=" ")
MAPE_auto_arima<-paste(round(MAPE_Per_Day,3),"%")
MAPE_auto.arima_Model<-paste(MAPE_Per_Day ,"%")
paste (" MAPE that's Error of Forecasting for ",validation_data_days," days in bats Model for ==> ",y_lab, sep=" ")
## [1] " MAPE that's Error of Forecasting for 7 days in bats Model for ==> (Daily Covid 19 Infection cases in Russia)"
paste(MAPE_Mean_All.ARIMA,"%")
## [1] "1.676 % MAPE 7 days (Daily Covid 19 Infection cases in Russia) %"
paste ("MAPE that's Error of Forecasting day by day for ",validation_data_days," days in bats Model for ==> ",y_lab, sep=" ")
## [1] "MAPE that's Error of Forecasting day by day for 7 days in bats Model for ==> (Daily Covid 19 Infection cases in Russia)"
print(ascii(data.frame(date_auto.arima=validation_dates,validation_data_by_name,actual_data=testing_data,forecasting_auto.arima=validation_forecast,MAPE_auto.arima_Model)), type = "rest")
##
## +---+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | | date_auto.arima | validation_data_by_name | actual_data | forecasting_auto.arima | MAPE_auto.arima_Model |
## +===+=================+=========================+=============+========================+=======================+
## | 1 | 2021-07-25 | Sunday | 24072.00 | 23640.87 | 1.791 % |
## +---+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 2 | 2021-07-26 | Monday | 23239.00 | 23533.30 | 1.266 % |
## +---+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 3 | 2021-07-27 | Tuesday | 23032.00 | 23443.22 | 1.785 % |
## +---+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 4 | 2021-07-28 | Wednesday | 22420.00 | 23358.88 | 4.188 % |
## +---+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 5 | 2021-07-29 | Thursday | 23270.00 | 23463.91 | 0.833 % |
## +---+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 6 | 2021-07-30 | Friday | 23564.00 | 23463.91 | 0.425 % |
## +---+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 7 | 2021-07-31 | Saturday | 23807.00 | 23463.91 | 1.441 % |
## +---+-----------------+-------------------------+-------------+------------------------+-----------------------+
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting_by_auto.arima=tail(forecasting_auto_arima$mean,N_forecasting_days),Lower=tail(forecasting_auto_arima$lower,N_forecasting_days),Upper=tail(forecasting_auto_arima$upper,N_forecasting_days))), type = "rest")
##
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
## | | FD | forecating_date | forecasting_by_auto.arima | Lower.80. | Lower.95. | Upper.80. | Upper.95. |
## +===+============+=================+===========================+===========+===========+===========+===========+
## | 1 | 2021-08-01 | Sunday | 23463.91 | 21625.02 | 20651.57 | 25302.80 | 26276.25 |
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
## | 2 | 2021-08-02 | Monday | 23463.91 | 21474.53 | 20421.42 | 25453.29 | 26506.41 |
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
## | 3 | 2021-08-03 | Tuesday | 23463.91 | 21334.65 | 20207.49 | 25593.17 | 26720.34 |
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
## | 4 | 2021-08-04 | Wednesday | 23463.91 | 21203.41 | 20006.77 | 25724.41 | 26921.05 |
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
## | 5 | 2021-08-05 | Thursday | 23463.91 | 21079.38 | 19817.09 | 25848.44 | 27110.74 |
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
## | 6 | 2021-08-06 | Friday | 23463.91 | 20961.49 | 19636.79 | 25966.33 | 27291.03 |
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
## | 7 | 2021-08-07 | Saturday | 23463.91 | 20848.92 | 19464.62 | 26078.91 | 27463.21 |
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
plot(forecasting_auto_arima)
x1_test <- ts(testing_data, start =(rows-validation_data_days+1) )
lines(x1_test, col='red',lwd=2)

graph5<-autoplot(forecasting_auto_arima,xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab)
graph5+scale_y_continuous(labels = scales::comma)+
forecast::autolayer(forecasting_auto_arima$mean, series="auto.arima 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"))

#########################################################################################
# Returns local linear forecasts and prediction intervals using cubic smoothing splines.#
# Testing Data Evaluation #
#########################################################################################
forecasting_splinef <- splinef(original_data,h=N_forecasting_days+validation_data_days)
summary(forecasting_splinef)
##
## Forecast method: Cubic Smoothing Spline
##
## Model Information:
## $beta
## [1] 46.15989
##
## $call
## splinef(y = original_data, h = N_forecasting_days + validation_data_days)
##
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.2995464 655.3222 432.3311 NaN Inf 1.087395 0.3248142
##
## Forecasts:
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 577 23334.87 22495.00 24174.74 22050.40 24619.34
## 578 23354.20 22403.10 24305.29 21899.63 24808.77
## 579 23373.53 22279.56 24467.49 21700.45 25046.60
## 580 23392.85 22128.46 24657.24 21459.13 25326.57
## 581 23412.18 21953.28 24871.08 21180.99 25643.37
## 582 23431.51 21756.24 25106.78 20869.40 25993.61
## 583 23450.84 21540.86 25360.81 20529.78 26371.89
## 584 23470.16 21309.08 25631.25 20165.08 26775.25
## 585 23489.49 21061.80 25917.18 19776.66 27202.33
## 586 23508.82 20800.88 26216.76 19367.39 27650.25
## 587 23528.15 20526.91 26529.39 18938.14 28118.15
## 588 23547.48 20240.63 26854.32 18490.09 28604.86
## 589 23566.80 19942.85 27190.76 18024.44 29109.17
## 590 23586.13 19634.04 27538.23 17541.92 29630.34
validation_forecast<-head(forecasting_splinef$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 cubic smoothing splines Model for ==> ",y_lab, sep=" ")
## [1] "MAPE % For 7 days by using cubic smoothing splines Model for ==> (Daily Covid 19 Infection cases in Russia)"
MAPE_Mean_All.splinef_Model<-round(mean(MAPE_Per_Day),3)
MAPE_Mean_All.splinef<-paste(round(mean(MAPE_Per_Day),3),"% MAPE ",validation_data_days,frequency,y_lab,sep=" ")
MAPE_splinef<-paste(round(MAPE_Per_Day,3),"%")
MAPE_splinef_Model<-paste(MAPE_Per_Day ,"%")
paste (" MAPE that's Error of Forecasting for ",validation_data_days," days in cubic smoothing splines Model for ==> ",y_lab, sep=" ")
## [1] " MAPE that's Error of Forecasting for 7 days in cubic smoothing splines Model for ==> (Daily Covid 19 Infection cases in Russia)"
paste(MAPE_Mean_All.splinef,"%")
## [1] "1.721 % MAPE 7 days (Daily Covid 19 Infection cases in Russia) %"
paste ("MAPE that's Error of Forecasting day by day for ",validation_data_days," days in cubic smoothing splines Model for ==> ",y_lab, sep=" ")
## [1] "MAPE that's Error of Forecasting day by day for 7 days in cubic smoothing splines Model for ==> (Daily Covid 19 Infection cases in Russia)"
print(ascii(data.frame(date_splinef=validation_dates,validation_data_by_name,actual_data=testing_data,forecasting_splinef=validation_forecast,MAPE_splinef_Model)), type = "rest")
##
## +---+--------------+-------------------------+-------------+----------+--------------------+
## | | date_splinef | validation_data_by_name | actual_data | Series.1 | MAPE_splinef_Model |
## +===+==============+=========================+=============+==========+====================+
## | 1 | 2021-07-25 | Sunday | 24072.00 | 23334.87 | 3.062 % |
## +---+--------------+-------------------------+-------------+----------+--------------------+
## | 2 | 2021-07-26 | Monday | 23239.00 | 23354.20 | 0.496 % |
## +---+--------------+-------------------------+-------------+----------+--------------------+
## | 3 | 2021-07-27 | Tuesday | 23032.00 | 23373.53 | 1.483 % |
## +---+--------------+-------------------------+-------------+----------+--------------------+
## | 4 | 2021-07-28 | Wednesday | 22420.00 | 23392.85 | 4.339 % |
## +---+--------------+-------------------------+-------------+----------+--------------------+
## | 5 | 2021-07-29 | Thursday | 23270.00 | 23412.18 | 0.611 % |
## +---+--------------+-------------------------+-------------+----------+--------------------+
## | 6 | 2021-07-30 | Friday | 23564.00 | 23431.51 | 0.562 % |
## +---+--------------+-------------------------+-------------+----------+--------------------+
## | 7 | 2021-07-31 | Saturday | 23807.00 | 23450.84 | 1.496 % |
## +---+--------------+-------------------------+-------------+----------+--------------------+
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting_by_splinef=tail(forecasting_splinef$mean,N_forecasting_days),Lower=tail(forecasting_holt$lower,N_forecasting_days),Upper=tail(forecasting_holt$upper,N_forecasting_days))), type = "rest")
##
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
## | | FD | forecating_date | Series.1 | Lower.80. | Lower.95. | Upper.80. | Upper.95. |
## +===+============+=================+==========+===========+===========+===========+===========+
## | 1 | 2021-08-01 | Sunday | 23470.16 | 21790.52 | 20401.06 | 27358.34 | 28914.24 |
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
## | 2 | 2021-08-02 | Monday | 23489.49 | 21621.56 | 20116.38 | 27685.23 | 29387.30 |
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
## | 3 | 2021-08-03 | Tuesday | 23508.82 | 21450.96 | 19830.07 | 28016.06 | 29867.16 |
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
## | 4 | 2021-08-04 | Wednesday | 23528.15 | 21278.35 | 19541.63 | 28351.25 | 30354.51 |
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
## | 5 | 2021-08-05 | Thursday | 23547.48 | 21103.52 | 19250.75 | 28691.14 | 30849.88 |
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
## | 6 | 2021-08-06 | Friday | 23566.80 | 20926.29 | 18957.23 | 29035.99 | 31353.70 |
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
## | 7 | 2021-08-07 | Saturday | 23586.13 | 20746.56 | 18660.96 | 29386.00 | 31866.32 |
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
plot(forecasting_splinef)
x1_test <- ts(testing_data, start =(rows-validation_data_days+1) )
lines(x1_test, col='red',lwd=2)

graph6<-autoplot(forecasting_splinef,xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab)
graph6+scale_y_continuous(labels = scales::comma)+
forecast::autolayer(forecasting_splinef$mean, series="cubic smoothing splines 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"))

######################
#Ensembling (Average)#
######################
re_NNAR<-forecasting_NNAR$mean
re_BATS<-forecasting_bats$mean
re_TBATS<-forecasting_tbats$mean
re_holt<-forecasting_holt$mean
re_autoarima<-forecasting_auto_arima$mean
splinef_model<-data.frame(forecasting_splinef)
splinef<-splinef_model$Point.Forecast
result_df<-data.frame(re_NNAR,re_BATS,re_TBATS,re_holt,re_autoarima,splinef)
average_models<-rowMeans(result_df)
# Testing Data Evaluation
Ensembling_average1<-head(average_models,validation_data_days)
MAPE_Per_Day<-round(abs(((testing_data-Ensembling_average1)/testing_data)*100) ,3)
paste ("MAPE % For ",validation_data_days,frequency,"by using Ensembling (Average) for ==> ",y_lab, sep=" ")
## [1] "MAPE % For 7 days by using Ensembling (Average) for ==> (Daily Covid 19 Infection cases in Russia)"
MAPE_Mean_EnsemblingAverage<-round(mean(MAPE_Per_Day),3)
MAPE_Mean_Ensembling<-paste(round(mean(MAPE_Per_Day),3),"% MAPE ",validation_data_days,frequency,y_lab,sep=" ")
MAPE_Ensembling<-paste(round(MAPE_Per_Day,3),"%")
MAPE_Ensembling_Model<-paste(MAPE_Per_Day ,"%")
paste (" MAPE that's Error of Forecasting for ",validation_data_days," days in Ensembling Model for ==> ",y_lab, sep=" ")
## [1] " MAPE that's Error of Forecasting for 7 days in Ensembling Model for ==> (Daily Covid 19 Infection cases in Russia)"
paste(MAPE_Mean_EnsemblingAverage,"%")
## [1] "1.693 %"
paste ("MAPE that's Error of Forecasting day by day for ",validation_data_days," days in Ensembling (Average) for ==> ",y_lab, sep=" ")
## [1] "MAPE that's Error of Forecasting day by day for 7 days in Ensembling (Average) for ==> (Daily Covid 19 Infection cases in Russia)"
print(ascii(data.frame(date_Ensembling=validation_dates,validation_data_by_name,actual_data=testing_data,Ensembling=Ensembling_average1,MAPE_Ensembling)), type = "rest")
##
## +---+-----------------+-------------------------+-------------+------------+-----------------+
## | | date_Ensembling | validation_data_by_name | actual_data | Ensembling | MAPE_Ensembling |
## +===+=================+=========================+=============+============+=================+
## | 1 | 2021-07-25 | Sunday | 24072.00 | 23733.11 | 1.408 % |
## +---+-----------------+-------------------------+-------------+------------+-----------------+
## | 2 | 2021-07-26 | Monday | 23239.00 | 23573.02 | 1.437 % |
## +---+-----------------+-------------------------+-------------+------------+-----------------+
## | 3 | 2021-07-27 | Tuesday | 23032.00 | 23465.30 | 1.881 % |
## +---+-----------------+-------------------------+-------------+------------+-----------------+
## | 4 | 2021-07-28 | Wednesday | 22420.00 | 23474.12 | 4.702 % |
## +---+-----------------+-------------------------+-------------+------------+-----------------+
## | 5 | 2021-07-29 | Thursday | 23270.00 | 23594.00 | 1.392 % |
## +---+-----------------+-------------------------+-------------+------------+-----------------+
## | 6 | 2021-07-30 | Friday | 23564.00 | 23667.49 | 0.439 % |
## +---+-----------------+-------------------------+-------------+------------+-----------------+
## | 7 | 2021-07-31 | Saturday | 23807.00 | 23666.51 | 0.59 % |
## +---+-----------------+-------------------------+-------------+------------+-----------------+
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,Ensembling_Average=tail(average_models,N_forecasting_days))), type = "rest")
##
## +---+------------+-----------------+--------------------+
## | | FD | forecating_date | Ensembling_Average |
## +===+============+=================+====================+
## | 1 | 2021-08-01 | Sunday | 23605.48 |
## +---+------------+-----------------+--------------------+
## | 2 | 2021-08-02 | Monday | 23510.81 |
## +---+------------+-----------------+--------------------+
## | 3 | 2021-08-03 | Tuesday | 23439.83 |
## +---+------------+-----------------+--------------------+
## | 4 | 2021-08-04 | Wednesday | 23473.64 |
## +---+------------+-----------------+--------------------+
## | 5 | 2021-08-05 | Thursday | 23582.38 |
## +---+------------+-----------------+--------------------+
## | 6 | 2021-08-06 | Friday | 23649.28 |
## +---+------------+-----------------+--------------------+
## | 7 | 2021-08-07 | Saturday | 23657.61 |
## +---+------------+-----------------+--------------------+
#############################
#Ensembling (weight average)#
#############################
weight.model<-0.90# priotizer the weights ( weight average)
re_NNAR<-forecasting_NNAR$mean
re_BATS<-forecasting_bats$mean
re_TBATS<-forecasting_tbats$mean
re_holt<-forecasting_holt$mean
re_autoarima<-forecasting_auto_arima$mean
re_splinef<-c(forecasting_splinef$mean)
re_bestmodel<-min(MAPE_Mean_All_NNAR,MAPE_Mean_All.bats_Model,MAPE_Mean_All.TBATS_Model,MAPE_Mean_All.Holt_Model,MAPE_Mean_All.ARIMA_Model,MAPE_Mean_All.splinef_Model)
y1<-if(re_bestmodel >= MAPE_Mean_All.bats_Model) {re_BATS*weight.model
} else {
(re_BATS*(1-weight.model))/5
}
y2<-if(re_bestmodel >= MAPE_Mean_All.TBATS_Model) {re_TBATS*weight.model
} else {
(re_TBATS*(1-weight.model))/5
}
y3<-if(re_bestmodel >= MAPE_Mean_All.Holt_Model) {re_holt*weight.model
} else {
(re_holt*(1-weight.model))/5
}
y4<-if(re_bestmodel >= MAPE_Mean_All.ARIMA_Model) {re_autoarima*weight.model
} else {
(re_autoarima*(1-weight.model))/5
}
y5<-if(re_bestmodel >= MAPE_Mean_All_NNAR) {re_NNAR*weight.model
} else {
(re_NNAR*(1-weight.model))/5
}
y6<-if(re_bestmodel >= MAPE_Mean_All.splinef_Model) {re_splinef*weight.model
} else {
(splinef*(1-weight.model))/5
}
Ensembling.weight<-(y1+y2+y3+y4+y5+y6)
# Testing Data Evaluation
validation_forecast2<-head(Ensembling.weight,validation_data_days)
MAPE_Per_Day<-round(abs(((testing_data-validation_forecast2)/testing_data)*100) ,3)
paste ("MAPE % For ",validation_data_days,frequency,"by using Ensembling (weight average) for ==> ",y_lab, sep=" ")
## [1] "MAPE % For 7 days by using Ensembling (weight average) for ==> (Daily Covid 19 Infection cases in Russia)"
MAPE_Mean_EnsemblingAverage1<-round(mean(MAPE_Per_Day),3)
MAPE_Mean_Ensembling<-paste(round(mean(MAPE_Per_Day),3),"% MAPE ",validation_data_days,frequency,y_lab,sep=" ")
MAPE_Ensembling<-paste(round(MAPE_Per_Day,3),"%")
MAPE_Ensembling_Model<-paste(MAPE_Per_Day ,"%")
paste (" MAPE that's Error of Forecasting for ",validation_data_days," days in Ensembling weight average for ==> ",y_lab, sep=" ")
## [1] " MAPE that's Error of Forecasting for 7 days in Ensembling weight average for ==> (Daily Covid 19 Infection cases in Russia)"
paste(MAPE_Mean_EnsemblingAverage1,"%")
## [1] "1.317 %"
paste ("MAPE that's Error of Forecasting day by day for ",validation_data_days," days in Ensembling weight average for ==> ",y_lab, sep=" ")
## [1] "MAPE that's Error of Forecasting day by day for 7 days in Ensembling weight average for ==> (Daily Covid 19 Infection cases in Russia)"
print(ascii(data.frame(date_Ensembling=validation_dates,validation_data_by_name,actual_data=testing_data,Ensembling=validation_forecast2,MAPE_Ensembling)), type = "rest")
##
## +---+-----------------+-------------------------+-------------+------------+-----------------+
## | | date_Ensembling | validation_data_by_name | actual_data | Ensembling | MAPE_Ensembling |
## +===+=================+=========================+=============+============+=================+
## | 1 | 2021-07-25 | Sunday | 24072.00 | 23728.79 | 1.426 % |
## +---+-----------------+-------------------------+-------------+------------+-----------------+
## | 2 | 2021-07-26 | Monday | 23239.00 | 23368.42 | 0.557 % |
## +---+-----------------+-------------------------+-------------+------------+-----------------+
## | 3 | 2021-07-27 | Tuesday | 23032.00 | 23025.77 | 0.027 % |
## +---+-----------------+-------------------------+-------------+------------+-----------------+
## | 4 | 2021-07-28 | Wednesday | 22420.00 | 23080.94 | 2.948 % |
## +---+-----------------+-------------------------+-------------+------------+-----------------+
## | 5 | 2021-07-29 | Thursday | 23270.00 | 23298.12 | 0.121 % |
## +---+-----------------+-------------------------+-------------+------------+-----------------+
## | 6 | 2021-07-30 | Friday | 23564.00 | 23240.54 | 1.373 % |
## +---+-----------------+-------------------------+-------------+------------+-----------------+
## | 7 | 2021-07-31 | Saturday | 23807.00 | 23148.24 | 2.767 % |
## +---+-----------------+-------------------------+-------------+------------+-----------------+
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting_by_Ensembling=tail(Ensembling.weight,N_forecasting_days))), type = "rest")
##
## +---+------------+-----------------+---------------------------+
## | | FD | forecating_date | forecasting_by_Ensembling |
## +===+============+=================+===========================+
## | 1 | 2021-08-01 | Sunday | 23061.68 |
## +---+------------+-----------------+---------------------------+
## | 2 | 2021-08-02 | Monday | 22831.16 |
## +---+------------+-----------------+---------------------------+
## | 3 | 2021-08-03 | Tuesday | 22692.93 |
## +---+------------+-----------------+---------------------------+
## | 4 | 2021-08-04 | Wednesday | 22802.59 |
## +---+------------+-----------------+---------------------------+
## | 5 | 2021-08-05 | Thursday | 22955.48 |
## +---+------------+-----------------+---------------------------+
## | 6 | 2021-08-06 | Friday | 22994.57 |
## +---+------------+-----------------+---------------------------+
## | 7 | 2021-08-07 | Saturday | 22973.08 |
## +---+------------+-----------------+---------------------------+
graph8<-autoplot(Ensembling.weight,xlab = paste ("Time in", frequency ,y_lab,"by using Ensembling weight average" , sep=" "), ylab=y_lab)
graph8+scale_y_continuous(labels = scales::comma)+
forecast::autolayer(Ensembling.weight, series="Ensembling weight average",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"))

# Table for MAPE For counry
best_recommended_model <- min(MAPE_Mean_All_NNAR,MAPE_Mean_All.bats_Model,MAPE_Mean_All.TBATS_Model,MAPE_Mean_All.Holt_Model,MAPE_Mean_All.ARIMA_Model,MAPE_Mean_All.splinef_Model,MAPE_Mean_EnsemblingAverage,MAPE_Mean_EnsemblingAverage1)
paste("System Choose Least Error ==> ( MAPE %) of Forecasting by using NNAR model, BATS Model, TBATS Model, Holt's Linear Model , autoarima Model, cubic smoothing splines Model, Ensembling (Average), and Ensembling weight average , for ==> ", y_lab , sep=" ")
## [1] "System Choose Least Error ==> ( MAPE %) of Forecasting by using NNAR model, BATS Model, TBATS Model, Holt's Linear Model , autoarima Model, cubic smoothing splines Model, Ensembling (Average), and Ensembling weight average , for ==> (Daily Covid 19 Infection cases in Russia)"
best_recommended_model
## [1] 1.317
x1<-if(best_recommended_model >= MAPE_Mean_All.bats_Model) {paste("BATS Model")}
x2<-if(best_recommended_model >= MAPE_Mean_All.TBATS_Model) {paste("TBATS Model")}
x3<-if(best_recommended_model >= MAPE_Mean_All.Holt_Model) {paste("Holt Model")}
x4<-if(best_recommended_model >= MAPE_Mean_All.ARIMA_Model) {paste("ARIMA Model")}
x5<-if(best_recommended_model >= MAPE_Mean_All_NNAR) {paste("NNAR Model")}
x6<-if(best_recommended_model >= MAPE_Mean_All.splinef_Model) {paste("cubic smoothing splines")}
x7<-if(best_recommended_model >= MAPE_Mean_EnsemblingAverage) {paste("Ensembling (Average)")}
x8<-if(best_recommended_model >= MAPE_Mean_EnsemblingAverage1) {paste("Ensembling weight average")}
panderOptions('table.split.table', Inf)
paste("Forecasting by using NNAR Model ==> ", y_lab , sep=" ")
## [1] "Forecasting by using NNAR Model ==> (Daily Covid 19 Infection cases in Russia)"
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 | 22987.53 |
## +---+------------+-----------------+---------------------+
## | 2 | 2021-08-02 | Monday | 22738.48 |
## +---+------------+-----------------+---------------------+
## | 3 | 2021-08-03 | Tuesday | 22591.08 |
## +---+------------+-----------------+---------------------+
## | 4 | 2021-08-04 | Wednesday | 22711.09 |
## +---+------------+-----------------+---------------------+
## | 5 | 2021-08-05 | Thursday | 22869.99 |
## +---+------------+-----------------+---------------------+
## | 6 | 2021-08-06 | Friday | 22905.29 |
## +---+------------+-----------------+---------------------+
## | 7 | 2021-08-07 | Saturday | 22879.74 |
## +---+------------+-----------------+---------------------+
paste("Forecasting by using BATS Model ==> ", y_lab , sep=" ")
## [1] "Forecasting by using BATS Model ==> (Daily Covid 19 Infection cases in Russia)"
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting_by_bats=tail(forecasting_bats$mean,N_forecasting_days),lower=tail(forecasting_bats$lower,N_forecasting_days),Upper=tail(forecasting_bats$lower,N_forecasting_days))), type = "rest")
##
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | | FD | forecating_date | forecasting_by_bats | lower.80. | lower.95. | Upper.80. | Upper.95. |
## +===+============+=================+=====================+===========+===========+===========+===========+
## | 1 | 2021-08-01 | Sunday | 22452.66 | 20636.85 | 19675.62 | 20636.85 | 19675.62 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 2 | 2021-08-02 | Monday | 21867.31 | 19827.58 | 18747.82 | 19827.58 | 18747.82 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 3 | 2021-08-03 | Tuesday | 21444.63 | 19192.69 | 18000.59 | 19192.69 | 18000.59 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 4 | 2021-08-04 | Wednesday | 21377.25 | 18928.16 | 17631.69 | 18928.16 | 17631.69 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 5 | 2021-08-05 | Thursday | 21587.74 | 18944.39 | 17545.08 | 18944.39 | 17545.08 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 6 | 2021-08-06 | Friday | 21791.94 | 18939.50 | 17429.51 | 18939.50 | 17429.51 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 7 | 2021-08-07 | Saturday | 21716.01 | 18628.31 | 16993.79 | 18628.31 | 16993.79 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
paste("Forecasting by using TBATS Model ==> ", y_lab , sep=" ")
## [1] "Forecasting by using TBATS Model ==> (Daily Covid 19 Infection cases in Russia)"
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting_by_TBATS=tail(forecasting_tbats$mean,N_forecasting_days),Lower=tail(forecasting_tbats$lower,N_forecasting_days),Upper=tail(forecasting_tbats$upper,N_forecasting_days))), type = "rest")
##
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
## | | FD | forecating_date | forecasting_by_TBATS | Lower.80. | Lower.95. | Upper.80. | Upper.95. |
## +===+============+=================+======================+===========+===========+===========+===========+
## | 1 | 2021-08-01 | Sunday | 24746.34 | 22869.01 | 21875.22 | 26623.66 | 27617.45 |
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
## | 2 | 2021-08-02 | Monday | 24925.77 | 22945.49 | 21897.19 | 26906.06 | 27954.36 |
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
## | 3 | 2021-08-03 | Tuesday | 24982.98 | 22904.82 | 21804.72 | 27061.13 | 28161.24 |
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
## | 4 | 2021-08-04 | Wednesday | 25046.09 | 22874.68 | 21725.20 | 27217.50 | 28366.98 |
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
## | 5 | 2021-08-05 | Thursday | 25241.96 | 22983.81 | 21788.42 | 27500.11 | 28695.50 |
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
## | 6 | 2021-08-06 | Friday | 25316.65 | 22976.90 | 21738.31 | 27656.40 | 28894.99 |
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
## | 7 | 2021-08-07 | Saturday | 25380.81 | 22962.53 | 21682.37 | 27799.10 | 29079.26 |
## +---+------------+-----------------+----------------------+-----------+-----------+-----------+-----------+
paste("Forecasting by using Holt's Linear Trend Model ==> ", y_lab , sep=" ")
## [1] "Forecasting by using Holt's Linear Trend Model ==> (Daily Covid 19 Infection cases in Russia)"
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting_by_holt=tail(forecasting_holt$mean,N_forecasting_days),Lower=tail(forecasting_holt$lower,N_forecasting_days),Upper=tail(forecasting_holt$upper,N_forecasting_days))), type = "rest")
##
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | | FD | forecating_date | forecasting_by_holt | Lower.80. | Lower.95. | Upper.80. | Upper.95. |
## +===+============+=================+=====================+===========+===========+===========+===========+
## | 1 | 2021-08-01 | Sunday | 24512.30 | 21790.52 | 20401.06 | 27358.34 | 28914.24 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 2 | 2021-08-02 | Monday | 24579.91 | 21621.56 | 20116.38 | 27685.23 | 29387.30 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 3 | 2021-08-03 | Tuesday | 24647.59 | 21450.96 | 19830.07 | 28016.06 | 29867.16 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 4 | 2021-08-04 | Wednesday | 24715.34 | 21278.35 | 19541.63 | 28351.25 | 30354.51 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 5 | 2021-08-05 | Thursday | 24783.17 | 21103.52 | 19250.75 | 28691.14 | 30849.88 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 6 | 2021-08-06 | Friday | 24851.07 | 20926.29 | 18957.23 | 29035.99 | 31353.70 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
## | 7 | 2021-08-07 | Saturday | 24919.04 | 20746.56 | 18660.96 | 29386.00 | 31866.32 |
## +---+------------+-----------------+---------------------+-----------+-----------+-----------+-----------+
paste("Forecasting by using ARIMA Model ==> ", y_lab , sep=" ")
## [1] "Forecasting by using ARIMA Model ==> (Daily Covid 19 Infection cases in Russia)"
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting_by_auto.arima=tail(forecasting_auto_arima$mean,N_forecasting_days),Lower=tail(forecasting_auto_arima$lower,N_forecasting_days),Upper=tail(forecasting_auto_arima$upper,N_forecasting_days))), type = "rest")
##
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
## | | FD | forecating_date | forecasting_by_auto.arima | Lower.80. | Lower.95. | Upper.80. | Upper.95. |
## +===+============+=================+===========================+===========+===========+===========+===========+
## | 1 | 2021-08-01 | Sunday | 23463.91 | 21625.02 | 20651.57 | 25302.80 | 26276.25 |
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
## | 2 | 2021-08-02 | Monday | 23463.91 | 21474.53 | 20421.42 | 25453.29 | 26506.41 |
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
## | 3 | 2021-08-03 | Tuesday | 23463.91 | 21334.65 | 20207.49 | 25593.17 | 26720.34 |
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
## | 4 | 2021-08-04 | Wednesday | 23463.91 | 21203.41 | 20006.77 | 25724.41 | 26921.05 |
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
## | 5 | 2021-08-05 | Thursday | 23463.91 | 21079.38 | 19817.09 | 25848.44 | 27110.74 |
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
## | 6 | 2021-08-06 | Friday | 23463.91 | 20961.49 | 19636.79 | 25966.33 | 27291.03 |
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
## | 7 | 2021-08-07 | Saturday | 23463.91 | 20848.92 | 19464.62 | 26078.91 | 27463.21 |
## +---+------------+-----------------+---------------------------+-----------+-----------+-----------+-----------+
paste("Forecasting by using cubic smoothing splines Model ==> ", y_lab , sep=" ")
## [1] "Forecasting by using cubic smoothing splines Model ==> (Daily Covid 19 Infection cases in Russia)"
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting_splinef=tail(forecasting_splinef$mean,N_forecasting_days),Lower=tail(forecasting_splinef$lower,N_forecasting_days),Upper=tail(forecasting_splinef$upper,N_forecasting_days))), type = "rest")
##
## +---+------------+-----------------+----------+----------------+----------------+----------------+----------------+
## | | FD | forecating_date | Series.1 | Lower.Series.1 | Lower.Series.2 | Upper.Series.1 | Upper.Series.2 |
## +===+============+=================+==========+================+================+================+================+
## | 1 | 2021-08-01 | Sunday | 23470.16 | 21309.08 | 20165.08 | 25631.25 | 26775.25 |
## +---+------------+-----------------+----------+----------------+----------------+----------------+----------------+
## | 2 | 2021-08-02 | Monday | 23489.49 | 21061.80 | 19776.66 | 25917.18 | 27202.33 |
## +---+------------+-----------------+----------+----------------+----------------+----------------+----------------+
## | 3 | 2021-08-03 | Tuesday | 23508.82 | 20800.88 | 19367.39 | 26216.76 | 27650.25 |
## +---+------------+-----------------+----------+----------------+----------------+----------------+----------------+
## | 4 | 2021-08-04 | Wednesday | 23528.15 | 20526.91 | 18938.14 | 26529.39 | 28118.15 |
## +---+------------+-----------------+----------+----------------+----------------+----------------+----------------+
## | 5 | 2021-08-05 | Thursday | 23547.48 | 20240.63 | 18490.09 | 26854.32 | 28604.86 |
## +---+------------+-----------------+----------+----------------+----------------+----------------+----------------+
## | 6 | 2021-08-06 | Friday | 23566.80 | 19942.85 | 18024.44 | 27190.76 | 29109.17 |
## +---+------------+-----------------+----------+----------------+----------------+----------------+----------------+
## | 7 | 2021-08-07 | Saturday | 23586.13 | 19634.04 | 17541.92 | 27538.23 | 29630.34 |
## +---+------------+-----------------+----------+----------------+----------------+----------------+----------------+
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting_by_splinef=tail(forecasting_splinef$mean,N_forecasting_days),Lower=tail(forecasting_holt$lower,N_forecasting_days),Upper=tail(forecasting_holt$upper,N_forecasting_days))), type = "rest")
##
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
## | | FD | forecating_date | Series.1 | Lower.80. | Lower.95. | Upper.80. | Upper.95. |
## +===+============+=================+==========+===========+===========+===========+===========+
## | 1 | 2021-08-01 | Sunday | 23470.16 | 21790.52 | 20401.06 | 27358.34 | 28914.24 |
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
## | 2 | 2021-08-02 | Monday | 23489.49 | 21621.56 | 20116.38 | 27685.23 | 29387.30 |
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
## | 3 | 2021-08-03 | Tuesday | 23508.82 | 21450.96 | 19830.07 | 28016.06 | 29867.16 |
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
## | 4 | 2021-08-04 | Wednesday | 23528.15 | 21278.35 | 19541.63 | 28351.25 | 30354.51 |
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
## | 5 | 2021-08-05 | Thursday | 23547.48 | 21103.52 | 19250.75 | 28691.14 | 30849.88 |
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
## | 6 | 2021-08-06 | Friday | 23566.80 | 20926.29 | 18957.23 | 29035.99 | 31353.70 |
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
## | 7 | 2021-08-07 | Saturday | 23586.13 | 20746.56 | 18660.96 | 29386.00 | 31866.32 |
## +---+------------+-----------------+----------+-----------+-----------+-----------+-----------+
result<-c(x1,x2,x3,x4,x5,x6,x7,x8)
table.error<-data.frame(country.name,NNAR.model=MAPE_Mean_All_NNAR, BATS.Model=MAPE_Mean_All.bats_Model,TBATS.Model=MAPE_Mean_All.TBATS_Model,Holt.Model=MAPE_Mean_All.Holt_Model,ARIMA.Model=MAPE_Mean_All.ARIMA_Model,cubic_smoothing.splines=MAPE_Mean_All.splinef_Model,Ensembling_Average=MAPE_Mean_EnsemblingAverage,Ensembling_weight=MAPE_Mean_EnsemblingAverage1,Best.Model=result)
knitr::kable(table.error,caption = paste("Accuracy MAPE % daily Covid-19 infection cases for testing data last" , validation_data_days ,frequency, y_lab , sep=" "))
Accuracy MAPE % daily Covid-19 infection cases for testing data last 7 days (Daily Covid 19 Infection cases in Russia)
| Russia |
1.371 |
2.046 |
4.42 |
3.937 |
1.676 |
1.721 |
1.693 |
1.317 |
Ensembling weight average |
MAPE.Value<-c(MAPE_Mean_All_NNAR,MAPE_Mean_All.bats_Model,MAPE_Mean_All.TBATS_Model,MAPE_Mean_All.Holt_Model,MAPE_Mean_All.ARIMA_Model,MAPE_Mean_All.splinef_Model,MAPE_Mean_EnsemblingAverage,MAPE_Mean_EnsemblingAverage1)
Model<-c("NNAR model","BATS Model","TBATS Model","Holt Model","ARIMA Model","cubic smoothing splines","Ensembling (Average)","Ensembling weight")
channel_data<-data.frame(Model,MAPE.Value)
#comparison and visualization plot accuracy models.
ggplot(channel_data, aes(x = Model, y = MAPE.Value)) +
geom_bar(stat = "identity") +
geom_text(aes(label = MAPE.Value)) + # x AND y INHERITED. WE JUST NEED TO SPECIFY "label"
coord_flip() +
scale_y_continuous(expand = c(0, 0))

message("System finished Modelling and Forecasting by using NNAR, BATS, TBATS, Holt's Linear Trend, ARIMA, cubic smoothing splines, Ensembling (Average), and Ensembling weight ==>",y_lab, sep=" ")
## System finished Modelling and Forecasting by using NNAR, BATS, TBATS, Holt's Linear Trend, ARIMA, cubic smoothing splines, Ensembling (Average), and Ensembling weight ==>(Daily Covid 19 Infection cases in Russia)
message(" Thank you for using our System For Modelling and Forecasting ==> ",y_lab, sep=" ")
## Thank you for using our System For Modelling and Forecasting ==> (Daily Covid 19 Infection cases in Russia)