Forecasting with ARIMA: R Code

Step-1: ###R Packages Installed

library(fpp2)

library(readxl)

library(SPlit)

library(fpp2)

 

###Declare time-series data

###In this study I have file naming as HPI_AUS. All the date excel format.

HPI_AUS <-ts(AUSHPI[,2], start=c(2002,3), frequency=4)

 

Step-3: ###While forecasting, first that should be done within the sample, and if it is found valid, then only we forrecast out of sample.

###Split the given data into the training and test sets

split_HPI_AUS <- ts_split(ts.obj = HPI_AUS, sample.out= 8)

training <- split_AUSHPI$train

testing <- split_AUSHPI$test

length(training)

length(testing)


 Step-4:###Select the appropriate forecasting tool. Here I have used ARIMA (easy and quick model)

###ARIMA In-sample testing

arima_diag(training)

 

 

###Model forecasting In-sample

arima1 <- auto.arima(training, seasonal= TRUE)

autoplot(arima1)

check_res(arima1)

 

###Forecasting In-sample

fcast1<- forecast(arima1, h=8)

test_forecast(actual=HPI_AUS, forecast.obj = fcast1, test = testing)

accuracy(fcast1, testing)

 

###Auto ARIMA model Out of Sample

arima_diag(HPI_AUS)

fit_arima <- auto.arima(HPI_AUS, seasonal = TRUE )

autoplot(fit_arima)

check_res(fit_arima)

 

###Forecasting Out of Sample

fcast1 <- forecast(HPI_AUS, model = fit_arima, h=8)

plot_forecast(fcast1)

summary(fcast1)

 

###Do we have any option to validate the Forecasting Result?

Box.test(resid(fcast1), lag=1, type = "Ljung-Box")

Box.test(resid(fcast1), lag=5, type = "Ljung-Box")

Box.test(resid(fcast1), lag=10, type = "Ljung-Box")

Box.test(resid(fcast1), lag=15, type = "Ljung-Box")

Box.test(resid(fcast1), lag=20, type = "Ljung-Box")

Thank you