Automating a simple model

Apart from automating your charts, EconData makes it possible for you to automate your models.

Naive automated model forecast

Model automation is usually not a trivial thing to do. But EconData’s functionality and its integration into R makes this easy. In this blog post, we illustrate how to create a naive automated forecasting model using EconData. The source code can be found here.

The specification of the model is handled by the R forecast package, which employs an algorithm to choose an ARIMA model that best fits the data. All that is required to estimate the model and produce an updated forecast is to run the script.

First, load the required packages. If you missed our previous post on installing the EconData package, here is a link for the installation and account creation details.

library(econdatar)
library(magrittr)
library(forecast)
library(xts)
library(gridExtra)

We can now load the data using the read_econdata() function. Specify the data set id, data key, and releasedescription.

cpi <- read_econdata(id = "CPI_COICOP_5",
                     key = "TC.00.0.0.0",
                     releasedescription = "2022-02-16")

In this post, we focus on the releasedescription argument of the read_econdata() function. This next section can be skipped without loss of continuity.

Each data set available on the EconData has multiple releases. Each release corresponds to a snapshot of the data as was available at the time of the release, sometimes referred to as a 'data vintage'. By making it easy to access historical data vintages, EconData enables easy testing of the real-time performance of a model. This is very important for modelling economic data that get revised, such as estimating the output gap using GDP data. In order to query the available releases, we use the read_release() function. We must, at a minimum, specify the data set id whose releases we want returned, i.e. read_release(id = "CPI_COICOP_5"). Each release will have a release description which we pass to read_econdata() in order to return the data for that release. If no releasedescription argument is passed to read_econdata(), the latest unreleased data will be returned. If the releasedescription is equal to "newest" the latest release will be returned. We will delve deeper into the workings of EconData's system for cataloging and identification of data in the next post.

To ease manipulation of the data, we will convert the input data into xts format.

headline <- xts(cpi$TC.00.0.0.0,
                order.by = rownames(cpi$TC.00.0.0.0) %>%
                  as.Date() %>%
                  as.yearmon())

Next, we can convert the headline CPI data into a year-on-year inflation rate, and plot the data to have a better idea of how to proceed with modeling and forecasting.

headline_yoy <- (headline / stats::lag(headline, k = 12) - 1) * 100
plot(headline_yoy, main =  "Headline y-o-y inflation", col = "#273b8d", lwd = 3)

We are now able to estimate the model. We are using a a simple ARIMA model using the forecast package. The auto.arima() function from the package will automatically choose the model specification that best fits the data.

arima_mod <- auto.arima(headline_yoy)

Now that we have an estimated ARIMA model, we can use the model to forecast. In this example, we are forecasting 12 months ahead.

f_cast <- forecast(arima_mod, h=12)

The forecasts, along with associated confidence intervals, can now be summarised in a plot or table.

plot(f_cast, main = "Forecast of 12-month ahead y-o-y headline inflation", 
     col = "#273b8d",
     lwd = 3)

last_date <- last(index(headline_yoy))+1/12
f_cast_data <- data.frame((seq(as.Date(last_date), length=12, by="month")+1),
                f_cast$mean[1:12])
colnames(f_cast_data) <- c("Date","Forecast")
grid.table(f_cast_data, rows=NULL)

Table of forecasts

The Codera Analytics team

Leave a comment

Your email address will not be published. Required fields are marked *