-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpet-forecast-arima.R
106 lines (83 loc) · 3.24 KB
/
pet-forecast-arima.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#Import Necessary Library
library(lubridate)
library(ggplot2)
library(tidyverse)
library(dplyr)
library(astsa)
library(forecast)
library(readxl)
library(urca)
library(ggfortify)
library(tsutils)
library(writexl)
#Converting To Time Series
bwn_ts <- ts(data = bwn[,2], frequency = 12, start = c(1993,1))
#Selecting Data
bwn_ts <- window(bwn_ts, start=c(1993,1))
bwn_ts
#Plot Time Series Data
autoplot(bwn_ts) + ylab("PET (mm/month)") + xlab("Datetime") +
scale_x_date(date_labels = '%b - %Y', breaks = '4 year', minor_breaks = '2 month') +
theme_bw() + ggtitle("Bahawalnagar PET 1993 - 2020")
#Decomposition using stl()
decomp <- stl(bwn_ts[,1], s.window = 'periodic')
#Plot decomposition
autoplot(decomp) + theme_bw() + scale_x_date(date_labels = '%b - %Y', breaks = '4 year', minor_breaks = '2 month') +
ggtitle("Remainder")
Tt <- trendcycle(decomp)
St <- seasonal(decomp)
Rt <- remainder(decomp)
#Trend Strength Calculation
Ft <- round(max(0,1 - (var(Rt)/var(Tt + Rt))),1)
#Seasonal Strength Calculation
Fs <- round(max(0,1 - (var(Rt)/var(St + Rt))),1)
data.frame('Trend Strength' = Ft , 'Seasonal Strength' = Fs)
#Seasonal Plot
seasonplot(bwn_ts, year.labels = TRUE, col = 1:13,
main = "Bahawalnagar Seasonal Plot", ylab= "PET (mm/month)")
#Seasonal Sub-Series Plot
seasplot(bwn_ts, outplot = 3, trend = FALSE,
main = "Seasonal Subseries Plot", ylab= "PET (mm/month)")
#Seasonal Boxplot
seasplot(bwn_ts, outplot = 2, trend = FALSE,
main = "Seasonal Box Plot", ylab= "PET (mm/month)")
#Create Train Set
bwn_train <- window(bwn_ts, end = c(2017,12))
#Create Test Set
bwn_test <- window(bwn_ts, start = c(2018,1))
#Kwiatkowski–Phillips–Schmidt–Shin Test
summary(ur.kpss(bwn_train))
#Dickey-Fuller Test
summary(ur.df(bwn_train))
#ACF/PACF Plot
acf2(bwn_train)
fit1 <- Arima(bwn_train, order = c(1,1,0), seasonal = c(1,1,0))
fit2 <- Arima(bwn_train, order = c(1,1,2), seasonal = c(1,1,2))
fit3 <- Arima(bwn_train, order = c(0,0,2), seasonal = c(0,0,2))
fit4 <- Arima(bwn_train, order = c(2,0,1), seasonal = c(2,0,1))
fit5 <- Arima(bwn_train, order = c(1,2,1), seasonal = c(1,2,1))
fit6 <- auto.arima(bwn_train, stepwise = FALSE,
approximation = FALSE)
data.frame('Model-1' = fit1$aicc, 'Model-2' = fit2$aicc,
'Model-3' = fit3$aicc,
'Model-4' = fit4$aicc,
'Model-5' = fit5$aicc,'Auto.Arima'= fit6$aicc,
row.names = "AICc Value")
checkresiduals(fit2)
#ARIMA Model Accuracy
accuracy(forecast(fit2, h=240), bwn_test)
#Create Model
ARIMA_Model <- Arima(bwn_ts, order = c(1,1,2),
seasonal = c(1,1,2))
#ARIMA Model Forecast
autoplot(forecast(ARIMA_Model, h=252)) + theme_bw() +
ylab("PET (mm/month)") + xlab("Datetime") +
scale_x_date(date_labels = '%b - %Y',
breaks = '4 year', minor_breaks = '2 month') +
theme_bw() + ggtitle("Bahawalnagar PET ARIMA Forecast 2021-2041")
# ARIMA Model Forecast
ARIMAforecast_data <- forecast(ARIMA_Model, h = 252)
# Convert forecast data to data frame
forecast_df <- as.data.frame(ARIMAforecast_data)
# Save forecast data to Excel
write_xlsx(list(forecast_df = forecast_df), "ARIMAforecast_data.xlsx")