Assignment instruction: Repeat the ts(), HoltWinters(), predict() and plot() functions on the Nile data as in these slides Repeat the same functions with different values of alpha, beta, and gamma of your choosing on AirPassengers or EuStockMarkets. When using the EuStockMarkets, choose one column.

Solution: Nile Data

Create a data set for time series analysis and a Holtwinters object

Nile
## Time Series:
## Start = 1871 
## End = 1970 
## Frequency = 1 
##   [1] 1120 1160  963 1210 1160 1160  813 1230 1370 1140  995  935 1110  994
##  [15] 1020  960 1180  799  958 1140 1100 1210 1150 1250 1260 1220 1030 1100
##  [29]  774  840  874  694  940  833  701  916  692 1020 1050  969  831  726
##  [43]  456  824  702 1120 1100  832  764  821  768  845  864  862  698  845
##  [57]  744  796 1040  759  781  865  845  944  984  897  822 1010  771  676
##  [71]  649  846  812  742  801 1040  860  874  848  890  744  749  838 1050
##  [85]  918  986  797  923  975  815 1020  906  901 1170  912  746  919  718
##  [99]  714  740
plot(Nile)

frequency(Nile)
## [1] 1
nile_timeseries <- ts(Nile, start=1, frequency=2) 
summary(nile_timeseries)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   456.0   798.5   893.5   919.4  1032.0  1370.0
plot(decompose(nile_timeseries))

nile_hw <- HoltWinters(nile_timeseries, gamma=F, seasonal = "additive") 
nile_hw
## Holt-Winters exponential smoothing with trend and without seasonal component.
## 
## Call:
## HoltWinters(x = nile_timeseries, gamma = F, seasonal = "additive")
## 
## Smoothing parameters:
##  alpha: 0.4190643
##  beta : 0.05987705
##  gamma: FALSE
## 
## Coefficients:
##         [,1]
## a 756.913740
## b  -7.424597
plot(nile_timeseries)

plot(nile_hw)

Predict future values of the next 10 periods using the Holt-Winters function and plot the prediction

nile_predict <- predict(nile_hw, n.ahead=10)
ts.plot(nile_timeseries, nile_predict) 

Air Passengers Data

Explore the data set

AirPassengers
##      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1949 112 118 132 129 121 135 148 148 136 119 104 118
## 1950 115 126 141 135 125 149 170 170 158 133 114 140
## 1951 145 150 178 163 172 178 199 199 184 162 146 166
## 1952 171 180 193 181 183 218 230 242 209 191 172 194
## 1953 196 196 236 235 229 243 264 272 237 211 180 201
## 1954 204 188 235 227 234 264 302 293 259 229 203 229
## 1955 242 233 267 269 270 315 364 347 312 274 237 278
## 1956 284 277 317 313 318 374 413 405 355 306 271 306
## 1957 315 301 356 348 355 422 465 467 404 347 305 336
## 1958 340 318 362 348 363 435 491 505 404 359 310 337
## 1959 360 342 406 396 420 472 548 559 463 407 362 405
## 1960 417 391 419 461 472 535 622 606 508 461 390 432
start(AirPassengers)
## [1] 1949    1
end(AirPassengers)
## [1] 1960   12
frequency(AirPassengers)
## [1] 12
summary(AirPassengers) 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   104.0   180.0   265.5   280.3   360.5   622.0
class(AirPassengers) 
## [1] "ts"
mode(AirPassengers)
## [1] "numeric"
plot(AirPassengers)

Create a data set for time series analysis and a Holtwinters object

AirP_timeseries <- ts(AirPassengers, start=c(1949,1), frequency=12)
summary(AirP_timeseries)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   104.0   180.0   265.5   280.3   360.5   622.0
plot(decompose(AirP_timeseries))

AirP_hw <- HoltWinters(AirP_timeseries,seasonal="mult") 
AirP_hw
## Holt-Winters exponential smoothing with trend and multiplicative seasonal component.
## 
## Call:
## HoltWinters(x = AirP_timeseries, seasonal = "mult")
## 
## Smoothing parameters:
##  alpha: 0.2755925
##  beta : 0.03269295
##  gamma: 0.8707292
## 
## Coefficients:
##            [,1]
## a   469.3232206
## b     3.0215391
## s1    0.9464611
## s2    0.8829239
## s3    0.9717369
## s4    1.0304825
## s5    1.0476884
## s6    1.1805272
## s7    1.3590778
## s8    1.3331706
## s9    1.1083381
## s10   0.9868813
## s11   0.8361333
## s12   0.9209877
AirP_hw$SSE
## [1] 16570.78

See the results of the Holt-Winters function graphically

plot(AirP_timeseries)

plot(AirP_hw)

AirP_predict <- predict(AirP_hw,n.ahead = 12)
plot(AirP_hw,AirP_predict)

ts.plot(AirP_timeseries, AirP_predict)