6.9 question 7

library(forecast)
## Warning: package 'forecast' was built under R version 3.4.4
library(fma)
library(fpp)
## Loading required package: expsmooth
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 3.4.4
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: tseries
## Warning: package 'tseries' was built under R version 3.4.4
writing
##           Jan      Feb      Mar      Apr      May      Jun      Jul
## 1968  562.674  599.000  668.516  597.798  579.889  668.233  499.232
## 1969  634.712  639.283  712.182  621.557  621.000  675.989  501.322
## 1970  646.783  658.442  712.906  687.714  723.916  707.183  629.000
## 1971  676.155  748.183  810.681  729.363  701.108  790.079  594.621
## 1972  747.636  773.392  813.788  766.713  728.875  749.197  680.954
## 1973  795.337  788.421  889.968  797.393  751.000  821.255  691.605
## 1974  843.038  847.000  941.952  804.309  840.307  871.528  656.330
## 1975  778.139  856.075  938.833  813.023  783.417  828.110  657.311
## 1976  895.217  856.075  893.268  875.000  835.088  934.595  832.500
## 1977  875.024  992.968  976.804  968.697  871.675 1006.852  832.037
##           Aug      Sep      Oct      Nov      Dec
## 1968  215.187  555.813  586.935  546.136  571.111
## 1969  220.286  560.727  602.530  626.379  605.508
## 1970  237.530  613.296  730.444  734.925  651.812
## 1971  230.716  617.189  691.389  701.067  705.777
## 1972  241.424  680.234  708.326  694.238  772.071
## 1973  290.655  727.147  868.355  812.390  799.556
## 1974  370.508  742.000  847.152  731.675  898.527
## 1975  310.032  780.000  860.000  780.000  807.993
## 1976  300.000  791.443  900.000  781.729  880.000
## 1977  345.587  849.528  913.871  868.746  993.733
tsdata<- ts(writing ,frequency = 12)
hist(tsdata)#here, based on the histogram, it looks like normal distribution. Based on this, my guess is that I do not need to use box-cox transfomation. However, I'm still going to run twice to verify my guess. 

naive method without using box-cox transformation

fcast1<-stlf(writing,method="naive",lambda = FALSE)
plot(fcast1)

acc1=accuracy(fcast1)

rwdrift method without using box-cox transformation

fcast2<-stlf(writing,method="rwdrift",lambda = FALSE)
plot(fcast2)

acc2=accuracy(fcast2)

Adding lambda using box-cox transformation

fcast3<-stlf(writing,method="naive", lambda=TRUE)
plot(fcast3)

fcast4<- stlf(writing, method="rwdrift",lambda=TRUE)
plot(fcast4)

acc3<-accuracy(fcast3)
acc4<-accuracy(fcast4)

Compare the result

combine=rbind(acc1,acc2,acc3,acc4)
combine
##                        ME     RMSE      MAE        MPE     MAPE      MASE
## Training set 3.475443e+00 45.40980 34.81644  0.2482138 5.109330 0.7121799
## Training set 4.848273e-02 45.38429 34.79116 -0.2322143 5.117906 0.7116628
## Training set 3.537169e+00 46.69230 37.81126  0.3325985 6.197585 0.7734397
## Training set 3.768864e-13 46.55813 37.83434 -0.2183412 6.217677 0.7739118
##                    ACF1
## Training set -0.5447733
## Training set -0.5447738
## Training set -0.5434606
## Training set -0.5434606
#Comparing the models, model 2 has the lowest MAE and MASE value, model 2 is the best model
#the best model is using method "rwdrift", without using box-cox transformation
#it proves my guess eariler was right, I do not need to use box-cox transformation here 

6.9 question 8

fancy
##            Jan       Feb       Mar       Apr       May       Jun       Jul
## 1987   1664.81   2397.53   2840.71   3547.29   3752.96   3714.74   4349.61
## 1988   2499.81   5198.24   7225.14   4806.03   5900.88   4951.34   6179.12
## 1989   4717.02   5702.63   9957.58   5304.78   6492.43   6630.80   7349.62
## 1990   5921.10   5814.58  12421.25   6369.77   7609.12   7224.75   8121.22
## 1991   4826.64   6470.23   9638.77   8821.17   8722.37  10209.48  11276.55
## 1992   7615.03   9849.69  14558.40  11587.33   9332.56  13082.09  16732.78
## 1993  10243.24  11266.88  21826.84  17357.33  15997.79  18601.53  26155.15
##            Aug       Sep       Oct       Nov       Dec
## 1987   3566.34   5021.82   6423.48   7600.60  19756.21
## 1988   4752.15   5496.43   5835.10  12600.08  28541.72
## 1989   8176.62   8573.17   9690.50  15151.84  34061.01
## 1990   7979.25   8093.06   8476.70  17914.66  30114.41
## 1991  12552.22  11637.39  13606.89  21822.11  45060.69
## 1992  19888.61  23933.38  25391.35  36024.80  80721.71
## 1993  28586.52  30505.41  30821.33  46634.38 104660.67
tsdata2<- ts(fancy ,frequency = 12)
hist(tsdata2)#from histrogram, it's hard to tell whether or not should I use box-cox here, so I will run twice to verify the result

naive method without using box-cox transformation

fcast1f<-stlf(fancy,method="naive",lambda = FALSE)
plot(fcast1f)

acc1f=accuracy(fcast1f)

rwdrift method without using box-cox transformation

fcast2f<-stlf(fancy, method="rwdrift",lambda = FALSE)
plot(fcast2f)

acc2f=accuracy(fcast2f)

adding box-cox transformation with both naive method and rwdrift method

fcast3f<-stlf(fancy,method="naive", lambda=TRUE)
plot(fcast3f)

fcast4f<- stlf(fancy, method="rwdrift",lambda=TRUE)
plot(fcast4f)

acc3f<-accuracy(fcast3f)
acc4f<-accuracy(fcast4f)

Compare the result

combinef=rbind(acc1f,acc2f,acc3f,acc4f)
combinef
##                         ME     RMSE      MAE       MPE     MAPE      MASE
## Training set  3.860377e+02 2129.968 1488.218  1.372862 12.42621 0.3362916
## Training set  3.637622e+00 2038.948 1470.163 -1.305388 12.72765 0.3322117
## Training set  7.362193e+02 6151.814 3137.640 11.255325 34.54275 0.7090103
## Training set -5.564859e-11 6107.602 3184.904  2.090983 35.99669 0.7196905
##                    ACF1
## Training set -0.3328497
## Training set -0.3327513
## Training set -0.2027257
## Training set -0.2027257
#by comparing MAE, MASE, model 2 has the lowest number which means model 2 is the best model
#the best model is using method of "rwdrift", and does not requried box- cox tranformation