library(readr)
library(ggplot2)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(GGally)
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
library(fpp2)
## ── Attaching packages ─────────────────────────────────────────────────────── fpp2 2.4 ──
## ✓ fma 2.4 ✓ expsmooth 2.3
##
library(seasonal)
library(psych)
##
## Attaching package: 'psych'
## The following object is masked from 'package:seasonal':
##
## outlier
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
#install.packages("kableExtra")
library(kableExtra)
##
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
##
## group_rows
7.1 Consider the pigs series - the number of pigs slaughtered in Victoria each month. a. Use the ses function in R to find the optimal values of alpha and l0, and generate forecasts for the next four
pigs
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct
## 1980 76378 71947 33873 96428 105084 95741 110647 100331 94133 103055
## 1981 76889 81291 91643 96228 102736 100264 103491 97027 95240 91680
## 1982 76892 85773 95210 93771 98202 97906 100306 94089 102680 77919
## 1983 81225 88357 106175 91922 104114 109959 97880 105386 96479 97580
## 1984 90974 98981 107188 94177 115097 113696 114532 120110 93607 110925
## 1985 103069 103351 111331 106161 111590 99447 101987 85333 86970 100561
## 1986 82719 79498 74846 73819 77029 78446 86978 75878 69571 75722
## 1987 63292 59380 78332 72381 55971 69750 85472 70133 79125 85805
## 1988 69069 79556 88174 66698 72258 73445 76131 86082 75443 73969
## 1989 66269 73776 80034 70694 81823 75640 75540 82229 75345 77034
## 1990 75982 78074 77588 84100 97966 89051 93503 84747 74531 91900
## 1991 81022 78265 77271 85043 95418 79568 103283 95770 91297 101244
## 1992 93866 95171 100183 103926 102643 108387 97077 90901 90336 88732
## 1993 73292 78943 94399 92937 90130 91055 106062 103560 104075 101783
## 1994 82413 83534 109011 96499 102430 103002 91815 99067 110067 101599
## 1995 88905 89936 106723 84307 114896 106749 87892 100506
## Nov Dec
## 1980 90595 101457
## 1981 101259 109564
## 1982 93561 117062
## 1983 109490 110191
## 1984 103312 120184
## 1985 89543 89265
## 1986 64182 77357
## 1987 81778 86852
## 1988 78139 78646
## 1989 78589 79769
## 1990 81635 89797
## 1991 114525 101139
## 1992 83759 99267
## 1993 93791 102313
## 1994 97646 104930
## 1995
str(pigs)
## Time-Series [1:188] from 1980 to 1996: 76378 71947 33873 96428 105084 ...
pigs.ses = ses(pigs,h=4)
pigs.ses$model
## Simple exponential smoothing
##
## Call:
## ses(y = pigs, h = 4)
##
## Smoothing parameters:
## alpha = 0.2971
##
## Initial states:
## l = 77260.0561
##
## sigma: 10308.58
##
## AIC AICc BIC
## 4462.955 4463.086 4472.665
pigs.ses$upper[1, "95%"]
## 95%
## 119020.8
pigs.ses$lower[1, "95%"]
## 95%
## 78611.97
sd = sd(pigs.ses$residuals)
pigs.ses$mean[1] + 1.96*sd
## [1] 118952.8
pigs.ses$mean[1] - 1.96*sd
## [1] 78679.97
autoplot(pigs.ses) + autolayer(pigs.ses$fitted)
2.Write your own function to implement simple exponential smoothing. The function should take arguments y (the time series), alpha (the smoothing parameter α) and level (the initial level ℓ0). It should return the forecast of the next observation in the series. Does it give the same forecast as ses()?
pigsfc = ses(pigs, h=4, alpha=0.1, initial="simple")
pigsfc
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Sep 1995 98389.03 84025.49 112752.6 76421.90 120356.2
## Oct 1995 98389.03 83953.85 112824.2 76312.34 120465.7
## Nov 1995 98389.03 83882.57 112895.5 76203.31 120574.7
## Dec 1995 98389.03 83811.63 112966.4 76094.83 120683.2
forecast(pigsfc, h=4)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Sep 1995 98389.03 84025.49 112752.6 76421.90 120356.2
## Oct 1995 98389.03 83953.85 112824.2 76312.34 120465.7
## Nov 1995 98389.03 83882.57 112895.5 76203.31 120574.7
## Dec 1995 98389.03 83811.63 112966.4 76094.83 120683.2
The forecasts is abit different as shown here
SES = function(pars = c(alpha, l0), y){
error = 0
SSE = 0
alpha = pars[1]
l0 = pars[2]
y.hat <- l0
for(index in 1:length(y)){
error = y[index] - y.hat
SSE = SSE + error^2
y.hat = alpha*y[index] + (1 - alpha)*y.hat
}
return(SSE)
}
# compare ses and SES by applyting pig data
SES_pigs = optim(par = c(0.5, pigs[1]), y = pigs, fn = SES)
writeLines(paste(
"Optimal parameters of SES function: ",
"\n",
as.character(SES_pigs$par[1]),
", ",
as.character(SES_pigs$par[2]),
sep = ""
))
## Optimal parameters of SES function:
## 0.299008094014243, 76379.2653476235
writeLines(paste(
"Parameters from ses function: ",
"\n",
as.character(pigs.ses$model$par[1]),
", ",
as.character(pigs.ses$model$par[2]),
sep = ""
))
## Parameters from ses function:
## 0.297148833372095, 77260.0561458528
we see that alpha is almost the same from both ses or SES function
5.Data set books contains the daily sales of paperback and hardcover books at the same store. The task is to forecast the next four days’ sales for paperback and hardcover books a.Plot the series and discuss the main features of the data. b.Use the ses() function to forecast each series, and plot the forecasts. c.Compute the RMSE values for the training data in each case.
str(books)
## Time-Series [1:30, 1:2] from 1 to 30: 199 172 111 209 161 119 195 195 131 183 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:2] "Paperback" "Hardcover"
head(books)
## Time Series:
## Start = 1
## End = 6
## Frequency = 1
## Paperback Hardcover
## 1 199 139
## 2 172 128
## 3 111 172
## 4 209 139
## 5 161 191
## 6 119 168
autoplot(books)
paperback.ses = ses(books[, "Paperback"], h = 4)
hardcover.ses = ses(books[, "Hardcover"], h = 4)
autoplot(books[, "Paperback"], series = "Paperback") +
autolayer(paperback.ses, series = "Paperback") +
autolayer(books[, "Hardcover"], series = "Hardcover") +
autolayer(hardcover.ses, series = "Hardcover", PI = FALSE)
#RMASE values
sqrt(mean(paperback.ses$residuals^2))
## [1] 33.63769
sqrt(mean(hardcover.ses$residuals^2))
## [1] 31.93101
6.We will continue with the daily sales of paperback and hardcover books in data set books.
a.Apply Holt’s linear method to the paperback and hardback series and compute four-day forecasts in each case. b.Compare the RMSE measures of Holt’s method for the two series to those of simple exponential smoothing in the previous question. (Remember that Holt’s method is using one more parameter than SES.) Discuss the merits of the two forecasting methods for these data sets. c.Compare the forecasts for the two series using both methods. Which do you think is best? d.Calculate a 95% prediction interval for the first forecast for each series, using the RMSE values and assuming normal errors. Compare your intervals with those produced using ses and holt.
paperback.holt = holt(books[, "Paperback"], h = 4)
hardcover.holt = holt(books[, "Hardcover"], h = 4)
autoplot(books[, "Paperback"]) +
autolayer(paperback.holt)
autoplot(books[, "Hardcover"]) +
autolayer(hardcover.holt)
Linear trend could be observed fromt the forecasts
paperback.rmse = sqrt(mean(paperback.holt$residuals^2))
hardcover.rmse = sqrt(mean(hardcover.holt$residuals^2))
paperback.rmse
## [1] 31.13692
hardcover.rmse
## [1] 27.19358
Lower RMSE with Holt, and lower RMSE for hardcover meaning that the forcass for hardcover sales is slightly better.
writeLines("95% prediction interval of paperback sales by Holt's")
## 95% prediction interval of paperback sales by Holt's
paperback.holt$upper[1, "95%"]
## 95%
## 275.0205
paperback.holt$lower[1, "95%"]
## 95%
## 143.913
writeLines("95% prediction intervaof paperback sales by formula")
## 95% prediction intervaof paperback sales by formula
paperback.holt$mean[1] + 1.96*paperback.rmse
## [1] 270.4951
paperback.holt$mean[1] - 1.96*paperback.rmse
## [1] 148.4384
writeLines("95% prediction interva of hardcover sales by Holt's")
## 95% prediction interva of hardcover sales by Holt's
hardcover.holt$upper[1, "95%"]
## 95%
## 307.4256
hardcover.holt$lower[1, "95%"]
## 95%
## 192.9222
writeLines("95% prediction interva of hardcover sales by formula")
## 95% prediction interva of hardcover sales by formula
hardcover.holt$mean[1] + 1.96*hardcover.rmse
## [1] 303.4733
hardcover.holt$mean[1] - 1.96*hardcover.rmse
## [1] 196.8745
7.For this exercise use data set eggs, the price of a dozen eggs in the United States from 1900–1993. Experiment with the various options in the holt() function to see how much the forecasts change with damped trend, or with a Box-Cox transformation. Try to develop an intuition of what each argument is doing to the forecasts. [Hint: use h=100 when calling holt() so you can clearly see the differences between the various options when plotting the forecasts.] Which model gives the best RMSE?
egg.holt = holt(eggs, h=100)
egg.holt
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 1994 59.78553313 24.970286 94.60078 6.540207 113.0309
## 1995 57.06372643 12.206005 101.92145 -11.540238 125.6677
## 1996 54.34191973 1.308668 107.37517 -26.765440 135.4493
## 1997 51.62011302 -8.488401 111.72863 -40.307926 143.5482
## 1998 48.89830632 -17.537664 115.33428 -52.706742 150.5034
## 1999 46.17649962 -26.035964 118.38896 -64.262933 156.6159
## 2000 43.45469292 -34.106500 121.01589 -75.164916 162.0743
## 2001 40.73288622 -41.832449 123.29822 -85.539898 167.0057
## 2002 38.01107951 -49.273098 125.29526 -95.478551 171.5007
## 2003 35.28927281 -56.472472 127.05102 -105.048206 175.6268
## 2004 32.56746611 -63.464327 128.59926 -114.300487 179.4354
## 2005 29.84565941 -70.275216 129.96654 -123.276007 182.9673
## 2006 27.12385271 -76.926479 131.17418 -132.007398 186.2551
## 2007 24.40204600 -83.435566 132.23966 -140.521350 189.3254
## 2008 21.68023930 -89.816966 133.17744 -148.840022 192.2005
## 2009 18.95843260 -96.082866 133.99973 -156.982051 194.8989
## 2010 16.23662590 -102.243631 134.71688 -164.963291 197.4365
## 2011 13.51481920 -108.308165 135.33780 -172.797358 199.8270
## 2012 10.79301249 -114.284184 135.87021 -180.496053 202.0821
## 2013 8.07120579 -120.178426 136.32084 -188.069681 204.2121
## 2014 5.34939909 -125.996817 136.69562 -195.527304 206.2261
## 2015 2.62759239 -131.744601 136.99979 -202.876944 208.1321
## 2016 -0.09421431 -137.426446 137.23802 -210.125737 209.9373
## 2017 -2.81602102 -143.046526 137.41448 -217.280071 211.6480
## 2018 -5.53782772 -148.608596 137.53294 -224.345686 213.2700
## 2019 -8.25963442 -154.116046 137.59678 -231.327766 214.8085
## 2020 -10.98144112 -159.571946 137.60906 -238.231008 216.2681
## 2021 -13.70324782 -164.979092 137.57260 -245.059687 217.6532
## 2022 -16.42505453 -170.340036 137.48993 -251.817706 218.9676
## 2023 -19.14686123 -175.657116 137.36339 -258.508640 220.2149
## 2024 -21.86866793 -180.932478 137.19514 -265.135773 221.3984
## 2025 -24.59047463 -186.168101 136.98715 -271.702130 222.5212
## 2026 -27.31228133 -191.365811 136.74125 -278.210504 223.5859
## 2027 -30.03408804 -196.527300 136.45912 -284.663482 224.5953
## 2028 -32.75589474 -201.654137 136.14235 -291.063466 225.5517
## 2029 -35.47770144 -206.747783 135.79238 -297.412688 226.4573
## 2030 -38.19950814 -211.809598 135.41058 -303.713228 227.3142
## 2031 -40.92131484 -216.840851 134.99822 -309.967029 228.1244
## 2032 -43.64312155 -221.842733 134.55649 -316.175908 228.8897
## 2033 -46.36492825 -226.816355 134.08650 -322.341569 229.6117
## 2034 -49.08673495 -231.762762 133.58929 -328.465610 230.2921
## 2035 -51.80854165 -236.682939 133.06586 -334.549533 230.9324
## 2036 -54.53034835 -241.577809 132.51711 -340.594753 231.5341
## 2037 -57.25215506 -246.448244 131.94393 -346.602603 232.0983
## 2038 -59.97396176 -251.295068 131.34714 -352.574344 232.6264
## 2039 -62.69576846 -256.119059 130.72752 -358.511164 233.1196
## 2040 -65.41757516 -260.920954 130.08580 -364.414190 233.5790
## 2041 -68.13938186 -265.701450 129.42269 -370.284491 234.0057
## 2042 -70.86118857 -270.461210 128.73883 -376.123079 234.4007
## 2043 -73.58299527 -275.200863 128.03487 -381.930915 234.7649
## 2044 -76.30480197 -279.921006 127.31140 -387.708914 235.0993
## 2045 -79.02660867 -284.622209 126.56899 -393.457946 235.4047
## 2046 -81.74841537 -289.305013 125.80818 -399.178839 235.6820
## 2047 -84.47022208 -293.969936 125.02949 -404.872385 235.9319
## 2048 -87.19202878 -298.617469 124.23341 -410.539337 236.1553
## 2049 -89.91383548 -303.248085 123.42041 -416.180415 236.3527
## 2050 -92.63564218 -307.862233 122.59095 -421.796308 236.5250
## 2051 -95.35744888 -312.460345 121.74545 -427.387675 236.6728
## 2052 -98.07925559 -317.042831 120.88432 -432.955146 236.7966
## 2053 -100.80106229 -321.610088 120.00796 -438.499326 236.8972
## 2054 -103.52286899 -326.162494 119.11676 -444.020793 236.9751
## 2055 -106.24467569 -330.700413 118.21106 -449.520103 237.0308
## 2056 -108.96648239 -335.224193 117.29123 -454.997790 237.0648
## 2057 -111.68828910 -339.734170 116.35759 -460.454367 237.0778
## 2058 -114.41009580 -344.230666 115.41047 -465.890327 237.0701
## 2059 -117.13190250 -348.713991 114.45019 -471.306143 237.0423
## 2060 -119.85370920 -353.184443 113.47702 -476.702272 236.9949
## 2061 -122.57551590 -357.642310 112.49128 -482.079153 236.9281
## 2062 -125.29732261 -362.087868 111.49322 -487.437211 236.8426
## 2063 -128.01912931 -366.521384 110.48313 -492.776852 236.7386
## 2064 -130.74093601 -370.943117 109.46124 -498.098471 236.6166
## 2065 -133.46274271 -375.353314 108.42783 -503.402447 236.4770
## 2066 -136.18454941 -379.752215 107.38312 -508.689149 236.3200
## 2067 -138.90635612 -384.140052 106.32734 -513.958929 236.1462
## 2068 -141.62816282 -388.517050 105.26072 -519.212132 235.9558
## 2069 -144.34996952 -392.883424 104.18349 -524.449088 235.7491
## 2070 -147.07177622 -397.239385 103.09583 -529.670117 235.5266
## 2071 -149.79358292 -401.585134 101.99797 -534.875530 235.2884
## 2072 -152.51538963 -405.920870 100.89009 -540.065628 235.0348
## 2073 -155.23719633 -410.246781 99.77239 -545.240700 234.7663
## 2074 -157.95900303 -414.563051 98.64505 -550.401029 234.4830
## 2075 -160.68080973 -418.869861 97.50824 -555.546889 234.1853
## 2076 -163.40261643 -423.167382 96.36215 -560.678543 233.8733
## 2077 -166.12442314 -427.455784 95.20694 -565.796249 233.5474
## 2078 -168.84622984 -431.735228 94.04277 -570.900257 233.2078
## 2079 -171.56803654 -436.005874 92.86980 -575.990809 232.8547
## 2080 -174.28984324 -440.267874 91.68819 -581.068139 232.4885
## 2081 -177.01164994 -444.521379 90.49808 -586.132476 232.1092
## 2082 -179.73345665 -448.766534 89.29962 -591.184043 231.7171
## 2083 -182.45526335 -453.003480 88.09295 -596.223054 231.3125
## 2084 -185.17707005 -457.232353 86.87821 -601.249720 230.8956
## 2085 -187.89887675 -461.453288 85.65553 -606.264245 230.4665
## 2086 -190.62068345 -465.666414 84.42505 -611.266828 230.0255
## 2087 -193.34249016 -469.871857 83.18688 -616.257661 229.5727
## 2088 -196.06429686 -474.069741 81.94115 -621.236934 229.1083
## 2089 -198.78610356 -478.260186 80.68798 -626.204828 228.6326
## 2090 -201.50791026 -482.443308 79.42749 -631.161524 228.1457
## 2091 -204.22971696 -486.619220 78.15979 -636.107193 227.6478
## 2092 -206.95152367 -490.788035 76.88499 -641.042007 227.1390
## 2093 -209.67333037 -494.949859 75.60320 -645.966131 226.6195
egg.lambda = BoxCox.lambda(eggs)
egg.holt1= holt(eggs, h=100)
egg.holt2 =holt(eggs, lambda=egg.lambda,h=100,)
egg.holt3 =holt(eggs, lambda=egg.lambda, damped=TRUE,h=100,)
autoplot(holt(eggs,h=100)) + autolayer(egg.holt1, series="holts") + autolayer(egg.holt2, series="boxcox")+ autolayer(egg.holt3, series="boxcox_damped") + ylab("Eggs") + xlab("Year")
#Comparison
writeLines("RMSE values from holt function")
## RMSE values from holt function
sqrt(mean(egg.holt$residuals^2))
## [1] 26.58219
writeLines("RMSEvalues from holt function with damped option")
## RMSEvalues from holt function with damped option
sqrt(mean(egg.holt3 $residuals^2))
## [1] 1.039187
writeLines("RMSE values holt function with Box-Cox transformation")
## RMSE values holt function with Box-Cox transformation
sqrt(mean(egg.holt2$residuals^2))
## [1] 1.032217
Box-Cox appears to be the best model
8.Recall your retail time series data (from Exercise 3 in Section 2.10). a.Why is multiplicative seasonality necessary for this series? b.Apply Holt-Winters’ multiplicative method to the data. Experiment with making the trend damped. c.Compare the RMSE of the one-step forecasts from the two methods. Which do you prefer? d.Check that the residuals from the best method look like white noise. e.Now find the test set RMSE, while training the model to the end of 2010. Can you beat the seasonal naïve approach from Exercise 8 in Section 3.7?
library(readr)
library(readxl)
retail = read_excel("Desktop/retail.xlsx")
## New names:
## * `` -> ...1
retail.ts = ts(retail[,80], frequency=12, start=c(1982,4))
autoplot(retail.ts)
Since we are seeing increaseing tren here, I believe multiplicative seasonality shoule be used for this seris
#DAMPED = FALSE
holt1 = hw(retail.ts, seasonal="multiplicative", damped=FALSE, h=30)
autoplot(retail.ts) + autolayer(holt1) + ylab("Retail Sales") + xlab("Year")
#Damped = TRUE
holt2 = hw(retail.ts, seasonal="multiplicative", damped=TRUE, h=30)
autoplot(retail.ts) + autolayer(holt2) + ylab("Retail Sales") + xlab("Year")
#RMSE and accuracy
holt1$mean[1]
## [1] 225.1675
holt2$mean[1]
## [1] 218.6831
kable(accuracy(holt1))
| ME | RMSE | MAE | MPE | MAPE | MASE | ACF1 | |
|---|---|---|---|---|---|---|---|
| Training set | 0.073773 | 13.26086 | 9.883469 | -10.03632 | 22.2147 | 0.742967 | 0.2380573 |
kable(accuracy(holt2))
| ME | RMSE | MAE | MPE | MAPE | MASE | ACF1 | |
|---|---|---|---|---|---|---|---|
| Training set | 0.4280832 | 13.62929 | 10.01654 | -19.09215 | 30.7254 | 0.7529702 | 0.259142 |
When DAMPED = true, it fits better with a lower RMSE value
checkresiduals(holt1)
##
## Ljung-Box test
##
## data: Residuals from Holt-Winters' multiplicative method
## Q* = 81.314, df = 8, p-value = 2.653e-14
##
## Model df: 16. Total lags used: 24
checkresiduals(holt2)
##
## Ljung-Box test
##
## data: Residuals from Damped Holt-Winters' multiplicative method
## Q* = 118.2, df = 7, p-value < 2.2e-16
##
## Model df: 17. Total lags used: 24
9.For the same retail data, try an STL decomposition applied to the Box-Cox transformed series, followed by ETS on the seasonally adjusted data. How does that compare with your best previous forecasts on the test set?
lambda.ts =- BoxCox.lambda(retail.ts)
Boxcox1 = BoxCox(retail.ts, lambda.ts)
Boxcox1
## Jan Feb Mar Apr May Jun Jul
## 1982 1.2565661 0.5344261 0.7357995 0.0000000
## 1983 1.2233431 0.9132268 0.7357995 1.0791091 0.9966807 1.0791091 1.0241471
## 1984 1.2178207 1.0460937 1.0460937 1.1124775 1.0241471 1.1641323 1.0918836
## 1985 1.2239462 1.1602840 1.1284074 1.1676930 1.1465957 1.1867663 1.1351060
## 1986 1.2362711 1.1769481 1.1641323 1.1740753 1.1845315 1.1945542 1.1641323
## 1987 1.2339812 1.1962581 1.1740753 1.1845315 1.1888759 1.1908710 1.1821596
## 1988 1.2380021 1.1908710 1.1994247 1.2073636 1.1945542 1.2023070 1.2049428
## 1989 1.2420270 1.2085017 1.2036536 1.2200509 1.2126365 1.2200509 1.2185878
## 1990 1.2409578 1.2200509 1.2116606 1.2256587 1.2207491 1.2261994 1.2162093
## 1991 1.2435765 1.2309192 1.2245327 1.2300643 1.2193308 1.2282277 1.2153622
## 1992 1.2397989 1.2287037 1.2162093 1.2178207 1.2233431 1.2170283 1.2126365
## 1993 1.2433927 1.2277398 1.2162093 1.2328948 1.2356490 1.2287037 1.2239462
## 1994 1.2437577 1.2325169 1.2239462 1.2256587 1.2220841 1.2227227 1.2153622
## 1995 1.2432062 1.2239462 1.2261994 1.2359631 1.2339812 1.2368698 1.2332646
## 1996 1.2447929 1.2256587 1.2233431 1.2296215 1.2214265 1.2328948 1.2261994
## 1997 1.2430169 1.2346686 1.2261994 1.2346686 1.2362711 1.2382726 1.2336267
## 1998 1.2461962 1.2402740 1.2387992 1.2400384 1.2424319 1.2413956 1.2395554
## 1999 1.2513454 1.2428248 1.2405056 1.2439364 1.2416093 1.2411784 1.2400384
## 2000 1.2521160 1.2413956 1.2413956 1.2437577 1.2424319 1.2449573 1.2446263
## 2001 1.2534804 1.2454372 1.2441125 1.2460483 1.2463422 1.2464864 1.2457467
## 2002 1.2549494 1.2469082 1.2460483 1.2483303 1.2491456 1.2494761 1.2473148
## 2003 1.2557764 1.2479606 1.2470454 1.2475777 1.2506982 1.2524383 1.2497962
## 2004 1.2562313 1.2510735 1.2479606 1.2505049 1.2494761 1.2494761 1.2485698
## 2005 1.2562801 1.2501062 1.2477069 1.2495840 1.2480853 1.2490330 1.2474470
## 2006 1.2560829 1.2484508 1.2478345 1.2506021 1.2503076 1.2489191 1.2482085
## 2007 1.2561327 1.2516960 1.2508877 1.2523588 1.2489191 1.2507935 1.2496906
## 2008 1.2564721 1.2530493 1.2527492 1.2529005 1.2514343 1.2533391 1.2500040
## 2009 1.2565192 1.2518663 1.2512557 1.2522786 1.2520335 1.2531227 1.2514343
## 2010 1.2560327 1.2546524 1.2534100 1.2538233 1.2541525 1.2543437 1.2544064
## 2011 1.2563286 1.2542167 1.2536193 1.2540877 1.2552912 1.2552353 1.2547726
## 2012 1.2564246 1.2547127 1.2542167 1.2553467 1.2559312 1.2558284 1.2558800
## 2013 1.2561822 1.2539566 1.2516096 1.2528252 1.2538902 1.2551790 1.2527492
## 2014 1.2563768
## Aug Sep Oct Nov Dec
## 1982 0.9132268 0.8443807 0.9611725 1.0460937 1.1209167
## 1983 1.1209167 1.1515653 1.1561101 1.0640762 1.1411359
## 1984 1.0791091 1.1561101 1.1028880 1.1676930 1.1867663
## 1985 1.1709981 1.1845315 1.1641323 1.1927609 1.2008988
## 1986 1.1796369 1.1888759 1.1821596 1.1978796 1.1945542
## 1987 1.1978796 1.1978796 1.1994247 1.2061783 1.2085017
## 1988 1.2095955 1.2162093 1.2095955 1.2162093 1.2207491
## 1989 1.2170283 1.2233431 1.2267262 1.2233431 1.2282277
## 1990 1.2135775 1.2251033 1.2251033 1.2233431 1.2350020
## 1991 1.2245327 1.2277398 1.2227227 1.2287037 1.2321307
## 1992 1.2144855 1.2106477 1.2291682 1.2304967 1.2272395
## 1993 1.2261994 1.2233431 1.2291682 1.2304967 1.2343284
## 1994 1.2261994 1.2309192 1.2332646 1.2317359 1.2353287
## 1995 1.2313322 1.2365733 1.2371608 1.2325169 1.2359631
## 1996 1.2309192 1.2328948 1.2304967 1.2346686 1.2339812
## 1997 1.2385382 1.2377268 1.2374464 1.2390556 1.2393076
## 1998 1.2424319 1.2407335 1.2418198 1.2444574 1.2442862
## 1999 1.2416093 1.2426298 1.2413956 1.2439364 1.2455930
## 2000 1.2422310 1.2452794 1.2451194 1.2455930 1.2458985
## 2001 1.2466287 1.2467693 1.2471809 1.2482085 1.2492570
## 2002 1.2470454 1.2514343 1.2504068 1.2517816 1.2529005
## 2003 1.2486876 1.2482085 1.2494761 1.2515224 1.2526725
## 2004 1.2492570 1.2488040 1.2493671 1.2518663 1.2531954
## 2005 1.2492570 1.2500040 1.2490330 1.2505049 1.2525951
## 2006 1.2480853 1.2511650 1.2502074 1.2519503 1.2529752
## 2007 1.2491456 1.2516096 1.2509811 1.2532676 1.2542167
## 2008 1.2516960 1.2525170 1.2522786 1.2536193 1.2544686
## 2009 1.2521976 1.2518663 1.2499006 1.2515224 1.2535501
## 2010 1.2537559 1.2540224 1.2545304 1.2548909 1.2550074
## 2011 1.2556182 1.2556713 1.2550651 1.2551222 1.2559821
## 2012 1.2555647 1.2554018 1.2542804 1.2555108 1.2554565
## 2013 1.2548320 1.2536879 1.2545916 1.2555108 1.2557240
## 2014
retail.stl = stl(Boxcox1[,1], s.window="periodic")
fc.retail = forecast(retail.stl, h=36)
kable(accuracy(fc.retail))
| ME | RMSE | MAE | MPE | MAPE | MASE | ACF1 | |
|---|---|---|---|---|---|---|---|
| Training set | 0.0033619 | 0.0570686 | 0.0192986 | -5.943122e+15 | 5.943122e+15 | 1.225298 | -0.0248119 |
10.For this exercise use data set ukcars, the quarterly UK passenger vehicle production data from 1977Q1–2005Q1.
a.Plot the data and describe the main features of the series. b.Decompose the series using STL and obtain the seasonally adjusted data. c.Forecast the next two years of the series using an additive damped trend method applied to the seasonally adjusted data. (This can be done in one step using stlf() with arguments etsmodel=“AAN”, damped=TRUE.) d.Forecast the next two years of the series using Holt’s linear method applied to the seasonally adjusted data (as before but with damped=FALSE). e.Now use ets() to choose a seasonal model for the data. f.Compare the RMSE of the ETS model with the RMSE of the models you obtained using STL decompositions. Which gives the better in-sample fits? g.Compare the forecasts from the three approaches? Which seems most reasonable? h.Check the residuals of your preferred model.
ukcars
## Qtr1 Qtr2 Qtr3 Qtr4
## 1977 330.371 371.051 270.670 343.880
## 1978 358.491 362.822 261.281 240.355
## 1979 325.382 316.700 171.153 257.217
## 1980 298.127 251.464 181.555 192.598
## 1981 245.652 245.526 225.261 238.211
## 1982 257.385 228.461 175.371 226.462
## 1983 266.150 287.251 225.883 265.313
## 1984 272.759 234.134 196.462 205.551
## 1985 291.283 284.422 221.571 250.697
## 1986 253.757 267.016 220.388 277.801
## 1987 283.233 302.072 259.720 297.658
## 1988 306.129 322.106 256.723 341.877
## 1989 356.004 361.540 270.433 311.105
## 1990 326.688 327.059 274.257 367.606
## 1991 346.163 348.211 250.008 292.518
## 1992 343.318 343.429 275.386 329.747
## 1993 364.521 378.448 300.798 331.757
## 1994 362.536 389.133 323.322 391.832
## 1995 421.646 416.823 311.713 381.902
## 1996 422.982 427.722 376.850 458.580
## 1997 436.225 441.487 369.566 450.723
## 1998 462.442 468.232 403.636 413.948
## 1999 460.496 448.932 407.787 469.408
## 2000 494.311 433.240 335.106 378.795
## 2001 387.100 372.395 335.790 397.080
## 2002 449.755 402.252 391.847 385.890
## 2003 424.325 433.280 391.213 408.740
## 2004 445.458 428.202 379.048 394.042
## 2005 432.796
str(ukcars)
## Time-Series [1:113] from 1977 to 2005: 330 371 271 344 358 ...
autoplot(ukcars) + ylab("Vehicle Production") # there are trend and seasonality
#With stl decomposition
ukcars.stl = stl(ukcars, s.window="periodic")
ukcars.stl
## Call:
## stl(x = ukcars, s.window = "periodic")
##
## Components
## seasonal trend remainder
## 1977 Q1 25.66677 318.5295 -13.825249626
## 1977 Q2 21.01432 324.6948 25.341911916
## 1977 Q3 -45.12476 330.9708 -15.176049732
## 1977 Q4 -1.55632 334.7004 10.735966254
## 1978 Q1 25.66677 334.0139 -1.189641048
## 1978 Q2 21.01432 320.7959 21.011758342
## 1978 Q3 -45.12476 300.9207 5.485038711
## 1978 Q4 -1.55632 288.6423 -46.730931160
## 1979 Q1 25.66677 276.1832 23.532046833
## 1979 Q2 21.01432 267.2862 28.399513279
## 1979 Q3 -45.12476 261.5016 -45.223827211
## 1979 Q4 -1.55632 252.2466 6.526756134
## 1980 Q1 25.66677 247.9784 24.481824797
## 1980 Q2 21.01432 239.4564 -9.006725034
## 1980 Q3 -45.12476 223.2180 3.461793086
## 1980 Q4 -1.55632 215.7849 -21.630623541
## 1981 Q1 25.66677 220.2357 -0.250430089
## 1981 Q2 21.01432 233.6016 -9.089932014
## 1981 Q3 -45.12476 241.8626 28.523188447
## 1981 Q4 -1.55632 240.7936 -1.026285436
## 1982 Q1 25.66677 230.3050 1.413217582
## 1982 Q2 21.01432 222.3072 -14.860538991
## 1982 Q3 -45.12476 222.2015 -1.705761122
## 1982 Q4 -1.55632 231.0511 -3.032794821
## 1983 Q1 25.66677 244.9445 -4.461265566
## 1983 Q2 21.01432 257.0965 9.140190429
## 1983 Q3 -45.12476 263.2765 7.731214593
## 1983 Q4 -1.55632 257.4187 9.450666648
## 1984 Q1 25.66677 245.2947 1.797509364
## 1984 Q2 21.01432 233.9046 -20.784913580
## 1984 Q3 -45.12476 228.4033 13.183489842
## 1984 Q4 -1.55632 237.1828 -30.075524618
## 1985 Q1 25.66677 247.7529 17.863310026
## 1985 Q2 21.01432 258.0678 5.339865349
## 1985 Q3 -45.12476 258.1793 8.516496453
## 1985 Q4 -1.55632 250.2516 2.001681939
## 1986 Q1 25.66677 246.4844 -18.394218794
## 1986 Q2 21.01432 250.3747 -4.372980363
## 1986 Q3 -45.12476 259.5206 5.992168272
## 1986 Q4 -1.55632 267.0536 12.303689570
## 1987 Q1 25.66677 274.7015 -17.135282062
## 1987 Q2 21.01432 282.7800 -1.722305893
## 1987 Q3 -45.12476 290.1326 14.712201563
## 1987 Q4 -1.55632 294.2213 4.992997392
## 1988 Q1 25.66677 295.1384 -14.676141546
## 1988 Q2 21.01432 300.0450 1.046637055
## 1988 Q3 -45.12476 313.0266 -11.178866207
## 1988 Q4 -1.55632 324.8864 18.546947724
## 1989 Q1 25.66677 331.9778 -1.640598597
## 1989 Q2 21.01432 329.0151 11.510606843
## 1989 Q3 -45.12476 321.2409 -5.683114815
## 1989 Q4 -1.55632 312.5455 0.115820802
## 1990 Q1 25.66677 308.5850 -7.563781787
## 1990 Q2 21.01432 315.1534 -9.108686479
## 1990 Q3 -45.12476 326.9998 -7.618043743
## 1990 Q4 -1.55632 333.4080 35.754296494
## 1991 Q1 25.66677 332.0422 -11.545969032
## 1991 Q2 21.01432 318.1429 9.053818020
## 1991 Q3 -45.12476 307.8729 -12.740133598
## 1991 Q4 -1.55632 306.5401 -12.465738791
## 1992 Q1 25.66677 310.8608 6.790443375
## 1992 Q2 21.01432 318.7590 3.655635693
## 1992 Q3 -45.12476 325.3152 -4.804421022
## 1992 Q4 -1.55632 332.1973 -0.893989077
## 1993 Q1 25.66677 340.5537 -1.699468477
## 1993 Q2 21.01432 344.6912 12.742515376
## 1993 Q3 -45.12476 344.0280 1.894724342
## 1993 Q4 -1.55632 343.3536 -10.040311206
## 1994 Q1 25.66677 348.0348 -11.165536675
## 1994 Q2 21.01432 359.2368 8.881872441
## 1994 Q3 -45.12476 374.3526 -5.905812714
## 1994 Q4 -1.55632 385.3718 8.016551486
## 1995 Q1 25.66677 388.6002 7.378976268
## 1995 Q2 21.01432 384.3830 11.425715058
## 1995 Q3 -45.12476 381.6207 -24.782895640
## 1995 Q4 -1.55632 383.7478 -0.289469197
## 1996 Q1 25.66677 394.5880 2.727254896
## 1996 Q2 21.01432 411.2334 -4.525738213
## 1996 Q3 -45.12476 424.2350 -2.260256000
## 1996 Q4 -1.55632 428.3269 31.809443631
## 1997 Q1 25.66677 427.3670 -16.808791145
## 1997 Q2 21.01432 423.5993 -3.126575508
## 1997 Q3 -45.12476 427.6065 -12.915722425
## 1997 Q4 -1.55632 434.9783 17.301020647
## 1998 Q1 25.66677 442.4171 -5.641897664
## 1998 Q2 21.01432 442.2694 4.948265854
## 1998 Q3 -45.12476 437.2456 11.515205896
## 1998 Q4 -1.55632 433.4005 -17.896137200
## 1999 Q1 25.66677 431.3057 3.523496436
## 1999 Q2 21.01432 439.1628 -11.245141015
## 1999 Q3 -45.12476 450.8969 2.014845258
## 1999 Q4 -1.55632 455.5570 15.407293417
## 2000 Q1 25.66677 444.3540 24.290258982
## 2000 Q2 21.01432 421.1754 -8.949762594
## 2000 Q3 -45.12476 395.4301 -15.199289613
## 2000 Q4 -1.55632 375.7606 4.590727142
## 2001 Q1 25.66677 367.5362 -6.102990303
## 2001 Q2 21.01432 369.1569 -17.776203042
## 2001 Q3 -45.12476 380.2567 0.658094924
## 2001 Q4 -1.55632 394.1493 4.486990545
## 2002 Q1 25.66677 403.5459 20.542371093
## 2002 Q2 21.01432 408.8754 -27.637700761
## 2002 Q3 -45.12476 404.8623 32.109499840
## 2002 Q4 -1.55632 404.8806 -17.434230990
## 2003 Q1 25.66677 406.8758 -8.217613506
## 2003 Q2 21.01432 412.2684 -0.002686131
## 2003 Q3 -45.12476 418.0638 18.273925233
## 2003 Q4 -1.55632 419.2933 -8.996989753
## 2004 Q1 25.66677 416.2195 3.571765783
## 2004 Q2 21.01432 413.9253 -6.737593135
## 2004 Q3 -45.12476 410.1435 14.029258400
## 2004 Q4 -1.55632 407.2154 -11.617080187
## 2005 Q1 25.66677 403.8199 3.309325330
summary(ukcars.stl)
## Call:
## stl(x = ukcars, s.window = "periodic")
##
## Time.series components:
## seasonal trend remainder
## Min. :-45.12476 Min. :215.7849 Min. :-46.73093
## 1st Qu.: -1.55632 1st Qu.:263.2765 1st Qu.: -9.08993
## Median : 21.01432 Median :329.0151 Median : -0.00269
## Mean : 0.22714 Mean :333.1863 Mean : 0.06435
## 3rd Qu.: 25.66677 3rd Qu.:403.8199 3rd Qu.: 8.88187
## Max. : 25.66677 Max. :455.5570 Max. : 35.75430
## IQR:
## STL.seasonal STL.trend STL.remainder data
## 27.22 140.54 17.97 124.83
## % 21.8 112.6 14.4 100.0
##
## Weights: all == 1
##
## Other components: List of 5
## $ win : Named num [1:3] 1131 7 5
## $ deg : Named int [1:3] 0 1 1
## $ jump : Named num [1:3] 114 1 1
## $ inner: int 2
## $ outer: int 0
ukcars.stlf1 = stlf(ukcars, etsmodel="AAN", damped=TRUE, h=8)
ukcars.stlf2 = stlf(ukcars, etsmodel="AAN", damped=FALSE, h=8)
ukcars.ets = ets(ukcars)
ukcars.stlf1
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2005 Q2 415.0289 384.4576 445.6001 368.2742 461.7836
## 2005 Q3 364.2543 326.8700 401.6386 307.0799 421.4287
## 2005 Q4 400.8059 357.6702 443.9416 334.8355 466.7762
## 2006 Q1 432.5663 384.3596 480.7731 358.8404 506.2922
## 2006 Q2 415.0250 362.2312 467.8189 334.2838 495.7663
## 2006 Q3 364.2507 307.2369 421.2646 277.0556 451.4459
## 2006 Q4 400.8026 339.8596 461.7456 307.5983 494.0069
## 2007 Q1 432.5633 367.9289 497.1976 333.7136 531.4130
ukcars.stlf2
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2005 Q2 416.1915 385.7950 446.5881 369.7040 462.6791
## 2005 Q3 366.3343 328.7907 403.8780 308.9163 423.7524
## 2005 Q4 403.8032 360.2690 447.3375 337.2233 470.3831
## 2006 Q1 436.4809 387.6847 485.2771 361.8535 511.1083
## 2006 Q2 419.8568 366.3120 473.4016 337.9671 501.7465
## 2006 Q3 369.9996 312.0932 427.9060 281.4394 458.5598
## 2006 Q4 407.4685 345.5056 469.4313 312.7045 502.2325
## 2007 Q1 440.1461 374.3755 505.9167 339.5587 540.7336
ukcars.ets
## ETS(A,N,A)
##
## Call:
## ets(y = ukcars)
##
## Smoothing parameters:
## alpha = 0.6199
## gamma = 1e-04
##
## Initial states:
## l = 314.2568
## s = -1.7579 -44.9601 21.1956 25.5223
##
## sigma: 25.9302
##
## AIC AICc BIC
## 1277.752 1278.819 1296.844
# PICK ETS for forecasting
fc1 = forecast(ukcars.ets)
fc2 = forecast(ukcars.stlf1)
fc3 = forecast(ukcars.stlf2)
kable(accuracy(ukcars.ets))
| ME | RMSE | MAE | MPE | MAPE | MASE | ACF1 | |
|---|---|---|---|---|---|---|---|
| Training set | 1.313884 | 25.23244 | 20.17907 | -0.1570979 | 6.629003 | 0.6576259 | 0.0257333 |
kable(accuracy(ukcars.stlf1))
| ME | RMSE | MAE | MPE | MAPE | MASE | ACF1 | |
|---|---|---|---|---|---|---|---|
| Training set | 1.551267 | 23.32113 | 18.48987 | 0.0412197 | 6.042764 | 0.602576 | 0.0226267 |
kable(accuracy(ukcars.stlf2))
| ME | RMSE | MAE | MPE | MAPE | MASE | ACF1 | |
|---|---|---|---|---|---|---|---|
| Training set | -0.3412727 | 23.295 | 18.1605 | -0.5970778 | 5.98018 | 0.5918418 | 0.0210358 |
#I will pick model stl2 since it has a lower RMSE
checkresiduals(ukcars.stlf2)
## Warning in checkresiduals(ukcars.stlf2): The fitted degrees of freedom is based
## on the model used for the seasonally adjusted data.
##
## Ljung-Box test
##
## data: Residuals from STL + ETS(A,A,N)
## Q* = 22.061, df = 4, p-value = 0.0001949
##
## Model df: 4. Total lags used: 8
summary(ukcars.stlf2$residuals)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -74.4850 -17.8438 2.2317 -0.3413 14.8230 53.6357
11.For this exercise use data set visitors, the monthly Australian short-term overseas visitors data, May 1985–April 2005. a.Make a time plot of your data and describe the main features of the series. b.Split your data into a training set and a test set comprising the last two years of available data. Forecast the test set using Holt-Winters’ multiplicative method. c.Why is multiplicative seasonality necessary here? d.Forecast the two-year test set using each of the following methods: an ETS model; an additive ETS model applied to a Box-Cox transformed series; a seasonal naïve method; an STL decomposition applied to the Box-Cox transformed data followed by an ETS model applied to the seasonally adjusted (transformed) data. e.Which method gives the best forecasts? Does it pass the residual tests? f.Compare the same four methods using time series cross-validation with the tsCV() function instead of using a training and test set. Do you come to the same conclusions?
autoplot(visitors) + xlab("Year")
## create Testing set
Visitor.test = window(visitors, start=2004)
fc1 = hw(Visitor.test, seasonal="multiplicative", damped=FALSE, h=12)
## Warning in ets(x, "MAM", alpha = alpha, beta = beta, gamma = gamma, phi = phi, :
## Seasonal component could not be estimated
fc1
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## May 2005 441.3111 354.24112 528.3810 308.149069 574.4731
## Jun 2005 439.6645 325.75496 553.5740 265.454900 613.8740
## Jul 2005 438.0179 294.86439 581.1714 219.083511 656.9522
## Aug 2005 436.3713 261.77064 610.9719 169.342651 703.3999
## Sep 2005 434.7247 226.62773 642.8216 116.467867 752.9815
## Oct 2005 433.0781 189.55935 676.5968 60.648333 805.5078
## Nov 2005 431.4315 150.66814 712.1948 2.041018 860.8219
## Dec 2005 429.7849 110.04129 749.5284 -59.220727 918.7904
## Jan 2006 428.1383 67.75421 788.5223 -123.021579 979.2981
## Feb 2006 426.4917 23.87303 829.1103 -189.260392 1042.2437
## Mar 2006 424.8450 -21.54358 871.2337 -257.847447 1107.5375
## Apr 2006 423.1984 -68.44309 914.8400 -328.702399 1175.0993
# create Training Set
fc2 = hw(visitors, seasonal="multiplicative", damped=FALSE, h=12)
fc2
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## May 2005 363.6434 337.2901 389.9967 323.3395 403.9474
## Jun 2005 389.5974 356.8764 422.3184 339.5550 439.6399
## Jul 2005 482.7709 437.0247 528.5172 412.8081 552.7338
## Aug 2005 428.5001 383.4951 473.5050 359.6709 497.3293
## Sep 2005 420.4548 372.1236 468.7860 346.5386 494.3710
## Oct 2005 475.5963 416.3290 534.8636 384.9547 566.2379
## Nov 2005 503.4598 435.9471 570.9725 400.2080 606.7116
## Dec 2005 608.3779 521.1122 695.6436 474.9165 741.8393
## Jan 2006 455.1525 385.6597 524.6452 348.8725 561.4324
## Feb 2006 509.0590 426.6702 591.4478 383.0562 635.0618
## Mar 2006 496.8101 411.8771 581.7431 366.9162 626.7039
## Apr 2006 441.9586 362.3923 521.5249 320.2724 563.6448
#Multiplicative seasonality is necessary here because there is observable increasing trend
#ETS model
visitor.ets = ets(Visitor.test)
fc1=forecast(visitor.ets,h=24)
fc1
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## May 2005 465.1445 385.2825 545.0065 343.0061 587.2829
## Jun 2005 465.1445 380.5627 549.7263 335.7878 594.5012
## Jul 2005 465.1445 376.0927 554.1963 328.9515 601.3375
## Aug 2005 465.1445 371.8366 558.4524 322.4424 607.8466
## Sep 2005 465.1445 367.7664 562.5226 316.2175 614.0715
## Oct 2005 465.1445 363.8596 566.4294 310.2426 620.0464
## Nov 2005 465.1445 360.0980 570.1910 304.4897 625.7993
## Dec 2005 465.1445 356.4665 573.8225 298.9358 631.3532
## Jan 2006 465.1445 352.9525 577.3365 293.5616 636.7274
## Feb 2006 465.1445 349.5452 580.7438 288.3507 641.9383
## Mar 2006 465.1445 346.2356 584.0534 283.2890 647.0000
## Apr 2006 465.1445 343.0156 587.2734 278.3645 651.9245
## May 2006 465.1445 339.8784 590.4106 273.5665 656.7225
## Jun 2006 465.1445 336.8178 593.4712 268.8857 661.4033
## Jul 2006 465.1445 333.8285 596.4605 264.3141 665.9749
## Aug 2006 465.1445 330.9058 599.3832 259.8442 670.4448
## Sep 2006 465.1445 328.0454 602.2436 255.4696 674.8195
## Oct 2006 465.1445 325.2435 605.0455 251.1844 679.1047
## Nov 2006 465.1445 322.4966 607.7924 246.9833 683.3057
## Dec 2006 465.1445 319.8016 610.4875 242.8616 687.4274
## Jan 2007 465.1445 317.1556 613.1334 238.8150 691.4740
## Feb 2007 465.1445 314.5562 615.7328 234.8395 695.4495
## Mar 2007 465.1445 312.0008 618.2882 230.9315 699.3576
## Apr 2007 465.1445 309.4874 620.8016 227.0875 703.2015
#ETS model under BocCox transformation
visitor.ets.bc = ets(Visitor.test, model="AZZ", lambda="auto")
fc2=forecast(visitor.ets.bc)
fc2
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## May 2005 465.1511 385.2891 545.0131 343.0127 587.2895
## Jun 2005 465.1511 380.5737 549.7286 335.8011 594.5011
## Jul 2005 465.1511 376.1076 554.1946 328.9708 601.3314
## Aug 2005 465.1511 371.8551 558.4471 322.4672 607.8350
## Sep 2005 465.1511 367.7882 562.5141 316.2473 614.0549
## Oct 2005 465.1511 363.8844 566.4178 310.2771 620.0252
## Nov 2005 465.1511 360.1257 570.1765 304.5286 625.7737
## Dec 2005 465.1511 356.4969 573.8053 298.9788 631.3234
## Jan 2006 465.1511 352.9854 577.3168 293.6085 636.6937
## Feb 2006 465.1511 349.5806 580.7216 288.4013 641.9009
## Mar 2006 465.1511 346.2733 584.0290 283.3431 646.9591
## Apr 2006 465.1511 343.0555 587.2467 278.4220 651.8803
## May 2006 465.1511 339.9204 590.3819 273.6272 656.6750
## Jun 2006 465.1511 336.8618 593.4404 268.9496 661.3526
## Jul 2006 465.1511 333.8745 596.4277 264.3809 665.9213
## Aug 2006 465.1511 330.9537 599.3485 259.9139 670.3883
## Sep 2006 465.1511 328.0951 602.2071 255.5421 674.7601
## Oct 2006 465.1511 325.2950 605.0072 251.2596 679.0426
## Nov 2006 465.1511 322.5498 607.7524 247.0612 683.2410
## Dec 2006 465.1511 319.8564 610.4458 242.9421 687.3601
## Jan 2007 465.1511 317.2121 613.0901 238.8980 691.4042
## Feb 2007 465.1511 314.6143 615.6879 234.9249 695.3773
## Mar 2007 465.1511 312.0605 618.2417 231.0192 699.2830
## Apr 2007 465.1511 309.5486 620.7536 227.1776 703.1246
#Naive method
visitor.naive = snaive(Visitor.test, h=24)
fc3=forecast(visitor.naive, h=24)
fc3
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## May 2005 341.3 279.6464 402.9536 247.0090 435.5910
## Jun 2005 367.3 305.6464 428.9536 273.0090 461.5910
## Jul 2005 472.0 410.3464 533.6536 377.7090 566.2910
## Aug 2005 405.8 344.1464 467.4536 311.5090 500.0910
## Sep 2005 395.6 333.9464 457.2536 301.3090 489.8910
## Oct 2005 449.9 388.2464 511.5536 355.6090 544.1910
## Nov 2005 479.9 418.2464 541.5536 385.6090 574.1910
## Dec 2005 593.1 531.4464 654.7536 498.8090 687.3910
## Jan 2006 462.4 400.7464 524.0536 368.1090 556.6910
## Feb 2006 501.6 439.9464 563.2536 407.3090 595.8910
## Mar 2006 504.7 443.0464 566.3536 410.4090 598.9910
## Apr 2006 409.5 347.8464 471.1536 315.2090 503.7910
## May 2006 341.3 254.1087 428.4913 207.9524 474.6476
## Jun 2006 367.3 280.1087 454.4913 233.9524 500.6476
## Jul 2006 472.0 384.8087 559.1913 338.6524 605.3476
## Aug 2006 405.8 318.6087 492.9913 272.4524 539.1476
## Sep 2006 395.6 308.4087 482.7913 262.2524 528.9476
## Oct 2006 449.9 362.7087 537.0913 316.5524 583.2476
## Nov 2006 479.9 392.7087 567.0913 346.5524 613.2476
## Dec 2006 593.1 505.9087 680.2913 459.7524 726.4476
## Jan 2007 462.4 375.2087 549.5913 329.0524 595.7476
## Feb 2007 501.6 414.4087 588.7913 368.2524 634.9476
## Mar 2007 504.7 417.5087 591.8913 371.3524 638.0476
## Apr 2007 409.5 322.3087 496.6913 276.1524 542.8476
#STL decomposition applied to Box-Cox transformed dataset
visitor.stl = stl(visitors, s.window="periodic")
fc4=forecast(visitor.stl, h=24)
fc4
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## May 2005 401.2570 375.1438 427.3701 361.3204 441.1935
## Jun 2005 416.4592 387.3886 445.5298 371.9996 460.9189
## Jul 2005 471.8865 440.1316 503.6413 423.3216 520.4514
## Aug 2005 443.2371 409.0069 477.4674 390.8865 495.5878
## Sep 2005 435.2928 398.7535 471.8320 379.4108 491.1747
## Oct 2005 472.7590 434.0474 511.4706 413.5547 531.9633
## Nov 2005 490.5152 449.7460 531.2844 428.1641 552.8663
## Dec 2005 560.3010 517.5724 603.0296 494.9533 625.6488
## Jan 2006 464.0268 419.4240 508.6296 395.8128 532.2408
## Feb 2006 496.6757 450.2737 543.0777 425.7099 567.6414
## Mar 2006 488.9345 440.7998 537.0692 415.3188 562.5502
## Apr 2006 454.2768 404.4689 504.0847 378.1022 530.4514
## May 2006 418.2968 366.8695 469.7241 339.6456 496.9481
## Jun 2006 433.4991 380.5012 486.4969 352.4459 514.5523
## Jul 2006 488.9263 434.4025 543.4502 405.5394 572.3133
## Aug 2006 460.2770 404.2682 516.2858 374.6189 545.9351
## Sep 2006 452.3326 394.8766 509.7887 364.4612 540.2040
## Oct 2006 489.7989 430.9306 548.6671 399.7677 579.8301
## Nov 2006 507.5551 447.3072 567.8030 415.4139 599.6963
## Dec 2006 577.3409 515.7436 638.9381 483.1360 671.5457
## Jan 2007 481.0667 418.1485 543.9848 384.8416 577.2917
## Feb 2007 513.7155 449.5031 577.9280 415.5111 611.9200
## Mar 2007 505.9744 440.4927 571.4560 405.8288 606.1199
## Apr 2007 471.3167 404.5894 538.0439 369.2662 573.3672
checkresiduals(fc1)
##
## Ljung-Box test
##
## data: Residuals from ETS(A,N,N)
## Q* = 1.3521, df = 3, p-value = 0.7168
##
## Model df: 2. Total lags used: 5
checkresiduals(fc2)
##
## Ljung-Box test
##
## data: Residuals from ETS(A,N,N)
## Q* = 1.3525, df = 3, p-value = 0.7167
##
## Model df: 2. Total lags used: 5
checkresiduals(fc3)
##
## Ljung-Box test
##
## data: Residuals from Seasonal naive method
## Q* = 1.4962, df = 3, p-value = 0.6832
##
## Model df: 0. Total lags used: 3
checkresiduals(fc4)
## Warning in checkresiduals(fc4): The fitted degrees of freedom is based on the
## model used for the seasonally adjusted data.
##
## Ljung-Box test
##
## data: Residuals from STL + ETS(A,A,N)
## Q* = 109.94, df = 20, p-value = 2.021e-14
##
## Model df: 4. Total lags used: 24
#Conclusion: the seasonal navie method provides the best result
14.Use ets() on the following series: bicoal, chicken, dole, usdeaths, lynx, ibmclose, eggs. Does it always give good forecasts? Find an example where it does not work well. Can you figure out why?
ets(bicoal)
## ETS(M,N,N)
##
## Call:
## ets(y = bicoal)
##
## Smoothing parameters:
## alpha = 0.8205
##
## Initial states:
## l = 542.665
##
## sigma: 0.1262
##
## AIC AICc BIC
## 595.2499 595.7832 600.9253
ets(chicken)
## ETS(M,N,N)
##
## Call:
## ets(y = chicken)
##
## Smoothing parameters:
## alpha = 0.98
##
## Initial states:
## l = 159.8322
##
## sigma: 0.1691
##
## AIC AICc BIC
## 635.2382 635.6018 641.9836
ets(dole)
## ETS(M,Ad,M)
##
## Call:
## ets(y = dole)
##
## Smoothing parameters:
## alpha = 0.697
## beta = 0.124
## gamma = 0.303
## phi = 0.902
##
## Initial states:
## l = 2708.6621
## b = 836.017
## s = 1.0404 0.8893 0.9103 1.0301 1.0576 1.0584
## 0.9801 0.9632 1.021 0.9838 1.0145 1.0514
##
## sigma: 0.0935
##
## AIC AICc BIC
## 10602.67 10604.30 10676.19
ets(usdeaths)
## ETS(A,N,A)
##
## Call:
## ets(y = usdeaths)
##
## Smoothing parameters:
## alpha = 0.5972
## gamma = 0.0019
##
## Initial states:
## l = 9195.6403
## s = -62.6129 -270.0351 263.3823 -89.4907 1005.529 1662.647
## 795.2585 333.326 -551.161 -737.5102 -1552.872 -796.4611
##
## sigma: 294.4663
##
## AIC AICc BIC
## 1141.016 1149.587 1175.166
ets(lynx)
## ETS(M,N,N)
##
## Call:
## ets(y = lynx)
##
## Smoothing parameters:
## alpha = 0.9999
##
## Initial states:
## l = 2372.8047
##
## sigma: 0.9594
##
## AIC AICc BIC
## 2058.138 2058.356 2066.346
ets(ibmclose)
## ETS(A,N,N)
##
## Call:
## ets(y = ibmclose)
##
## Smoothing parameters:
## alpha = 0.9999
##
## Initial states:
## l = 459.9339
##
## sigma: 7.2637
##
## AIC AICc BIC
## 3648.450 3648.515 3660.182
ets(eggs)
## ETS(M,N,N)
##
## Call:
## ets(y = eggs)
##
## Smoothing parameters:
## alpha = 0.8198
##
## Initial states:
## l = 278.8889
##
## sigma: 0.1355
##
## AIC AICc BIC
## 1043.286 1043.553 1050.916
15.Show that the point forecasts from an ETS(M,A,M) model are the same as those obtained using Holt-Winters’ multiplicative method.
since M,A,N each stands for Additive and Multiplicative , meaning that For ETS(M,A,M, the model ) utilities multiplicative and additive process, which is the same multiplicative method applied through Holt-winter
16.Show that the forecast variance for an ETS(A,N,N) model is given by: σ^2[1+α^2(h-1)] Not sure for this one, but the traditional parameter region is 0<α<1. 17.Write down 95% prediction intervals for an ETS(A,N,N) model as a function of ℓT, α, h and σ, assuming normally distributed errors. a 95% prediction interval for the h-step forecast is ^yT+h|T ± 1.96^estimate.sd, hence More generally, a prediction interval could be expressed as ^yT+h|T ± c^estimate.as