library(tframePlus)
library(tseries)
library(forecast)
library(readr)
HPI_IE_Q = ts(HPI_IE$HPIValues, start = c(1976, 2), frequency = 4) 
plot.ts(HPI_IE_Q)

Part 1 Augmented Dickey-Fuller (ADF) test on the time series.The p-value is less than 0.05, therefore reject the null hypothesis of non-stationarity. This means the time series is stationary and we can proceed.

adf.test(HPI_IE_Q)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  HPI_IE_Q
## Dickey-Fuller = -3.3773, Lag order = 5, p-value = 0.06054
## alternative hypothesis: stationary

Part 2 Decompose the time series into three components: trend, seasonality, and residual (random noise). This helps in understanding the underlying structure of the data.

HPI_IE_Q = ts(HPI_IE$HPIValues, start = c(1976, 2), frequency = 4)
decomp = stl(HPI_IE_Q, s.window="periodic")

plot(decomp)

Seasonal adjustment (get rid of seasonality)

deseasonal_IE <- seasadj(decomp)

Part 3 Differencing is applied to remove any remaining trend in the data, making it stationary if necessary.

count_d1 = diff(deseasonal_IE, differences = 1) 
plot(count_d1)
adf.test(count_d1, alternative = "stationary")

count_d2 = diff(deseasonal_IE, differences = 2) 
plot(count_d2)
adf.test(count_d2, alternative = "stationary")

count_d3 = diff(deseasonal_IE, differences = 3) 
plot(count_d3)
adf.test(count_d3, alternative = "stationary")

The Autocorrelation Function (ACF)

acf(count_d2, main='ACF for Differenced Series')

Partial Autocorrelation Function (PACF)

pacf(count_d2, main='PACF for Differenced Series')

Part 4 Fitting the ARIMA Model

fit = auto.arima(deseasonal_IE, seasonal=FALSE)
fit 
## Series: deseasonal_IE 
## ARIMA(1,1,3) 
## 
## Coefficients:
##          ar1     ma1      ma2     ma3
##       0.8299  0.2230  -0.0965  0.1614
## s.e.  0.0589  0.0914   0.0910  0.0870
## 
## sigma^2 = 9.018:  log likelihood = -429.52
## AIC=869.05   AICc=869.41   BIC=884.75

Part 5 Analyze whether the residuals (errors) of the fitted model behave randomly and resemble white noise.

tsdisplay(residuals(fit), lag.max=45, main='(1,1,3) Model Residuals')

Part 6 Use the fitted ARIMA model to forecast future values of the time series. In this case 24 future periods (6 years of quarterly data).

fcast = forecast(fit, h=24)
plot(fcast)