## Time-Series [1:188] from 1980 to 1996: 76378 71947 33873 96428 105084 ...
## [1] forecast for next 4 months:
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Sep 1995 98816.41 85605.43 112027.4 78611.97 119020.8
## Oct 1995 98816.41 85034.52 112598.3 77738.83 119894.0
## Nov 1995 98816.41 84486.34 113146.5 76900.46 120732.4
## Dec 1995 98816.41 83958.37 113674.4 76092.99 121539.8
## 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
## Mar Apr May Jun Jul Aug
## 1995 106723 84307 114896 106749 87892 100506
| min. based on full residuals | 78679.9711418255 |
| max. based on full residuals | 118952.848858174 |
| min. based on last three years’ residuals | 78969.952246465 |
| max. based on last three years’ residuals | 118662.867753535 |
## 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"
| paperbacks | 33.637686782912 |
| hardcovers | 31.9310149844547 |
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 31 209.4668 166.6035 252.3301 143.9130 275.0205
## 32 210.7177 167.8544 253.5811 145.1640 276.2715
## 33 211.9687 169.1054 254.8320 146.4149 277.5225
## 34 213.2197 170.3564 256.0830 147.6659 278.7735
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 31 250.1739 212.7390 287.6087 192.9222 307.4256
## 32 253.4765 216.0416 290.9113 196.2248 310.7282
## 33 256.7791 219.3442 294.2140 199.5274 314.0308
## 34 260.0817 222.6468 297.5166 202.8300 317.3334
| paperbacks-Holt linear | 31.1369230162347 |
| hardcovers-Holt linear | 27.1935779818511 |
## Mar Apr May Jun Jul Aug
## 1995 106723 84307 114896 106749 87892 100506
| min. for paperbacks | 147.839044920336 |
| min. for paperbacks | 271.094555079664 |
| min. for hardcovers | 195.963995925 |
| max. for hardcovers | 304.383804075 |
## [1] "paperbacks"
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 31 209.4668 166.6035 252.3301 143.9130 275.0205
## 32 210.7177 167.8544 253.5811 145.1640 276.2715
## 33 211.9687 169.1054 254.8320 146.4149 277.5225
## 34 213.2197 170.3564 256.0830 147.6659 278.7735
## [1] "hardcovers"
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 31 250.1739 212.7390 287.6087 192.9222 307.4256
## 32 253.4765 216.0416 290.9113 196.2248 310.7282
## 33 256.7791 219.3442 294.2140 199.5274 314.0308
## 34 260.0817 222.6468 297.5166 202.8300 317.3334
## [1] 0.3956183
| no modification | 0.0449908695633474 |
| high damping | 3.31114451284137 |
| low damping | 1.19650419858032 |
| Box-Cox low lambda | 1.13857756677201e-06 |
| Box-Cox high lambda | 45.1115980951156 |
| Box-Cox MLE lambda | 0.00247085256848212 |
## [1] RMSE for undamped Holt-Winter multiplicative model
## [1] 5.431613e-05
## [1] RMSE for damped Holt-Winter multiplicative model
## [1] 0.009815399
## [1] 2.86398
## ME RMSE MAE MPE MAPE MASE
## Training set -0.01025188 0.9534696 0.7324588 -0.4347743 4.952780 0.5071923
## Test set -2.36907333 2.8639797 2.5062068 -8.7973452 9.303342 1.7354270
## ACF1 Theil's U
## Training set 0.01839391 NA
## Test set 0.65462182 0.5562065
## [1] 1.24733
## [1] "RMSE for STL model"
## [1] 3.304199
## ETS(M,N,A)
##
## Call:
## ets(y = turnover.train, model = "ZZA")
##
## Smoothing parameters:
## alpha = 0.4512
## gamma = 0.3094
##
## Initial states:
## l = 5.2379
## s = -0.1283 -1.5478 -1.2293 2.2136 0.2006 -0.0526
## -0.3004 -0.4622 0.0403 -0.1608 0.8024 0.6247
##
## sigma: 0.0687
##
## AIC AICc BIC
## 2021.343 2022.802 2078.997
## [1] 1.879095
## [1] 1.550265
str(pigs) exp_smooth<-ses(pigs,h=4) full_resids_stdev<-sd(exp_smooth\(residuals) recent_three_stdev<-sd(window(exp_smooth\)residuals, start=c(1992,9))) #full_resids_stdev #recent_three_stdev noquote(‘forecast for next 4 months:’) exp_smooth exp_smooth$model
ninety_five_ci_estimate_table<-c(“min. based on full residuals”,“max. based on full residuals”,“min. based on last three years’ residuals”,“max. based on last three years’ residuals”) ninety_five_ci_estimate_table<-cbind(ninety_five_ci_estimate_table,ninety_five_ci_estimate_table) colnames(ninety_five_ci_estimate_table)<-c(‘’,’’) ninety_five_ci_estimate_table[1,2]<-(98816.41-1.96full_resids_stdev) ninety_five_ci_estimate_table[2,2]<-(98816.41+1.96full_resids_stdev) ninety_five_ci_estimate_table[3,2]<-(98816.41-1.96recent_three_stdev) ninety_five_ci_estimate_table[4,2]<-(98816.41+1.96recent_three_stdev) tail(pigs) kable(ninety_five_ci_estimate_table, “html”) %>% kable_styling(“striped”, full_width = F) %>% column_spec(1, bold = T, color = “white”, background = “#73b587”) %>% column_spec(2, bold = T, color = “#73b587”, background = “white”)
str(books) books<-ts(books) autoplot(books)+ theme(axis.text.x = element_text(angle = 30, hjust = .9),text = element_text(family = “corben”,color=‘#249382’,size=16))+ theme(panel.background = element_rect(fill = ‘#f4f4ef’),panel.grid.major = element_blank(), panel.grid.minor = element_blank())+xlab(‘’) paperback_ses<-ses(books[,1],h=4) hardcover_ses<-ses(books[,2],h=4) #plot ses for softback,hardcover autoplot(paperback_ses) + autolayer(fitted(paperback_ses), series=“Fitted”)+ ylab(“paperbacks smoothed”) + xlab(“”)+ylim(50,350)+ theme(panel.background = element_rect(fill =’#f4f4ef’))+ theme(axis.text.x = element_text(angle = 30, hjust = .9),text = element_text(family = “corben”,color=‘#656aa0’,size=16))+ggtitle(‘simple exponential smoothing’)
autoplot(hardcover_ses) + autolayer(fitted(hardcover_ses), series=“Fitted”)+ ylab(“hardcovers smoothed”) + xlab(“”)+ylim(50,350)+ theme(panel.background = element_rect(fill = ‘#f4f4ef’))+ theme(axis.text.x = element_text(angle = 30, hjust = .9),text = element_text(family = “corben”,color=‘#656aa0’,size=16))+ggtitle(‘simple exponential smoothing’)
#RMSE for paperback,hardcover ses rmse_table<-c(“paperbacks”,“hardcovers”) rmse_table<-cbind(rmse_table,rmse_table) colnames(rmse_table)<-c(‘’,’’) rmse_table[1,2]<-sqrt(mean(paperback_ses\(residuals^2)) rmse_table[2,2]<-sqrt(mean(hardcover_ses\)residuals^2)) kable(rmse_table, “html”) %>% kable_styling(“striped”, full_width = F) %>% column_spec(1, bold = T, color = “white”, background = “#73b587”) %>% column_spec(2, bold = T, color = “#73b587”, background = “white”)
paperback_holt <- holt(books[,1], h=4) hardcover_holt <- holt(books[,2], h=4)
paperback_holt hardcover_holt
rmse_table<-c(“paperbacks-Holt linear”,“hardcovers-Holt linear”) rmse_table<-cbind(rmse_table,rmse_table) colnames(rmse_table)<-c(‘’,’’) rmse_table[1,2]<-sqrt(mean(paperback_holt\(residuals^2)) rmse_table[2,2]<-sqrt(mean(hardcover_holt\)residuals^2)) kable(rmse_table, “html”) %>% kable_styling(“striped”, full_width = F) %>% column_spec(1, bold = T, color = “white”, background = “#73b587”) %>% column_spec(2, bold = T, color = “#73b587”, background = “white”)
autoplot(hardcover_ses) + autolayer(fitted(paperback_holt), series=“Fitted”)+ ylab(“paperbacks smoothed”) + xlab(“”)+ylim(50,350)+ theme(panel.background = element_rect(fill = ‘#f4f4ef’))+ theme(axis.text.x = element_text(angle = 30, hjust = .9),text = element_text(family = “corben”,color=‘#656aa0’,size=16))+ggtitle(“Holt’s linear”) autoplot(hardcover_ses) + autolayer(fitted(hardcover_holt), series=“Fitted”)+ ylab(“hardcovers smoothed”) + xlab(“”)+ylim(50,350)+ theme(panel.background = element_rect(fill = ‘#f4f4ef’))+ theme(axis.text.x = element_text(angle = 30, hjust = .9),text = element_text(family = “corben”,color=‘#656aa0’,size=16))+ggtitle(“Holt’s linear”)
pback_resids_stdev<-sd(paperback_holt\(residuals) hcover_resids_stdev<-sd(hardcover_holt\)residuals)
holt_estimate_table<-c(“min. for paperbacks”,“min. for paperbacks”,“min. for hardcovers”,“max. for hardcovers”) holt_estimate_table<-cbind(holt_estimate_table,holt_estimate_table) colnames(holt_estimate_table)<-c(‘’,’‘) holt_estimate_table[1,2]<-209.4668-1.96pback_resids_stdev holt_estimate_table[2,2]<-209.4668+1.96pback_resids_stdev holt_estimate_table[3,2]<-250.1739-1.96hcover_resids_stdev holt_estimate_table[4,2]<-250.1739+1.96hcover_resids_stdev tail(pigs) kable(holt_estimate_table, “html”) %>% kable_styling(“striped”, full_width = F) %>% column_spec(1, bold = T, color = “white”, background = “#73b587”) %>% column_spec(2, bold = T, color = “#73b587”, background = “white”) print(’paperbacks’) paperback_holt print(‘hardcovers’) hardcover_holt
autoplot(eggs) holt_no_mod<-holt(eggs,h=100) autoplot(holt_no_mod)+xlab(“Holt’s linear”)+ylab(‘’)+ggtitle(’‘) holt_high_damping<-holt(eggs, damped=TRUE, phi = 0.8,h=100) autoplot(holt_high_damping)+xlab(“Holt’s damped 80%”)+ylab(’‘)+ theme(axis.text.x = element_text(angle = 30, hjust = .9),text = element_text(family = “corben”,color=’#249382’,size=16))+ggtitle(‘’) holt_low_damping<-holt(eggs, damped=TRUE, phi = 0.98,h=100) autoplot(holt_low_damping)+xlab(“Holt’s damped 98%”)+ylab(’‘)+ theme(axis.text.x = element_text(angle = 30, hjust = .9),text = element_text(family = “corben”,color=’#249382’,size=16))+ggtitle(’’) holt_low_lambda<-holt(eggs, lambda=-2, h=100)
autoplot(holt_low_lambda)+xlab(“Holt’s - lambda -2”)+ylab(‘’)+ theme(axis.text.x = element_text(angle = 30, hjust = .9),text = element_text(family = “corben”,color=’#249382’,size=16))+ggtitle(‘’) holt_high_lambda<-holt(eggs, lambda=1.9, h=100) autoplot(holt_high_lambda)+xlab(“Holt’s - lambda 1.9”)+ylab(’‘)+ theme(axis.text.x = element_text(angle = 30, hjust = .9),text = element_text(family = “corben”,color=’#249382’,size=16))+ggtitle(‘’) BoxCox.lambda(eggs) holt_MLE_lambda<-holt(eggs, lambda=.3956, h=100) autoplot(holt_MLE_lambda)+xlab(“Holt’s - lambda optimal”)+ylab(’‘)+ theme(axis.text.x = element_text(angle = 30, hjust = .9),text = element_text(family = “corben”,color=’#249382’,size=16))+ggtitle(‘’) #compute the RMSE boxcox_damp_rmse_table<-c(’no modification’,‘high damping’,‘low damping’,‘Box-Cox low lambda’,‘Box-Cox high lambda’,‘Box-Cox MLE lambda’) boxcox_damp_rmse_table<-cbind(boxcox_damp_rmse_table,boxcox_damp_rmse_table) colnames(boxcox_damp_rmse_table)<-c(‘’,’’)
boxcox_damp_rmse_table[1,2]<-sqrt(mean(holt_no_mod\(residuals)^2) boxcox_damp_rmse_table[2,2]<-sqrt(mean(holt_high_damping\)residuals)2) boxcox_damp_rmse_table[3,2]<-sqrt(mean(holt_low_damping\(residuals)^2) boxcox_damp_rmse_table[4,2]<-sqrt(mean(holt_low_lambda\)residuals)2) boxcox_damp_rmse_table[5,2]<-sqrt(mean(holt_high_lambda\(residuals)^2) boxcox_damp_rmse_table[6,2]<-sqrt(mean(holt_MLE_lambda\)residuals)^2) kable(boxcox_damp_rmse_table, “html”) %>% kable_styling(“striped”, full_width = F) %>% column_spec(1, bold = T, color = “white”, background = “#73b587”) %>% column_spec(2, bold = T, color = “#73b587”, background = “white”)
retaildata <- readxl::read_excel(“C:/Users/dawig/Desktop/Data624/retail.xlsx”, skip=1) turnover <- ts(retaildata[,“A3349608L”], frequency=12, start=c(1982,4)) autoplot(turnover, ylab=“turnover”, xlab=“”,color=‘#249382’)+ theme(panel.background = element_rect(fill = ‘#efeae8’))+ theme(axis.text.x = element_text(angle = 30, hjust = .9),text = element_text(family = “corben”,color=‘#249382’,size=16)) #check seasonality to see if multiplicative smoothing is useful autoplot(seas(turnover)) + xlab(“Year”) + ggtitle(“Seasonal Adjustment”) autoplot(hw(turnover,seasonal=‘multiplicative’), ylab=“holt winter multiplicative”, xlab=“”,color=‘#249382’)+ theme(panel.background = element_rect(fill = ‘#efeae8’))+ theme(axis.text.x = element_text(angle = 30, hjust = .9),text = element_text(family = “corben”,color=‘#249382’,size=16))
autoplot(hw(turnover,seasonal=“multiplicative”,damped=TRUE,phi=.8,h=50),xlab=(‘’),ylab=(’damped, 50 periods’))+ theme(axis.text.x = element_text(angle = 30, hjust = .9),text = element_text(family = “corben”,color=‘#249382’,size=16)) noquote(‘RMSE for undamped Holt-Winter multiplicative model’) sqrt(mean(hw(turnover,seasonal=“multiplicative”,h=1)\(residuals)^2) noquote('RMSE for damped Holt-Winter multiplicative model') sqrt(mean(hw(turnover,seasonal="multiplicative",damped=TRUE,phi=.8,h=1)\)residuals)^2)
autoplot(hw(turnover,seasonal=“multiplicative”,h=1)$residuals,xlab=‘residuals’,ylab=‘’)+ theme(axis.text.x = element_text(angle = 30, hjust = .9),text = element_text(family = “corben”,color=’#249382’,size=16))
turnover.train <- window(turnover, end=c(2010,12)) turnover.test <- window(turnover, start=2011) accuracy(hw(turnover.train,seasonal=“multiplicative”),turnover.test)[4] accuracy(hw(turnover.train,seasonal=“multiplicative”),turnover.test) fc <- snaive(turnover.train) accuracy(fc,turnover.test)[4] RMSE_set<-rep(0,6) RMSE_set[4]<-accuracy(hw(turnover.train,seasonal=“multiplicative”),turnover.test)[4]
turnover.train <- window(turnover, end=c(2009,6)) turnover.test <- window(turnover, start=c(2009,7)) fc <- hw(turnover.train,seasonal=“multiplicative”) RMSE_set[1]<-accuracy(fc,turnover.test)[4]
turnover.train <- window(turnover, end=c(2009,12)) turnover.test <- window(turnover, start=2010) fc <- hw(turnover.train,seasonal=“multiplicative”) RMSE_set[2]<-accuracy(fc,turnover.test)[4]
turnover.train <- window(turnover, end=c(2010,6)) turnover.test <- window(turnover, start=c(2010,7)) fc <- hw(turnover.train,seasonal=“multiplicative”) RMSE_set[3]<-accuracy(fc,turnover.test)[4]
turnover.train <- window(turnover, end=c(2011,6)) turnover.test <- window(turnover, start=c(2011,7)) fc <-hw(turnover.train,seasonal=“multiplicative”) RMSE_set[5]<-accuracy(fc,turnover.test)[4]
turnover.train <- window(turnover, end=c(2012,12)) turnover.test <- window(turnover, start=2013) fc <- hw(turnover.train,seasonal=“multiplicative”) RMSE_set[6]<-accuracy(fc,turnover.test)[4]
ggplot()+geom_line(aes(y=RMSE_set,x=seq_along(RMSE_set)),size=1.5,color=‘#3dc666’)+scale_x_discrete(limits=c(2,4,6), labels=c(“Dec. 2009”,“Dec. 2010”,“Dec. 2011”))+labs(y=‘RMSE’,x=‘train/test split’)+theme(text = element_text(family = “corben”,color=‘#3dc666’,size=20))+ggtitle(‘HW multiplicative’)
lambda_input<-BoxCox.lambda(turnover) turnover.train <- window(turnover, end=c(2010,12)) turnover.test <- window(turnover, start=2011) stl_model<-stlf(turnover.train,lambda=lambda_input) print(‘RMSE for STL model’) accuracy(stl_model,turnover.test)[4] RMSE_set[4]<-accuracy(stl_model,turnover.test)[4] #———–
turnover.train <- window(turnover, end=c(2009,6)) turnover.test <- window(turnover, start=c(2009,7)) fc <- stlf(turnover.train,lambda=lambda_input) RMSE_set[1]<-accuracy(fc,turnover.test)[4]
turnover.train <- window(turnover, end=c(2009,12)) turnover.test <- window(turnover, start=2010) fc <- stlf(turnover.train,lambda=lambda_input) RMSE_set[2]<-accuracy(fc,turnover.test)[4]
turnover.train <- window(turnover, end=c(2010,6)) turnover.test <- window(turnover, start=c(2010,7)) fc <- stlf(turnover.train,lambda=lambda_input) RMSE_set[3]<-accuracy(fc,turnover.test)[4]
turnover.train <- window(turnover, end=c(2011,6)) turnover.test <- window(turnover, start=c(2011,7)) fc <-stlf(turnover.train,lambda=lambda_input) RMSE_set[5]<-accuracy(fc,turnover.test)[4]
turnover.train <- window(turnover, end=c(2012,12)) turnover.test <- window(turnover, start=2013) fc <- stlf(turnover.train,lambda=lambda_input) RMSE_set[6]<-accuracy(fc,turnover.test)[4]
ggplot()+geom_line(aes(y=RMSE_set,x=seq_along(RMSE_set)),size=1.5,color=‘#3dc666’)+scale_x_discrete(limits=c(2,4,6), labels=c(“Dec. 2009”,“Dec. 2010”,“Dec. 2011”))+labs(y=‘RMSE’,x=‘train/test split’)+theme(text = element_text(family = “corben”,color=‘#3dc666’,size=20))+ggtitle(‘STL with Box-Cox model’)
turnover.train <- window(turnover, end=c(2010,12)) turnover.test <- window(turnover, start=2011) ets_mult_fit<-ets(turnover.train, model=“ZZA”) ets_mult_fit
ets_mult_fit %>% forecast(h=36)->ets_predict accuracy(ets_predict,turnover.test)[4] RMSE_set[4]<-accuracy(ets_predict,turnover.test)[4]
turnover.train <- window(turnover, end=c(2009,6)) turnover.test <- window(turnover, start=c(2009,7)) ets_mult_fit<-ets(turnover.train, model=“ZZA”) ets_mult_fit %>% forecast(h=36)->ets_predict RMSE_set[1]<-accuracy(ets_predict,turnover.test)[4]
turnover.train <- window(turnover, end=c(2009,12)) turnover.test <- window(turnover, start=2010) ets_mult_fit<-ets(turnover.train, model=“ZZA”) ets_mult_fit %>% forecast(h=36)->ets_predict RMSE_set[2]<-accuracy(ets_predict,turnover.test)[4]
turnover.train <- window(turnover, end=c(2010,6)) turnover.test <- window(turnover, start=c(2010,7)) ets_mult_fit<-ets(turnover.train, model=“ZZA”) ets_mult_fit %>% forecast(h=36)->ets_predict RMSE_set[3]<-accuracy(ets_predict,turnover.test)[4] accuracy(ets_predict,turnover.test)[4]
turnover.train <- window(turnover, end=c(2011,6)) turnover.test <- window(turnover, start=c(2011,7)) ets_mult_fit<-ets(turnover.train, model=“ZZA”) ets_mult_fit %>% forecast(h=36)->ets_predict RMSE_set[5]<-accuracy(ets_predict,turnover.test)[4]
turnover.train <- window(turnover, end=c(2012,12)) turnover.test <- window(turnover, start=2013) ets_mult_fit<-ets(turnover.train, model=“ZZA”) ets_mult_fit %>% forecast(h=36)->ets_predict RMSE_set[6]<-accuracy(ets_predict,turnover.test)[4]
ggplot()+geom_line(aes(y=RMSE_set,x=seq_along(RMSE_set)),size=1.5,color=‘#3dc666’)+scale_x_discrete(limits=c(2,4,6), labels=c(“Dec. 2009”,“Dec. 2010”,“Dec. 2011”))+labs(y=‘RMSE’,x=‘train/test split’)+theme(text = element_text(family = “corben”,color=‘#3dc666’,size=20))+ggtitle(‘Additive ETS model’)