 Forecasting Air Passenger Data - Maple Help

Forecasting Air Passenger Data

The following examples demonstrate techniques for analyzing time series data with the TimeSeriesAnalysis package. Air Passenger Data

The following example uses a data set containing the number of monthly air passengers in thousands of passengers from 1949 until 1960. The data is from Box, Jenkins, and Reinsel, as noted in the references below.

First step is to import the data:

 >
 ${\mathrm{data}}{≔}\left[\begin{array}{c}{\mathrm{145 x 2}}{\mathrm{Matrix}}\\ {\mathrm{Data Type:}}{\mathrm{anything}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (1)

The first column lists annual dates and the second column lists number of monthly passengers for those dates. The first row is a header containing column names.

 > $\mathrm{data}\left[1..5\right]$
 $\left[\begin{array}{cc}{"Date"}& {"Monthly Passengers"}\\ {"1949-01"}& {112}\\ {"1949-02"}& {118}\\ {"1949-03"}& {132}\\ {"1949-04"}& {129}\end{array}\right]$ (2)

To work with this data using the TimeSeriesAnalysis package, construct a TimeSeries object. Such an object can contain one or more data sets, measured at a common set of time points, as well as data headers and other metadata. You can construct this time series object as follows:

 > $\mathrm{with}\left(\mathrm{TimeSeriesAnalysis}\right):$
 > $\mathrm{ts}:=\mathrm{TimeSeries}\left(\mathrm{data},'\mathrm{header}'=1,'\mathrm{dates}'=1,'\mathrm{period}'=12\right)$
 ${\mathrm{ts}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Monthly Passengers}}\\ {\mathrm{144 rows of data:}}\\ {\mathrm{1949-01-01 - 1960-12-01}}\end{array}\right]$ (3)

This data can be plotted using the TimeSeriesPlot command:

 > $\mathrm{TimeSeriesPlot}\left(\mathrm{ts}\right)$  Visualizing Seasonality and Trend

From the previous plot, it can be observed that the time series seems to have a strong seasonal component that may be either linear or quadratic. Moreover, the magnitude of the seasonal variation increases as the number of passenger arrivals increases. To see if applying a log transformation to the data in order to make the magnitude of the seasonal variation more constant, you can first try to plot the time series with a log scale:

 > $\mathrm{TimeSeriesPlot}\left(\mathrm{ts},\mathrm{axis}\left[2\right]=\left[\mathrm{mode}=\mathrm{log}\right]\right)$ This does indeed make the seasonal variation more constant. As such, you can apply a log transformation to the time series:

 >
 ${\mathrm{logts}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Logarithm of Monthly Passengers}}\\ {\mathrm{144 rows of data:}}\\ {\mathrm{1949-01-01 - 1960-12-01}}\end{array}\right]$ (4)
 > $\mathrm{TimeSeriesPlot}\left(\mathrm{logts}\right)$ You can also look for trends in the data using the SeasonalSubseriesPlot command. The following plot shows that the year-to-year increase for each month is constant for the number of passenger arrivals.

 > $\mathrm{SeasonalSubseriesPlot}\left(\mathrm{logts},\mathrm{seasonnames}=\left["January","February","March","April","May","June","July","August","September","October","November","December"\right],\mathrm{size}=\left[800,400\right],\mathrm{title}="Yearly trend by month"\right)$  Modeling Future Passenger Arrivals

To forecast future passenger arrivals, first find a suitable model to match the actual data. Maple can select a suitable model from a family of 30 related models and adjust it to this time series.

 > $\mathrm{model}≔\mathrm{ExponentialSmoothingModel}\left(\mathrm{logts}\right)$
 ${\mathrm{model}}{:=}{\mathrm{< an ETS\left(M,A,M\right) model >}}$ (5)

To evaluate the fit of the model, view the OneStepForecast:

 > $\mathrm{modelts}≔\mathrm{OneStepForecasts}\left(\mathrm{model},\mathrm{logts}\right)$
 ${\mathrm{modelts}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Logarithm of Monthly Passengers \left(1 step forecasts\right)}}\\ {\mathrm{144 rows of data:}}\\ {\mathrm{1949-01-01 - 1960-12-01}}\end{array}\right]$ (6)
 > $\mathrm{TimeSeriesPlot}\left(\left[\mathrm{modelts},\mathrm{color}="CornflowerBlue",\mathrm{thickness}=2\right],\left[\mathrm{logts},\mathrm{color}="Niagara Burgundy",\mathrm{thickness}=2\right]\right)$ From this plot, you can see that there is reasonable agreement between the forecast and the data. Using the model data, you can predict two years of future data using the forecast command, including a 95% confidence interval:

 > $\mathrm{forecast_ts}≔\mathrm{Forecast}\left(\mathrm{model},\mathrm{logts},24,\mathrm{output}=\mathrm{confidenceintervals}\left(95\right)\right)$
 ${\mathrm{forecast_ts}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Logarithm of Monthly Passengers \left(forecast - 2 percentile\right), Logarithm of Monthly Passengers \left(forecast - 50 percentile\right), Logarithm of Monthly Passengers \left(forecast - 98 percentile\right)}}\\ {\mathrm{24 rows of data:}}\\ {\mathrm{1960-12-31 - 1962-12-01}}\end{array}\right]$ (7)
 > $\mathrm{TimeSeriesPlot}\left(\mathrm{logts},\left[\mathrm{forecast_ts},\mathrm{linestyle}=\mathrm{dash}\right],\mathrm{thickness}=1\right)$  Further Explorations

The chosen model can be used to decompose the data set into several components. The number of components depends on the model. This is done using the Decomposition command. For an exponential smoothing model, there are always level and residual components. There can also be trend and seasonal components depending on whether or not the model has those properties. This can, for example, be used to correct for seasonal influences or smooth data.

 > $\mathrm{decomposition}:=\mathrm{Decomposition}\left(\mathrm{model},\mathrm{logts}\right)$
 ${\mathrm{decomposition}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Logarithm of Monthly Passengers \left(residuals\right), ..., Logarithm of Monthly Passengers \left(seasonal\right)}}\\ {\mathrm{144 rows of data:}}\\ {\mathrm{1949-01-01 - 1960-12-01}}\end{array}\right]$ (8)
 > $\mathrm{TimeSeriesPlot}\left(\mathrm{logts},\mathrm{decomposition},\mathrm{split}=\mathrm{separate}\right)$     The seasonal component is multiplicative, therefore you can compensate for it by dividing the original data by it.

 > $\mathrm{trainingdata}:=\mathrm{GetData}\left(\mathrm{logts}\right)$
 ${\mathrm{trainingdata}}{:=}\left[\begin{array}{c}{\mathrm{144 x 1}}{\mathrm{Matrix}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (9)
 >
 ${\mathrm{seasonaldata}}{:=}\left[\begin{array}{c}{\mathrm{144 x 1}}{\mathrm{Matrix}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (10)
 >
 ${\mathrm{nonseasonaldata}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Logarithm of Monthly Passengers \left(deseasonalized\right)}}\\ {\mathrm{144 rows of data:}}\\ {\mathrm{1949-01-01 - 1960-12-01}}\end{array}\right]$ (11)
 > $\mathrm{TimeSeriesPlot}\left(\mathrm{nonseasonaldata}\right)$  References

 Box, G.E.P.; Jenkins, G.M.; and Reinsel, G.C. Time Series Analysis, Forecasting and Control. Third Edition. Holden-Day. Series G, 1976.
 Hyndman, R.J. and Athanasopoulos, G. Forecasting: principles and practice. 2013. http://otexts.org/fpp/. Accessed on 2013-10-09.
 Hyndman, R.J.; Koehler, A.B.; Ord, J.K.; and Snyder, R.D. Forecasting with Exponential Smoothing: The State Space Approach. Springer Series in Statistics. Springer-Verlag Berlin Heidelberg, 2008.