Predicting Seasonal Change in Passenger numbers using Time Series in R

I decided to look at Time Series algorithms and will take you through a simple example using the inbuilt Air Passenger Data in ‘R’.

First load in the data and assign a simple variable name to the data. Next place the variable into a class and R will give you an output indicating that it knows it is a time series as it will print ‘ts’, see below. Then explore the data by first plotting a graph of the passenger numbers over the years.

data("AirPassengers")

ap = AirPassengers

ap

Air Passengers 1949 to 1960 data output

class(ap)

plot(ap,col=c("blue"))

Air Passengers 1949 to 1960 Observed Data Plot

There is clearly an upward trend, we can say that the observed data 'yt' is composed of three parts:

Yt = at + st + et (trend, seasonal data and white noise or error variable at the end drawn from a Normal distribution with mean zero and some variance sigma squared [N(0,o2]). Et is also known as the ‘Random’ part.

In time series analysis one decomposes the data into these parts. Luckily R can do this for us with one command:

Plot(decompose(ap))

Decomposition of Additive Time Series for Air Passengers 1949 to 1960

When we decompose the data it is split into ‘Random’, ‘Seasonal’, ‘Trend’ and ‘Observed’ over time, random being the error data.

We can see that there is uniformity in the way the seasonal part looks and we see from the observed how things are increasing over time.

Now let’s break out the figures to take a closer look:

plot(stl(ap,"periodic"))

Visual representation of Trend Seasonality and Randoms (or Residuals) Air Passengers 1949 to 1960

The above chart shows a visual representation of the trend and seasonality. However we can see the same thing by looking at the numbers.

ap.decom=decompose(ap, type="mult")

plot(ap.decom)

trend=ap.decom$trend

trend

If we run the top four lines above together we get the following readout:

A closer look at the figures behind the trend line can be seen from the figure from Air Passengers 1949 to 1960

So looking at the figures we can see that the trend is always upward, year on year.

seasonal=ap.decom$seasonal

seasonal

And if we run the seasonal part we get the following:

Looking at the figures behind the seasonal graphed data for Air Passengers 1949 to 1960

We can see from the above figures that they remain constant for every month as we can see in the plot view.

The last thing we’ll do is to run the trend and seasonality graph together.

ts.plot(cbind(trend, trend*seasonal)lty=1:2)

Trend and seasonality graph run together for Air Passengers 1949 to 1960

So you can see in the graph above visually all three parts together; the seasonal, the trend and it is randomly distributed.

In future blog entry we shall examine another well-known ‘Time Series’ algorithm called ‘Random Walks’ often used for predicting stocks or the gold price.

## Comments