Due Date: November 29, 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)

#install.packages("ggplot2")

Problem 1

Complete Chapter 6, Exercise 4 from the textbook.

#install.packages("ggfortify")
#install.packages("tseries")
#install.packages("forecast")
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.2
library(tseries)
## Warning: package 'tseries' was built under R version 4.1.2
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(forecast)
## Warning: package 'forecast' was built under R version 4.1.2
library(ggfortify)
## Warning: package 'ggfortify' was built under R version 4.1.2
## Registered S3 methods overwritten by 'ggfortify':
##   method                 from    
##   autoplot.Arima         forecast
##   autoplot.acf           forecast
##   autoplot.ar            forecast
##   autoplot.bats          forecast
##   autoplot.decomposed.ts forecast
##   autoplot.ets           forecast
##   autoplot.forecast      forecast
##   autoplot.stl           forecast
##   autoplot.ts            forecast
##   fitted.ar              forecast
##   fortify.ts             forecast
##   residuals.ar           forecast

Problem 1A

##Fit a suitable regression model to the air passenger series. Comment
##on the correlogram of the residuals from the fitted regression model.

data(AirPassengers)
AP <- AirPassengers
AP.ts <- ts(AP, start = 1949, freq = 12)
#autoplot(AP.ts) + geom_smooth(method="lm")+ labs(x ="Date", y = "Passenger numbers (1000's)", title="Air Passengers from 1949 to 1961") 
arimaAP <- auto.arima(AP)
arimaAP
## Series: AP 
## ARIMA(2,1,1)(0,1,0)[12] 
## 
## Coefficients:
##          ar1     ar2      ma1
##       0.5960  0.2143  -0.9819
## s.e.  0.0888  0.0880   0.0292
## 
## sigma^2 estimated as 132.3:  log likelihood=-504.92
## AIC=1017.85   AICc=1018.17   BIC=1029.35

Problem 1B

#Fit an ARMA(p, q) model for values of p and q no greater than 2
#to the residual series of the fitted regression model. Choose the best
#fitting model based on the AIC and comment on its correlogram.

ggtsdiag(arimaAP)

Problem 1C

#Forecast the number of passengers travelling on the airline in 1961.
forecastAP <- forecast(arimaAP, level = c(95), h = 36)
autoplot(forecastAP)

Problem 1D

Problem 2

Complete Chapter 7, Exercise 5

get.best.arima <- function(x.ts, maxord = c(1,1,1,1,1,1))
  {
    best.aic <- 1e8
    n <- length(x.ts)
    for (p in 0:maxord[1]) for(d in 0:maxord[2]) for(q in 0:maxord[3])
      for (P in 0:maxord[4]) for(D in 0:maxord[5]) for(Q in 0:maxord[6])
      {
        fit <- arima(x.ts, order = c(p,d,q),
              seas = list(order = c(P,D,Q),
              frequency(x.ts)), method = "CSS")
        fit.aic <- -2 * fit$loglik + (log(n) + 1) * length(fit$coef)
        if (fit.aic < best.aic)
        {
          best.aic <- fit.aic
          best.fit <- fit
          best.model <- c(p,d,q,P,D,Q)
        }
      }
    list(best.aic, best.fit, best.model)
}

Problem 2

#Use the get.best.arima function from ยง7.3.2 to obtain a best-fitting
#ARIMA(p, d, q)(P, D, Q)12 for all p, d, q, P, D, Q  2 to the
#logarithm of the Australian chocolate production series (in the file at
#http://www.massey.ac.nz/pscowper/ts/cbe.dat). Check that the correlogram
#of the residuals for the best-fitting model is representative of white
#noise. Check the correlogram of the squared residuals. Comment on the
#results.

choc <- read.csv(file = "C:/CS-637-Time-Series-and-Forecasting--main/Lecture Notes/data/choc.csv", header = F)
choc.ts <- ts(choc, start = 1958, freq = 12)
plot(choc.ts)

best.arima.choc <- get.best.arima( log(choc.ts),
      maxord = c(2,2,2,2,2,2))
## Warning in arima(x.ts, order = c(p, d, q), seas = list(order = c(P, D, Q), :
## possible convergence problem: optim gave code = 1
best.fit.choc <- best.arima.choc[[2]]
acf( resid(best.fit.choc) )

best.arima.choc [[3]]
## [1] 2 1 1 2 1 1
ts.plot( cbind( window(choc.ts,start = 1958),
exp(predict(best.fit.choc,12)$pred) ), lty = 1:2)
## Warning in predict.Arima(best.fit.choc, 12): MA part of model is not invertible