Getting to Grips with Time Series Predictive Analysis
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