comparison between two systems for forecasting covid 19 cumulative infected case

cumulative Covid 19 Infection cases In USA
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)
library(pander)
## 
## Attaching package: 'pander'
## The following object is masked from 'package:ascii':
## 
##     Pandoc
library(ascii)
require(tseries) # need to install tseries tj test Stationarity in time series 
library(forecast)   # install library forecast    
library(tseries)
##Global vriable##
Full_original_data <- read.csv("data.csv") # path of your data ( time series data)
original_data<-Full_original_data$cases #Cumulative #cases
y_lab <- "Forecasting cumulative Covid 19 Infection cases in USA"   # input name of data
Actual_date_interval <- c("2020/03/01","2021/05/08")
Forecast_date_interval <- c("2021/05/09","2021/05/30")
validation_data_days <-45
frequency<-"day"
Number_Neural<-5# Number of Neural For model NNAR Model
NNAR_Model<- FALSE     #create new model (TRUE/FALSE)
frequency<-"days"
country.name <- "USA"
# Data Preparation & calculate some of statistics measures
summary(original_data) # Summary your time series
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
##        0  1147668  6072726 10617004 20513513 32257416
# calculate standard deviation 
data.frame(kurtosis=kurtosis(original_data))   # calculate Cofficient of kurtosis
##   kurtosis
## 1  2.01343
data.frame(skewness=skewness(original_data))  # calculate Cofficient of skewness
##    skewness
## 1 0.7693384
data.frame(Standard.deviation =sd(original_data))
##   Standard.deviation
## 1           11198982
#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(1,5) 
## Call:   nnetar(y = data_series, size = Number_Neural)
## 
## Average of 20 networks, each of which is
## a 1-5-1 network with 16 weights
## options were - linear output units 
## 
## sigma^2 estimated as 496887154
# 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  45 days by using NNAR Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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  45  days in NNAR Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
paste(MAPE_Mean_All,"%")
## [1] "3.465 % MAPE  45 days Forecasting cumulative Covid 19 Infection cases in USA %"
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  45  days in NNAR Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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-03-25 | Thursday                | 29653604.00 | 29619474.34      | 0.115 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 2  | 2021-03-26 | Friday                  | 29718930.00 | 29643242.06      | 0.255 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 3  | 2021-03-27 | Saturday                | 29788519.00 | 29666180.46      | 0.411 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 4  | 2021-03-28 | Sunday                  | 29859706.00 | 29688316.99      | 0.574 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 5  | 2021-03-29 | Monday                  | 29921599.00 | 29709678.31      | 0.708 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 6  | 2021-03-30 | Tuesday                 | 29968464.00 | 29730290.28      | 0.795 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 7  | 2021-03-31 | Wednesday               | 30033063.00 | 29750177.99      | 0.942 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 8  | 2021-04-01 | Thursday                | 30095776.00 | 29769365.77      | 1.085 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 9  | 2021-04-02 | Friday                  | 30164185.00 | 29787877.20      | 1.248 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 10 | 2021-04-03 | Saturday                | 30238692.00 | 29805735.16      | 1.432 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 11 | 2021-04-04 | Sunday                  | 30304462.00 | 29822961.80      | 1.589 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 12 | 2021-04-05 | Monday                  | 30372016.00 | 29839578.60      | 1.753 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 13 | 2021-04-06 | Tuesday                 | 30413124.00 | 29855606.35      | 1.833 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 14 | 2021-04-07 | Wednesday               | 30475874.00 | 29871065.21      | 1.985 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 15 | 2021-04-08 | Thursday                | 30541000.00 | 29885974.66      | 2.145 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 16 | 2021-04-09 | Friday                  | 30615849.00 | 29900353.61      | 2.337 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 17 | 2021-04-10 | Saturday                | 30692226.00 | 29914220.34      | 2.535 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 18 | 2021-04-11 | Sunday                  | 30772857.00 | 29927592.52      | 2.747 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 19 | 2021-04-12 | Monday                  | 30840411.00 | 29940487.29      | 2.918 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 20 | 2021-04-13 | Tuesday                 | 30888765.00 | 29952921.21      | 3.03 %          |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 21 | 2021-04-14 | Wednesday               | 30951566.00 | 29964910.30      | 3.188 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 22 | 2021-04-15 | Thursday                | 31029700.00 | 29976470.05      | 3.394 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 23 | 2021-04-16 | Friday                  | 31103006.00 | 29987615.45      | 3.586 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 24 | 2021-04-17 | Saturday                | 31176938.00 | 29998361.00      | 3.78 %          |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 25 | 2021-04-18 | Sunday                  | 31250635.00 | 30008720.70      | 3.974 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 26 | 2021-04-19 | Monday                  | 31311941.00 | 30018708.08      | 4.13 %          |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 27 | 2021-04-20 | Tuesday                 | 31350025.00 | 30028336.25      | 4.216 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 28 | 2021-04-21 | Wednesday               | 31407189.00 | 30037617.85      | 4.361 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 29 | 2021-04-22 | Thursday                | 31467572.00 | 30046565.09      | 4.516 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 30 | 2021-04-23 | Friday                  | 31530214.00 | 30055189.80      | 4.678 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 31 | 2021-04-24 | Saturday                | 31593420.00 | 30063503.37      | 4.843 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 32 | 2021-04-25 | Sunday                  | 31656636.00 | 30071516.83      | 5.007 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 33 | 2021-04-26 | Monday                  | 31708445.00 | 30079240.83      | 5.138 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 34 | 2021-04-27 | Tuesday                 | 31742914.00 | 30086685.64      | 5.218 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 35 | 2021-04-28 | Wednesday               | 31783375.00 | 30093861.19      | 5.316 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 36 | 2021-04-29 | Thursday                | 31835314.00 | 30100777.06      | 5.448 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 37 | 2021-04-30 | Friday                  | 31889171.00 | 30107442.52      | 5.587 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 38 | 2021-05-01 | Saturday                | 31948761.00 | 30113866.50      | 5.743 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 39 | 2021-05-02 | Sunday                  | 32002328.00 | 30120057.62      | 5.882 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 40 | 2021-05-03 | Monday                  | 32047478.00 | 30126024.21      | 5.996 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 41 | 2021-05-04 | Tuesday                 | 32083656.00 | 30131774.32      | 6.084 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 42 | 2021-05-05 | Wednesday               | 32123136.00 | 30137315.68      | 6.182 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 43 | 2021-05-06 | Thursday                | 32167970.00 | 30142655.80      | 6.296 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 44 | 2021-05-07 | Friday                  | 32210817.00 | 30147801.90      | 6.405 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 45 | 2021-05-08 | Saturday                | 32257416.00 | 30152760.94      | 6.525 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
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-05-09 | Sunday          | 30157539.65         |
## +----+------------+-----------------+---------------------+
## | 2  | 2021-05-10 | Monday          | 30162144.52         |
## +----+------------+-----------------+---------------------+
## | 3  | 2021-05-11 | Tuesday         | 30166581.82         |
## +----+------------+-----------------+---------------------+
## | 4  | 2021-05-12 | Wednesday       | 30170857.57         |
## +----+------------+-----------------+---------------------+
## | 5  | 2021-05-13 | Thursday        | 30174977.62         |
## +----+------------+-----------------+---------------------+
## | 6  | 2021-05-14 | Friday          | 30178947.57         |
## +----+------------+-----------------+---------------------+
## | 7  | 2021-05-15 | Saturday        | 30182772.85         |
## +----+------------+-----------------+---------------------+
## | 8  | 2021-05-16 | Sunday          | 30186458.68         |
## +----+------------+-----------------+---------------------+
## | 9  | 2021-05-17 | Monday          | 30190010.12         |
## +----+------------+-----------------+---------------------+
## | 10 | 2021-05-18 | Tuesday         | 30193432.01         |
## +----+------------+-----------------+---------------------+
## | 11 | 2021-05-19 | Wednesday       | 30196729.06         |
## +----+------------+-----------------+---------------------+
## | 12 | 2021-05-20 | Thursday        | 30199905.78         |
## +----+------------+-----------------+---------------------+
## | 13 | 2021-05-21 | Friday          | 30202966.54         |
## +----+------------+-----------------+---------------------+
## | 14 | 2021-05-22 | Saturday        | 30205915.53         |
## +----+------------+-----------------+---------------------+
## | 15 | 2021-05-23 | Sunday          | 30208756.83         |
## +----+------------+-----------------+---------------------+
## | 16 | 2021-05-24 | Monday          | 30211494.32         |
## +----+------------+-----------------+---------------------+
## | 17 | 2021-05-25 | Tuesday         | 30214131.79         |
## +----+------------+-----------------+---------------------+
## | 18 | 2021-05-26 | Wednesday       | 30216672.87         |
## +----+------------+-----------------+---------------------+
## | 19 | 2021-05-27 | Thursday        | 30219121.06         |
## +----+------------+-----------------+---------------------+
## | 20 | 2021-05-28 | Friday          | 30221479.74         |
## +----+------------+-----------------+---------------------+
## | 21 | 2021-05-29 | Saturday        | 30223752.16         |
## +----+------------+-----------------+---------------------+
## | 22 | 2021-05-30 | Sunday          | 30225941.46         |
## +----+------------+-----------------+---------------------+
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

##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 318.276 18242.95 9185.65 -Inf  Inf 0.1384295 0.01047745
# Print Model Parameters
model_bats
## BATS(1, {0,0}, 1, -)
## 
## Call: bats(y = data_series)
## 
## Parameters
##   Alpha: 1.109368
##   Beta: 0.3808696
##   Damping Parameter: 1
## 
## Seed States:
##           [,1]
## [1,] -108.9141
## [2,]  227.8479
## 
## Sigma: 18242.95
## AIC: 11507.36
#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  45 days by using bats Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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  45  days in bats Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
paste(MAPE_Mean_All.bats,"%")
## [1] "0.627 % MAPE  45 days Forecasting cumulative Covid 19 Infection cases in USA %"
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  45  days in bats Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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-03-25 | Thursday                | 29653604.00 | 29650219.13      | 0.011 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 2  | 2021-03-26 | Friday                  | 29718930.00 | 29704633.06      | 0.048 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 3  | 2021-03-27 | Saturday                | 29788519.00 | 29759046.99      | 0.099 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 4  | 2021-03-28 | Sunday                  | 29859706.00 | 29813460.91      | 0.155 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 5  | 2021-03-29 | Monday                  | 29921599.00 | 29867874.84      | 0.18 %          |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 6  | 2021-03-30 | Tuesday                 | 29968464.00 | 29922288.77      | 0.154 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 7  | 2021-03-31 | Wednesday               | 30033063.00 | 29976702.69      | 0.188 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 8  | 2021-04-01 | Thursday                | 30095776.00 | 30031116.62      | 0.215 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 9  | 2021-04-02 | Friday                  | 30164185.00 | 30085530.55      | 0.261 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 10 | 2021-04-03 | Saturday                | 30238692.00 | 30139944.47      | 0.327 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 11 | 2021-04-04 | Sunday                  | 30304462.00 | 30194358.40      | 0.363 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 12 | 2021-04-05 | Monday                  | 30372016.00 | 30248772.33      | 0.406 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 13 | 2021-04-06 | Tuesday                 | 30413124.00 | 30303186.25      | 0.361 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 14 | 2021-04-07 | Wednesday               | 30475874.00 | 30357600.18      | 0.388 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 15 | 2021-04-08 | Thursday                | 30541000.00 | 30412014.11      | 0.422 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 16 | 2021-04-09 | Friday                  | 30615849.00 | 30466428.03      | 0.488 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 17 | 2021-04-10 | Saturday                | 30692226.00 | 30520841.96      | 0.558 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 18 | 2021-04-11 | Sunday                  | 30772857.00 | 30575255.89      | 0.642 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 19 | 2021-04-12 | Monday                  | 30840411.00 | 30629669.81      | 0.683 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 20 | 2021-04-13 | Tuesday                 | 30888765.00 | 30684083.74      | 0.663 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 21 | 2021-04-14 | Wednesday               | 30951566.00 | 30738497.67      | 0.688 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 22 | 2021-04-15 | Thursday                | 31029700.00 | 30792911.59      | 0.763 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 23 | 2021-04-16 | Friday                  | 31103006.00 | 30847325.52      | 0.822 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 24 | 2021-04-17 | Saturday                | 31176938.00 | 30901739.45      | 0.883 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 25 | 2021-04-18 | Sunday                  | 31250635.00 | 30956153.37      | 0.942 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 26 | 2021-04-19 | Monday                  | 31311941.00 | 31010567.30      | 0.962 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 27 | 2021-04-20 | Tuesday                 | 31350025.00 | 31064981.23      | 0.909 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 28 | 2021-04-21 | Wednesday               | 31407189.00 | 31119395.16      | 0.916 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 29 | 2021-04-22 | Thursday                | 31467572.00 | 31173809.08      | 0.934 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 30 | 2021-04-23 | Friday                  | 31530214.00 | 31228223.01      | 0.958 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 31 | 2021-04-24 | Saturday                | 31593420.00 | 31282636.94      | 0.984 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 32 | 2021-04-25 | Sunday                  | 31656636.00 | 31337050.86      | 1.01 %          |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 33 | 2021-04-26 | Monday                  | 31708445.00 | 31391464.79      | 1 %             |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 34 | 2021-04-27 | Tuesday                 | 31742914.00 | 31445878.72      | 0.936 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 35 | 2021-04-28 | Wednesday               | 31783375.00 | 31500292.64      | 0.891 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 36 | 2021-04-29 | Thursday                | 31835314.00 | 31554706.57      | 0.881 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 37 | 2021-04-30 | Friday                  | 31889171.00 | 31609120.50      | 0.878 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 38 | 2021-05-01 | Saturday                | 31948761.00 | 31663534.42      | 0.893 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 39 | 2021-05-02 | Sunday                  | 32002328.00 | 31717948.35      | 0.889 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 40 | 2021-05-03 | Monday                  | 32047478.00 | 31772362.28      | 0.858 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 41 | 2021-05-04 | Tuesday                 | 32083656.00 | 31826776.20      | 0.801 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 42 | 2021-05-05 | Wednesday               | 32123136.00 | 31881190.13      | 0.753 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 43 | 2021-05-06 | Thursday                | 32167970.00 | 31935604.06      | 0.722 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 44 | 2021-05-07 | Friday                  | 32210817.00 | 31990017.98      | 0.685 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 45 | 2021-05-08 | Saturday                | 32257416.00 | 32044431.91      | 0.66 %          |
## +----+------------+-------------------------+-------------+------------------+-----------------+
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-05-09 | Sunday          | 32098845.84         | 30367323.33 | 29450710.69 | 30367323.33 | 29450710.69 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 2  | 2021-05-10 | Monday          | 32153259.76         | 30367800.04 | 29422634.76 | 30367800.04 | 29422634.76 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 3  | 2021-05-11 | Tuesday         | 32207673.69         | 30367728.45 | 29393720.28 | 30367728.45 | 29393720.28 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 4  | 2021-05-12 | Wednesday       | 32262087.62         | 30367114.03 | 29363975.60 | 30367114.03 | 29363975.60 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 5  | 2021-05-13 | Thursday        | 32316501.54         | 30365962.05 | 29333408.82 | 30365962.05 | 29333408.82 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 6  | 2021-05-14 | Friday          | 32370915.47         | 30364277.67 | 29302027.79 | 30364277.67 | 29302027.79 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 7  | 2021-05-15 | Saturday        | 32425329.40         | 30362065.88 | 29269840.15 | 30362065.88 | 29269840.15 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 8  | 2021-05-16 | Sunday          | 32479743.32         | 30359331.55 | 29236853.36 | 30359331.55 | 29236853.36 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 9  | 2021-05-17 | Monday          | 32534157.25         | 30356079.39 | 29203074.62 | 30356079.39 | 29203074.62 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 10 | 2021-05-18 | Tuesday         | 32588571.18         | 30352314.02 | 29168510.99 | 30352314.02 | 29168510.99 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 11 | 2021-05-19 | Wednesday       | 32642985.10         | 30348039.91 | 29133169.32 | 30348039.91 | 29133169.32 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 12 | 2021-05-20 | Thursday        | 32697399.03         | 30343261.44 | 29097056.28 | 30343261.44 | 29097056.28 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 13 | 2021-05-21 | Friday          | 32751812.96         | 30337982.86 | 29060178.39 | 30337982.86 | 29060178.39 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 14 | 2021-05-22 | Saturday        | 32806226.88         | 30332208.31 | 29022541.99 | 30332208.31 | 29022541.99 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 15 | 2021-05-23 | Sunday          | 32860640.81         | 30325941.86 | 28984153.28 | 30325941.86 | 28984153.28 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 16 | 2021-05-24 | Monday          | 32915054.74         | 30319187.44 | 28945018.30 | 30319187.44 | 28945018.30 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 17 | 2021-05-25 | Tuesday         | 32969468.66         | 30311948.93 | 28905142.96 | 30311948.93 | 28905142.96 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 18 | 2021-05-26 | Wednesday       | 33023882.59         | 30304230.08 | 28864533.01 | 30304230.08 | 28864533.01 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 19 | 2021-05-27 | Thursday        | 33078296.52         | 30296034.58 | 28823194.08 | 30296034.58 | 28823194.08 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 20 | 2021-05-28 | Friday          | 33132710.44         | 30287366.03 | 28781131.69 | 30287366.03 | 28781131.69 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 21 | 2021-05-29 | Saturday        | 33187124.37         | 30278227.95 | 28738351.20 | 30278227.95 | 28738351.20 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 22 | 2021-05-30 | Sunday          | 33241538.30         | 30268623.77 | 28694857.89 | 30268623.77 | 28694857.89 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
plot(forecasting_bats)
x1_test <- ts(testing_data, start =(rows-validation_data_days+1) )
lines(x1_test, col='red',lwd=2)

graph1<-autoplot(forecasting_bats,xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab)
graph1

## 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 317.1624 18138.84 9511.215 NaN  Inf 0.1433358 0.008424101
# Print Model Parameters
model_TBATS
## TBATS(1, {0,0}, 1, {<6,2>})
## 
## Call: NULL
## 
## Parameters
##   Alpha: 1.115982
##   Beta: 0.3820274
##   Damping Parameter: 1
##   Gamma-1 Values: -0.00238997
##   Gamma-2 Values: 0.00130653
## 
## Seed States:
##            [,1]
## [1,]    8.32221
## [2,]  174.69300
## [3,] -457.42635
## [4,] -543.09596
## [5,] -723.30575
## [6,]  587.27074
## 
## Sigma: 18138.84
## AIC: 11514.24
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  45 days by using TBATS Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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  45  days in TBATS Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
paste(MAPE_Mean_All.TBATS,"%")
## [1] "0.633 % MAPE  45 days Forecasting cumulative Covid 19 Infection cases in USA %"
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  45  days in TBATS Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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-03-25 | Thursday                | 29653604.00 | 29649898.68       | 0.012 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 2  | 2021-03-26 | Friday                  | 29718930.00 | 29705830.40       | 0.044 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 3  | 2021-03-27 | Saturday                | 29788519.00 | 29759316.98       | 0.098 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 4  | 2021-03-28 | Sunday                  | 29859706.00 | 29811673.15       | 0.161 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 5  | 2021-03-29 | Monday                  | 29921599.00 | 29867130.31       | 0.182 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 6  | 2021-03-30 | Tuesday                 | 29968464.00 | 29921848.45       | 0.156 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 7  | 2021-03-31 | Wednesday               | 30033063.00 | 29975910.71       | 0.19 %           |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 8  | 2021-04-01 | Thursday                | 30095776.00 | 30031842.43       | 0.212 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 9  | 2021-04-02 | Friday                  | 30164185.00 | 30085329.01       | 0.261 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 10 | 2021-04-03 | Saturday                | 30238692.00 | 30137685.18       | 0.334 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 11 | 2021-04-04 | Sunday                  | 30304462.00 | 30193142.34       | 0.367 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 12 | 2021-04-05 | Monday                  | 30372016.00 | 30247860.48       | 0.409 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 13 | 2021-04-06 | Tuesday                 | 30413124.00 | 30301922.74       | 0.366 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 14 | 2021-04-07 | Wednesday               | 30475874.00 | 30357854.46       | 0.387 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 15 | 2021-04-08 | Thursday                | 30541000.00 | 30411341.04       | 0.425 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 16 | 2021-04-09 | Friday                  | 30615849.00 | 30463697.20       | 0.497 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 17 | 2021-04-10 | Saturday                | 30692226.00 | 30519154.37       | 0.564 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 18 | 2021-04-11 | Sunday                  | 30772857.00 | 30573872.51       | 0.647 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 19 | 2021-04-12 | Monday                  | 30840411.00 | 30627934.77       | 0.689 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 20 | 2021-04-13 | Tuesday                 | 30888765.00 | 30683866.49       | 0.663 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 21 | 2021-04-14 | Wednesday               | 30951566.00 | 30737353.07       | 0.692 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 22 | 2021-04-15 | Thursday                | 31029700.00 | 30789709.23       | 0.773 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 23 | 2021-04-16 | Friday                  | 31103006.00 | 30845166.40       | 0.829 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 24 | 2021-04-17 | Saturday                | 31176938.00 | 30899884.54       | 0.889 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 25 | 2021-04-18 | Sunday                  | 31250635.00 | 30953946.80       | 0.949 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 26 | 2021-04-19 | Monday                  | 31311941.00 | 31009878.52       | 0.965 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 27 | 2021-04-20 | Tuesday                 | 31350025.00 | 31063365.10       | 0.914 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 28 | 2021-04-21 | Wednesday               | 31407189.00 | 31115721.26       | 0.928 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 29 | 2021-04-22 | Thursday                | 31467572.00 | 31171178.43       | 0.942 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 30 | 2021-04-23 | Friday                  | 31530214.00 | 31225896.57       | 0.965 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 31 | 2021-04-24 | Saturday                | 31593420.00 | 31279958.83       | 0.992 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 32 | 2021-04-25 | Sunday                  | 31656636.00 | 31335890.55       | 1.013 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 33 | 2021-04-26 | Monday                  | 31708445.00 | 31389377.13       | 1.006 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 34 | 2021-04-27 | Tuesday                 | 31742914.00 | 31441733.29       | 0.949 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 35 | 2021-04-28 | Wednesday               | 31783375.00 | 31497190.46       | 0.9 %            |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 36 | 2021-04-29 | Thursday                | 31835314.00 | 31551908.60       | 0.89 %           |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 37 | 2021-04-30 | Friday                  | 31889171.00 | 31605970.86       | 0.888 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 38 | 2021-05-01 | Saturday                | 31948761.00 | 31661902.58       | 0.898 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 39 | 2021-05-02 | Sunday                  | 32002328.00 | 31715389.16       | 0.897 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 40 | 2021-05-03 | Monday                  | 32047478.00 | 31767745.32       | 0.873 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 41 | 2021-05-04 | Tuesday                 | 32083656.00 | 31823202.49       | 0.812 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 42 | 2021-05-05 | Wednesday               | 32123136.00 | 31877920.63       | 0.763 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 43 | 2021-05-06 | Thursday                | 32167970.00 | 31931982.89       | 0.734 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 44 | 2021-05-07 | Friday                  | 32210817.00 | 31987914.61       | 0.692 %          |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 45 | 2021-05-08 | Saturday                | 32257416.00 | 32041401.19       | 0.67 %           |
## +----+------------+-------------------------+-------------+-------------------+------------------+
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-05-09 | Sunday          | 32093757.35          | 31926609.99 | 31838127.52 | 32260904.72 | 32349387.19 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 2  | 2021-05-10 | Monday          | 32149214.52          | 31980427.11 | 31891076.45 | 32318001.92 | 32407352.58 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 3  | 2021-05-11 | Tuesday         | 32203932.66          | 32033542.29 | 31943343.08 | 32374323.02 | 32464522.23 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 4  | 2021-05-12 | Wednesday       | 32257994.92          | 32086031.58 | 31994999.68 | 32429958.27 | 32520990.16 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 5  | 2021-05-13 | Thursday        | 32313926.64          | 32140406.10 | 32048549.88 | 32487447.17 | 32579303.40 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 6  | 2021-05-14 | Friday          | 32367413.22          | 32192355.57 | 32099685.65 | 32542470.87 | 32635140.80 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 7  | 2021-05-15 | Saturday        | 32419769.38          | 32243188.00 | 32149711.46 | 32596350.77 | 32689827.31 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 8  | 2021-05-16 | Sunday          | 32475226.55          | 32297135.57 | 32202859.90 | 32653317.52 | 32747593.19 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 9  | 2021-05-17 | Monday          | 32529944.69          | 32350376.74 | 32255319.21 | 32709512.63 | 32804570.16 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 10 | 2021-05-18 | Tuesday         | 32584006.95          | 32402988.15 | 32307162.59 | 32765025.75 | 32860851.31 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 11 | 2021-05-19 | Wednesday       | 32639938.67          | 32457481.99 | 32360895.27 | 32822395.34 | 32918982.07 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 12 | 2021-05-20 | Thursday        | 32693425.25          | 32509547.79 | 32412208.94 | 32877302.71 | 32974641.56 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 13 | 2021-05-21 | Friday          | 32745781.41          | 32560494.06 | 32462408.85 | 32931068.77 | 33029153.97 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 14 | 2021-05-22 | Saturday        | 32801238.57          | 32614553.01 | 32515727.64 | 32987924.14 | 33086749.51 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 15 | 2021-05-23 | Sunday          | 32855956.71          | 32667902.09 | 32568351.99 | 33044011.33 | 33143561.44 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 16 | 2021-05-24 | Monday          | 32910018.98          | 32720618.46 | 32620355.87 | 33099419.51 | 33199682.09 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 17 | 2021-05-25 | Tuesday         | 32965950.70          | 32775215.12 | 32674245.80 | 33156686.27 | 33257655.59 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 18 | 2021-05-26 | Wednesday       | 33019437.28          | 32827381.44 | 32725713.22 | 33211493.12 | 33313161.34 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 19 | 2021-05-27 | Thursday        | 33071793.44          | 32878426.35 | 32776064.00 | 33265160.53 | 33367522.89 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 20 | 2021-05-28 | Friday          | 33127250.60          | 32932582.08 | 32829530.79 | 33321919.13 | 33424970.42 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 21 | 2021-05-29 | Saturday        | 33181968.74          | 32986025.17 | 32882298.91 | 33377912.32 | 33481638.58 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 22 | 2021-05-30 | Sunday          | 33236031.01          | 33038833.17 | 32934442.94 | 33433228.85 | 33537619.08 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
plot(forecasting_tbats)
x1_test <- ts(testing_data, start =(rows-validation_data_days+1) )
lines(x1_test, col='red',lwd=2)

graph2<-autoplot(forecasting_tbats,xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab)
graph2

## 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 -797.6627 18542.85 9589.94 NaN  Inf 0.1445222 0.1585094
# 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.5411 
## 
##   Smoothing parameters:
##     alpha = 0.9999 
##     beta  = 0.3438 
## 
##   Initial states:
##     l = -2.2178 
##     b = -0.1666 
## 
##   sigma:  13.4909
## 
##      AIC     AICc      BIC 
## 5060.030 5060.166 5080.543 
## 
## Training set error measures:
##                     ME     RMSE     MAE MPE MAPE      MASE      ACF1
## Training set -797.6627 18542.85 9589.94 NaN  Inf 0.1445222 0.1585094
# 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 Model for  ==> ",y_lab, sep=" ")
## [1] "MAPE % For  45 days by using holt Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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 Model for  ==> ",y_lab, sep=" ")
## [1] " MAPE that's Error of Forecasting for  45  days in holt Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
paste(MAPE_Mean_All.Holt,"%")
## [1] "0.612 % MAPE  45 days Forecasting cumulative Covid 19 Infection cases in USA %"
paste ("MAPE that's Error of Forecasting day by day for ",validation_data_days," days in holt Model for  ==> ",y_lab, sep=" ")
## [1] "MAPE that's Error of Forecasting day by day for  45  days in holt Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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-03-25 | Thursday                | 29653604.00 | 29648861.25      | 0.016 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 2  | 2021-03-26 | Friday                  | 29718930.00 | 29702919.26      | 0.054 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 3  | 2021-03-27 | Saturday                | 29788519.00 | 29757022.46      | 0.106 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 4  | 2021-03-28 | Sunday                  | 29859706.00 | 29811170.84      | 0.163 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 5  | 2021-03-29 | Monday                  | 29921599.00 | 29865364.40      | 0.188 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 6  | 2021-03-30 | Tuesday                 | 29968464.00 | 29919603.12      | 0.163 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 7  | 2021-03-31 | Wednesday               | 30033063.00 | 29973887.01      | 0.197 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 8  | 2021-04-01 | Thursday                | 30095776.00 | 30028216.06      | 0.224 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 9  | 2021-04-02 | Friday                  | 30164185.00 | 30082590.25      | 0.271 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 10 | 2021-04-03 | Saturday                | 30238692.00 | 30137009.59      | 0.336 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 11 | 2021-04-04 | Sunday                  | 30304462.00 | 30191474.06      | 0.373 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 12 | 2021-04-05 | Monday                  | 30372016.00 | 30245983.66      | 0.415 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 13 | 2021-04-06 | Tuesday                 | 30413124.00 | 30300538.39      | 0.37 %          |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 14 | 2021-04-07 | Wednesday               | 30475874.00 | 30355138.23      | 0.396 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 15 | 2021-04-08 | Thursday                | 30541000.00 | 30409783.17      | 0.43 %          |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 16 | 2021-04-09 | Friday                  | 30615849.00 | 30464473.23      | 0.494 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 17 | 2021-04-10 | Saturday                | 30692226.00 | 30519208.37      | 0.564 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 18 | 2021-04-11 | Sunday                  | 30772857.00 | 30573988.61      | 0.646 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 19 | 2021-04-12 | Monday                  | 30840411.00 | 30628813.92      | 0.686 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 20 | 2021-04-13 | Tuesday                 | 30888765.00 | 30683684.32      | 0.664 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 21 | 2021-04-14 | Wednesday               | 30951566.00 | 30738599.78      | 0.688 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 22 | 2021-04-15 | Thursday                | 31029700.00 | 30793560.30      | 0.761 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 23 | 2021-04-16 | Friday                  | 31103006.00 | 30848565.88      | 0.818 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 24 | 2021-04-17 | Saturday                | 31176938.00 | 30903616.51      | 0.877 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 25 | 2021-04-18 | Sunday                  | 31250635.00 | 30958712.19      | 0.934 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 26 | 2021-04-19 | Monday                  | 31311941.00 | 31013852.90      | 0.952 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 27 | 2021-04-20 | Tuesday                 | 31350025.00 | 31069038.63      | 0.896 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 28 | 2021-04-21 | Wednesday               | 31407189.00 | 31124269.39      | 0.901 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 29 | 2021-04-22 | Thursday                | 31467572.00 | 31179545.17      | 0.915 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 30 | 2021-04-23 | Friday                  | 31530214.00 | 31234865.96      | 0.937 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 31 | 2021-04-24 | Saturday                | 31593420.00 | 31290231.75      | 0.96 %          |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 32 | 2021-04-25 | Sunday                  | 31656636.00 | 31345642.54      | 0.982 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 33 | 2021-04-26 | Monday                  | 31708445.00 | 31401098.31      | 0.969 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 34 | 2021-04-27 | Tuesday                 | 31742914.00 | 31456599.07      | 0.902 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 35 | 2021-04-28 | Wednesday               | 31783375.00 | 31512144.81      | 0.853 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 36 | 2021-04-29 | Thursday                | 31835314.00 | 31567735.52      | 0.841 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 37 | 2021-04-30 | Friday                  | 31889171.00 | 31623371.19      | 0.834 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 38 | 2021-05-01 | Saturday                | 31948761.00 | 31679051.82      | 0.844 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 39 | 2021-05-02 | Sunday                  | 32002328.00 | 31734777.40      | 0.836 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 40 | 2021-05-03 | Monday                  | 32047478.00 | 31790547.93      | 0.802 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 41 | 2021-05-04 | Tuesday                 | 32083656.00 | 31846363.39      | 0.74 %          |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 42 | 2021-05-05 | Wednesday               | 32123136.00 | 31902223.79      | 0.688 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 43 | 2021-05-06 | Thursday                | 32167970.00 | 31958129.11      | 0.652 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 44 | 2021-05-07 | Friday                  | 32210817.00 | 32014079.34      | 0.611 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 45 | 2021-05-08 | Saturday                | 32257416.00 | 32070074.50      | 0.581 %         |
## +----+------------+-------------------------+-------------+------------------+-----------------+
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-05-09 | Sunday          | 32126114.55         | 28978910.65 | 27373326.61 | 35421584.20 | 37225711.08 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 2  | 2021-05-10 | Monday          | 32182199.51         | 28936677.50 | 27282876.64 | 35585346.48 | 37450224.41 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 3  | 2021-05-11 | Tuesday         | 32238329.36         | 28893500.71 | 27191118.51 | 35750527.03 | 37677036.10 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 4  | 2021-05-12 | Wednesday       | 32294504.09         | 28849391.93 | 27098073.32 | 35917121.88 | 37906143.04 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 5  | 2021-05-13 | Thursday        | 32350723.71         | 28804362.57 | 27003761.79 | 36085127.29 | 38137542.49 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 6  | 2021-05-14 | Friday          | 32406988.20         | 28758423.78 | 26908204.25 | 36254539.76 | 38371232.08 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 7  | 2021-05-15 | Saturday        | 32463297.56         | 28711586.48 | 26811420.67 | 36425356.06 | 38607209.82 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 8  | 2021-05-16 | Sunday          | 32519651.77         | 28663861.35 | 26713430.67 | 36597573.14 | 38845474.02 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 9  | 2021-05-17 | Monday          | 32576050.85         | 28615258.86 | 26614253.53 | 36771188.20 | 39086023.36 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 10 | 2021-05-18 | Tuesday         | 32632494.77         | 28565789.27 | 26513908.26 | 36946198.62 | 39328856.78 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 11 | 2021-05-19 | Wednesday       | 32688983.53         | 28515462.64 | 26412413.51 | 37122602.00 | 39573973.54 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 12 | 2021-05-20 | Thursday        | 32745517.13         | 28464288.85 | 26309787.70 | 37300396.09 | 39821373.17 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 13 | 2021-05-21 | Friday          | 32802095.55         | 28412277.59 | 26206048.94 | 37479578.85 | 40071055.48 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 14 | 2021-05-22 | Saturday        | 32858718.80         | 28359438.38 | 26101215.10 | 37660148.39 | 40323020.50 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 15 | 2021-05-23 | Sunday          | 32915386.87         | 28305780.57 | 25995303.80 | 37842102.99 | 40577268.53 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 16 | 2021-05-24 | Monday          | 32972099.74         | 28251313.37 | 25888332.39 | 38025441.08 | 40833800.11 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 17 | 2021-05-25 | Tuesday         | 33028857.42         | 28196045.81 | 25780318.04 | 38210161.25 | 41092615.98 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 18 | 2021-05-26 | Wednesday       | 33085659.90         | 28139986.79 | 25671277.66 | 38396262.22 | 41353717.11 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 19 | 2021-05-27 | Thursday        | 33142507.16         | 28083145.07 | 25561227.98 | 38583742.84 | 41617104.65 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 20 | 2021-05-28 | Friday          | 33199399.21         | 28025529.28 | 25450185.50 | 38772602.13 | 41882779.99 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 21 | 2021-05-29 | Saturday        | 33256336.04         | 27967147.90 | 25338166.55 | 38962839.18 | 42150744.66 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 22 | 2021-05-30 | Sunday          | 33313317.65         | 27908009.32 | 25225187.27 | 39154453.25 | 42421000.40 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
plot(forecasting_holt)
x1_test <- ts(testing_data, start =(rows-validation_data_days+1) )
lines(x1_test, col='red',lwd=2)

graph3<-autoplot(forecasting_holt,xlab = paste ("Time in", frequency ,y_lab , sep=" "),  ylab=y_lab)
graph3

#Auto arima model
##################
paste ("tests For Check Stationarity in series  ==> ",y_lab, sep=" ")
## [1] "tests For Check Stationarity in series  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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 = 6.5024, Truncation lag parameter = 5, p-value = 0.01
pp.test(data_series)   # applay pp test
## Warning in pp.test(data_series): p-value greater than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  data_series
## Dickey-Fuller Z(alpha) = -0.4596, Truncation lag parameter = 5, p-value
## = 0.99
## alternative hypothesis: stationary
adf.test(data_series)  # applay adf test
## 
##  Augmented Dickey-Fuller Test
## 
## data:  data_series
## Dickey-Fuller = -3.5943, Lag order = 7, p-value = 0.03345
## alternative hypothesis: stationary
ndiffs(data_series)    # Doing first diffrencing on data
## [1] 2
#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  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
kpss.test(diff1_x1)   # applay kpss test after taking first differences
## Warning in kpss.test(diff1_x1): p-value smaller than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  diff1_x1
## KPSS Level = 4.5112, Truncation lag parameter = 5, p-value = 0.01
pp.test(diff1_x1)     # applay pp test after taking first differences
## 
##  Phillips-Perron Unit Root Test
## 
## data:  diff1_x1
## Dickey-Fuller Z(alpha) = -18.398, Truncation lag parameter = 5, p-value
## = 0.09526
## alternative hypothesis: stationary
adf.test(diff1_x1)    # applay adf test after taking first differences
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff1_x1
## Dickey-Fuller = -0.76434, Lag order = 7, p-value = 0.9646
## 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 Forecasting cumulative Covid 19 Infection cases in USA"
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.10419, Truncation lag parameter = 5, 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) = -487.25, Truncation lag parameter = 5, 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 = -7.8527, Lag order = 7, 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,2,0)                    : 10098.88
##  ARIMA(0,2,1)                    : 10008.93
##  ARIMA(0,2,2)                    : 10003.59
##  ARIMA(0,2,3)                    : 10001.12
##  ARIMA(0,2,4)                    : 10001.59
##  ARIMA(0,2,5)                    : 9997.4
##  ARIMA(1,2,0)                    : 10039.4
##  ARIMA(1,2,1)                    : 10002.17
##  ARIMA(1,2,2)                    : 10004.14
##  ARIMA(1,2,3)                    : 10002.75
##  ARIMA(1,2,4)                    : 9997.538
##  ARIMA(2,2,0)                    : 10033.29
##  ARIMA(2,2,1)                    : 10004.03
##  ARIMA(2,2,2)                    : 10003.98
##  ARIMA(2,2,3)                    : Inf
##  ARIMA(3,2,0)                    : 10019.78
##  ARIMA(3,2,1)                    : 9996.959
##  ARIMA(3,2,2)                    : 9942.13
##  ARIMA(4,2,0)                    : 10006.36
##  ARIMA(4,2,1)                    : 9991.898
##  ARIMA(5,2,0)                    : 9981.209
## 
## 
## 
##  Best model: ARIMA(3,2,2)
model1 # show the result of autoarima 
## Series: data_series 
## ARIMA(3,2,2) 
## 
## Coefficients:
##          ar1      ar2      ar3      ma1     ma2
##       0.7838  -0.3911  -0.4443  -1.3134  0.9189
## s.e.  0.0473   0.0545   0.0450   0.0294  0.0239
## 
## sigma^2 estimated as 289247715:  log likelihood=-4964.97
## AIC=9941.94   AICc=9942.13   BIC=9966.53
#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] 3 2 2
strtoi(bestmodel[3])
## [1] 2
#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:
##          ar1      ar2      ar3      ma1     ma2
##       0.7838  -0.3911  -0.4443  -1.3134  0.9189
## s.e.  0.0473   0.0545   0.0450   0.0294  0.0239
## 
## sigma^2 estimated as 2.86e+08:  log likelihood = -4964.97,  aic = 9941.94
paste ("accuracy of autoarima Model For  ==> ",y_lab, sep=" ")
## [1] "accuracy of autoarima Model For  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
accuracy(x1_model1)  # aacuracy of best model from auto arima
##                    ME     RMSE      MAE       MPE     MAPE      MASE
## Training set 202.3156 16873.59 7962.389 0.5154827 2.617159 0.1199947
##                     ACF1
## Training set -0.07500919
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(3,2,2)
## Q* = 30.074, df = 5, p-value = 1.426e-05
## 
## 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   ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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 = 132.28, 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 = 22801, 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 and start from rows-6
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  45 days by using bats Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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  45  days in bats Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
paste(MAPE_Mean_All.ARIMA,"%")
## [1] "0.795 % MAPE  45 days Forecasting cumulative Covid 19 Infection cases in USA %"
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  45  days in bats Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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-03-25      | Thursday                | 29653604.00 | 29647942.05            | 0.019 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 2  | 2021-03-26      | Friday                  | 29718930.00 | 29704348.77            | 0.049 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 3  | 2021-03-27      | Saturday                | 29788519.00 | 29756920.82            | 0.106 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 4  | 2021-03-28      | Sunday                  | 29859706.00 | 29807231.81            | 0.176 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 5  | 2021-03-29      | Monday                  | 29921599.00 | 29855798.34            | 0.22 %                |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 6  | 2021-03-30      | Tuesday                 | 29968464.00 | 29905585.60            | 0.21 %                |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 7  | 2021-03-31      | Wednesday               | 30033063.00 | 29958016.43            | 0.25 %                |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 8  | 2021-04-01      | Thursday                | 30095776.00 | 30012816.75            | 0.276 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 9  | 2021-04-02      | Friday                  | 30164185.00 | 30067897.91            | 0.319 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 10 | 2021-04-03      | Saturday                | 30238692.00 | 30121097.97            | 0.389 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 11 | 2021-04-04      | Sunday                  | 30304462.00 | 30171661.14            | 0.438 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 12 | 2021-04-05      | Monday                  | 30372016.00 | 30220768.59            | 0.498 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 13 | 2021-04-06      | Tuesday                 | 30413124.00 | 30270602.17            | 0.469 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 14 | 2021-04-07      | Wednesday               | 30475874.00 | 30322745.70            | 0.502 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 15 | 2021-04-08      | Thursday                | 30541000.00 | 30377062.42            | 0.537 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 16 | 2021-04-09      | Friday                  | 30615849.00 | 30431856.34            | 0.601 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 17 | 2021-04-10      | Saturday                | 30692226.00 | 30485148.04            | 0.675 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 18 | 2021-04-11      | Sunday                  | 30772857.00 | 30536110.25            | 0.769 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 19 | 2021-04-12      | Monday                  | 30840411.00 | 30585622.22            | 0.826 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 20 | 2021-04-13      | Tuesday                 | 30888765.00 | 30635576.06            | 0.82 %                |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 21 | 2021-04-14      | Wednesday               | 30951566.00 | 30687478.35            | 0.853 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 22 | 2021-04-15      | Thursday                | 31029700.00 | 30741379.24            | 0.929 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 23 | 2021-04-16      | Friday                  | 31103006.00 | 30795888.17            | 0.987 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 24 | 2021-04-17      | Saturday                | 31176938.00 | 30849226.33            | 1.051 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 25 | 2021-04-18      | Sunday                  | 31250635.00 | 30900521.16            | 1.12 %                |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 26 | 2021-04-19      | Monday                  | 31311941.00 | 30950402.26            | 1.155 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 27 | 2021-04-20      | Tuesday                 | 31350025.00 | 31000494.67            | 1.115 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 28 | 2021-04-21      | Wednesday               | 31407189.00 | 31052213.43            | 1.13 %                |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 29 | 2021-04-22      | Thursday                | 31467572.00 | 31105752.28            | 1.15 %                |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 30 | 2021-04-23      | Friday                  | 31530214.00 | 31159987.68            | 1.174 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 31 | 2021-04-24      | Saturday                | 31593420.00 | 31213334.59            | 1.203 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 32 | 2021-04-25      | Sunday                  | 31656636.00 | 31264904.10            | 1.237 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 33 | 2021-04-26      | Monday                  | 31708445.00 | 31315118.59            | 1.24 %                |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 34 | 2021-04-27      | Tuesday                 | 31742914.00 | 31365360.98            | 1.189 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 35 | 2021-04-28      | Wednesday               | 31783375.00 | 31416944.85            | 1.153 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 36 | 2021-04-29      | Thursday                | 31835314.00 | 31470171.21            | 1.147 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 37 | 2021-04-30      | Friday                  | 31889171.00 | 31524147.82            | 1.145 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 38 | 2021-05-01      | Saturday                | 31948761.00 | 31577474.05            | 1.162 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 39 | 2021-05-02      | Sunday                  | 32002328.00 | 31629267.40            | 1.166 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 40 | 2021-05-03      | Monday                  | 32047478.00 | 31679780.38            | 1.147 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 41 | 2021-05-04      | Tuesday                 | 32083656.00 | 31730178.36            | 1.102 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 42 | 2021-05-05      | Wednesday               | 32123136.00 | 31781667.99            | 1.063 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 43 | 2021-05-06      | Thursday                | 32167970.00 | 31834627.02            | 1.036 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 44 | 2021-05-07      | Friday                  | 32210817.00 | 31888361.83            | 1.001 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 45 | 2021-05-08      | Saturday                | 32257416.00 | 31941644.99            | 0.979 %               |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
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-05-09 | Sunday          | 31993617.91               | 29679407.99 | 28454339.26 | 34307827.83 | 35532896.56 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 2  | 2021-05-10 | Monday          | 32044395.91               | 29655983.76 | 28391634.74 | 34432808.06 | 35697157.08 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 3  | 2021-05-11 | Tuesday         | 32094950.50               | 29631673.13 | 28327692.86 | 34558227.87 | 35862208.14 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 4  | 2021-05-12 | Wednesday       | 32146379.44               | 29607351.27 | 28263270.94 | 34685407.61 | 36029487.94 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 5  | 2021-05-13 | Thursday        | 32199111.91               | 29583317.21 | 28198599.13 | 34814906.62 | 36199624.69 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 6  | 2021-05-14 | Friday          | 32252623.32               | 29559101.92 | 28133237.83 | 34946144.72 | 36372008.82 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 7  | 2021-05-15 | Saturday        | 32305846.95               | 29533836.18 | 28066422.32 | 35077857.72 | 36545271.58 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 8  | 2021-05-16 | Sunday          | 32357961.25               | 29506894.45 | 27997630.85 | 35209028.05 | 36718291.64 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 9  | 2021-05-17 | Monday          | 32408972.59               | 29478342.94 | 27926961.32 | 35339602.24 | 36890983.86 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 10 | 2021-05-18 | Tuesday         | 32459681.21               | 29448875.54 | 27855051.29 | 35470486.89 | 37064311.13 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 11 | 2021-05-19 | Wednesday       | 32511076.81               | 29419298.90 | 27782610.54 | 35602854.71 | 37239543.07 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 12 | 2021-05-20 | Thursday        | 32563619.23               | 29389951.88 | 27709913.87 | 35737286.58 | 37417324.59 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 13 | 2021-05-21 | Friday          | 32616926.30               | 29360480.91 | 27636622.85 | 35873371.68 | 37597229.74 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 14 | 2021-05-22 | Saturday        | 32670078.92               | 29330121.67 | 27562055.10 | 36010036.16 | 37778102.73 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 15 | 2021-05-23 | Sunday          | 32722301.91               | 29298267.18 | 27485692.68 | 36146336.65 | 37958911.14 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 16 | 2021-05-24 | Monday          | 32773517.01               | 29264904.12 | 27407556.66 | 36282129.89 | 38139477.36 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 17 | 2021-05-25 | Tuesday         | 32824374.36               | 29230607.67 | 27328182.50 | 36418141.05 | 38320566.22 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 18 | 2021-05-26 | Wednesday       | 32875758.53               | 29196112.25 | 27248225.17 | 36555404.82 | 38503291.90 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 19 | 2021-05-27 | Thursday        | 32928143.32               | 29161780.96 | 27167989.17 | 36694505.67 | 38688297.46 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 20 | 2021-05-28 | Friday          | 32981265.22               | 29127359.87 | 27087225.61 | 36835170.58 | 38875304.83 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 21 | 2021-05-29 | Saturday        | 33034339.45               | 29092185.01 | 27005334.51 | 36976493.88 | 39063344.38 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 22 | 2021-05-30 | Sunday          | 33086643.46               | 29055677.77 | 26921813.43 | 37117609.15 | 39251473.49 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
plot(forecasting_auto_arima)
x1_test <- ts(testing_data, start =(rows-validation_data_days+1) )
lines(x1_test, col='red',lwd=2)

graph4<-autoplot(forecasting_auto_arima,xlab = paste ("Time in", frequency ,y_lab , sep=" "), ylab=y_lab)
graph4

MAPE_Mean_All.ARIMA
## [1] "0.795 % MAPE  45 days Forecasting cumulative Covid 19 Infection cases in USA"
## Ensembling (Average)
weight.model<-0.90#  optimization 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_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)
y1<-if(re_bestmodel >= MAPE_Mean_All.bats_Model) {re_BATS*weight.model
} else {
  (re_BATS*(1-weight.model))/4
}

y2<-if(re_bestmodel >= MAPE_Mean_All.TBATS_Model) {re_TBATS*weight.model
} else {
  (re_TBATS*(1-weight.model))/4
}

y3<-if(re_bestmodel >= MAPE_Mean_All.Holt_Model) {re_holt*weight.model
} else {
  (re_holt*(1-weight.model))/4
}
y4<-if(re_bestmodel >= MAPE_Mean_All.ARIMA_Model) {re_autoarima*weight.model
} else {
  (re_autoarima*(1-weight.model))/4
}
y5<-if(re_bestmodel >= MAPE_Mean_All_NNAR) {re_NNAR*weight.model
} else {
  (re_NNAR*(1-weight.model))/4
}
Ensembling.Average<-(y1+y2+y3+y4+y5)
# Testing Data Evaluation
validation_forecast<-head(Ensembling.Average,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 Ensembling (Average) for  ==> ",y_lab, sep=" ")
## [1] "MAPE % For  45 days by using Ensembling (Average) for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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  45  days in Ensembling Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
paste(MAPE_Mean_EnsemblingAverage,"%")
## [1] "0.689 %"
paste ("MAPE that's Error of Forecasting day by day for ",validation_data_days," days in Ensembling Model for  ==> ",y_lab, sep=" ")
## [1] "MAPE that's Error of Forecasting day by day for  45  days in Ensembling Model for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
print(ascii(data.frame(date_Ensembling=validation_dates,validation_data_by_name,actual_data=testing_data,Ensembling=validation_forecast,MAPE_Ensembling)), type = "rest")
## 
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## |    | date_Ensembling | validation_data_by_name | actual_data | Ensembling  | MAPE_Ensembling |
## +====+=================+=========================+=============+=============+=================+
## | 1  | 2021-03-25      | Thursday                | 29653604.00 | 29648163.48 | 0.018 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 2  | 2021-03-26      | Friday                  | 29718930.00 | 29701578.69 | 0.058 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 3  | 2021-03-27      | Saturday                | 29788519.00 | 29754856.84 | 0.113 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 4  | 2021-03-28      | Sunday                  | 29859706.00 | 29808070.83 | 0.173 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 5  | 2021-03-29      | Monday                  | 29921599.00 | 29861340.00 | 0.201 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 6  | 2021-03-30      | Tuesday                 | 29968464.00 | 29914643.14 | 0.18 %          |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 7  | 2021-03-31      | Wednesday               | 30033063.00 | 29968018.51 | 0.217 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 8  | 2021-04-01      | Thursday                | 30095776.00 | 30021522.99 | 0.247 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 9  | 2021-04-02      | Friday                  | 30164185.00 | 30074997.09 | 0.296 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 10 | 2021-04-03      | Saturday                | 30238692.00 | 30128420.20 | 0.365 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 11 | 2021-04-04      | Sunday                  | 30304462.00 | 30181879.75 | 0.405 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 12 | 2021-04-05      | Monday                  | 30372016.00 | 30235309.80 | 0.45 %          |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 13 | 2021-04-06      | Tuesday                 | 30413124.00 | 30288767.49 | 0.409 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 14 | 2021-04-07      | Wednesday               | 30475874.00 | 30342356.04 | 0.438 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 15 | 2021-04-08      | Thursday                | 30541000.00 | 30395964.66 | 0.475 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 16 | 2021-04-09      | Friday                  | 30615849.00 | 30449584.28 | 0.543 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 17 | 2021-04-10      | Saturday                | 30692226.00 | 30503271.65 | 0.616 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 18 | 2021-04-11      | Sunday                  | 30772857.00 | 30556910.53 | 0.702 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 19 | 2021-04-12      | Monday                  | 30840411.00 | 30610525.38 | 0.745 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 20 | 2021-04-13      | Tuesday                 | 30888765.00 | 30664227.07 | 0.727 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 21 | 2021-04-14      | Wednesday               | 30951566.00 | 30717945.79 | 0.755 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 22 | 2021-04-15      | Thursday                | 31029700.00 | 30771716.03 | 0.831 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 23 | 2021-04-16      | Friday                  | 31103006.00 | 30825609.19 | 0.892 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 24 | 2021-04-17      | Saturday                | 31176938.00 | 30879485.15 | 0.954 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 25 | 2021-04-18      | Sunday                  | 31250635.00 | 30933324.52 | 1.015 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 26 | 2021-04-19      | Monday                  | 31311941.00 | 30987206.51 | 1.037 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 27 | 2021-04-20      | Tuesday                 | 31350025.00 | 31041064.20 | 0.986 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 28 | 2021-04-21      | Wednesday               | 31407189.00 | 31094966.15 | 0.994 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 29 | 2021-04-22      | Thursday                | 31467572.00 | 31149023.28 | 1.012 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 30 | 2021-04-23      | Friday                  | 31530214.00 | 31203111.79 | 1.037 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 31 | 2021-04-24      | Saturday                | 31593420.00 | 31257194.42 | 1.064 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 32 | 2021-04-25      | Sunday                  | 31656636.00 | 31311312.34 | 1.091 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 33 | 2021-04-26      | Monday                  | 31708445.00 | 31365368.51 | 1.082 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 34 | 2021-04-27      | Tuesday                 | 31742914.00 | 31419430.63 | 1.019 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 35 | 2021-04-28      | Wednesday               | 31783375.00 | 31473637.56 | 0.975 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 36 | 2021-04-29      | Thursday                | 31835314.00 | 31527901.05 | 0.966 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 37 | 2021-04-30      | Friday                  | 31889171.00 | 31582201.11 | 0.963 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 38 | 2021-05-01      | Saturday                | 31948761.00 | 31636566.08 | 0.977 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 39 | 2021-05-02      | Sunday                  | 32002328.00 | 31690866.22 | 0.973 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 40 | 2021-05-03      | Monday                  | 32047478.00 | 31745140.94 | 0.943 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 41 | 2021-05-04      | Tuesday                 | 32083656.00 | 31799525.34 | 0.886 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 42 | 2021-05-05      | Wednesday               | 32123136.00 | 31853953.77 | 0.838 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 43 | 2021-05-06      | Thursday                | 32167970.00 | 31908437.94 | 0.807 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 44 | 2021-05-07      | Friday                  | 32210817.00 | 31963023.82 | 0.769 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
## | 45 | 2021-05-08      | Saturday                | 32257416.00 | 32017573.02 | 0.744 %         |
## +----+-----------------+-------------------------+-------------+-------------+-----------------+
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting_by_Ensembling=tail(Ensembling.Average,N_forecasting_days))), type = "rest")
## 
## +----+------------+-----------------+---------------------------+
## |    | FD         | forecating_date | forecasting_by_Ensembling |
## +====+============+=================+===========================+
## | 1  | 2021-05-09 | Sunday          | 32072097.12               |
## +----+------------+-----------------+---------------------------+
## | 2  | 2021-05-10 | Monday          | 32126704.93               |
## +----+------------+-----------------+---------------------------+
## | 3  | 2021-05-11 | Tuesday         | 32181324.89               |
## +----+------------+-----------------+---------------------------+
## | 4  | 2021-05-12 | Wednesday       | 32235986.67               |
## +----+------------+-----------------+---------------------------+
## | 5  | 2021-05-13 | Thursday        | 32290764.28               |
## +----+------------+-----------------+---------------------------+
## | 6  | 2021-05-14 | Friday          | 32345536.87               |
## +----+------------+-----------------+---------------------------+
## | 7  | 2021-05-15 | Saturday        | 32400310.76               |
## +----+------------+-----------------+---------------------------+
## | 8  | 2021-05-16 | Sunday          | 32455171.34               |
## +----+------------+-----------------+---------------------------+
## | 9  | 2021-05-17 | Monday          | 32510022.88               |
## +----+------------+-----------------+---------------------------+
## | 10 | 2021-05-18 | Tuesday         | 32564887.57               |
## +----+------------+-----------------+---------------------------+
## | 11 | 2021-05-19 | Wednesday       | 32619853.42               |
## +----+------------+-----------------+---------------------------+
## | 12 | 2021-05-20 | Thursday        | 32674824.15               |
## +----+------------+-----------------+---------------------------+
## | 13 | 2021-05-21 | Friday          | 32729823.18               |
## +----+------------+-----------------+---------------------------+
## | 14 | 2021-05-22 | Saturday        | 32784933.42               |
## +----+------------+-----------------+---------------------------+
## | 15 | 2021-05-23 | Sunday          | 32840039.59               |
## +----+------------+-----------------+---------------------------+
## | 16 | 2021-05-24 | Monday          | 32895141.89               |
## +----+------------+-----------------+---------------------------+
## | 17 | 2021-05-25 | Tuesday         | 32950319.82               |
## +----+------------+-----------------+---------------------------+
## | 18 | 2021-05-26 | Wednesday       | 33005487.69               |
## +----+------------+-----------------+---------------------------+
## | 19 | 2021-05-27 | Thursday        | 33060690.30               |
## +----+------------+-----------------+---------------------------+
## | 20 | 2021-05-28 | Friday          | 33116026.94               |
## +----+------------+-----------------+---------------------------+
## | 21 | 2021-05-29 | Saturday        | 33171382.06               |
## +----+------------+-----------------+---------------------------+
## | 22 | 2021-05-30 | Sunday          | 33226739.74               |
## +----+------------+-----------------+---------------------------+
graph5<-autoplot(Ensembling.Average,xlab = paste ("Time in", frequency ,y_lab,"by using Ensembling models" , sep=" "), ylab=y_lab)
graph5

# 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_EnsemblingAverage)
paste("System Choose Least Error ==> ( MAPE %) of Forecasting  by using bats model and BATS Model, Holt's Linear Models , and autoarima for  ==> ", y_lab , sep=" ")
## [1] "System Choose Least Error ==> ( MAPE %) of Forecasting  by using bats model and BATS Model, Holt's Linear Models , and autoarima for  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
best_recommended_model
## [1] 0.612
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_EnsemblingAverage) {paste("Ensembling")}
panderOptions('table.split.table', Inf)
paste("Forecasting by using BATS Model  ==> ", y_lab , sep=" ")
## [1] "Forecasting by using BATS Model  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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-05-09 | Sunday          | 32098845.84         | 30367323.33 | 29450710.69 | 30367323.33 | 29450710.69 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 2  | 2021-05-10 | Monday          | 32153259.76         | 30367800.04 | 29422634.76 | 30367800.04 | 29422634.76 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 3  | 2021-05-11 | Tuesday         | 32207673.69         | 30367728.45 | 29393720.28 | 30367728.45 | 29393720.28 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 4  | 2021-05-12 | Wednesday       | 32262087.62         | 30367114.03 | 29363975.60 | 30367114.03 | 29363975.60 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 5  | 2021-05-13 | Thursday        | 32316501.54         | 30365962.05 | 29333408.82 | 30365962.05 | 29333408.82 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 6  | 2021-05-14 | Friday          | 32370915.47         | 30364277.67 | 29302027.79 | 30364277.67 | 29302027.79 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 7  | 2021-05-15 | Saturday        | 32425329.40         | 30362065.88 | 29269840.15 | 30362065.88 | 29269840.15 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 8  | 2021-05-16 | Sunday          | 32479743.32         | 30359331.55 | 29236853.36 | 30359331.55 | 29236853.36 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 9  | 2021-05-17 | Monday          | 32534157.25         | 30356079.39 | 29203074.62 | 30356079.39 | 29203074.62 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 10 | 2021-05-18 | Tuesday         | 32588571.18         | 30352314.02 | 29168510.99 | 30352314.02 | 29168510.99 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 11 | 2021-05-19 | Wednesday       | 32642985.10         | 30348039.91 | 29133169.32 | 30348039.91 | 29133169.32 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 12 | 2021-05-20 | Thursday        | 32697399.03         | 30343261.44 | 29097056.28 | 30343261.44 | 29097056.28 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 13 | 2021-05-21 | Friday          | 32751812.96         | 30337982.86 | 29060178.39 | 30337982.86 | 29060178.39 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 14 | 2021-05-22 | Saturday        | 32806226.88         | 30332208.31 | 29022541.99 | 30332208.31 | 29022541.99 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 15 | 2021-05-23 | Sunday          | 32860640.81         | 30325941.86 | 28984153.28 | 30325941.86 | 28984153.28 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 16 | 2021-05-24 | Monday          | 32915054.74         | 30319187.44 | 28945018.30 | 30319187.44 | 28945018.30 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 17 | 2021-05-25 | Tuesday         | 32969468.66         | 30311948.93 | 28905142.96 | 30311948.93 | 28905142.96 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 18 | 2021-05-26 | Wednesday       | 33023882.59         | 30304230.08 | 28864533.01 | 30304230.08 | 28864533.01 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 19 | 2021-05-27 | Thursday        | 33078296.52         | 30296034.58 | 28823194.08 | 30296034.58 | 28823194.08 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 20 | 2021-05-28 | Friday          | 33132710.44         | 30287366.03 | 28781131.69 | 30287366.03 | 28781131.69 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 21 | 2021-05-29 | Saturday        | 33187124.37         | 30278227.95 | 28738351.20 | 30278227.95 | 28738351.20 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 22 | 2021-05-30 | Sunday          | 33241538.30         | 30268623.77 | 28694857.89 | 30268623.77 | 28694857.89 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
paste("Forecasting by using TBATS Model  ==> ", y_lab , sep=" ")
## [1] "Forecasting by using TBATS Model  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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-05-09 | Sunday          | 32093757.35          | 31926609.99 | 31838127.52 | 32260904.72 | 32349387.19 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 2  | 2021-05-10 | Monday          | 32149214.52          | 31980427.11 | 31891076.45 | 32318001.92 | 32407352.58 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 3  | 2021-05-11 | Tuesday         | 32203932.66          | 32033542.29 | 31943343.08 | 32374323.02 | 32464522.23 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 4  | 2021-05-12 | Wednesday       | 32257994.92          | 32086031.58 | 31994999.68 | 32429958.27 | 32520990.16 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 5  | 2021-05-13 | Thursday        | 32313926.64          | 32140406.10 | 32048549.88 | 32487447.17 | 32579303.40 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 6  | 2021-05-14 | Friday          | 32367413.22          | 32192355.57 | 32099685.65 | 32542470.87 | 32635140.80 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 7  | 2021-05-15 | Saturday        | 32419769.38          | 32243188.00 | 32149711.46 | 32596350.77 | 32689827.31 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 8  | 2021-05-16 | Sunday          | 32475226.55          | 32297135.57 | 32202859.90 | 32653317.52 | 32747593.19 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 9  | 2021-05-17 | Monday          | 32529944.69          | 32350376.74 | 32255319.21 | 32709512.63 | 32804570.16 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 10 | 2021-05-18 | Tuesday         | 32584006.95          | 32402988.15 | 32307162.59 | 32765025.75 | 32860851.31 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 11 | 2021-05-19 | Wednesday       | 32639938.67          | 32457481.99 | 32360895.27 | 32822395.34 | 32918982.07 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 12 | 2021-05-20 | Thursday        | 32693425.25          | 32509547.79 | 32412208.94 | 32877302.71 | 32974641.56 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 13 | 2021-05-21 | Friday          | 32745781.41          | 32560494.06 | 32462408.85 | 32931068.77 | 33029153.97 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 14 | 2021-05-22 | Saturday        | 32801238.57          | 32614553.01 | 32515727.64 | 32987924.14 | 33086749.51 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 15 | 2021-05-23 | Sunday          | 32855956.71          | 32667902.09 | 32568351.99 | 33044011.33 | 33143561.44 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 16 | 2021-05-24 | Monday          | 32910018.98          | 32720618.46 | 32620355.87 | 33099419.51 | 33199682.09 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 17 | 2021-05-25 | Tuesday         | 32965950.70          | 32775215.12 | 32674245.80 | 33156686.27 | 33257655.59 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 18 | 2021-05-26 | Wednesday       | 33019437.28          | 32827381.44 | 32725713.22 | 33211493.12 | 33313161.34 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 19 | 2021-05-27 | Thursday        | 33071793.44          | 32878426.35 | 32776064.00 | 33265160.53 | 33367522.89 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 20 | 2021-05-28 | Friday          | 33127250.60          | 32932582.08 | 32829530.79 | 33321919.13 | 33424970.42 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 21 | 2021-05-29 | Saturday        | 33181968.74          | 32986025.17 | 32882298.91 | 33377912.32 | 33481638.58 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
## | 22 | 2021-05-30 | Sunday          | 33236031.01          | 33038833.17 | 32934442.94 | 33433228.85 | 33537619.08 |
## +----+------------+-----------------+----------------------+-------------+-------------+-------------+-------------+
paste("Forecasting by using Holt's Linear Trend Model  ==> ", y_lab , sep=" ")
## [1] "Forecasting by using Holt's Linear Trend Model  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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-05-09 | Sunday          | 32126114.55         | 28978910.65 | 27373326.61 | 35421584.20 | 37225711.08 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 2  | 2021-05-10 | Monday          | 32182199.51         | 28936677.50 | 27282876.64 | 35585346.48 | 37450224.41 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 3  | 2021-05-11 | Tuesday         | 32238329.36         | 28893500.71 | 27191118.51 | 35750527.03 | 37677036.10 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 4  | 2021-05-12 | Wednesday       | 32294504.09         | 28849391.93 | 27098073.32 | 35917121.88 | 37906143.04 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 5  | 2021-05-13 | Thursday        | 32350723.71         | 28804362.57 | 27003761.79 | 36085127.29 | 38137542.49 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 6  | 2021-05-14 | Friday          | 32406988.20         | 28758423.78 | 26908204.25 | 36254539.76 | 38371232.08 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 7  | 2021-05-15 | Saturday        | 32463297.56         | 28711586.48 | 26811420.67 | 36425356.06 | 38607209.82 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 8  | 2021-05-16 | Sunday          | 32519651.77         | 28663861.35 | 26713430.67 | 36597573.14 | 38845474.02 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 9  | 2021-05-17 | Monday          | 32576050.85         | 28615258.86 | 26614253.53 | 36771188.20 | 39086023.36 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 10 | 2021-05-18 | Tuesday         | 32632494.77         | 28565789.27 | 26513908.26 | 36946198.62 | 39328856.78 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 11 | 2021-05-19 | Wednesday       | 32688983.53         | 28515462.64 | 26412413.51 | 37122602.00 | 39573973.54 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 12 | 2021-05-20 | Thursday        | 32745517.13         | 28464288.85 | 26309787.70 | 37300396.09 | 39821373.17 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 13 | 2021-05-21 | Friday          | 32802095.55         | 28412277.59 | 26206048.94 | 37479578.85 | 40071055.48 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 14 | 2021-05-22 | Saturday        | 32858718.80         | 28359438.38 | 26101215.10 | 37660148.39 | 40323020.50 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 15 | 2021-05-23 | Sunday          | 32915386.87         | 28305780.57 | 25995303.80 | 37842102.99 | 40577268.53 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 16 | 2021-05-24 | Monday          | 32972099.74         | 28251313.37 | 25888332.39 | 38025441.08 | 40833800.11 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 17 | 2021-05-25 | Tuesday         | 33028857.42         | 28196045.81 | 25780318.04 | 38210161.25 | 41092615.98 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 18 | 2021-05-26 | Wednesday       | 33085659.90         | 28139986.79 | 25671277.66 | 38396262.22 | 41353717.11 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 19 | 2021-05-27 | Thursday        | 33142507.16         | 28083145.07 | 25561227.98 | 38583742.84 | 41617104.65 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 20 | 2021-05-28 | Friday          | 33199399.21         | 28025529.28 | 25450185.50 | 38772602.13 | 41882779.99 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 21 | 2021-05-29 | Saturday        | 33256336.04         | 27967147.90 | 25338166.55 | 38962839.18 | 42150744.66 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
## | 22 | 2021-05-30 | Sunday          | 33313317.65         | 27908009.32 | 25225187.27 | 39154453.25 | 42421000.40 |
## +----+------------+-----------------+---------------------+-------------+-------------+-------------+-------------+
paste("Forecasting by using ARIMA Model  ==> ", y_lab , sep=" ")
## [1] "Forecasting by using ARIMA Model  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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-05-09 | Sunday          | 31993617.91               | 29679407.99 | 28454339.26 | 34307827.83 | 35532896.56 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 2  | 2021-05-10 | Monday          | 32044395.91               | 29655983.76 | 28391634.74 | 34432808.06 | 35697157.08 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 3  | 2021-05-11 | Tuesday         | 32094950.50               | 29631673.13 | 28327692.86 | 34558227.87 | 35862208.14 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 4  | 2021-05-12 | Wednesday       | 32146379.44               | 29607351.27 | 28263270.94 | 34685407.61 | 36029487.94 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 5  | 2021-05-13 | Thursday        | 32199111.91               | 29583317.21 | 28198599.13 | 34814906.62 | 36199624.69 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 6  | 2021-05-14 | Friday          | 32252623.32               | 29559101.92 | 28133237.83 | 34946144.72 | 36372008.82 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 7  | 2021-05-15 | Saturday        | 32305846.95               | 29533836.18 | 28066422.32 | 35077857.72 | 36545271.58 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 8  | 2021-05-16 | Sunday          | 32357961.25               | 29506894.45 | 27997630.85 | 35209028.05 | 36718291.64 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 9  | 2021-05-17 | Monday          | 32408972.59               | 29478342.94 | 27926961.32 | 35339602.24 | 36890983.86 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 10 | 2021-05-18 | Tuesday         | 32459681.21               | 29448875.54 | 27855051.29 | 35470486.89 | 37064311.13 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 11 | 2021-05-19 | Wednesday       | 32511076.81               | 29419298.90 | 27782610.54 | 35602854.71 | 37239543.07 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 12 | 2021-05-20 | Thursday        | 32563619.23               | 29389951.88 | 27709913.87 | 35737286.58 | 37417324.59 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 13 | 2021-05-21 | Friday          | 32616926.30               | 29360480.91 | 27636622.85 | 35873371.68 | 37597229.74 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 14 | 2021-05-22 | Saturday        | 32670078.92               | 29330121.67 | 27562055.10 | 36010036.16 | 37778102.73 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 15 | 2021-05-23 | Sunday          | 32722301.91               | 29298267.18 | 27485692.68 | 36146336.65 | 37958911.14 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 16 | 2021-05-24 | Monday          | 32773517.01               | 29264904.12 | 27407556.66 | 36282129.89 | 38139477.36 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 17 | 2021-05-25 | Tuesday         | 32824374.36               | 29230607.67 | 27328182.50 | 36418141.05 | 38320566.22 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 18 | 2021-05-26 | Wednesday       | 32875758.53               | 29196112.25 | 27248225.17 | 36555404.82 | 38503291.90 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 19 | 2021-05-27 | Thursday        | 32928143.32               | 29161780.96 | 27167989.17 | 36694505.67 | 38688297.46 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 20 | 2021-05-28 | Friday          | 32981265.22               | 29127359.87 | 27087225.61 | 36835170.58 | 38875304.83 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 21 | 2021-05-29 | Saturday        | 33034339.45               | 29092185.01 | 27005334.51 | 36976493.88 | 39063344.38 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
## | 22 | 2021-05-30 | Sunday          | 33086643.46               | 29055677.77 | 26921813.43 | 37117609.15 | 39251473.49 |
## +----+------------+-----------------+---------------------------+-------------+-------------+-------------+-------------+
paste("Forecasting by using NNAR Model  ==> ", y_lab , sep=" ")
## [1] "Forecasting by using NNAR Model  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
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-05-09 | Sunday          | 30157539.65         |
## +----+------------+-----------------+---------------------+
## | 2  | 2021-05-10 | Monday          | 30162144.52         |
## +----+------------+-----------------+---------------------+
## | 3  | 2021-05-11 | Tuesday         | 30166581.82         |
## +----+------------+-----------------+---------------------+
## | 4  | 2021-05-12 | Wednesday       | 30170857.57         |
## +----+------------+-----------------+---------------------+
## | 5  | 2021-05-13 | Thursday        | 30174977.62         |
## +----+------------+-----------------+---------------------+
## | 6  | 2021-05-14 | Friday          | 30178947.57         |
## +----+------------+-----------------+---------------------+
## | 7  | 2021-05-15 | Saturday        | 30182772.85         |
## +----+------------+-----------------+---------------------+
## | 8  | 2021-05-16 | Sunday          | 30186458.68         |
## +----+------------+-----------------+---------------------+
## | 9  | 2021-05-17 | Monday          | 30190010.12         |
## +----+------------+-----------------+---------------------+
## | 10 | 2021-05-18 | Tuesday         | 30193432.01         |
## +----+------------+-----------------+---------------------+
## | 11 | 2021-05-19 | Wednesday       | 30196729.06         |
## +----+------------+-----------------+---------------------+
## | 12 | 2021-05-20 | Thursday        | 30199905.78         |
## +----+------------+-----------------+---------------------+
## | 13 | 2021-05-21 | Friday          | 30202966.54         |
## +----+------------+-----------------+---------------------+
## | 14 | 2021-05-22 | Saturday        | 30205915.53         |
## +----+------------+-----------------+---------------------+
## | 15 | 2021-05-23 | Sunday          | 30208756.83         |
## +----+------------+-----------------+---------------------+
## | 16 | 2021-05-24 | Monday          | 30211494.32         |
## +----+------------+-----------------+---------------------+
## | 17 | 2021-05-25 | Tuesday         | 30214131.79         |
## +----+------------+-----------------+---------------------+
## | 18 | 2021-05-26 | Wednesday       | 30216672.87         |
## +----+------------+-----------------+---------------------+
## | 19 | 2021-05-27 | Thursday        | 30219121.06         |
## +----+------------+-----------------+---------------------+
## | 20 | 2021-05-28 | Friday          | 30221479.74         |
## +----+------------+-----------------+---------------------+
## | 21 | 2021-05-29 | Saturday        | 30223752.16         |
## +----+------------+-----------------+---------------------+
## | 22 | 2021-05-30 | Sunday          | 30225941.46         |
## +----+------------+-----------------+---------------------+
paste("Forecasting by using Ensembling Model  ==> ", y_lab , sep=" ")
## [1] "Forecasting by using Ensembling Model  ==>  Forecasting cumulative Covid 19 Infection cases in USA"
print(ascii(data.frame(FD,forecating_date=forecasting_data_by_name,forecasting.Ensembling=tail(Ensembling.Average,N_forecasting_days))), type = "rest")
## 
## +----+------------+-----------------+------------------------+
## |    | FD         | forecating_date | forecasting.Ensembling |
## +====+============+=================+========================+
## | 1  | 2021-05-09 | Sunday          | 32072097.12            |
## +----+------------+-----------------+------------------------+
## | 2  | 2021-05-10 | Monday          | 32126704.93            |
## +----+------------+-----------------+------------------------+
## | 3  | 2021-05-11 | Tuesday         | 32181324.89            |
## +----+------------+-----------------+------------------------+
## | 4  | 2021-05-12 | Wednesday       | 32235986.67            |
## +----+------------+-----------------+------------------------+
## | 5  | 2021-05-13 | Thursday        | 32290764.28            |
## +----+------------+-----------------+------------------------+
## | 6  | 2021-05-14 | Friday          | 32345536.87            |
## +----+------------+-----------------+------------------------+
## | 7  | 2021-05-15 | Saturday        | 32400310.76            |
## +----+------------+-----------------+------------------------+
## | 8  | 2021-05-16 | Sunday          | 32455171.34            |
## +----+------------+-----------------+------------------------+
## | 9  | 2021-05-17 | Monday          | 32510022.88            |
## +----+------------+-----------------+------------------------+
## | 10 | 2021-05-18 | Tuesday         | 32564887.57            |
## +----+------------+-----------------+------------------------+
## | 11 | 2021-05-19 | Wednesday       | 32619853.42            |
## +----+------------+-----------------+------------------------+
## | 12 | 2021-05-20 | Thursday        | 32674824.15            |
## +----+------------+-----------------+------------------------+
## | 13 | 2021-05-21 | Friday          | 32729823.18            |
## +----+------------+-----------------+------------------------+
## | 14 | 2021-05-22 | Saturday        | 32784933.42            |
## +----+------------+-----------------+------------------------+
## | 15 | 2021-05-23 | Sunday          | 32840039.59            |
## +----+------------+-----------------+------------------------+
## | 16 | 2021-05-24 | Monday          | 32895141.89            |
## +----+------------+-----------------+------------------------+
## | 17 | 2021-05-25 | Tuesday         | 32950319.82            |
## +----+------------+-----------------+------------------------+
## | 18 | 2021-05-26 | Wednesday       | 33005487.69            |
## +----+------------+-----------------+------------------------+
## | 19 | 2021-05-27 | Thursday        | 33060690.30            |
## +----+------------+-----------------+------------------------+
## | 20 | 2021-05-28 | Friday          | 33116026.94            |
## +----+------------+-----------------+------------------------+
## | 21 | 2021-05-29 | Saturday        | 33171382.06            |
## +----+------------+-----------------+------------------------+
## | 22 | 2021-05-30 | Sunday          | 33226739.74            |
## +----+------------+-----------------+------------------------+
result<-c(x1,x2,x3,x4,x5,x6)
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,Ensemblingt=MAPE_Mean_EnsemblingAverage,Best.Model=result)
print(ascii(table(table.error)), type = "rest")
## 
## +---+--------------+------------+------------+-------------+------------+-------------+-------------+------------+------+
## |   | country.name | NNAR.model | BATS.Model | TBATS.Model | Holt.Model | ARIMA.Model | Ensemblingt | Best.Model | Freq |
## +===+==============+============+============+=============+============+=============+=============+============+======+
## | 1 | USA          | 3.465      | 0.627      | 0.633       | 0.612      | 0.795       | 0.689       | Holt Model | 1.00 |
## +---+--------------+------------+------------+-------------+------------+-------------+-------------+------------+------+
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_EnsemblingAverage)
Model<-c("NNAR.model","BATS.Model","TBATS.Model","Holt.Model","ARIMA.Model","Ensembling.weight")
channel_data<-data.frame(Model,MAPE.Value)
# Normally, the entire expression below would be assigned to an object, but we're
# going bare bones here.
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 BATS, TBATS, Holt's Linear Trend,ARIMA Model, and Ensembling Model ==>",y_lab, sep=" ")
## System finished Modelling and Forecasting  by using BATS, TBATS, Holt's Linear Trend,ARIMA Model, and Ensembling Model ==>Forecasting cumulative Covid 19 Infection cases in USA
message(" Thank you for using our System For Modelling and Forecasting ==> ",y_lab, sep=" ")
##  Thank you for using our System For Modelling and Forecasting ==> Forecasting cumulative Covid 19 Infection cases in USA