- Published on
A Primer on Time Series using R and Python
- Authors
- Name
- Jared Chung
Time Series is an area of data science which is often overlooked. The majority of books and tutorials mainly focus on traditional Classification and Regression based problems.
In this blog we will look at variety of forecasting time series techniques while utilizing the programming languages R and Python.
The topics to be covered include:
- Naive method
- Simple Average
- Moving Average
- Exponential Smoothing
- Holt's Winter method
- ARIMA
I'll be using the daily US Beer Sales dataset to test out the forecasting techniques. The data is comprised of sales between 1992 to 2016 as shown in the table below. The main packages that will be used is the forecast
package for R and the statsmodels
package for Python.
There is a vast range of different metrics used to measure the accuracy of forecasting techniques. For this analysis, I decided to use the mean squared error MSE for simplicity.
Naive Method
The Naive approach is by far the simplest forecasting technique. It is calculated by taking the last data point available and forecasting all future values based on this value. The below code represents how simplicity of the implementation. This method is most effective if the time series is quite stable overtime. The forecast results in a MSE of 2,598,044.
#R
data[nrow(data)-1,] #Extract the last value
#Python
data[len(data)-1] #Extract the last value
Simple Average
You can see how the Naive method might create some issues, if the last value is unusually high or low, this will then be used to forecast future values. Another way to tackle this problem is to take a Simple Average , which is calculated by taking an average of all previous values. This technique can smooth out volatility in the data. The forecast results in a MSE of 1840232.
#R
mean(data$value)
#Python
data.values.mean()
Moving Average
A variation to Simple Average is the Moving Average. Using a specified window (e.g 12), take the average of the data points after this shift one point and repeat. This is sometimes called a "sliding window". The benefit of using a moving average is that it is able to adapt to changes over time and as such can capture the "Trend" of the data. The forecast results in a MSE of 1,645,909.
#R
library(forecast)
ma(train$count, order = 12)
#Python
train['Count'].rolling(12).mean()
Exponential Smoothing
We have looked at a few examples involving taking averages of past points, in many cases, only the most recent information is needed to influence our future forecast. Exponential Smoothing applies a weighting to past values with larger weights assigned to more recent observations. The weights decrease exponentially the further into the past. The forecast results in a MSE of 2,121,549.
#R
library(forecast)
ses(data, alpha = 0.2, initital = "simple", h = 36)
#Python
from statsmodels.tsa.api import SimpleExpSmoothing
fit = SimpleExpSmoothing(np.asarray(train['Count'])).fit(smoothing_level = 0.2, optimzed = False)
fit.forecast(len(test))
Holt Winters
The Holt Winters technique extends the simple exponential smoothing. The techniques encompasses three smoothing equations, the first part is the level (essentially exponential smoothing), the second part is the trend and the last part is the seasonality. This can be broken into two variations. One variation is the "additive" method, which is suitable for constant seasonality. The second variation is the "multiplicative" method which allows seasonality to change over time. The forecast results in a MSE of 370,809.9.
library(forecast)
fit <- hw(data, seasonal = "additive")
# "ets" function is an automated version
ets_model <- ets(ts_data)
ets_forecast <- forecast(ets_model, h = 36)
#Python
from statsmodel.tsa.api import ExponentialSmoothing
fit = ExponentialSmoothing(np.asarray(train['Count]), seasonal_period = 7, trend = 'add', seasonal = 'add')
Arima
Another popular method is Arima which is made up of three parts, autoregressive, integrated and moving average . One of the main differences compared to the previous methods is that it is based on autocorrelations. The first part of states the # of linear combinations of past values. The second part denotes how many times the time series has been "differenced" (transformed) so it is stationary (time series which doesn't depend on time). The third and final part is the moving average which is similar to the first part except that it is a linear combination of the error terms. The forecast results in a MSE of 248,802.
#R
library(forecast)
fit <- auto.arima(data)
#Python
from statsmodels.tsa.statespace import SARIMAX
fit = SARIMAX(train['count'], order = (2,1,4), seasonal_order = (0,1,1,7)).fit()
Conclusion
In this blog we have explored a variety of different forecasting techniques which can be easily applied. The choose of technique depends significantly on the data, for example, determining whether there is a particular trend or seasonality.
One of the main take always, is that there is not a single technique which is supreme but it depends on the particular dataset. Benchmarking each method using MSE results in ARIMA producing the best results for this particular dataset.
References
- Forecast R package - https://pkg.robjhyndman.com/forecast/
- https://www.statsmodels.org/stable/index.html