5 simples steps to build your time series forecasting model | by Mahbubul Alam | Towards Data Science

I am a strong believer in “learning by doing” philosophy.
Data science is an applied field, so you need to get your feet wet to learn something. One can read all the “how to” tutorials on swimming, but at some point, they do have to test the water.
Beginners in data science often get caught into the impression that they have to learn everything under the sun before they can do a project. Wrong! I believe people can learn faster not by reading stuff but by doing small bits and pieces of projects.
In this article I want you to learn how to fit a time series forecasting model ARIMA — which, for many, is an intimidating algorithm. In this article, you will learn it in just 5 easy steps and make real forecasts. You are not going to build a Ferrari, but I’m sure you will learn to build a car that you can take to the streets.
Let’s roll the sleeves.
For this demo, we are going to use a forecasting package calledfpp2 in R programming environment. Let’s load that package.
# Required packageslibrary(fpp2)
I’ve got some data that I extracted from an actual time series. The following are the values, let’s copy them in the R script as well.
# your datavalues = c(92.1,  92.6,  89.5,  80.9,  95.6,  72.5,  71.2,  78.8,  73.8,  83.5,  97.9, 93.4,  98.0,  90.2,  96.7, 100.0, 103.6,  74.6,  78.9,  92.0,  83.4,  98.1, 109.9, 102.2, 102.1,  96.2, 106.9,  95.1, 113.4,  84.0,  88.6,  94.9,  94.7, 105.7, 108.6, 101.9,  113.9, 100.9, 100.2,  91.9,  99.6,  87.2,  92.1, 104.9, 103.4, 103.3, 103.9, 108.5)
Like every other modeling software, this package has a specific data formatting requirement. The ts() function takes care of it by converting data into a time series object.
In this function we specify the starting year (2015) and 12-month frequency.
# your time seriestime_series = ts(values, start = 2015, frequency =12)
Decomposition basically means deconstructing and visualizing the series into its component parts.
# time series decompositionautoplot(decompose(time_series)) + theme(plot.title = element_text(size=8))
This figure below displays 4 pieces of information: your data (top one), overall trend and seasonality. The final piece is called the remainder or the random part.
The actual model building is a simple 2-lines code using auto.arima() function. auto.arima will take care of the optimum parameter values, you just need to specify a few boolean parameters.
model = auto.arima(time_series, seasonal = TRUE, stepwise = FALSE, approximation = FALSE)
Making an actual forecast is the simplest of all the steps above, just half of a line length code— can you believe? We are using forecast() function and passing the model above and specifying the number of time steps into the future you want to forecast (I specified 30 months ahead)
# making forecastforecast_arima = forecast(model, h=30)
You are practically done with forecasting. You can print the forecast values with the print(forecast_arima) function.
Or, you may want to visualize the forecast values, the input series and confidence intervals altogether.
# visualizing forecastautoplot(time_series, series = " Data") +  autolayer(forecast_arima, series = "Forecast") +  ggtitle(" Forecasting with ARIMA") +  theme(plot.title = element_text(size=8))
This is an extra step for model evaluation and accuracy tests. First, let’s check out model description:
# model descriptionmodel['model']
I highlighted few things that you might be interested in: the description of the model (ARIMA(0,1,2(0,1,1)[12]) and AIC values. AIC is often used to compare the performance of two or more models.
In most machine learning models accuracy is determined based on RMSE or MAE values. Let’s print them as well.
# accuracyaccuracy(model)
That is all!
You have just built and implemented a forecasting model using 5 simple steps. Does that mean you became a master of forecasting? No, but you know the overall structure of the model from beginning to end and able to play with it with different datasets, different parameter values etc.
Just like I said in the beginning, you haven’t built a Ferrari but you’ve built a car that you can take to the grocery store!
I can be reached via Twitter or LinkedIn.