-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTS_Exponential_Smoothing_Models.R
194 lines (127 loc) · 5.64 KB
/
TS_Exponential_Smoothing_Models.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#------------------------------------#
# #
# Exponential Smoothing #
# Models #
# #
# #
#------------------------------------#
getwd()
setwd("C:/Users/sanya/Desktop/Time Series/R_Time-Series/datasets")
train = read.csv(file='train_data.csv', header = TRUE)
test = read.csv(file='test_data.csv', header = TRUE)
# Needed Libraries for Analysis #
library(haven)
library(forecast)
library(fma)
library(tseries)
library(expsmooth)
library(lmtest)
library(zoo)
# Creation of Time Series Data Object #
# ts(y-variable, start, frequency)
# Frequency- the level of granularity, monthly-12, quarterly-4...
# the data must be sorted on time variable
Temp.ts <- ts(train$TEMP, start = 1, frequency = 24)
plot(Temp.ts)
Test.ts <- ts(test$TEMP, start = 27, frequency = 24)
plot(Test.ts)
# Time Series Decomposition #
# you must have the frequency specified in the above code if you want the decomposed model to give you seasons
# s.window - to specify the span of seasons
# stl stands for Seasonal Decomposition of Time Series by Loess
model <- stl(Temp.ts, s.window = 5)
plot(model)
model
# plotting the time series data Temp.ts
# lwd is line width
plot(Temp.ts, col = "grey", main = "TEMPERATURE", xlab = "", ylab = "Hourly Temp", lwd = 3)
# fitting a trend line on the times series plot
lines(model$time.series[,2], col = "red", lwd = 2)
# calculating seasonally adjusted data, remove seasonality from data
seasonal_comp = Temp.ts-model$time.series[,1]
# plotting the time series data passenger
plot(Temp.ts, col = "grey", main = "US Airline Passengers - Seasonally Adjusted", xlab = "", ylab = "Number of Passengers (Thousands)", lwd = 2)
# plotting the seasonally adjusted values on the time series plot
lines(seasonal_comp, col = "red", lwd = 2)
# Building a Single Exponential Smoothing Model - Steel Data #
Temp.ses = ses(Temp.ts, initial = "optimal", h= 24)
summary(Temp.ses)
plot(Temp.ses, main = "Temp forecast with Simple ESM Forecast", xlab = "Date", ylab = "Temp-hourly")
abline(v = 26, col = "red", lty = "dashed")
# Ljung-Box Test for Steel ES Model #
SES.acf = acf(Temp.HWES$residuals, lag.max = 20)
SES.acf = acf(Temp.LDES$residuals, lag.max = 20)
SES.acf = acf(Temp.LES$residuals, lag.max = 20)
SES.acf = acf(Temp.ses$residuals, lag.max = 20)
Box.test(Temp.HWES$residuals, lag = 20, type = "Ljung")
Box.test(Temp.LDES$residuals, lag = 20, type = "Ljung")
Box.test(Temp.LES$residuals, lag = 20, type = "Ljung")
Box.test(Temp.ses$residuals, lag = 20, type = "Ljung")
plot.ts(Temp.HWES$residuals)
plot.ts(Temp.LDES$residuals)
plot.ts(Temp.LES$residuals)
plot.ts(Temp.ses$residuals)
White.LB <- rep(NA, 10)
for(i in 1:10){
White.LB[i] <- Box.test(Temp.HWES$residuals, lag = i, type = "Ljung", fitdf = 1)$p.value
}
White.LB
White.LB <- pmin(White.LB, 0.2)
barplot(White.LB, main = "Ljung-Box Test P-values", ylab = "Probabilities", xlab = "Lags", ylim = c(0, 0.2))
abline(h = 0.01, lty = "dashed", col = "black")
abline(h = 0.05, lty = "dashed", col = "black")
White.LB <- rep(NA, 10)
for(i in 1:10){
White.LB[i] <- Box.test(Temp.LDES$residuals, lag = i, type = "Ljung", fitdf = 1)$p.value
}
White.LB
White.LB <- pmin(White.LB, 0.2)
barplot(White.LB, main = "Ljung-Box Test P-values", ylab = "Probabilities", xlab = "Lags", ylim = c(0, 0.2))
abline(h = 0.01, lty = "dashed", col = "black")
abline(h = 0.05, lty = "dashed", col = "black")
White.LB <- rep(NA, 10)
for(i in 1:10){
White.LB[i] <- Box.test(Temp.LES$residuals, lag = i, type = "Ljung", fitdf = 1)$p.value
}
White.LB
White.LB <- pmin(White.LB, 0.2)
barplot(White.LB, main = "Ljung-Box Test P-values", ylab = "Probabilities", xlab = "Lags", ylim = c(0, 0.2))
abline(h = 0.01, lty = "dashed", col = "black")
abline(h = 0.05, lty = "dashed", col = "black")
White.LB <- rep(NA, 10)
for(i in 1:10){
White.LB[i] <- Box.test(Temp.ses$residuals, lag = i, type = "Ljung", fitdf = 1)$p.value
}
White.LB
White.LB <- pmin(White.LB, 0.2)
barplot(White.LB, main = "Ljung-Box Test P-values", ylab = "Probabilities", xlab = "Lags", ylim = c(0, 0.2))
abline(h = 0.01, lty = "dashed", col = "black")
abline(h = 0.05, lty = "dashed", col = "black")
# Building a Linear Exponential Smoothing Model - Steel Data #
Temp.LES <- holt(Temp.ts, initial = "optimal", h = 24)
summary(Temp.LES)
plot(Temp.LES, main = "Temp- Linear Exponential Smoothing", xlab = "Date", ylab = "Temp")
abline(v = 1992, col = "red", lty = "dashed")
# damped trend model
Temp.LDES <- holt(Temp.ts, initial = "optimal", h = 24, damped = TRUE)
summary(Temp.LDES)
plot(Temp.LDES, main = "Temp with Linear Damped ESM Forecast", xlab = "Date", ylab = " Temp- hourly")
abline(v = 26, col = "red", lty = "dashed")
# Building a Holt-Winters ESM #
Temp.HWES<- hw(Temp.ts, seasonal = "additive")
summary(Temp.HWES)
plot(Temp.HWES, main = "Temp with Holt-Winters ESM Forecast", xlab = "Date", ylab = "Temp-hourly")
abline(v = 26, col = "red", lty = "dashed")
# Building a Holt-Winters ESM- multiplicative #
Temp.HWES.mul<- hw(Temp.ts, seasonal = "multiplicative")
summary(Temp.HWES.mul)
plot(Temp.HWES.mul, main = "Temp with Holt-Winters ESM Forecast", xlab = "Date", ylab = "Temp-hourly")
abline(v = 26, col = "red", lty = "dashed")
#forecasted values
Temp.HWES$fitted
#Checking the Accuracy statistics
accuracy(Temp.HWES, Test.ts)
accuracy(Temp.LDES, Test.ts)
accuracy(Temp.LES, Test.ts)
accuracy(Temp.ses, Test.ts)
accuracy(Temp.HWES.mul, Test.ts)