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
![](https://static.wixstatic.com/media/6c63cf_9721ed12ae8b47c29a7fb51d98c7132e~mv2.png/v1/fill/w_691,h_326,al_c,q_85,enc_auto/6c63cf_9721ed12ae8b47c29a7fb51d98c7132e~mv2.png)
Air Passengers 1949 to 1960 data output
class(ap)
plot(ap,col=c("blue"))
![](https://static.wixstatic.com/media/6c63cf_1c936e8a25374c95aace3c8f312742b9~mv2.png/v1/fill/w_940,h_617,al_c,q_90,enc_auto/6c63cf_1c936e8a25374c95aace3c8f312742b9~mv2.png)
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))
![](https://static.wixstatic.com/media/6c63cf_f465fd052e814594b4c23cee9b9cefb3~mv2.png/v1/fill/w_940,h_613,al_c,q_90,enc_auto/6c63cf_f465fd052e814594b4c23cee9b9cefb3~mv2.png)
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"))
![](https://static.wixstatic.com/media/6c63cf_742f86aa322f4192b79f19b9d35eed43~mv2.png/v1/fill/w_940,h_618,al_c,q_90,enc_auto/6c63cf_742f86aa322f4192b79f19b9d35eed43~mv2.png)
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:
![](https://static.wixstatic.com/media/6c63cf_e087d40bf48e4b14b1f2349f0c4b118f~mv2.png/v1/fill/w_940,h_538,al_c,q_90,enc_auto/6c63cf_e087d40bf48e4b14b1f2349f0c4b118f~mv2.png)
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:
![](https://static.wixstatic.com/media/6c63cf_72af20152a5347f1bb7ba5ccffcbacd9~mv2.png/v1/fill/w_940,h_506,al_c,q_90,enc_auto/6c63cf_72af20152a5347f1bb7ba5ccffcbacd9~mv2.png)
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)
![](https://static.wixstatic.com/media/6c63cf_0c93449d8a55474192c6c246caa93373~mv2.png/v1/fill/w_940,h_614,al_c,q_90,enc_auto/6c63cf_0c93449d8a55474192c6c246caa93373~mv2.png)
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