# Load necessary libraries library(forecast) library(dplyr) #create lagged data library(ggplot2) library(TSPred) #test the prediction dataset <- read.csv("C:/Work/DMU Teaching/ECON 5001/Block teaching/Block 4/UK_quarterly_inflation.csv", sep = ",", header = TRUE) #View(dataset) # Generate CPI and its lagged CPI <- dataset$CPI.INDEX.00..ALL.ITEMS.2015.100 CPI_L1 <- dplyr::lag(CPI, n=1) #Check the new lagged CPI x <- data.frame(CPI, CPI_L1) #Compute Inflation inflation_data <- ((CPI-CPI_L1)/CPI_L1)*100 # Convert the data into a time series object ts_inflation <- ts(inflation_data, start = c(2000,1), frequency = 4) #the frequency indicates quarterly data. 1 is monthly and 12 is annually #Split the data into a learning sample and a test sample first.q <- 2000.25 last.q <-2018.75 inflation_learning <- window(ts_inflation, end=last.q) inflation_test <- window(ts_inflation, start=last.q+0.25) #View inflation autoplot(ts_inflation) + ggtitle("Quarterly Inflation from 2000 to 2020") + ylab("Inflation") + xlab("Quarter") # Build and Fit an ARIMA(Auto-Regressive Integrated Moving Average) model to the data arima_model <- auto.arima(inflation_learning, seasonal = TRUE) #Check the model: if the model is correct, the residuals should be white noise (i.e. no pattern), ACF should not exceed the dashed line checkresiduals(arima_model) # #Now the model is ready, we can start forecasting future inflation future_inflation <- forecast(arima_model, h = 12) # This forecasts the next 12 periods future_inflation <- forecast(arima_model, length(inflation_test)) # Plot the forecast in blue against actual inflation rate in red autoplot plot(future_inflation,type="o", pch=16, xlim=c(2000,2023), ylim = c(-1,5), main="MA(0,1) Mod el Multistep Forecasts") lines(future_inflation$mean, type="p", pch=16, lty="dashed", col="blue") lines(inflation_test, type="o", pch=16, lty="dotted", col = "red") # Print the forecasted values print(future_inflation)