Due Date: October 18, 2021

Instructions

Please complete each problem below to the best of your ability. Where plain text is required, you can type directly into the .RMD file. Where code and output is required, be sure to include all code in the code chunks provided. The assignment must be submitted, via email, as both the .RMD file and the knitted file (whether .html or .pdf, whichever is best for yourself)

Problem 1

Complete Chapter 3, Exercise 1 from the textbook.

Part 1A

wine <- read.csv2('C:/CS-637-Time-Series-and-Forecasting--main/Lecture Notes/data/wine.csv')

#Describe the association and calculate the ccf between x and y for k
#equal to 1, 10, and 100.


w <- 1:100
k <- 100
x <- w + k * rnorm(100)
y <- w + k * rnorm(100)
ccf(x, y)

#When K is equal to 1 and 10, there is significant cross correlation between the #two time series for all lags, but this is not the case when k is equal to 100. #When this is the case, there is only cross correlation in a few lags.

Part 1B

Time <- 1:370
x <- sin(2 * pi * Time / 37)
y <- sin(2 * pi * (Time + 4) / 37)

ccf(x, y)

#Investigate the effect of adding independent random variation to x
#and y.

#There seems to be an identifiable pattern that is seen when looking at the cross correlation of the two time series. There seems to be a cross correlation throughout the entire time series.

Problem 2

#Complete Chapter 3, Exercise 6 from the textbook.Refer to the sweet white wine sales (§3.4.2).

#a) Use the HoltWinters procedure with and # set to 0.2 and compare #the SS1PE with the minimum obtained with R.

wine = read.csv(file ="C:/CS-637-Time-Series-and-Forecasting--main/Lecture Notes/data/wine.csv")

sweetw.ts <- ts(wine$sweetw, start = c(1980,1), freq = 12)

sweetw.hw <- HoltWinters (sweetw.ts, seasonal="mult")
sweetw.hw$SSE
## [1] 477693.9
sweet.hw1 <- HoltWinters(sweetw.ts, alpha = 0.2, beta = 0.2, gamma = 0.2)
sweet.hw1$SSE
## [1] 693521.9
sweet.hw1
## Holt-Winters exponential smoothing with trend and additive seasonal component.
## 
## Call:
## HoltWinters(x = sweetw.ts, alpha = 0.2, beta = 0.2, gamma = 0.2)
## 
## Smoothing parameters:
##  alpha: 0.2
##  beta : 0.2
##  gamma: 0.2
## 
## Coefficients:
##           [,1]
## a   243.053682
## b     1.845197
## s1    5.008604
## s2   11.368582
## s3   16.449351
## s4   68.786461
## s5  147.452725
## s6  -58.058742
## s7  -27.811882
## s8  -43.420420
## s9  -28.713225
## s10 -50.389279
## s11 -52.451059
## s12  13.755825
plot(sweet.hw1)

plot (sweetw.hw$fitted)

#the SS1PE we get with α, β and γ set to 0.2 is larger than the minimum one.

#b) Use the HoltWinters procedure on the logarithms of sales and compare #SS1PE with that obtained using sales.

wine = read.csv(file ="C:/CS-637-Time-Series-and-Forecasting--main/Lecture Notes/data/wine.csv")

sweetw.ts <- ts(wine$sweetw, start = c(1980,1), freq = 12)

sweetw.hw2 <- HoltWinters (log(sweetw.ts), seasonal = "mult")
sweetw.hw2$SSE
## [1] 7.61344
#The SS1PE we get is 7.61344, which is much smaller than the one we obtained using sales.

#c) What is the SS1PE if you predict next month’s sales will equal this #month’s sales?

#d) This is rather harder: What is the SS1PE if you find the optimum , # and from the data available at each time step before making the #one-step-ahead prediction?

Problem 3

#Complete Chapter 3, Exercise 7 from the textbook. #Continue the following exploratory time series analysis using the global #temperature series from §1.4.5.

#a) Produce a time plot of the data. Plot the aggregated annual mean #series and a boxplot that summarises the observed values for each #season, and comment on the plots.

global = read.csv(file ="C:/CS-637-Time-Series-and-Forecasting--main/Lecture Notes/data/Global.csv")

global.month.ts <- ts(global,start = c(1856,1),end = c(2005,12),frequency = 12)
global.annual.ts <- aggregate(global.month.ts, FUN=mean)
plot(t(global.month.ts), col = 1:5)
legend("topleft", legend = 1:5, col = 1:5, lty = 1)

boxplot(global.month.ts~cycle(global.month.ts))

#I assume the top months are in the summer due to warm weather

#b) Decompose the series into the components trend, seasonal effect, and #residuals, and plot the decomposed series. Produce a plot of the trend #with a superimposed seasonal effect.

global = read.csv(file ="C:/CS-637-Time-Series-and-Forecasting--main/Lecture Notes/data/Global.csv")

global.month.ts <- ts(global,start = c(1856,1),end = c(2005,12),frequency = 12)


global.decom <- decompose(global.month.ts, type = "mult")
trend <- global.decom$trend
seasonal <- global.decom$seasonal



#c) Plot the correlogram of the residuals from question 7b. Comment on
#the plot, explaining any ‘significant’ correlations at significant lags.

acf(global.decom$random[7:138])

#d) Fit an appropriate Holt-Winters model to the monthly data. Explain #why you chose that particular Holt-Winters model, and give the parameter #estimates.

global.hw <- HoltWinters(global.month.ts, seasonal = 'multiplicative')
global.hw
## Holt-Winters exponential smoothing with trend and multiplicative seasonal component.
## 
## Call:
## HoltWinters(x = global.month.ts, seasonal = "multiplicative")
## 
## Smoothing parameters:
##  alpha: 0
##  beta : 0
##  gamma: 0.4507887
## 
## Coefficients:
##              [,1]
## a    1.519316e+02
## b    7.053613e-03
## s1   1.302271e-04
## s2  -2.158342e-04
## s3  -1.129493e-03
## s4  -1.516722e-03
## s5   3.726133e-05
## s6  -1.424666e-03
## s7  -6.094412e-04
## s8  -1.016526e-03
## s9  -3.857544e-04
## s10 -6.119797e-06
## s11 -9.527412e-04
## s12  5.217264e-04
#plot(global.hw)
plot(fitted(global.hw))

#e) Using the fitted model, forecast values for the years 2005–2010. Add #these forecasts to a time plot of the original series. Under what circumstances #would these forecasts be valid? What comments of caution #would you make to an economist or politician who wanted to #use these forecasts to make statements about the potential impact of #global warming on the world economy?

plot(fitted(global.hw))

library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo

global.predict <- predict(global.hw,n.head=4*12,prediction.interval = TRUE)
global.predict
##                 fit                     
## Jan 3656 0.01978653 0.4115822 -0.3720092
## Feb 3656 0.01978653 0.3981956 -0.3586226
## Mar 3656 0.01978653 0.3852249 -0.3456518
## Apr 3656 0.01978653 0.3822334 -0.3426604
## May 3656 0.01978653 0.3752431 -0.3356700
## Jun 3656 0.01978653 0.3679706 -0.3283975
## Jul 3656 0.01978653 0.3650230 -0.3254500
## Aug 3656 0.01978653 0.3702189 -0.3306459
## Sep 3656 0.01978653 0.3699011 -0.3303280
## Oct 3656 0.01978653 0.3787182 -0.3391451
## Nov 3656 0.01978653 0.3877984 -0.3482253
## Dec 3656 0.01978653 0.3713250 -0.3317519
## Jan 3657 0.01978653 0.3981956 -0.3586226
## Feb 3657 0.01978653 0.4188141 -0.3792411
## Mar 3657 0.01978653 0.3928396 -0.3532665
## Apr 3657 0.01978653 0.3848396 -0.3452665
## May 3657 0.01978653 0.3767113 -0.3371382
## Jun 3657 0.01978653 0.3709981 -0.3314251
## Jul 3657 0.01978653 0.3690654 -0.3294923
## Aug 3657 0.01978653 0.3746195 -0.3350464
## Sep 3657 0.01978653 0.3705967 -0.3310236
## Oct 3657 0.01978653 0.3810911 -0.3415180
## Nov 3657 0.01978653 0.3909436 -0.3513705
## Dec 3657 0.01978653 0.3789158 -0.3393427
## Jan 3658 0.01978653 0.3852249 -0.3456518
## Feb 3658 0.01978653 0.3928396 -0.3532665
## Mar 3658 0.01978653 0.4047493 -0.3651763
## Apr 3658 0.01978653 0.3856962 -0.3461231
## May 3658 0.01978653 0.3814479 -0.3418748
## Jun 3658 0.01978653 0.3719911 -0.3324181
## Jul 3658 0.01978653 0.3707285 -0.3311554
## Aug 3658 0.01978653 0.3748602 -0.3352871
## Sep 3658 0.01978653 0.3697480 -0.3301749
## Oct 3658 0.01978653 0.3846176 -0.3450446
## Nov 3658 0.01978653 0.3911438 -0.3515707
## Dec 3658 0.01978653 0.3805524 -0.3409794
## Jan 3659 0.01978653 0.3822334 -0.3426604
## Feb 3659 0.01978653 0.3848396 -0.3452665
## Mar 3659 0.01978653 0.3856962 -0.3461231
## Apr 3659 0.01978653 0.3850849 -0.3455118
## May 3659 0.01978653 0.3775698 -0.3379967
## Jun 3659 0.01978653 0.3682950 -0.3287219
## Jul 3659 0.01978653 0.3660712 -0.3264981
## Aug 3659 0.01978653 0.3702130 -0.3306399
## Sep 3659 0.01978653 0.3687650 -0.3291919
## Oct 3659 0.01978653 0.3795456 -0.3399726
## Nov 3659 0.01978653 0.3858032 -0.3462301
## Dec 3659 0.01978653 0.3729762 -0.3334032
## Jan 3660 0.01978653 0.3752431 -0.3356700
## Feb 3660 0.01978653 0.3767113 -0.3371382
## Mar 3660 0.01978653 0.3814479 -0.3418748
## Apr 3660 0.01978653 0.3775698 -0.3379967
## May 3660 0.01978653 0.3819751 -0.3424020
## Jun 3660 0.01978653 0.3671732 -0.3276001
## Jul 3660 0.01978653 0.3653300 -0.3257569
## Aug 3660 0.01978653 0.3687402 -0.3291671
## Sep 3660 0.01978653 0.3667447 -0.3271716
## Oct 3660 0.01978653 0.3780535 -0.3384804
## Nov 3660 0.01978653 0.3830710 -0.3434979
## Dec 3660 0.01978653 0.3740746 -0.3345016
## Jan 3661 0.01978653 0.3679706 -0.3283975
## Feb 3661 0.01978653 0.3709981 -0.3314251
## Mar 3661 0.01978653 0.3719911 -0.3324181
## Apr 3661 0.01978653 0.3682950 -0.3287219
## May 3661 0.01978653 0.3671732 -0.3276001
## Jun 3661 0.01978653 0.3666733 -0.3271003
## Jul 3661 0.01978653 0.3626053 -0.3230323
## Aug 3661 0.01978653 0.3646624 -0.3250894
## Sep 3661 0.01978653 0.3629872 -0.3234142
## Oct 3661 0.01978653 0.3728175 -0.3332445
## Nov 3661 0.01978653 0.3792231 -0.3396500
## Dec 3661 0.01978653 0.3705326 -0.3309596
## Jan 3662 0.01978653 0.3650230 -0.3254500
## Feb 3662 0.01978653 0.3690654 -0.3294923
## Mar 3662 0.01978653 0.3707285 -0.3311554
## Apr 3662 0.01978653 0.3660712 -0.3264981
## May 3662 0.01978653 0.3653300 -0.3257569
## Jun 3662 0.01978653 0.3626053 -0.3230323
## Jul 3662 0.01978653 0.3656184 -0.3260454
## Aug 3662 0.01978653 0.3654998 -0.3259267
## Sep 3662 0.01978653 0.3632044 -0.3236313
## Oct 3662 0.01978653 0.3724521 -0.3328790
## Nov 3662 0.01978653 0.3792074 -0.3396344
## Dec 3662 0.01978653 0.3696518 -0.3300788
## Jan 3663 0.01978653 0.3702189 -0.3306459
## Feb 3663 0.01978653 0.3746195 -0.3350464
## Mar 3663 0.01978653 0.3748602 -0.3352871
## Apr 3663 0.01978653 0.3702130 -0.3306399
## May 3663 0.01978653 0.3687402 -0.3291671
## Jun 3663 0.01978653 0.3646624 -0.3250894
## Jul 3663 0.01978653 0.3654998 -0.3259267
## Aug 3663 0.01978653 0.3726706 -0.3330976
## Sep 3663 0.01978653 0.3679454 -0.3283724
## Oct 3663 0.01978653 0.3786055 -0.3390324
## Nov 3663 0.01978653 0.3857595 -0.3461864
## Dec 3663 0.01978653 0.3750250 -0.3354520
## Jan 3664 0.01978653 0.3699011 -0.3303280
## Feb 3664 0.01978653 0.3705967 -0.3310236
## Mar 3664 0.01978653 0.3697480 -0.3301749
## Apr 3664 0.01978653 0.3687650 -0.3291919
## May 3664 0.01978653 0.3667447 -0.3271716
## Jun 3664 0.01978653 0.3629872 -0.3234142
## Jul 3664 0.01978653 0.3632044 -0.3236313
## Aug 3664 0.01978653 0.3679454 -0.3283724
## Sep 3664 0.01978653 0.3714469 -0.3318738
## Oct 3664 0.01978653 0.3798432 -0.3402701
## Nov 3664 0.01978653 0.3860855 -0.3465124
## Dec 3664 0.01978653 0.3737662 -0.3341931
## Jan 3665 0.01978653 0.3787182 -0.3391451
## Feb 3665 0.01978653 0.3810911 -0.3415180
## Mar 3665 0.01978653 0.3846176 -0.3450446
## Apr 3665 0.01978653 0.3795456 -0.3399726
## May 3665 0.01978653 0.3780535 -0.3384804
## Jun 3665 0.01978653 0.3728175 -0.3332445
## Jul 3665 0.01978653 0.3724521 -0.3328790
## Aug 3665 0.01978653 0.3786055 -0.3390324
## Sep 3665 0.01978653 0.3798432 -0.3402701
## Oct 3665 0.01978653 0.3986247 -0.3590516
## Nov 3665 0.01978653 0.4006732 -0.3611001
## Dec 3665 0.01978653 0.3882569 -0.3486839
## Jan 3666 0.01978653 0.3877984 -0.3482253
## Feb 3666 0.01978653 0.3909436 -0.3513705
## Mar 3666 0.01978653 0.3911438 -0.3515707
## Apr 3666 0.01978653 0.3858032 -0.3462301
## May 3666 0.01978653 0.3830710 -0.3434979
## Jun 3666 0.01978653 0.3792231 -0.3396500
## Jul 3666 0.01978653 0.3792074 -0.3396344
## Aug 3666 0.01978653 0.3857595 -0.3461864
## Sep 3666 0.01978653 0.3860855 -0.3465124
## Oct 3666 0.01978653 0.4006732 -0.3611001
## Nov 3666 0.01978653 0.4219599 -0.3823868
## Dec 3666 0.01978653 0.3995440 -0.3599710
## Jan 3667 0.01978653 0.3713250 -0.3317519
## Feb 3667 0.01978653 0.3789158 -0.3393427
## Mar 3667 0.01978653 0.3805524 -0.3409794
## Apr 3667 0.01978653 0.3729762 -0.3334032
## May 3667 0.01978653 0.3740746 -0.3345016
## Jun 3667 0.01978653 0.3705326 -0.3309596
## Jul 3667 0.01978653 0.3696518 -0.3300788
## Aug 3667 0.01978653 0.3750250 -0.3354520
## Sep 3667 0.01978653 0.3737662 -0.3341931
## Oct 3667 0.01978653 0.3882569 -0.3486839
## Nov 3667 0.01978653 0.3995440 -0.3599710
## Dec 3667 0.01978653 0.4087835 -0.3692104
ts.plot(global.month.ts,global.predict,lty=1:2)