ARIMA Model Time Series | Towards Data Science

Climate Change is real. What makes it worse is because of the human activities that are not environment friendly, in the example using fossil fuel for the vehicle, coal-powered generator, cement production, etc. One of the effects of these activities is increasing in temperature and depletion on the ozone layer.
As we know, that developed countries, especially in Europe, are reducing the emission amount that contributes to climate change. If we visualize some European countries by their time series, it shows no significant growth on it and it has the same emission that is produced and even the amount decreases in some cases, especially on Russian Federation after USSR collapsed in 1991. It shows almost no significant growth on it.
It is a different story when we look at developing countries. If we look at this plot from ASEAN countries, it shows a slightly linear growth from time to time. As you can see below,
Based on this problem, this article will show you how to forecast the time-series data on CO2 emission from European Commission which you can get the data here using ARIMA model in R and the reason why we have to use the model.
By analyze and forecast the production amount of emission, we will know how much the emission has been produced and also it is very useful to make what kind of policy to reduce its growth.
This is different from my previous article that I’ve made before where I forecast the CPI in Indonesia which is a stationary data. In this case, I will use Indonesia’s CO2 emission to show you how to work on non-stationary data.
But wait, what is the differences between stationary and non-stationary data?
What makes the difference between stationary and non-stationary data is from the mean and the variance by time.
The stationary data has a constant mean, variance, and correlation over time. While the non-stationary data has mean, variance, and correlation that changes over time.
Still don’t have any idea? Well, take a look at these visualizations:
Recall my CPI plot above from my previous article, this is a stationary data. The reason why is that the CPI value doesn’t not increasing over time or in other words, no trend exists over time whether it’s increasing or not.
Here is Malaysia’s CO2 emission that has been produced over time. As we can see, it shows some linear trend on it. We can say, that the mean, variance, and correlation is increasing over time. That’s why it’s non-stationary data.
I hope you can understand at least the differences between stationary and non-stationary time series data right now.
Before we conduct the analysis, I will give you how do I work on this data. Here are the steps that I’ve done:
Data ExplorationModel Identification and DiagnosticsForecasting
Data Exploration
Model Identification and Diagnostics
Forecasting
Before we build the model, we have to know the data first. How it looks, how different does the pattern from the other countries, and many more. I will describe what the data set really is and what the information contains on it.
The data set represents the total amount of CO2 emission that has been produced from 1970 to 2015. It records the amount from all countries around the world. The CO2 amount that is recorded by the European Commission Joint Research Center is the emission that comes from fossil fuel use and industrial process use, such as cement production, chemical processes, non-energy use, and many more. This doesn’t include biomass burning and the other agricultural-related activities, such as forest fires and agricultural waste burning.
Because I’ve already told you about some countries CO2 emissions, I will show you specifically on how Indonesia’s CO2 emission is. Here it shows the visualization on it,
As we can see here, it has a linear trend over time. This is a non-stationary data because the mean, variance, and correlation change over time. In 1970, the CO2 emission is just 30.066 kilotons and in 2015, the CO2 emission is 502.961 kilotons.
That’s a big change. Based on this data, we want to forecast using the ARIMA model to know what is the emission amount that will be produced years ahead if there are no changes in the government’s policy.
Because this is not a stationary time series, we have to find the differenced data with the previous value.
If the differenced data has stationary properties, then it can be modelled using the ARMA model. Therefore, we can find which perfect lag for the model from it.
To do that, we have to make the value as a time series object and then we look at the result of the time-series that is applied on diff function. Here is the code look like,
indo <- df %>%  filter(nation == "Indonesia") %>%  pivot_longer(2:47, names_to = "year", values_to = "co2")indo.ts <- ts(indo$co2, frequency = 1, start = 1970)indo.ts# Create Time Series Plot from plot(diff(indo.ts))
And here is the visualization result from it,
Based on this visualization, we can see that the differenced data has slightly more stationary properties to it and there’s no trend on it. Therefore, we can use this differenced data to find which AR and MA parameters are good for our ARIMA model.
To find which AR’s and MA’s parameter values that suit our ARIMA model, we can use ACF and PACF function to find which are the best value,
Here is the function that we can visualize the ACF and PACF plot,
library(astsa)acf2(diff(indo.ts), max.lag = 40)
Here is the result,
Based on this plot, we know that the ACF and the PACF function have slightly tail off on it and it’s not reaching 0’s quickly. Because of that, we can assume that it will use parameter p with 1 and parameter q also with 1. Therefore, the ARMA model that suits is ARMA(1,1).
To make the model becomes an ARIMA model, we just add another parameter, which is d that stands for the differences that are given to the data. With that, our ARIMA model will be ARIMA(1, 1, 1) where each parameter represents the p, d, and q. The p is AR model parameter, d is the amount of the differences that gives to the data, and q is the MA model parameter.
The ARIMA(1, 1, 1) will be our model if there are no significant changes between the other model. In this case, we will try ARIMA(2, 1, 1) and ARIMA(1, 1, 2) to see if the parameter has a significant p-value on it.
We can diagnose this model simply by using this function below,
# ARIMA(1, 1, 1)sarima(indo.ts, 1, 1, 1)# ARIMA(2, 1, 1)sarima(indo.ts, 2, 1, 1)# ARIMA(1, 1, 2)sarima(indo.ts, 1, 1, 2)
Here are the p-values for each parameters,
As we can see here, the ARIMA(1,1,1) has a significant p-value on its parameters. Then, both models, ARIMA(1,1,2) and ARIMA(2,1,1), have a non-significant p-values on it. By changing its parameters doesn’t have any effect on the model. Therefore, we can use ARIMA(1,1,1) model.
After we chose the best model for the data, we have to diagnose one more thing, which is the residuals. Below are the residual analysis result from the sarima function,
If we want to use this model, the residual must have a normal distribution to it and there is no correlation between each lag of its values. Based on this visualization, we can see that the residual don’t have any significant autocorrelation to it as we can see from the ACF plot, most of the points also get aligned on the line as we can see from the Q-Q plot, and the Ljung-Box statistic p values doesn’t cross the blue line. Therefore, we can use this model to do forecasting.
If we want to do some forecasting, we can use sarima.for function to do it. Each parameter for this function consist of the data, what time ahead we want to forecast, and then set the p, d, and q parameter that represents our ARIMA model.
In this example, we want to know what is the CO2 emission amount that will be produced for the next 35 years, and what is the amount of CO2 emission that is produced in 2050.
Here is the function that is used to make a forecast,
sarima.for(indo.ts, n.ahead = 35, 1, 1, 1)
Here is the visualization from it,
As we can see here, the black dot represents our data, and the red one represents the forecast result. As we can see here, the forecast result also shows the grey boundary between the red line. It basically indicates the standard error from the forecast result. The boundary doesn’t have a large boundary on it, therefore the model has a good result to it.
Based on the forecast result, we know that in 2050 the amount of CO2 emission that will produce around 904.186 kilotons. This is a huge amount of CO2 emission. Because of that, the government should think the policy on how to reduce the emission so it can fight the climate change and make our earth more livable and not broken because of our activities that consequences on earth’s condition.
In conclusion, the ARIMA model is a good model to make a forecasting on non-stationary time series data.
I know, that the model is not perfect, but at least it gives you how the value is in the future. This is really important to know, especially if you are a policymaker, by know this result, you will know what kind of policy to make.
I will close this article with this quote to remind us that,
“All models are wrong, but some are useful” — George Box
[1] Stoffer, D. S. & Shumway, R. H. Time Series: A Data Analysis Approach Using R (2019), Taylor & Francis Group.[2] Hyndman, R.J. & Athanasopoulos, G. Forecasting: principles and practice (2018), 2nd edition, OTexts.