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 Spain" # 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<-3# Number of Neural For model NNAR Model
NNAR_Model<- TRUE #create new model (TRUE/FALSE)
frequency<-"days"
country.name <- "Spain"
# Data Preparation & calculate some of statistics measures
summary(original_data) # Summary your time series
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0 227641 522283 1176543 2019324 3559222
# calculate standard deviation
data.frame(kurtosis=kurtosis(original_data)) # calculate Cofficient of kurtosis
## kurtosis
## 1 1.998213
data.frame(skewness=skewness(original_data)) # calculate Cofficient of skewness
## skewness
## 1 0.7478569
data.frame(Standard.deviation =sd(original_data))
## Standard.deviation
## 1 1225431
#processing on data (input data)
rows <- NROW(original_data) # calculate number of rows in time series (number of days)
training_data<-original_data[1:(rows-validation_data_days)] # Training data
testing_data<-original_data[(rows-validation_data_days+1):rows] #testing data
AD<-fulldate<-seq(as.Date(Actual_date_interval[1]),as.Date(Actual_date_interval[2]), frequency) #input range for actual date
FD<-seq(as.Date(Forecast_date_interval[1]),as.Date(Forecast_date_interval[2]), frequency) #input range forecasting date
N_forecasting_days<-nrow(data.frame(FD)) #calculate number of days that you want to forecasting
validation_dates<-tail(AD,validation_data_days) # select validation_dates
validation_data_by_name<-weekdays(validation_dates) # put names of validation dates
forecasting_data_by_name<-weekdays(FD) # put names of Forecasting dates
#NNAR Model
if(NNAR_Model==TRUE){
data_series<-ts(training_data)
model_NNAR<-nnetar(data_series, size = Number_Neural)
saveRDS(model_NNAR, file = "model_NNAR.RDS")
my_model <- readRDS("model_NNAR.RDS")
accuracy(model_NNAR) # accuracy on training data #Print Model Parameters
model_NNAR
}
## Series: data_series
## Model: NNAR(1,3)
## Call: nnetar(y = data_series, size = Number_Neural)
##
## Average of 20 networks, each of which is
## a 1-3-1 network with 10 weights
## options were - linear output units
##
## sigma^2 estimated as 21607840
if(NNAR_Model==FALSE){
data_series<-ts(training_data)
#model_NNAR<-nnetar(data_series, size = Number_Numeral)
model_NNAR <- readRDS("model_NNAR.RDS")
accuracy(model_NNAR) # accuracy on training data #Print Model Parameters
model_NNAR
}
# Testing Data Evaluation
forecasting_NNAR <- forecast(model_NNAR, h=N_forecasting_days+validation_data_days)
validation_forecast<-head(forecasting_NNAR$mean,validation_data_days)
MAPE_Per_Day<-round( abs(((testing_data-validation_forecast)/testing_data)*100) ,3)
paste ("MAPE % For ",validation_data_days,frequency,"by using NNAR Model for ==> ",y_lab, sep=" ")
## [1] "MAPE % For 45 days by using NNAR Model for ==> Forecasting cumulative Covid 19 Infection cases in Spain"
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 Spain"
paste(MAPE_Mean_All,"%")
## [1] "5.651 % MAPE 45 days Forecasting cumulative Covid 19 Infection cases in Spain %"
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 Spain"
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 | 3248230.00 | 3236872.18 | 0.35 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 2 | 2021-03-26 | Friday | 3254952.00 | 3233211.20 | 0.668 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 3 | 2021-03-27 | Saturday | 3262114.00 | 3229974.73 | 0.985 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 4 | 2021-03-28 | Sunday | 3269572.00 | 3227110.72 | 1.299 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 5 | 2021-03-29 | Monday | 3273596.00 | 3224574.10 | 1.497 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 6 | 2021-03-30 | Tuesday | 3276454.00 | 3222325.73 | 1.652 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 7 | 2021-03-31 | Wednesday | 3283659.00 | 3220331.48 | 1.929 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 8 | 2021-04-01 | Thursday | 3291714.00 | 3218561.59 | 2.222 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 9 | 2021-04-02 | Friday | 3300557.00 | 3216989.96 | 2.532 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 10 | 2021-04-03 | Saturday | 3306079.00 | 3215593.74 | 2.737 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 11 | 2021-04-04 | Sunday | 3309784.00 | 3214352.83 | 2.883 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 12 | 2021-04-05 | Monday | 3314226.00 | 3213249.54 | 3.047 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 13 | 2021-04-06 | Tuesday | 3318361.00 | 3212268.29 | 3.197 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 14 | 2021-04-07 | Wednesday | 3325910.00 | 3211395.32 | 3.443 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 15 | 2021-04-08 | Thursday | 3336583.00 | 3210618.49 | 3.775 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 16 | 2021-04-09 | Friday | 3347006.00 | 3209927.04 | 4.096 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 17 | 2021-04-10 | Saturday | 3357203.00 | 3209311.46 | 4.405 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 18 | 2021-04-11 | Sunday | 3367567.00 | 3208763.34 | 4.716 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 19 | 2021-04-12 | Monday | 3373134.00 | 3208275.19 | 4.887 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 20 | 2021-04-13 | Tuesday | 3376983.00 | 3207840.39 | 5.009 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 21 | 2021-04-14 | Wednesday | 3385794.00 | 3207453.07 | 5.267 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 22 | 2021-04-15 | Thursday | 3395813.00 | 3207107.99 | 5.557 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 23 | 2021-04-16 | Friday | 3406128.00 | 3206800.53 | 5.852 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 24 | 2021-04-17 | Saturday | 3415681.00 | 3206526.55 | 6.123 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 25 | 2021-04-18 | Sunday | 3425481.00 | 3206282.39 | 6.399 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 26 | 2021-04-19 | Monday | 3430708.00 | 3206064.78 | 6.548 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 27 | 2021-04-20 | Tuesday | 3434562.00 | 3205870.83 | 6.659 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 28 | 2021-04-21 | Wednesday | 3443097.00 | 3205697.96 | 6.895 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 29 | 2021-04-22 | Thursday | 3453707.00 | 3205543.86 | 7.185 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 30 | 2021-04-23 | Friday | 3463275.00 | 3205406.50 | 7.446 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 31 | 2021-04-24 | Saturday | 3472978.00 | 3205284.04 | 7.708 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 32 | 2021-04-25 | Sunday | 3482762.00 | 3205174.88 | 7.97 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 33 | 2021-04-26 | Monday | 3487645.00 | 3205077.55 | 8.102 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 34 | 2021-04-27 | Tuesday | 3491418.00 | 3204990.78 | 8.204 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 35 | 2021-04-28 | Wednesday | 3500207.00 | 3204913.42 | 8.436 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 36 | 2021-04-29 | Thursday | 3509801.00 | 3204844.44 | 8.689 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 37 | 2021-04-30 | Friday | 3518940.00 | 3204782.94 | 8.928 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 38 | 2021-05-01 | Saturday | 3527161.00 | 3204728.11 | 9.141 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 39 | 2021-05-02 | Sunday | 3535951.00 | 3204679.22 | 9.369 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 40 | 2021-05-03 | Monday | 3539729.00 | 3204635.62 | 9.467 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 41 | 2021-05-04 | Tuesday | 3542654.00 | 3204596.75 | 9.542 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 42 | 2021-05-05 | Wednesday | 3548434.00 | 3204562.09 | 9.691 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 43 | 2021-05-06 | Thursday | 3554988.00 | 3204531.18 | 9.858 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 44 | 2021-05-07 | Friday | 3559222.00 | 3204503.62 | 9.966 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 45 | 2021-05-08 | Saturday | 3559222.00 | 3204479.05 | 9.967 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
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 | 3204457.14 |
## +----+------------+-----------------+---------------------+
## | 2 | 2021-05-10 | Monday | 3204437.60 |
## +----+------------+-----------------+---------------------+
## | 3 | 2021-05-11 | Tuesday | 3204420.17 |
## +----+------------+-----------------+---------------------+
## | 4 | 2021-05-12 | Wednesday | 3204404.64 |
## +----+------------+-----------------+---------------------+
## | 5 | 2021-05-13 | Thursday | 3204390.78 |
## +----+------------+-----------------+---------------------+
## | 6 | 2021-05-14 | Friday | 3204378.43 |
## +----+------------+-----------------+---------------------+
## | 7 | 2021-05-15 | Saturday | 3204367.41 |
## +----+------------+-----------------+---------------------+
## | 8 | 2021-05-16 | Sunday | 3204357.58 |
## +----+------------+-----------------+---------------------+
## | 9 | 2021-05-17 | Monday | 3204348.82 |
## +----+------------+-----------------+---------------------+
## | 10 | 2021-05-18 | Tuesday | 3204341.01 |
## +----+------------+-----------------+---------------------+
## | 11 | 2021-05-19 | Wednesday | 3204334.04 |
## +----+------------+-----------------+---------------------+
## | 12 | 2021-05-20 | Thursday | 3204327.83 |
## +----+------------+-----------------+---------------------+
## | 13 | 2021-05-21 | Friday | 3204322.29 |
## +----+------------+-----------------+---------------------+
## | 14 | 2021-05-22 | Saturday | 3204317.35 |
## +----+------------+-----------------+---------------------+
## | 15 | 2021-05-23 | Sunday | 3204312.95 |
## +----+------------+-----------------+---------------------+
## | 16 | 2021-05-24 | Monday | 3204309.02 |
## +----+------------+-----------------+---------------------+
## | 17 | 2021-05-25 | Tuesday | 3204305.51 |
## +----+------------+-----------------+---------------------+
## | 18 | 2021-05-26 | Wednesday | 3204302.39 |
## +----+------------+-----------------+---------------------+
## | 19 | 2021-05-27 | Thursday | 3204299.60 |
## +----+------------+-----------------+---------------------+
## | 20 | 2021-05-28 | Friday | 3204297.12 |
## +----+------------+-----------------+---------------------+
## | 21 | 2021-05-29 | Saturday | 3204294.90 |
## +----+------------+-----------------+---------------------+
## | 22 | 2021-05-30 | Sunday | 3204292.93 |
## +----+------------+-----------------+---------------------+
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 160.9778 2178.411 1196.605 NaN Inf 0.1646661 -0.04432059
# Print Model Parameters
model_bats
## BATS(1, {5,4}, 0.962, -)
##
## Call: bats(y = data_series)
##
## Parameters
## Alpha: 0.2998805
## Beta: 0.1376748
## Damping Parameter: 0.962086
## AR coefficients: 1.354782 -0.589899 -0.230266 0.104485 0.2754
## MA coefficients: -0.191869 -0.518122 0.465248 0.328327
##
## Seed States:
## [,1]
## [1,] 689.6705
## [2,] -70.9064
## [3,] 0.0000
## [4,] 0.0000
## [5,] 0.0000
## [6,] 0.0000
## [7,] 0.0000
## [8,] 0.0000
## [9,] 0.0000
## [10,] 0.0000
## [11,] 0.0000
##
## Sigma: 2178.411
## AIC: 9645.442
#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 Spain"
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 Spain"
paste(MAPE_Mean_All.bats,"%")
## [1] "2.007 % MAPE 45 days Forecasting cumulative Covid 19 Infection cases in Spain %"
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 Spain"
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 | 3248230.00 | 3247905.52 | 0.01 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 2 | 2021-03-26 | Friday | 3254952.00 | 3253459.35 | 0.046 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 3 | 2021-03-27 | Saturday | 3262114.00 | 3258658.29 | 0.106 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 4 | 2021-03-28 | Sunday | 3269572.00 | 3262730.29 | 0.209 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 5 | 2021-03-29 | Monday | 3273596.00 | 3266213.74 | 0.226 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 6 | 2021-03-30 | Tuesday | 3276454.00 | 3270199.69 | 0.191 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 7 | 2021-03-31 | Wednesday | 3283659.00 | 3275053.99 | 0.262 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 8 | 2021-04-01 | Thursday | 3291714.00 | 3280694.16 | 0.335 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 9 | 2021-04-02 | Friday | 3300557.00 | 3286385.54 | 0.429 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 10 | 2021-04-03 | Saturday | 3306079.00 | 3291360.09 | 0.445 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 11 | 2021-04-04 | Sunday | 3309784.00 | 3295368.79 | 0.436 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 12 | 2021-04-05 | Monday | 3314226.00 | 3298789.15 | 0.466 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 13 | 2021-04-06 | Tuesday | 3318361.00 | 3302357.35 | 0.482 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 14 | 2021-04-07 | Wednesday | 3325910.00 | 3306623.27 | 0.58 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 15 | 2021-04-08 | Thursday | 3336583.00 | 3311573.54 | 0.75 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 16 | 2021-04-09 | Friday | 3347006.00 | 3316667.48 | 0.906 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 17 | 2021-04-10 | Saturday | 3357203.00 | 3321235.09 | 1.071 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 18 | 2021-04-11 | Sunday | 3367567.00 | 3324951.29 | 1.265 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 19 | 2021-04-12 | Monday | 3373134.00 | 3328045.81 | 1.337 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 20 | 2021-04-13 | Tuesday | 3376983.00 | 3331116.08 | 1.358 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 21 | 2021-04-14 | Wednesday | 3385794.00 | 3334692.28 | 1.509 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 22 | 2021-04-15 | Thursday | 3395813.00 | 3338869.20 | 1.677 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 23 | 2021-04-16 | Friday | 3406128.00 | 3343259.71 | 1.846 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 24 | 2021-04-17 | Saturday | 3415681.00 | 3347287.36 | 2.002 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 25 | 2021-04-18 | Sunday | 3425481.00 | 3350597.91 | 2.186 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 26 | 2021-04-19 | Monday | 3430708.00 | 3353296.85 | 2.256 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 27 | 2021-04-20 | Tuesday | 3434562.00 | 3355854.71 | 2.292 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 28 | 2021-04-21 | Wednesday | 3443097.00 | 3358761.72 | 2.449 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 29 | 2021-04-22 | Thursday | 3453707.00 | 3362184.64 | 2.65 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 30 | 2021-04-23 | Friday | 3463275.00 | 3365865.59 | 2.813 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 31 | 2021-04-24 | Saturday | 3472978.00 | 3369322.37 | 2.985 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 32 | 2021-04-25 | Sunday | 3482762.00 | 3372196.45 | 3.175 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 33 | 2021-04-26 | Monday | 3487645.00 | 3374498.61 | 3.244 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 34 | 2021-04-27 | Tuesday | 3491418.00 | 3376585.12 | 3.289 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 35 | 2021-04-28 | Wednesday | 3500207.00 | 3378893.68 | 3.466 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 36 | 2021-04-29 | Thursday | 3509801.00 | 3381634.56 | 3.652 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 37 | 2021-04-30 | Friday | 3518940.00 | 3384654.94 | 3.816 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 38 | 2021-05-01 | Saturday | 3527161.00 | 3387563.34 | 3.958 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 39 | 2021-05-02 | Sunday | 3535951.00 | 3390015.14 | 4.127 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 40 | 2021-05-03 | Monday | 3539729.00 | 3391952.27 | 4.175 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 41 | 2021-05-04 | Tuesday | 3542654.00 | 3393631.54 | 4.207 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 42 | 2021-05-05 | Wednesday | 3548434.00 | 3395431.69 | 4.312 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 43 | 2021-05-06 | Thursday | 3554988.00 | 3397584.02 | 4.428 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 44 | 2021-05-07 | Friday | 3559222.00 | 3400018.48 | 4.473 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 45 | 2021-05-08 | Saturday | 3559222.00 | 3402427.53 | 4.405 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
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 | 3404492.95 | 3049309.50 | 2861286.73 | 3049309.50 | 2861286.73 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 2 | 2021-05-10 | Monday | 3406109.82 | 3039869.82 | 2845994.07 | 3039869.82 | 2845994.07 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 3 | 2021-05-11 | Tuesday | 3407451.01 | 3030185.26 | 2830472.84 | 3030185.26 | 2830472.84 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 4 | 2021-05-12 | Wednesday | 3408834.56 | 3020518.25 | 2814956.01 | 3020518.25 | 2814956.01 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 5 | 2021-05-13 | Thursday | 3410495.72 | 3011065.06 | 2799619.23 | 3011065.06 | 2799619.23 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 6 | 2021-05-14 | Friday | 3412427.28 | 3001824.81 | 2784464.99 | 3001824.81 | 2784464.99 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 7 | 2021-05-15 | Saturday | 3414396.73 | 2992612.12 | 2769332.84 | 2992612.12 | 2769332.84 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 8 | 2021-05-16 | Sunday | 3416120.09 | 2983198.23 | 2754023.24 | 2983198.23 | 2754023.24 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 9 | 2021-05-17 | Monday | 3417463.72 | 2973476.58 | 2738443.99 | 2973476.58 | 2738443.99 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 10 | 2021-05-18 | Tuesday | 3418531.74 | 2963536.15 | 2722676.04 | 2963536.15 | 2722676.04 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 11 | 2021-05-19 | Wednesday | 3419583.24 | 2953594.51 | 2706914.98 | 2953594.51 | 2706914.98 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 12 | 2021-05-20 | Thursday | 3420844.98 | 2943842.98 | 2691333.37 | 2943842.98 | 2691333.37 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 13 | 2021-05-21 | Friday | 3422355.25 | 2934316.43 | 2675964.29 | 2934316.43 | 2675964.29 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 14 | 2021-05-22 | Saturday | 3423946.65 | 2924878.25 | 2660687.40 | 2924878.25 | 2660687.40 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 15 | 2021-05-23 | Sunday | 3425373.43 | 2915325.80 | 2645322.90 | 2915325.80 | 2645322.90 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 16 | 2021-05-24 | Monday | 3426487.68 | 2905537.62 | 2629763.31 | 2905537.62 | 2629763.31 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 17 | 2021-05-25 | Tuesday | 3427339.06 | 2895557.58 | 2614049.47 | 2895557.58 | 2614049.47 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 18 | 2021-05-26 | Wednesday | 3428131.80 | 2885559.71 | 2598339.40 | 2885559.71 | 2598339.40 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 19 | 2021-05-27 | Thursday | 3429075.65 | 2875722.89 | 2582795.64 | 2875722.89 | 2582795.64 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 20 | 2021-05-28 | Friday | 3430239.78 | 2866107.32 | 2567473.64 | 2866107.32 | 2567473.64 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 21 | 2021-05-29 | Saturday | 3431511.67 | 2856619.32 | 2552289.69 | 2856619.32 | 2552289.69 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 22 | 2021-05-30 | Sunday | 3432684.82 | 2847085.29 | 2537087.63 | 2847085.29 | 2537087.63 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
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 25.26126 2920.826 1572.821 NaN Inf 0.2164376 0.03566067
# Print Model Parameters
model_TBATS
## TBATS(1, {0,0}, 1, {<6,2>})
##
## Call: NULL
##
## Parameters
## Alpha: 1.574247
## Beta: 0.4458191
## Damping Parameter: 1
## Gamma-1 Values: -0.00368897
## Gamma-2 Values: 0.001441878
##
## Seed States:
## [,1]
## [1,] 738.41322
## [2,] -88.35011
## [3,] -355.88161
## [4,] -49.44511
## [5,] -58.87846
## [6,] 16.58838
##
## Sigma: 2920.826
## AIC: 9881.625
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 Spain"
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 Spain"
paste(MAPE_Mean_All.TBATS,"%")
## [1] "1.439 % MAPE 45 days Forecasting cumulative Covid 19 Infection cases in Spain %"
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 Spain"
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 | 3248230.00 | 3247489.27 | 0.023 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 2 | 2021-03-26 | Friday | 3254952.00 | 3252537.43 | 0.074 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 3 | 2021-03-27 | Saturday | 3262114.00 | 3257306.19 | 0.147 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 4 | 2021-03-28 | Sunday | 3269572.00 | 3261684.13 | 0.241 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 5 | 2021-03-29 | Monday | 3273596.00 | 3267037.63 | 0.2 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 6 | 2021-03-30 | Tuesday | 3276454.00 | 3272448.76 | 0.122 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 7 | 2021-03-31 | Wednesday | 3283659.00 | 3277163.72 | 0.198 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 8 | 2021-04-01 | Thursday | 3291714.00 | 3282211.89 | 0.289 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 9 | 2021-04-02 | Friday | 3300557.00 | 3286980.65 | 0.411 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 10 | 2021-04-03 | Saturday | 3306079.00 | 3291358.58 | 0.445 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 11 | 2021-04-04 | Sunday | 3309784.00 | 3296712.08 | 0.395 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 12 | 2021-04-05 | Monday | 3314226.00 | 3302123.21 | 0.365 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 13 | 2021-04-06 | Tuesday | 3318361.00 | 3306838.17 | 0.347 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 14 | 2021-04-07 | Wednesday | 3325910.00 | 3311886.34 | 0.422 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 15 | 2021-04-08 | Thursday | 3336583.00 | 3316655.10 | 0.597 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 16 | 2021-04-09 | Friday | 3347006.00 | 3321033.03 | 0.776 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 17 | 2021-04-10 | Saturday | 3357203.00 | 3326386.53 | 0.918 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 18 | 2021-04-11 | Sunday | 3367567.00 | 3331797.66 | 1.062 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 19 | 2021-04-12 | Monday | 3373134.00 | 3336512.63 | 1.086 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 20 | 2021-04-13 | Tuesday | 3376983.00 | 3341560.79 | 1.049 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 21 | 2021-04-14 | Wednesday | 3385794.00 | 3346329.55 | 1.166 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 22 | 2021-04-15 | Thursday | 3395813.00 | 3350707.48 | 1.328 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 23 | 2021-04-16 | Friday | 3406128.00 | 3356060.99 | 1.47 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 24 | 2021-04-17 | Saturday | 3415681.00 | 3361472.12 | 1.587 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 25 | 2021-04-18 | Sunday | 3425481.00 | 3366187.08 | 1.731 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 26 | 2021-04-19 | Monday | 3430708.00 | 3371235.24 | 1.734 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 27 | 2021-04-20 | Tuesday | 3434562.00 | 3376004.01 | 1.705 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 28 | 2021-04-21 | Wednesday | 3443097.00 | 3380381.94 | 1.821 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 29 | 2021-04-22 | Thursday | 3453707.00 | 3385735.44 | 1.968 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 30 | 2021-04-23 | Friday | 3463275.00 | 3391146.57 | 2.083 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 31 | 2021-04-24 | Saturday | 3472978.00 | 3395861.53 | 2.22 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 32 | 2021-04-25 | Sunday | 3482762.00 | 3400909.70 | 2.35 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 33 | 2021-04-26 | Monday | 3487645.00 | 3405678.46 | 2.35 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 34 | 2021-04-27 | Tuesday | 3491418.00 | 3410056.39 | 2.33 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 35 | 2021-04-28 | Wednesday | 3500207.00 | 3415409.89 | 2.423 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 36 | 2021-04-29 | Thursday | 3509801.00 | 3420821.02 | 2.535 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 37 | 2021-04-30 | Friday | 3518940.00 | 3425535.98 | 2.654 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 38 | 2021-05-01 | Saturday | 3527161.00 | 3430584.15 | 2.738 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 39 | 2021-05-02 | Sunday | 3535951.00 | 3435352.91 | 2.845 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 40 | 2021-05-03 | Monday | 3539729.00 | 3439730.84 | 2.825 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 41 | 2021-05-04 | Tuesday | 3542654.00 | 3445084.34 | 2.754 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 42 | 2021-05-05 | Wednesday | 3548434.00 | 3450495.47 | 2.76 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 43 | 2021-05-06 | Thursday | 3554988.00 | 3455210.44 | 2.807 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 44 | 2021-05-07 | Friday | 3559222.00 | 3460258.60 | 2.78 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
## | 45 | 2021-05-08 | Saturday | 3559222.00 | 3465027.36 | 2.646 % |
## +----+------------+-------------------------+-------------+-------------------+------------------+
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 | 3469405.30 | 3431800.78 | 3411894.14 | 3507009.82 | 3526916.45 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 2 | 2021-05-10 | Monday | 3474758.80 | 3436785.86 | 3416684.21 | 3512731.73 | 3532833.39 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 3 | 2021-05-11 | Tuesday | 3480169.93 | 3441836.71 | 3421544.34 | 3518503.14 | 3538795.52 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 4 | 2021-05-12 | Wednesday | 3484884.89 | 3446198.49 | 3425719.14 | 3523571.30 | 3544050.64 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 5 | 2021-05-13 | Thursday | 3489933.06 | 3450897.71 | 3430233.65 | 3528968.40 | 3549632.46 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 6 | 2021-05-14 | Friday | 3494701.82 | 3455322.00 | 3434475.58 | 3534081.64 | 3554928.06 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 7 | 2021-05-15 | Saturday | 3499079.75 | 3459358.44 | 3438331.24 | 3538801.06 | 3559828.26 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 8 | 2021-05-16 | Sunday | 3504433.25 | 3464373.81 | 3443167.62 | 3544492.69 | 3565698.88 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 9 | 2021-05-17 | Monday | 3509844.38 | 3469453.93 | 3448072.52 | 3550234.83 | 3571616.25 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 10 | 2021-05-18 | Tuesday | 3514559.34 | 3473844.06 | 3452290.70 | 3555274.62 | 3576827.99 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 11 | 2021-05-19 | Wednesday | 3519607.51 | 3478570.97 | 3456847.53 | 3560644.05 | 3582367.48 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 12 | 2021-05-20 | Thursday | 3524376.27 | 3483022.24 | 3461130.74 | 3565730.30 | 3587621.80 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 13 | 2021-05-21 | Friday | 3528754.20 | 3487085.10 | 3465026.81 | 3570423.30 | 3592481.59 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 14 | 2021-05-22 | Saturday | 3534107.70 | 3492126.32 | 3469902.72 | 3576089.09 | 3598312.69 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 15 | 2021-05-23 | Sunday | 3539518.83 | 3497231.51 | 3474845.95 | 3581806.16 | 3604191.71 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 16 | 2021-05-24 | Monday | 3544233.80 | 3501646.01 | 3479101.40 | 3586821.58 | 3609366.19 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 17 | 2021-05-25 | Tuesday | 3549281.96 | 3506396.75 | 3483694.70 | 3592167.17 | 3614869.22 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 18 | 2021-05-26 | Wednesday | 3554050.72 | 3510871.35 | 3488013.57 | 3597230.10 | 3620087.88 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 19 | 2021-05-27 | Thursday | 3558428.65 | 3514957.10 | 3491944.65 | 3601900.21 | 3624912.66 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 20 | 2021-05-28 | Friday | 3563782.16 | 3520020.78 | 3496854.90 | 3607543.54 | 3630709.41 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 21 | 2021-05-29 | Saturday | 3569193.29 | 3525147.80 | 3501831.52 | 3613238.78 | 3636555.05 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 22 | 2021-05-30 | Sunday | 3573908.25 | 3529583.57 | 3506119.51 | 3618232.93 | 3641696.99 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
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 -41.16849 3485.675 1822.045 NaN Inf 0.2507336 0.08318591
# 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.5793
##
## Smoothing parameters:
## alpha = 0.9999
## beta = 0.9999
##
## Initial states:
## l = -1.2027
## b = -0.9838
##
## sigma: 9.0076
##
## AIC AICc BIC
## 4698.896 4699.032 4719.409
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -41.16849 3485.675 1822.045 NaN Inf 0.2507336 0.08318591
# 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 Spain"
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 Spain"
paste(MAPE_Mean_All.Holt,"%")
## [1] "0.746 % MAPE 45 days Forecasting cumulative Covid 19 Infection cases in Spain %"
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 Spain"
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 | 3248230.00 | 3247005.83 | 0.038 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 2 | 2021-03-26 | Friday | 3254952.00 | 3252998.70 | 0.06 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 3 | 2021-03-27 | Saturday | 3262114.00 | 3258996.20 | 0.096 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 4 | 2021-03-28 | Sunday | 3269572.00 | 3264998.36 | 0.14 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 5 | 2021-03-29 | Monday | 3273596.00 | 3271005.16 | 0.079 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 6 | 2021-03-30 | Tuesday | 3276454.00 | 3277016.61 | 0.017 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 7 | 2021-03-31 | Wednesday | 3283659.00 | 3283032.70 | 0.019 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 8 | 2021-04-01 | Thursday | 3291714.00 | 3289053.43 | 0.081 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 9 | 2021-04-02 | Friday | 3300557.00 | 3295078.80 | 0.166 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 10 | 2021-04-03 | Saturday | 3306079.00 | 3301108.81 | 0.15 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 11 | 2021-04-04 | Sunday | 3309784.00 | 3307143.46 | 0.08 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 12 | 2021-04-05 | Monday | 3314226.00 | 3313182.74 | 0.031 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 13 | 2021-04-06 | Tuesday | 3318361.00 | 3319226.66 | 0.026 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 14 | 2021-04-07 | Wednesday | 3325910.00 | 3325275.21 | 0.019 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 15 | 2021-04-08 | Thursday | 3336583.00 | 3331328.39 | 0.157 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 16 | 2021-04-09 | Friday | 3347006.00 | 3337386.21 | 0.287 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 17 | 2021-04-10 | Saturday | 3357203.00 | 3343448.65 | 0.41 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 18 | 2021-04-11 | Sunday | 3367567.00 | 3349515.73 | 0.536 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 19 | 2021-04-12 | Monday | 3373134.00 | 3355587.43 | 0.52 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 20 | 2021-04-13 | Tuesday | 3376983.00 | 3361663.75 | 0.454 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 21 | 2021-04-14 | Wednesday | 3385794.00 | 3367744.70 | 0.533 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 22 | 2021-04-15 | Thursday | 3395813.00 | 3373830.28 | 0.647 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 23 | 2021-04-16 | Friday | 3406128.00 | 3379920.47 | 0.769 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 24 | 2021-04-17 | Saturday | 3415681.00 | 3386015.29 | 0.869 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 25 | 2021-04-18 | Sunday | 3425481.00 | 3392114.72 | 0.974 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 26 | 2021-04-19 | Monday | 3430708.00 | 3398218.77 | 0.947 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 27 | 2021-04-20 | Tuesday | 3434562.00 | 3404327.44 | 0.88 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 28 | 2021-04-21 | Wednesday | 3443097.00 | 3410440.72 | 0.948 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 29 | 2021-04-22 | Thursday | 3453707.00 | 3416558.62 | 1.076 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 30 | 2021-04-23 | Friday | 3463275.00 | 3422681.12 | 1.172 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 31 | 2021-04-24 | Saturday | 3472978.00 | 3428808.24 | 1.272 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 32 | 2021-04-25 | Sunday | 3482762.00 | 3434939.97 | 1.373 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 33 | 2021-04-26 | Monday | 3487645.00 | 3441076.31 | 1.335 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 34 | 2021-04-27 | Tuesday | 3491418.00 | 3447217.26 | 1.266 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 35 | 2021-04-28 | Wednesday | 3500207.00 | 3453362.81 | 1.338 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 36 | 2021-04-29 | Thursday | 3509801.00 | 3459512.96 | 1.433 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 37 | 2021-04-30 | Friday | 3518940.00 | 3465667.72 | 1.514 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 38 | 2021-05-01 | Saturday | 3527161.00 | 3471827.08 | 1.569 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 39 | 2021-05-02 | Sunday | 3535951.00 | 3477991.04 | 1.639 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 40 | 2021-05-03 | Monday | 3539729.00 | 3484159.60 | 1.57 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 41 | 2021-05-04 | Tuesday | 3542654.00 | 3490332.76 | 1.477 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 42 | 2021-05-05 | Wednesday | 3548434.00 | 3496510.52 | 1.463 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 43 | 2021-05-06 | Thursday | 3554988.00 | 3502692.87 | 1.471 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 44 | 2021-05-07 | Friday | 3559222.00 | 3508879.82 | 1.414 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
## | 45 | 2021-05-08 | Saturday | 3559222.00 | 3515071.35 | 1.24 % |
## +----+------------+-------------------------+-------------+------------------+-----------------+
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 | 3521267.48 | 2409012.28 | 1893484.97 | 4805744.22 | 5552500.97 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 2 | 2021-05-10 | Monday | 3527468.20 | 2381190.54 | 1852600.92 | 4857282.30 | 5632337.42 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 3 | 2021-05-11 | Tuesday | 3533673.51 | 2353157.30 | 1811616.75 | 4909525.09 | 5713424.86 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 4 | 2021-05-12 | Wednesday | 3539883.41 | 2324921.62 | 1770552.23 | 4962473.63 | 5795768.02 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 5 | 2021-05-13 | Thursday | 3546097.89 | 2296492.44 | 1729427.07 | 5016129.08 | 5879371.77 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 6 | 2021-05-14 | Friday | 3552316.96 | 2267878.65 | 1688260.92 | 5070492.68 | 5964241.09 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 7 | 2021-05-15 | Saturday | 3558540.62 | 2239089.02 | 1647073.41 | 5125565.77 | 6050381.10 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 8 | 2021-05-16 | Sunday | 3564768.85 | 2210132.28 | 1605884.14 | 5181349.77 | 6137797.01 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 9 | 2021-05-17 | Monday | 3571001.67 | 2181017.05 | 1564712.71 | 5237846.19 | 6226494.12 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 10 | 2021-05-18 | Tuesday | 3577239.06 | 2151751.90 | 1523578.72 | 5295056.58 | 6316477.84 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 11 | 2021-05-19 | Wednesday | 3583481.04 | 2122345.36 | 1482501.80 | 5352982.60 | 6407753.66 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 12 | 2021-05-20 | Thursday | 3589727.59 | 2092805.88 | 1441501.60 | 5411625.97 | 6500327.17 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 13 | 2021-05-21 | Friday | 3595978.72 | 2063141.85 | 1400597.82 | 5470988.44 | 6594204.01 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 14 | 2021-05-22 | Saturday | 3602234.43 | 2033361.64 | 1359810.19 | 5531071.86 | 6689389.91 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 15 | 2021-05-23 | Sunday | 3608494.71 | 2003473.55 | 1319158.56 | 5591878.11 | 6785890.66 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 16 | 2021-05-24 | Monday | 3614759.56 | 1973485.86 | 1278662.81 | 5653409.14 | 6883712.11 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 17 | 2021-05-25 | Tuesday | 3621028.98 | 1943406.80 | 1238342.96 | 5715666.93 | 6982860.17 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 18 | 2021-05-26 | Wednesday | 3627302.98 | 1913244.59 | 1198219.09 | 5778653.54 | 7083340.82 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 19 | 2021-05-27 | Thursday | 3633581.54 | 1883007.38 | 1158311.45 | 5842371.05 | 7185160.07 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 20 | 2021-05-28 | Friday | 3639864.67 | 1852703.34 | 1118640.39 | 5906821.59 | 7288323.99 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 21 | 2021-05-29 | Saturday | 3646152.36 | 1822340.58 | 1079226.43 | 5972007.32 | 7392838.68 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 22 | 2021-05-30 | Sunday | 3652444.62 | 1791927.23 | 1040090.26 | 6037930.45 | 7498710.30 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
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 Spain"
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.5147, 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.53848, 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.9756, Lag order = 7, p-value = 0.01033
## 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 Spain"
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 = 3.5814, Truncation lag parameter = 5, p-value = 0.01
pp.test(diff1_x1) # applay pp test after taking first differences
## Warning in pp.test(diff1_x1): p-value smaller than printed p-value
##
## Phillips-Perron Unit Root Test
##
## data: diff1_x1
## Dickey-Fuller Z(alpha) = -32.177, Truncation lag parameter = 5, p-value
## = 0.01
## alternative hypothesis: stationary
adf.test(diff1_x1) # applay adf test after taking first differences
##
## Augmented Dickey-Fuller Test
##
## data: diff1_x1
## Dickey-Fuller = -2.0718, Lag order = 7, p-value = 0.5474
## 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 Spain"
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.055458, 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) = -240.46, 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 = -5.2228, 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) : 8522.012
## ARIMA(0,2,1) : 8504.162
## ARIMA(0,2,2) : 8380.122
## ARIMA(0,2,3) : 8379.514
## ARIMA(0,2,4) : 8331.634
## ARIMA(0,2,5) : 8386.15
## ARIMA(1,2,0) : 8520.919
## ARIMA(1,2,1) : 8469.808
## ARIMA(1,2,2) : 8380.353
## ARIMA(1,2,3) : 8380.564
## ARIMA(1,2,4) : 8292.548
## ARIMA(2,2,0) : 8421.288
## ARIMA(2,2,1) : 8421.589
## ARIMA(2,2,2) : 8373.814
## ARIMA(2,2,3) : 8238.172
## ARIMA(3,2,0) : 8423.305
## ARIMA(3,2,1) : 8418.458
## ARIMA(3,2,2) : 8371.785
## ARIMA(4,2,0) : 8353.901
## ARIMA(4,2,1) : 8294.654
## ARIMA(5,2,0) : 8215.032
##
##
##
## Best model: ARIMA(5,2,0)
model1 # show the result of autoarima
## Series: data_series
## ARIMA(5,2,0)
##
## Coefficients:
## ar1 ar2 ar3 ar4 ar5
## -0.0749 -0.5976 -0.2729 -0.3175 -0.5187
## s.e. 0.0404 0.0375 0.0452 0.0374 0.0401
##
## sigma^2 estimated as 5961306: log likelihood=-4101.42
## AIC=8214.84 AICc=8215.03 BIC=8239.43
#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] 5 2 0
strtoi(bestmodel[3])
## [1] 0
#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 ar4 ar5
## -0.0749 -0.5976 -0.2729 -0.3175 -0.5187
## s.e. 0.0404 0.0375 0.0452 0.0374 0.0401
##
## sigma^2 estimated as 5894325: log likelihood = -4101.42, aic = 8214.84
paste ("accuracy of autoarima Model For ==> ",y_lab, sep=" ")
## [1] "accuracy of autoarima Model For ==> Forecasting cumulative Covid 19 Infection cases in Spain"
accuracy(x1_model1) # aacuracy of best model from auto arima
## ME RMSE MAE MPE MAPE MASE
## Training set 31.41968 2422.386 1326.952 0.8654711 2.241335 0.1826033
## ACF1
## Training set -0.07820906
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(5,2,0)
## Q* = 232.91, df = 5, p-value < 2.2e-16
##
## Model df: 5. Total lags used: 10
paste("Box-Ljung test , Ljung-Box test For Modelling for ==> ",y_lab, sep=" ")
## [1] "Box-Ljung test , Ljung-Box test For Modelling for ==> Forecasting cumulative Covid 19 Infection cases in Spain"
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 = 328.11, 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 = 4348.7, 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 Spain"
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 Spain"
paste(MAPE_Mean_All.ARIMA,"%")
## [1] "1.355 % MAPE 45 days Forecasting cumulative Covid 19 Infection cases in Spain %"
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 Spain"
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 | 3248230.00 | 3247803.88 | 0.013 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 2 | 2021-03-26 | Friday | 3254952.00 | 3253884.37 | 0.033 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 3 | 2021-03-27 | Saturday | 3262114.00 | 3259607.75 | 0.077 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 4 | 2021-03-28 | Sunday | 3269572.00 | 3264682.26 | 0.15 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 5 | 2021-03-29 | Monday | 3273596.00 | 3268008.87 | 0.171 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 6 | 2021-03-30 | Tuesday | 3276454.00 | 3271759.52 | 0.143 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 7 | 2021-03-31 | Wednesday | 3283659.00 | 3277179.31 | 0.197 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 8 | 2021-04-01 | Thursday | 3291714.00 | 3283089.05 | 0.262 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 9 | 2021-04-02 | Friday | 3300557.00 | 3288740.44 | 0.358 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 10 | 2021-04-03 | Saturday | 3306079.00 | 3294434.89 | 0.352 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 11 | 2021-04-04 | Sunday | 3309784.00 | 3299396.91 | 0.314 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 12 | 2021-04-05 | Monday | 3314226.00 | 3303437.16 | 0.326 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 13 | 2021-04-06 | Tuesday | 3318361.00 | 3307800.21 | 0.318 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 14 | 2021-04-07 | Wednesday | 3325910.00 | 3313010.17 | 0.388 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 15 | 2021-04-08 | Thursday | 3336583.00 | 3318425.61 | 0.544 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 16 | 2021-04-09 | Friday | 3347006.00 | 3323904.03 | 0.69 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 17 | 2021-04-10 | Saturday | 3357203.00 | 3329399.47 | 0.828 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 18 | 2021-04-11 | Sunday | 3367567.00 | 3334363.61 | 0.986 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 19 | 2021-04-12 | Monday | 3373134.00 | 3338835.61 | 1.017 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 20 | 2021-04-13 | Tuesday | 3376983.00 | 3343530.71 | 0.991 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 21 | 2021-04-14 | Wednesday | 3385794.00 | 3348610.14 | 1.098 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 22 | 2021-04-15 | Thursday | 3395813.00 | 3353821.64 | 1.237 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 23 | 2021-04-16 | Friday | 3406128.00 | 3359164.55 | 1.379 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 24 | 2021-04-17 | Saturday | 3415681.00 | 3364498.25 | 1.498 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 25 | 2021-04-18 | Sunday | 3425481.00 | 3369480.33 | 1.635 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 26 | 2021-04-19 | Monday | 3430708.00 | 3374217.07 | 1.647 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 27 | 2021-04-20 | Tuesday | 3434562.00 | 3379074.59 | 1.616 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 28 | 2021-04-21 | Wednesday | 3443097.00 | 3384100.40 | 1.713 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 29 | 2021-04-22 | Thursday | 3453707.00 | 3389224.81 | 1.867 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 30 | 2021-04-23 | Friday | 3463275.00 | 3394468.59 | 1.987 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 31 | 2021-04-24 | Saturday | 3472978.00 | 3399687.50 | 2.11 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 32 | 2021-04-25 | Sunday | 3482762.00 | 3404693.95 | 2.242 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 33 | 2021-04-26 | Monday | 3487645.00 | 3409579.99 | 2.238 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 34 | 2021-04-27 | Tuesday | 3491418.00 | 3414519.74 | 2.202 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 35 | 2021-04-28 | Wednesday | 3500207.00 | 3419531.40 | 2.305 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 36 | 2021-04-29 | Thursday | 3509801.00 | 3424618.78 | 2.427 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 37 | 2021-04-30 | Friday | 3518940.00 | 3429791.30 | 2.533 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 38 | 2021-05-01 | Saturday | 3527161.00 | 3434937.99 | 2.615 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 39 | 2021-05-02 | Sunday | 3535951.00 | 3439964.37 | 2.715 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 40 | 2021-05-03 | Monday | 3539729.00 | 3444930.62 | 2.678 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 41 | 2021-05-04 | Tuesday | 3542654.00 | 3449914.00 | 2.618 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 42 | 2021-05-05 | Wednesday | 3548434.00 | 3454928.91 | 2.635 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 43 | 2021-05-06 | Thursday | 3554988.00 | 3459999.22 | 2.672 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 44 | 2021-05-07 | Friday | 3559222.00 | 3465123.37 | 2.644 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
## | 45 | 2021-05-08 | Saturday | 3559222.00 | 3470227.53 | 2.5 % |
## +----+-----------------+-------------------------+-------------+------------------------+-----------------------+
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 | 3475266.99 | 3256154.71 | 3140163.67 | 3694379.28 | 3810370.31 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 2 | 2021-05-10 | Monday | 3480274.61 | 3254370.11 | 3134783.49 | 3706179.11 | 3825765.73 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 3 | 2021-05-11 | Tuesday | 3485282.90 | 3252527.49 | 3129314.22 | 3718038.30 | 3841251.57 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 4 | 2021-05-12 | Wednesday | 3490306.24 | 3250633.61 | 3123758.59 | 3729978.87 | 3856853.89 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 5 | 2021-05-13 | Thursday | 3495367.66 | 3248707.35 | 3118133.27 | 3742027.97 | 3872602.04 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 6 | 2021-05-14 | Friday | 3500460.71 | 3246739.50 | 3112427.60 | 3754181.93 | 3888493.83 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 7 | 2021-05-15 | Saturday | 3505540.85 | 3244685.59 | 3106597.16 | 3766396.10 | 3904484.54 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 8 | 2021-05-16 | Sunday | 3510587.52 | 3242532.18 | 3100632.25 | 3778642.87 | 3920542.79 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 9 | 2021-05-17 | Monday | 3515615.89 | 3240301.33 | 3094558.60 | 3790930.46 | 3936673.19 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 10 | 2021-05-18 | Tuesday | 3520639.36 | 3238006.33 | 3088389.44 | 3803272.39 | 3952889.28 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 11 | 2021-05-19 | Wednesday | 3525670.95 | 3235657.27 | 3082133.30 | 3815684.64 | 3969208.61 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 12 | 2021-05-20 | Thursday | 3530727.19 | 3233268.49 | 3075803.37 | 3828185.89 | 3985651.02 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 13 | 2021-05-21 | Friday | 3535801.23 | 3230830.97 | 3069389.47 | 3840771.49 | 4002213.00 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 14 | 2021-05-22 | Saturday | 3540868.05 | 3228320.01 | 3062867.07 | 3853416.09 | 4018869.03 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 15 | 2021-05-23 | Sunday | 3545918.00 | 3225730.21 | 3056233.03 | 3866105.80 | 4035602.98 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 16 | 2021-05-24 | Monday | 3550956.64 | 3223070.76 | 3049498.46 | 3878842.52 | 4052414.83 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 17 | 2021-05-25 | Tuesday | 3555989.74 | 3220347.73 | 3042669.58 | 3891631.74 | 4069309.89 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 18 | 2021-05-26 | Wednesday | 3561027.67 | 3217570.56 | 3035755.35 | 3904484.78 | 4086299.99 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 19 | 2021-05-27 | Thursday | 3566080.75 | 3214748.36 | 3028764.22 | 3917413.14 | 4103397.27 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 20 | 2021-05-28 | Friday | 3571143.65 | 3211874.41 | 3021688.77 | 3930412.88 | 4120598.52 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 21 | 2021-05-29 | Saturday | 3576203.07 | 3208935.86 | 3014516.34 | 3943470.29 | 4137889.81 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 22 | 2021-05-30 | Sunday | 3581254.10 | 3205930.23 | 3007245.78 | 3956577.96 | 4155262.41 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
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] "1.355 % MAPE 45 days Forecasting cumulative Covid 19 Infection cases in Spain"
## 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 Spain"
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 Spain"
paste(MAPE_Mean_EnsemblingAverage,"%")
## [1] "0.931 %"
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 Spain"
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 | 3248230.00 | 3246807.02 | 0.044 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 2 | 2021-03-26 | Friday | 3254952.00 | 3252526.13 | 0.075 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 3 | 2021-03-27 | Saturday | 3262114.00 | 3258235.26 | 0.119 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 4 | 2021-03-28 | Sunday | 3269572.00 | 3263903.71 | 0.173 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 5 | 2021-03-29 | Monday | 3273596.00 | 3269550.50 | 0.124 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 6 | 2021-03-30 | Tuesday | 3276454.00 | 3275233.29 | 0.037 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 7 | 2021-03-31 | Wednesday | 3283659.00 | 3280972.64 | 0.082 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 8 | 2021-04-01 | Thursday | 3291714.00 | 3286762.00 | 0.15 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 9 | 2021-04-02 | Friday | 3300557.00 | 3292548.33 | 0.243 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 10 | 2021-04-03 | Saturday | 3306079.00 | 3298316.61 | 0.235 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 11 | 2021-04-04 | Sunday | 3309784.00 | 3304074.88 | 0.172 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 12 | 2021-04-05 | Monday | 3314226.00 | 3309804.44 | 0.133 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 13 | 2021-04-06 | Tuesday | 3318361.00 | 3315535.59 | 0.085 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 14 | 2021-04-07 | Wednesday | 3325910.00 | 3321320.57 | 0.138 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 15 | 2021-04-08 | Thursday | 3336583.00 | 3327127.37 | 0.283 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 16 | 2021-04-09 | Friday | 3347006.00 | 3332935.88 | 0.42 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 17 | 2021-04-10 | Saturday | 3357203.00 | 3338762.10 | 0.549 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 18 | 2021-04-11 | Sunday | 3367567.00 | 3344561.05 | 0.683 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 19 | 2021-04-12 | Monday | 3373134.00 | 3350320.42 | 0.676 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 20 | 2021-04-13 | Tuesday | 3376983.00 | 3356098.58 | 0.618 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 21 | 2021-04-14 | Wednesday | 3385794.00 | 3361897.36 | 0.706 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 22 | 2021-04-15 | Thursday | 3395813.00 | 3367709.91 | 0.828 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 23 | 2021-04-16 | Friday | 3406128.00 | 3373560.57 | 0.956 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 24 | 2021-04-17 | Saturday | 3415681.00 | 3379408.36 | 1.062 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 25 | 2021-04-18 | Sunday | 3425481.00 | 3385216.94 | 1.175 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 26 | 2021-04-19 | Monday | 3430708.00 | 3391017.24 | 1.157 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 27 | 2021-04-20 | Tuesday | 3434562.00 | 3396814.80 | 1.099 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 28 | 2021-04-21 | Wednesday | 3443097.00 | 3402620.20 | 1.176 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 29 | 2021-04-22 | Thursday | 3453707.00 | 3408469.97 | 1.31 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 30 | 2021-04-23 | Friday | 3463275.00 | 3414335.19 | 1.413 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 31 | 2021-04-24 | Saturday | 3472978.00 | 3420181.31 | 1.52 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 32 | 2021-04-25 | Sunday | 3482762.00 | 3426020.35 | 1.629 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 33 | 2021-04-26 | Monday | 3487645.00 | 3431839.55 | 1.6 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 34 | 2021-04-27 | Tuesday | 3491418.00 | 3437649.33 | 1.54 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 35 | 2021-04-28 | Wednesday | 3500207.00 | 3443495.24 | 1.62 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 36 | 2021-04-29 | Thursday | 3509801.00 | 3449359.64 | 1.722 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 37 | 2021-04-30 | Friday | 3518940.00 | 3455220.08 | 1.811 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 38 | 2021-05-01 | Saturday | 3527161.00 | 3461089.71 | 1.873 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 39 | 2021-05-02 | Sunday | 3535951.00 | 3466942.23 | 1.952 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 40 | 2021-05-03 | Monday | 3539729.00 | 3472774.88 | 1.892 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 41 | 2021-05-04 | Tuesday | 3542654.00 | 3478630.15 | 1.807 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 42 | 2021-05-05 | Wednesday | 3548434.00 | 3484494.92 | 1.802 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 43 | 2021-05-06 | Thursday | 3554988.00 | 3490356.70 | 1.818 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 44 | 2021-05-07 | Friday | 3559222.00 | 3496239.44 | 1.77 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
## | 45 | 2021-05-08 | Saturday | 3559222.00 | 3502118.26 | 1.604 % |
## +----+-----------------+-------------------------+-------------+------------+-----------------+
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 | 3507981.29 |
## +----+------------+-----------------+---------------------------+
## | 2 | 2021-05-10 | Monday | 3513860.90 |
## +----+------------+-----------------+---------------------------+
## | 3 | 2021-05-11 | Tuesday | 3519739.26 |
## +----+------------+-----------------+---------------------------+
## | 4 | 2021-05-12 | Wednesday | 3525605.83 |
## +----+------------+-----------------+---------------------------+
## | 5 | 2021-05-13 | Thursday | 3531492.78 |
## +----+------------+-----------------+---------------------------+
## | 6 | 2021-05-14 | Friday | 3537384.47 |
## +----+------------+-----------------+---------------------------+
## | 7 | 2021-05-15 | Saturday | 3543271.17 |
## +----+------------+-----------------+---------------------------+
## | 8 | 2021-05-16 | Sunday | 3549179.43 |
## +----+------------+-----------------+---------------------------+
## | 9 | 2021-05-17 | Monday | 3555083.32 |
## +----+------------+-----------------+---------------------------+
## | 10 | 2021-05-18 | Tuesday | 3560966.94 |
## +----+------------+-----------------+---------------------------+
## | 11 | 2021-05-19 | Wednesday | 3566862.83 |
## +----+------------+-----------------+---------------------------+
## | 12 | 2021-05-20 | Thursday | 3572761.74 |
## +----+------------+-----------------+---------------------------+
## | 13 | 2021-05-21 | Friday | 3578661.68 |
## +----+------------+-----------------+---------------------------+
## | 14 | 2021-05-22 | Saturday | 3584591.98 |
## +----+------------+-----------------+---------------------------+
## | 15 | 2021-05-23 | Sunday | 3590523.32 |
## +----+------------+-----------------+---------------------------+
## | 16 | 2021-05-24 | Monday | 3596433.28 |
## +----+------------+-----------------+---------------------------+
## | 17 | 2021-05-25 | Tuesday | 3602348.99 |
## +----+------------+-----------------+---------------------------+
## | 18 | 2021-05-26 | Wednesday | 3608260.49 |
## +----+------------+-----------------+---------------------------+
## | 19 | 2021-05-27 | Thursday | 3614170.50 |
## +----+------------+-----------------+---------------------------+
## | 20 | 2021-05-28 | Friday | 3620114.77 |
## +----+------------+-----------------+---------------------------+
## | 21 | 2021-05-29 | Saturday | 3626067.20 |
## +----+------------+-----------------+---------------------------+
## | 22 | 2021-05-30 | Sunday | 3632003.66 |
## +----+------------+-----------------+---------------------------+
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 Spain"
best_recommended_model
## [1] 0.746
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 Spain"
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 | 3404492.95 | 3049309.50 | 2861286.73 | 3049309.50 | 2861286.73 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 2 | 2021-05-10 | Monday | 3406109.82 | 3039869.82 | 2845994.07 | 3039869.82 | 2845994.07 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 3 | 2021-05-11 | Tuesday | 3407451.01 | 3030185.26 | 2830472.84 | 3030185.26 | 2830472.84 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 4 | 2021-05-12 | Wednesday | 3408834.56 | 3020518.25 | 2814956.01 | 3020518.25 | 2814956.01 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 5 | 2021-05-13 | Thursday | 3410495.72 | 3011065.06 | 2799619.23 | 3011065.06 | 2799619.23 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 6 | 2021-05-14 | Friday | 3412427.28 | 3001824.81 | 2784464.99 | 3001824.81 | 2784464.99 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 7 | 2021-05-15 | Saturday | 3414396.73 | 2992612.12 | 2769332.84 | 2992612.12 | 2769332.84 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 8 | 2021-05-16 | Sunday | 3416120.09 | 2983198.23 | 2754023.24 | 2983198.23 | 2754023.24 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 9 | 2021-05-17 | Monday | 3417463.72 | 2973476.58 | 2738443.99 | 2973476.58 | 2738443.99 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 10 | 2021-05-18 | Tuesday | 3418531.74 | 2963536.15 | 2722676.04 | 2963536.15 | 2722676.04 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 11 | 2021-05-19 | Wednesday | 3419583.24 | 2953594.51 | 2706914.98 | 2953594.51 | 2706914.98 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 12 | 2021-05-20 | Thursday | 3420844.98 | 2943842.98 | 2691333.37 | 2943842.98 | 2691333.37 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 13 | 2021-05-21 | Friday | 3422355.25 | 2934316.43 | 2675964.29 | 2934316.43 | 2675964.29 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 14 | 2021-05-22 | Saturday | 3423946.65 | 2924878.25 | 2660687.40 | 2924878.25 | 2660687.40 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 15 | 2021-05-23 | Sunday | 3425373.43 | 2915325.80 | 2645322.90 | 2915325.80 | 2645322.90 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 16 | 2021-05-24 | Monday | 3426487.68 | 2905537.62 | 2629763.31 | 2905537.62 | 2629763.31 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 17 | 2021-05-25 | Tuesday | 3427339.06 | 2895557.58 | 2614049.47 | 2895557.58 | 2614049.47 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 18 | 2021-05-26 | Wednesday | 3428131.80 | 2885559.71 | 2598339.40 | 2885559.71 | 2598339.40 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 19 | 2021-05-27 | Thursday | 3429075.65 | 2875722.89 | 2582795.64 | 2875722.89 | 2582795.64 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 20 | 2021-05-28 | Friday | 3430239.78 | 2866107.32 | 2567473.64 | 2866107.32 | 2567473.64 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 21 | 2021-05-29 | Saturday | 3431511.67 | 2856619.32 | 2552289.69 | 2856619.32 | 2552289.69 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 22 | 2021-05-30 | Sunday | 3432684.82 | 2847085.29 | 2537087.63 | 2847085.29 | 2537087.63 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
paste("Forecasting by using TBATS Model ==> ", y_lab , sep=" ")
## [1] "Forecasting by using TBATS Model ==> Forecasting cumulative Covid 19 Infection cases in Spain"
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 | 3469405.30 | 3431800.78 | 3411894.14 | 3507009.82 | 3526916.45 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 2 | 2021-05-10 | Monday | 3474758.80 | 3436785.86 | 3416684.21 | 3512731.73 | 3532833.39 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 3 | 2021-05-11 | Tuesday | 3480169.93 | 3441836.71 | 3421544.34 | 3518503.14 | 3538795.52 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 4 | 2021-05-12 | Wednesday | 3484884.89 | 3446198.49 | 3425719.14 | 3523571.30 | 3544050.64 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 5 | 2021-05-13 | Thursday | 3489933.06 | 3450897.71 | 3430233.65 | 3528968.40 | 3549632.46 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 6 | 2021-05-14 | Friday | 3494701.82 | 3455322.00 | 3434475.58 | 3534081.64 | 3554928.06 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 7 | 2021-05-15 | Saturday | 3499079.75 | 3459358.44 | 3438331.24 | 3538801.06 | 3559828.26 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 8 | 2021-05-16 | Sunday | 3504433.25 | 3464373.81 | 3443167.62 | 3544492.69 | 3565698.88 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 9 | 2021-05-17 | Monday | 3509844.38 | 3469453.93 | 3448072.52 | 3550234.83 | 3571616.25 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 10 | 2021-05-18 | Tuesday | 3514559.34 | 3473844.06 | 3452290.70 | 3555274.62 | 3576827.99 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 11 | 2021-05-19 | Wednesday | 3519607.51 | 3478570.97 | 3456847.53 | 3560644.05 | 3582367.48 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 12 | 2021-05-20 | Thursday | 3524376.27 | 3483022.24 | 3461130.74 | 3565730.30 | 3587621.80 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 13 | 2021-05-21 | Friday | 3528754.20 | 3487085.10 | 3465026.81 | 3570423.30 | 3592481.59 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 14 | 2021-05-22 | Saturday | 3534107.70 | 3492126.32 | 3469902.72 | 3576089.09 | 3598312.69 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 15 | 2021-05-23 | Sunday | 3539518.83 | 3497231.51 | 3474845.95 | 3581806.16 | 3604191.71 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 16 | 2021-05-24 | Monday | 3544233.80 | 3501646.01 | 3479101.40 | 3586821.58 | 3609366.19 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 17 | 2021-05-25 | Tuesday | 3549281.96 | 3506396.75 | 3483694.70 | 3592167.17 | 3614869.22 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 18 | 2021-05-26 | Wednesday | 3554050.72 | 3510871.35 | 3488013.57 | 3597230.10 | 3620087.88 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 19 | 2021-05-27 | Thursday | 3558428.65 | 3514957.10 | 3491944.65 | 3601900.21 | 3624912.66 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 20 | 2021-05-28 | Friday | 3563782.16 | 3520020.78 | 3496854.90 | 3607543.54 | 3630709.41 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 21 | 2021-05-29 | Saturday | 3569193.29 | 3525147.80 | 3501831.52 | 3613238.78 | 3636555.05 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
## | 22 | 2021-05-30 | Sunday | 3573908.25 | 3529583.57 | 3506119.51 | 3618232.93 | 3641696.99 |
## +----+------------+-----------------+----------------------+------------+------------+------------+------------+
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 Spain"
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 | 3521267.48 | 2409012.28 | 1893484.97 | 4805744.22 | 5552500.97 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 2 | 2021-05-10 | Monday | 3527468.20 | 2381190.54 | 1852600.92 | 4857282.30 | 5632337.42 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 3 | 2021-05-11 | Tuesday | 3533673.51 | 2353157.30 | 1811616.75 | 4909525.09 | 5713424.86 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 4 | 2021-05-12 | Wednesday | 3539883.41 | 2324921.62 | 1770552.23 | 4962473.63 | 5795768.02 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 5 | 2021-05-13 | Thursday | 3546097.89 | 2296492.44 | 1729427.07 | 5016129.08 | 5879371.77 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 6 | 2021-05-14 | Friday | 3552316.96 | 2267878.65 | 1688260.92 | 5070492.68 | 5964241.09 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 7 | 2021-05-15 | Saturday | 3558540.62 | 2239089.02 | 1647073.41 | 5125565.77 | 6050381.10 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 8 | 2021-05-16 | Sunday | 3564768.85 | 2210132.28 | 1605884.14 | 5181349.77 | 6137797.01 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 9 | 2021-05-17 | Monday | 3571001.67 | 2181017.05 | 1564712.71 | 5237846.19 | 6226494.12 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 10 | 2021-05-18 | Tuesday | 3577239.06 | 2151751.90 | 1523578.72 | 5295056.58 | 6316477.84 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 11 | 2021-05-19 | Wednesday | 3583481.04 | 2122345.36 | 1482501.80 | 5352982.60 | 6407753.66 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 12 | 2021-05-20 | Thursday | 3589727.59 | 2092805.88 | 1441501.60 | 5411625.97 | 6500327.17 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 13 | 2021-05-21 | Friday | 3595978.72 | 2063141.85 | 1400597.82 | 5470988.44 | 6594204.01 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 14 | 2021-05-22 | Saturday | 3602234.43 | 2033361.64 | 1359810.19 | 5531071.86 | 6689389.91 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 15 | 2021-05-23 | Sunday | 3608494.71 | 2003473.55 | 1319158.56 | 5591878.11 | 6785890.66 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 16 | 2021-05-24 | Monday | 3614759.56 | 1973485.86 | 1278662.81 | 5653409.14 | 6883712.11 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 17 | 2021-05-25 | Tuesday | 3621028.98 | 1943406.80 | 1238342.96 | 5715666.93 | 6982860.17 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 18 | 2021-05-26 | Wednesday | 3627302.98 | 1913244.59 | 1198219.09 | 5778653.54 | 7083340.82 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 19 | 2021-05-27 | Thursday | 3633581.54 | 1883007.38 | 1158311.45 | 5842371.05 | 7185160.07 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 20 | 2021-05-28 | Friday | 3639864.67 | 1852703.34 | 1118640.39 | 5906821.59 | 7288323.99 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 21 | 2021-05-29 | Saturday | 3646152.36 | 1822340.58 | 1079226.43 | 5972007.32 | 7392838.68 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
## | 22 | 2021-05-30 | Sunday | 3652444.62 | 1791927.23 | 1040090.26 | 6037930.45 | 7498710.30 |
## +----+------------+-----------------+---------------------+------------+------------+------------+------------+
paste("Forecasting by using ARIMA Model ==> ", y_lab , sep=" ")
## [1] "Forecasting by using ARIMA Model ==> Forecasting cumulative Covid 19 Infection cases in Spain"
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 | 3475266.99 | 3256154.71 | 3140163.67 | 3694379.28 | 3810370.31 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 2 | 2021-05-10 | Monday | 3480274.61 | 3254370.11 | 3134783.49 | 3706179.11 | 3825765.73 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 3 | 2021-05-11 | Tuesday | 3485282.90 | 3252527.49 | 3129314.22 | 3718038.30 | 3841251.57 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 4 | 2021-05-12 | Wednesday | 3490306.24 | 3250633.61 | 3123758.59 | 3729978.87 | 3856853.89 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 5 | 2021-05-13 | Thursday | 3495367.66 | 3248707.35 | 3118133.27 | 3742027.97 | 3872602.04 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 6 | 2021-05-14 | Friday | 3500460.71 | 3246739.50 | 3112427.60 | 3754181.93 | 3888493.83 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 7 | 2021-05-15 | Saturday | 3505540.85 | 3244685.59 | 3106597.16 | 3766396.10 | 3904484.54 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 8 | 2021-05-16 | Sunday | 3510587.52 | 3242532.18 | 3100632.25 | 3778642.87 | 3920542.79 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 9 | 2021-05-17 | Monday | 3515615.89 | 3240301.33 | 3094558.60 | 3790930.46 | 3936673.19 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 10 | 2021-05-18 | Tuesday | 3520639.36 | 3238006.33 | 3088389.44 | 3803272.39 | 3952889.28 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 11 | 2021-05-19 | Wednesday | 3525670.95 | 3235657.27 | 3082133.30 | 3815684.64 | 3969208.61 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 12 | 2021-05-20 | Thursday | 3530727.19 | 3233268.49 | 3075803.37 | 3828185.89 | 3985651.02 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 13 | 2021-05-21 | Friday | 3535801.23 | 3230830.97 | 3069389.47 | 3840771.49 | 4002213.00 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 14 | 2021-05-22 | Saturday | 3540868.05 | 3228320.01 | 3062867.07 | 3853416.09 | 4018869.03 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 15 | 2021-05-23 | Sunday | 3545918.00 | 3225730.21 | 3056233.03 | 3866105.80 | 4035602.98 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 16 | 2021-05-24 | Monday | 3550956.64 | 3223070.76 | 3049498.46 | 3878842.52 | 4052414.83 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 17 | 2021-05-25 | Tuesday | 3555989.74 | 3220347.73 | 3042669.58 | 3891631.74 | 4069309.89 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 18 | 2021-05-26 | Wednesday | 3561027.67 | 3217570.56 | 3035755.35 | 3904484.78 | 4086299.99 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 19 | 2021-05-27 | Thursday | 3566080.75 | 3214748.36 | 3028764.22 | 3917413.14 | 4103397.27 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 20 | 2021-05-28 | Friday | 3571143.65 | 3211874.41 | 3021688.77 | 3930412.88 | 4120598.52 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 21 | 2021-05-29 | Saturday | 3576203.07 | 3208935.86 | 3014516.34 | 3943470.29 | 4137889.81 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
## | 22 | 2021-05-30 | Sunday | 3581254.10 | 3205930.23 | 3007245.78 | 3956577.96 | 4155262.41 |
## +----+------------+-----------------+---------------------------+------------+------------+------------+------------+
paste("Forecasting by using NNAR Model ==> ", y_lab , sep=" ")
## [1] "Forecasting by using NNAR Model ==> Forecasting cumulative Covid 19 Infection cases in Spain"
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 | 3204457.14 |
## +----+------------+-----------------+---------------------+
## | 2 | 2021-05-10 | Monday | 3204437.60 |
## +----+------------+-----------------+---------------------+
## | 3 | 2021-05-11 | Tuesday | 3204420.17 |
## +----+------------+-----------------+---------------------+
## | 4 | 2021-05-12 | Wednesday | 3204404.64 |
## +----+------------+-----------------+---------------------+
## | 5 | 2021-05-13 | Thursday | 3204390.78 |
## +----+------------+-----------------+---------------------+
## | 6 | 2021-05-14 | Friday | 3204378.43 |
## +----+------------+-----------------+---------------------+
## | 7 | 2021-05-15 | Saturday | 3204367.41 |
## +----+------------+-----------------+---------------------+
## | 8 | 2021-05-16 | Sunday | 3204357.58 |
## +----+------------+-----------------+---------------------+
## | 9 | 2021-05-17 | Monday | 3204348.82 |
## +----+------------+-----------------+---------------------+
## | 10 | 2021-05-18 | Tuesday | 3204341.01 |
## +----+------------+-----------------+---------------------+
## | 11 | 2021-05-19 | Wednesday | 3204334.04 |
## +----+------------+-----------------+---------------------+
## | 12 | 2021-05-20 | Thursday | 3204327.83 |
## +----+------------+-----------------+---------------------+
## | 13 | 2021-05-21 | Friday | 3204322.29 |
## +----+------------+-----------------+---------------------+
## | 14 | 2021-05-22 | Saturday | 3204317.35 |
## +----+------------+-----------------+---------------------+
## | 15 | 2021-05-23 | Sunday | 3204312.95 |
## +----+------------+-----------------+---------------------+
## | 16 | 2021-05-24 | Monday | 3204309.02 |
## +----+------------+-----------------+---------------------+
## | 17 | 2021-05-25 | Tuesday | 3204305.51 |
## +----+------------+-----------------+---------------------+
## | 18 | 2021-05-26 | Wednesday | 3204302.39 |
## +----+------------+-----------------+---------------------+
## | 19 | 2021-05-27 | Thursday | 3204299.60 |
## +----+------------+-----------------+---------------------+
## | 20 | 2021-05-28 | Friday | 3204297.12 |
## +----+------------+-----------------+---------------------+
## | 21 | 2021-05-29 | Saturday | 3204294.90 |
## +----+------------+-----------------+---------------------+
## | 22 | 2021-05-30 | Sunday | 3204292.93 |
## +----+------------+-----------------+---------------------+
paste("Forecasting by using Ensembling Model ==> ", y_lab , sep=" ")
## [1] "Forecasting by using Ensembling Model ==> Forecasting cumulative Covid 19 Infection cases in Spain"
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 | 3507981.29 |
## +----+------------+-----------------+------------------------+
## | 2 | 2021-05-10 | Monday | 3513860.90 |
## +----+------------+-----------------+------------------------+
## | 3 | 2021-05-11 | Tuesday | 3519739.26 |
## +----+------------+-----------------+------------------------+
## | 4 | 2021-05-12 | Wednesday | 3525605.83 |
## +----+------------+-----------------+------------------------+
## | 5 | 2021-05-13 | Thursday | 3531492.78 |
## +----+------------+-----------------+------------------------+
## | 6 | 2021-05-14 | Friday | 3537384.47 |
## +----+------------+-----------------+------------------------+
## | 7 | 2021-05-15 | Saturday | 3543271.17 |
## +----+------------+-----------------+------------------------+
## | 8 | 2021-05-16 | Sunday | 3549179.43 |
## +----+------------+-----------------+------------------------+
## | 9 | 2021-05-17 | Monday | 3555083.32 |
## +----+------------+-----------------+------------------------+
## | 10 | 2021-05-18 | Tuesday | 3560966.94 |
## +----+------------+-----------------+------------------------+
## | 11 | 2021-05-19 | Wednesday | 3566862.83 |
## +----+------------+-----------------+------------------------+
## | 12 | 2021-05-20 | Thursday | 3572761.74 |
## +----+------------+-----------------+------------------------+
## | 13 | 2021-05-21 | Friday | 3578661.68 |
## +----+------------+-----------------+------------------------+
## | 14 | 2021-05-22 | Saturday | 3584591.98 |
## +----+------------+-----------------+------------------------+
## | 15 | 2021-05-23 | Sunday | 3590523.32 |
## +----+------------+-----------------+------------------------+
## | 16 | 2021-05-24 | Monday | 3596433.28 |
## +----+------------+-----------------+------------------------+
## | 17 | 2021-05-25 | Tuesday | 3602348.99 |
## +----+------------+-----------------+------------------------+
## | 18 | 2021-05-26 | Wednesday | 3608260.49 |
## +----+------------+-----------------+------------------------+
## | 19 | 2021-05-27 | Thursday | 3614170.50 |
## +----+------------+-----------------+------------------------+
## | 20 | 2021-05-28 | Friday | 3620114.77 |
## +----+------------+-----------------+------------------------+
## | 21 | 2021-05-29 | Saturday | 3626067.20 |
## +----+------------+-----------------+------------------------+
## | 22 | 2021-05-30 | Sunday | 3632003.66 |
## +----+------------+-----------------+------------------------+
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 | Spain | 5.651 | 2.007 | 1.439 | 0.746 | 1.355 | 0.931 | 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 Spain
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 Spain