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:
data≔Importdatasets/air_passengers.csv, base=datadir, output=Matrix
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.
data1..5
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:
withTimeSeriesAnalysis:
ts:=TimeSeries⁡data,'header'=1,'dates'=1,'period'=12
ts≔?
This data can be plotted using the TimeSeriesPlot command:
TimeSeriesPlot⁡ts
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:
TimeSeriesPlotts,axis2=mode=log
This does indeed make the seasonal variation more constant. As such, you can apply a log transformation to the time series:
logts≔ApplyLogTransform, ts
logts≔?
TimeSeriesPlotlogts
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.
SeasonalSubseriesPlotlogts,seasonnames=January,February,March,April,May,June,July,August,September,October,November,December,size=800,400,title=Yearly trend by month
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.
model≔ExponentialSmoothingModellogts
model≔< an ETS(M,A,M) model >
To evaluate the fit of the model, view the OneStepForecast:
modelts≔OneStepForecastsmodel,logts
TimeSeriesPlot⁡modelts,color=CornflowerBlue,thickness=2,logts,color=Niagara Burgundy,thickness=2
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:
forecast_ts≔Forecastmodel,logts,24,output=confidenceintervals95
TimeSeriesPlotlogts,forecast_ts,linestyle=dash,thickness=1
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.
decomposition:=Decomposition⁡model,logts
decomposition≔?
TimeSeriesPlot⁡logts,decomposition,split=separate
The seasonal component is multiplicative, therefore you can compensate for it by dividing the original data by it.
trainingdata:=GetData⁡logts
seasonaldata:=GetData⁡decomposition.., Logarithm of Monthly Passengers (seasonal)
nonseasonaldata ≔ TimeSeriestrainingdata /~ seasonaldata, 'header' = Logarithm of Monthly Passengers (deseasonalized), 'dates' = GetDateslogts
TimeSeriesPlot⁡nonseasonaldata
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.
Download Help Document