# Time Series Analysis and Forecasting – An Introduction

You need to analyze a time series but have no idea where to start? Then you’re in the right place. In this post, I give you a rough overview and pointers on what to learn next for your specific problem.

Here you can see an example time series of monthly airline passengers. We will discuss different properties of this dataset in the following parts.

## What are we even doing with time series?

Roughly speaking, we have two options for what to do with a new time series:

1. Analysis
2. Forecasting

You don’t need to decide between them – in fact, it is likely that you will do both in your project at multiple different stages. But it is important to know that there are two areas, because many books focus only on Analysis while others focus on forecasting methods and knowing about the difference helps you pick the resource for the specific knowledge you’re looking for.

Let’s look at these two areas:

## Time Series Analysis

Analysis in this context means exploration. Just as with any other type of data analysis, we first need to get a feel for the numbers we have available in our data before we can do a deep dive into specific interesting things.

### Exploring basic statistical properties of time series variables

First off, we can calculate and display various statistics and metrics about the data, like the following:

• Mean
• Standard deviation
• Standard error
• Confidence intervals

Caution! Most of the methods in both analysis and forecasting only concern one variable over time. For example, the number of passengers each day over a time period.

### Moving average over time series values

You will also often see people compute the moving average of their time series. This smooths out the data and shifts the focus to the big picture instead of small bumps that might be due to unimportant noise.

In this example you can see the average computed over a a window of 12 months at every month.

### Trend, Seasonality & Stationarity

Another important aspect of time series analysis is finding patterns.

First, because it’s interesting and could immediately lead to some business insights. But even more importantly we need to identify these patterns before we can move on to forecasting.

For many forecasting methods, it is mandatory that the time series data is “stationary”. Otherwise the algorithms will not give you useful results. Roughly speaking, a time series is stationary if it does not include a trend or seasonality.

Here is an example of a time series with an upward trend:

Trend is roughly characterized as the relative change of time series values over a longer stretch in measurements. In this example, the values overall go up, but a trend can also slope downwards or stay horizontal.

And this time series has seasonality:

We say a time series is seasonal, if some pattern of measurements repeats regularly. Regularly is any time frame typically within a year. It could be a weekly pattern, or monthly, yearly etc.

Here we see a yearly pattern with a peak in the middle of the year and lows towards the end and start of years.

It is also possible that a time series shows both seasonal behavior and an overall trend (like the original passenger time series we saw at the start).

### Decomposition

But before you can run most forecasting methods on this, you need to take these factors out of the time series “decomposing” the time series into its trend, seasonal and residual (the rest, the stuff that you will use to forecast) component.

For our airline passenger dataset this looks like this:

The attentive among you might notice that the trend and seasonal examples you just saw are actually the trend and seasonal components of the passenger data set.

## Time Series Forecasting

Forecasting is a typical machine learning task and that also means that we can choose between various models. Each model makes specific assumptions about the data.

### Pre-processing the data

Before applying any algorithm to your data, you should research how the your data needs to be prepared.

Two techniques are used quite often:

• normalising
• Differentiating or decomposing to achieve stationarity

Normalising means subtracting the mean from all values and dividing by the standard deviation. This is done for example before using data in a Recurrent Neural Network.

Differentiating means instead of taking the values $x_1, x_2, x_3$ and so on at the different time steps, you consider $x_2 – x_1, x_3 – x_2, …$. So at each step we only consider the difference to the previous value. And decomposition was briefly described above already.

Make sure you follow best practices for this, otherwise your results will not be meaningful or your algorithm will fail to converge and produce results at all.

### ARIMA and variations

A VERY common forecasting method is ARIMA. It’s short for “Autoregressive integrated moving average”, but don’t worry if this doesn’t mean much to you. Most coding languages provide packages that implement the algorithm – for python one of those libraries is “statsmodels”.

There are also variations of ARIMA, like SARIMA, which is Seasonal ARIMA.

These methods can be used with various orders, often written in the following format: ARIMA(2,0,1) and the numbers (p, d, q) are hyperparameters that need to be chosen carefully. Again, you should look for instructions on this or use a package like auto-arima, which tries out multiple different options for you to find the best out of those.

### Deep Learning: Recurrent Neural Networks and Transformers

In the world of neural networks we also have multiple options for forecasting time series.

In the post, RNNs (Recurrent Neural Networks) like LSTMs were often used. Recently, Transformer architectures seem to be preferred, which use the “attention”-mechanism (maybe you have heard of the research paper “Attention is all you need”, which was one of the first to talk about this concept).

### Forecasting recommendations from a fellow beginner

If you are new to this and need reliable results for work or a project, I would recommend to start with ARIMA or other classic methods, instead of neural networks.

However, if you just want to play around and neural networks are an interest of yours, feel free to go ahead and jump into the transformer world. Just be aware that you likely need some practice with basic neural networks and its specific training first.

Like mentioned at the start, this is not a full guide, but this should give you a starting point on what things you need to google to learn more about and what the general procedure is for dealing with time series.

## Our example dataset: Airline passengers

Here is a description and source information about the example dataset I used in this post.

https://rdrr.io/r/datasets/AirPassengers.html

The classic Box & Jenkins airline data. Monthly totals of international airline passengers, 1949 to 1960.

Box, G. E. P., Jenkins, G. M. and Reinsel, G. C. (1976) Time Series Analysis, Forecasting and Control. Third Edition. Holden-Day. Series G.

Consent Management Platform by Real Cookie Banner