Discussion #3

Go to Data Market (https://datamarket.com/data/list/?q=cat:ecc%20provider:tsdl (Links to an external site.) Pick a time series of interest to you. Using this data (be sure to sort in ascending order), build two or three ETS models. Which performs better? What explanation might explain that?

I continue with - Weekly closings of the Dow-Jones industrial average, July 1971 – August 1974.

# Loading libraries
library(forecast)
library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(TTR)
library(tseries)
library(ggplot2)

# LOADING DATASET
# weekly-closings-of-the-dowjones.csv
dj<-read.csv(file.choose())

names(dj)
## [1] "Week"        "close.price"
names(dj)[2]<-c("close.price")

head(dj)
##       Week close.price
## 1 1971-W27      890.19
## 2 1971-W28      901.80
## 3 1971-W29      888.51
## 4 1971-W30      887.78
## 5 1971-W31      858.43
## 6 1971-W32      850.61

Plotting data

# Plot original series
plot(dj$close.price,type="l",xlab="Week",ylab="closing price",main="historical data")

# boxplots by month//Qtr
dj.ts.q<-ts(dj,frequency = 4)
dj.ts.m<-ts(dj,frequency = 12)

# boxplot(dj.ts.m[[1]],dj.ts.m["close.price"])

Given the weekly data, I am still trying to find out how to make a monthly/quarterly boxplot as discussed in class tonight. However, I tried to gt the weekly boxplots out this time.

dj$weeknum<-substr(dj$Week,6,8)

ggplot(dj, aes(as.factor(dj$weeknum), dj$close.price)) +
    geom_boxplot()

And as expected, the weekly data as well is very variable over diff weeks over years.

Forecasting data

dj.ts.m<-ts(dj$close.price,frequency = 12)

# Simple Exponential Smoothing
ses1 <- ses(dj.ts.m, alpha=0.2, initial="simple", h=3)
ses2 <- ses(dj.ts.m, alpha=0.6, initial="simple", h=3)
ses3 <- ses(dj.ts.m, h=3)

# Holt Winters Linear Trend 
holt1 <- holt(dj.ts.m, alpha=0.8, beta=0.2, initial="simple", h=5) 

# Exponential Trend
holt2 <- holt(dj.ts.m, alpha=0.8, beta=0.2, initial="simple", exponential=TRUE, h=5) 

# Damped Trend
# holt3 <- holt(dj.ts.m, alpha=0.8, beta=0.2, damped=TRUE, initial="simple", h=5) 

# Results for first model:
holt1$model$state
##                l           b
## Dec  0  890.1900  11.6100000
## Jan  1  892.5120   9.7524000
## Feb  1  901.8929   9.6780960
## Mar  1  893.1222   5.9883398
## Apr  1  890.0461   4.1754542
## May  1  865.5883  -1.5511956
## Jun  1  853.2954  -3.6995342
## Jul  1  854.7352  -2.6716765
## Aug  1  875.1407   1.9437633
## Sep  1  901.9369   6.9142491
## Oct  1  911.9702   7.5380664
## Nov  1  912.7017   6.1767393
## Dec  1  910.3517   4.4713955
## Jan  2  894.4126   0.3893035
## Feb  2  894.1444   0.2577965
## Mar  2  894.0084   0.1790477
## Apr  2  878.7175  -2.9149497
## May  2  857.0565  -6.6641572
## Jun  2  841.2785  -8.4869336
## Jul  2  838.8703  -7.2711795
## Aug  2  816.6718 -10.2566399
## Sep  2  809.8190  -9.5758696
## Oct  2  813.2886  -6.9667764
## Nov  2  848.9364   1.5561264
## Dec  2  855.4985   2.5573268
## Jan  3  870.6512   5.0763946
## Feb  3  880.0815   5.9471850
## Mar  3  889.3657   6.6145935
## Apr  3  907.4921   8.9169402
## May  3  908.6258   7.3602991
## Jun  3  909.1492   5.9929231
## Jul  3  908.1324   4.5909801
## Aug  3  907.8887   3.6240347
## Sep  3  916.3745   4.5964001
## Oct  3  918.2102   4.0442492
## Nov  3  922.6829   4.1299391
## Dec  3  939.3066   6.6286868
## Jan  4  941.0831   5.6582465
## Feb  4  943.6523   5.0404390
## Mar  4  943.5625   4.0144072
## Apr  4  942.0754   2.9140957
## May  4  959.0779   5.7317781
## Jun  4  967.1379   6.1974301
## Jul  4  965.7071   4.6717717
## Aug  4  957.4118   2.0783565
## Sep  4  944.8820  -0.8432636
## Oct  4  942.2718  -1.1966654
## Nov  4  957.4470   2.0777207
## Dec  4  968.9049   3.9537626
## Jan  5  963.6837   2.1187690
## Feb  5  940.7205  -2.8976328
## Mar  5  943.6126  -1.7396919
## Apr  5  944.1266  -1.2889530
## May  5  931.7915  -3.4981728
## Jun  5  936.1067  -1.9355091
## Jul  5  924.6422  -3.8412949
## Aug  5  920.5202  -3.8974449
## Sep  5  924.6845  -2.2850837
## Oct  5  945.8879   2.4126019
## Nov  5  961.0041   4.9533228
## Dec  5  965.8555   4.9329353
## Jan  6  961.6457   3.1043881
## Feb  6  968.9900   3.9523766
## Mar  6  963.5805   2.0799940
## Apr  6  950.9161  -0.8688815
## May  6  944.4334  -1.9916356
## Jun  6  951.1044  -0.2591247
## Jul  6  946.4570  -1.1367626
## Aug  6  933.4321  -3.5144081
## Sep  6  940.2315  -1.4516319
## Oct  6  944.8920  -0.2292156
## Nov  6  976.2286   6.0839422
## Dec  6  992.6705   8.1555430
## Jan  7 1004.6212   8.9145763
## Feb  7 1022.8752  10.7824507
## Mar  7 1025.4755   9.1460335
## Apr  7 1033.4763   8.9169847
## May  7 1030.2707   6.4924574
## Jun  7 1010.7206   1.2839587
## Jul  7 1018.4169   2.5664256
## Aug  7 1042.1887   6.8074909
## Sep  7 1041.2872   5.2657054
## Oct  7 1030.2626   2.0076354
## Nov  7 1009.2860  -2.5892002
## Dec  7  985.9874  -6.7310953
## Jan  8  979.4193  -6.6984991
## Feb  8  977.9282  -5.6570200
## Mar  8  962.3662  -7.6380010
## Apr  8  960.0016  -6.5833170
## May  8  968.4677  -3.5734495
## Jun  8  963.4188  -3.8685241
## Jul  8  930.0781  -9.7629751
## Aug  8  944.8710  -4.8517893
## Sep  8  932.8598  -6.2836659
## Oct  8  952.8032  -1.0382546
## Nov  8  960.9130   0.7913483
## Dec  8  930.0929  -5.5309468
## Jan  9  948.0084  -0.8416543
## Feb  9  931.7453  -3.9259311
## Mar  9  901.6999  -9.1498375
## Apr  9  923.1820  -3.0234448
## May  9  899.1997  -7.2152151
## Jun  9  914.3969  -2.7327347
## Jul  9  893.1728  -6.4310011
## Aug  9  881.2044  -7.5384942
## Sep  9  888.1012  -4.6514337
## Oct  9  872.7779  -6.7857923
## Nov  9  881.9904  -3.5861372
## Dec  9  904.4009   1.6131758
## Jan 10  930.5708   6.5245302
## Feb 10  914.5151   2.0084763
## Mar 10  865.2087  -8.2544907
## Apr 10  868.8628  -5.8727656
## May 10  863.3900  -5.7927781
## Jun 10  881.5754  -0.9971361
## Jul 10  895.0197   1.8911341
## Aug 10  888.4702   0.2030067
## Sep 10  920.0546   6.4793001
## Oct 10  942.9868   9.7698708
## Nov 10  967.5513  12.7288056
## Dec 10  978.9600  12.4647837
## Jan 11  969.2690   8.0336139
## Feb 11  985.1085   9.5948017
## Mar 11  947.1647   0.0870710
## Apr 11  916.1863  -6.1260065
## May 11  895.0761  -9.1228610
## Jun 11  860.3906 -14.2353741
## Jul 11  827.0311 -18.0602169
## Aug 11  832.2342 -13.4075507
## Sep 11  816.2853 -13.9158094
## Oct 11  815.4579 -11.2981316
## Nov 11  839.2480  -4.2804950
## Dec 11  871.1775   2.9615115
## Jan 12  848.0118  -2.2639290
## Feb 12  853.5256  -0.7083885
## Mar 12  858.0754   0.3432618
## Apr 12  846.8357  -1.9733300
## May 12  825.2925  -5.8873156
## Jun 12  820.1370  -5.7409422
## Jul 12  847.6712   0.9140832
## Aug 12  851.2531   1.4476350
## Sep 12  872.9801   5.5035237
## Oct 12  885.9607   6.9989377
## Nov 12  881.0959   4.6261905
## Dec 12  854.4884  -1.6205495
## Jan 13  848.6056  -2.4730095
## Feb 13  845.0745  -2.6846200
## Mar 13  856.3980   0.1169971
## Apr 13  839.0150  -3.3829990
## May 13  843.8464  -1.7401184
## Jun 13  848.7733  -0.4067233
## Jul 13  824.7453  -5.1309686
## Aug 13  817.2429  -5.6052627
## Sep 13  804.0635  -7.1200795
## Oct 13  842.3647   1.9641699
## Nov 13  843.3378   1.7659526
## Dec 13  821.3327  -2.9882433
## Jan 14  805.5969  -5.5377635
## Feb 14  793.4278  -6.8640254
## Mar 14  787.0968  -6.7574337
## Apr 14  786.4199  -5.5413260
## May 14  783.8317  -4.9506923
## Jun 14  757.8402  -9.1588548
fitted(holt1)
##          Jan       Feb       Mar       Apr       May       Jun       Jul
## 1   901.8000  902.2644  911.5710  899.1105  894.2216  864.0371  849.5959
## 2   914.8231  894.8019  894.4022  894.1875  875.8025  850.3924  832.7915
## 3   858.0558  875.7276  886.0287  895.9803  916.4090  915.9861  915.1421
## 4   945.9353  946.7413  948.6927  947.5769  944.9895  964.8097  973.3354
## 5   972.8587  965.8025  937.8229  941.8729  942.8376  928.2934  934.1712
## 6   970.7884  964.7501  972.9424  965.6605  950.0472  942.4418  950.8452
## 7  1000.8260 1013.5358 1033.6576 1034.6216 1042.3933 1036.7631 1012.0046
## 8   979.2563  972.7208  972.2711  954.7282  953.4183  964.8942  959.5503
## 9   924.5619  947.1667  927.8194  892.5500  920.1586  891.9845  911.6642
## 10  906.0140  937.0953  916.5235  856.9542  862.9901  857.5972  880.5783
## 11  991.4248  977.3026  994.7033  947.2517  910.0603  885.9532  846.1553
## 12  874.1390  845.7479  852.8172  858.4187  844.8624  819.4052  814.3961
## 13  852.8679  846.1326  842.3899  856.5150  835.6320  842.1063  848.3665
## 14  818.3445  800.0591  786.5638  780.3393  780.8785  778.8810          
##          Aug       Sep       Oct       Nov       Dec
## 1   852.0635  877.0845  908.8511  919.5083  918.8784
## 2   831.5991  806.4152  800.2432  806.3219  850.4925
## 3   912.7234  911.5127  920.9709  922.2544  926.8128
## 4   970.3788  959.4901  944.0388  941.0751  959.5247
## 5   920.8009  916.6227  922.3995  948.3005  965.9574
## 6   945.3203  929.9176  938.7799  944.6628  982.3125
## 7  1020.9833 1048.9962 1046.5529 1032.2702 1006.6968
## 8   920.3151  940.0192  926.5762  951.7650  961.7043
## 9   886.7418  873.6659  883.4497  865.9922  878.4043
## 10  896.9108  888.6732  926.5339  952.7567  980.2801
## 11  808.9708  818.8266  802.3695  804.1598  834.9675
## 12  848.5853  852.7007  878.4837  892.9597  885.7221
## 13  819.6143  811.6376  796.9434  844.3289  845.1037
## 14
holt1$mean
##         Jul      Aug      Sep      Oct      Nov
## 14 748.6813 739.5225 730.3636 721.2048 712.0459

Plotting forecasts

# Simple Exponential Smoothing
plot(ses1, ylab="Dow Jones Closing Price",  xlab="Year", main="", fcol="white", type="o")
lines(fitted(ses1), col="blue", type="o")
lines(fitted(ses2), col="red", type="o")
lines(fitted(ses3), col="green", type="o")
lines(ses1$mean, col="blue", type="o")
lines(ses2$mean, col="red", type="o")
lines(ses3$mean, col="green", type="o")
legend("topleft",lty=1, col=c(1,"blue","red","green"), c("data", expression(alpha == 0.2),              expression(alpha == 0.6), expression(alpha == 0.89)),pch=1)

# Holt Winter Method with Linear/ Exp/ Damped Trend. 
plot(holt2, type="o", ylab="Dow Jones Closing Price ", xlab="Year", fcol="white", plot.conf=FALSE)
## Warning in plot.window(xlim, ylim, log, ...): "plot.conf" is not a
## graphical parameter
## Warning in title(main = main, xlab = xlab, ylab = ylab, ...): "plot.conf"
## is not a graphical parameter
## Warning in axis(1, ...): "plot.conf" is not a graphical parameter
## Warning in axis(2, ...): "plot.conf" is not a graphical parameter
## Warning in box(...): "plot.conf" is not a graphical parameter
lines(fitted(holt1), col="blue") 
lines(fitted(holt2), col="red")
# lines(fitted(holt3), col="green")
lines(holt1$mean, col="blue", type="o") 
lines(holt2$mean, col="red", type="o")
# lines(holt3$mean, col="green", type="o")
legend("topleft", lty=1, col=c("black","blue","red"), # ,"green"
   c("Data","Holt's linear trend","Exponential trend"))   # ,"Additive damped trend"

Which ones better ?

ses3.me<-mean(na.omit(ses3$residuals))
ses3.mae<-mean(na.omit(abs(ses3$residuals)))
ses3.mse<-mean(na.omit(ses3$residuals)^2)
ses3.rmse<-sqrt(ses3.mse)
ses3.mape<-mean(abs(na.omit(ses3$residuals/ses3$x)))

holt1.me<-mean(na.omit(holt1$residuals))
holt1.mae<-mean(na.omit(abs(holt1$residuals)))
holt1.mse<-mean(na.omit(holt1$residuals)^2)
holt1.rmse<-sqrt(holt1.mse)
holt1.mape<-mean(abs(na.omit(holt1$residuals/holt1$x)))

r<-c("Mean Error","Mean Absolute Error", "Mean Squared Error", "Root Mean Sq Error", "Mean Absolute Percentage Error")
m<-c(ses3.me,ses3.mae,ses3.mse,ses3.rmse,ses3.mape)
a<-c(holt1.me,holt1.mae,holt1.mse,holt1.rmse,holt1.mape)
bv<-c("Bias","Variance","Variance","Bias","Variance")
result<- data.frame(cbind(m,a,bv))
row.names(result)<-r
result
##                                                 m                  a
## Mean Error                     -0.878700375040265 -0.801267545602839
## Mean Absolute Error              15.4264590754452   16.5954338314235
## Mean Squared Error                389.39943276017   444.779962363359
## Root Mean Sq Error               19.7332063476813   21.0898070726918
## Mean Absolute Percentage Error 0.0171491707785437 0.0184287544298628
##                                      bv
## Mean Error                         Bias
## Mean Absolute Error            Variance
## Mean Squared Error             Variance
## Root Mean Sq Error                 Bias
## Mean Absolute Percentage Error Variance

Results/Conclusions
Looking at the residual plots in this case I believe the Holts’ Linear Trend model (in blue) fits in the best. However, with respect to the error term both seem at par for Holts’ Linear Trend and Simple exponential with high alpha high value(0.9).