Use R! Advisors: Robert Gentleman Kurt Hornik Giovanni Parmigiani For other titles published in this series, go to http://www.com/series/6991 Paul S. Metcalfe Introductory Time Series with R 123 Paul S. Information and School of Mathematical Mathematical Sciences Sciences Massey University University of Adelaide Auckland Adelaide SA 5005 Albany Campus Australia New Zealand andrew.nz Series Editors Robert Gentleman Kurt Hornik Program in Computational Biology Department of Statistik and Mathematik Division of Public Health Sciences Wirtschaftsuniversität Wien Augasse 2-6 Fred Hutchinson Cancer Research Center A-1090 Wien 1100 Fairview Avenue, N. M2-B876 Austria Seattle, Washington 98109 USA Giovanni Parmigiani The Sidney Kimmel Comprehensive Cancer Center at Johns Hopkins University 550 North Broadway Baltimore, MD 21205-2011 USA ISBN 978-0-387-88697-8 e-ISBN 978-0-387-88698-5 DOI 10.1007/978-0-387-88698-5 Springer Dordrecht Heidelberg London New York Library of Congress Control Number: 2009928496 c Springer Science+Business Media, LLC 2009 All rights reserved.
This work may not be translated or copied in whole or in part without the written permission of the publisher (Springer Science+Business Media, LLC, 233 Spring Street, New York, NY 10013, USA), except for brief excerpts in connection with reviews or scholarly analysis. Use in connection with any form of information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed is forbidden. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. Printed on acid-free paper Springer is part of Springer Science+Business Media (www.com) In memory of Ian Cowpertwait Preface R has a command line interface that offers considerable advantages over menu systems in terms of efficiency and speed once the commands are known and the language understood.
However, the command line system can be daunting for the first-time user, so there is a need for concise texts to enable the student or analyst to make progress with R in their area of study. This book aims to fulfil that need in the area of time series to enable the non-specialist to progress, at a fairly quick pace, to a level where they can confidently apply a range of time series methods to a variety of data sets. The book assumes the reader has a knowledge typical of a first-year university statistics course and is based around lecture notes from a range of time series courses that we have taught over the last twenty years. Some of this material has been delivered to post- graduate finance students during a concentrated six-week course and was well received, so a selection of the material could be mastered in a concentrated course, although in general it would be more suited to being spread over a complete semester.
The book is based around practical applications and generally follows a similar format for each time series model being studied. First, there is an introductory motivational section that describes practical reasons why the model may be needed. Second, the model is described and defined in math- ematical notation. The model is then used to simulate synthetic data using R code that closely reflects the model definition and then fitted to the syn- thetic data to recover the underlying model parameters.
Finally, the model is fitted to an example historical data set and appropriate diagnostic plots given. By using R, the whole procedure can be reproduced by the reader, and it is recommended that students work through most of the examples.1 Mathematical derivations are provided in separate frames and starred sec- 1 We used the R package Sweave to ensure that, in general, your code will produce the same output as ours. However, for stylistic reasons we sometimes edited our code; e., for the plots there will sometimes be minor differences between those generated by the code in the text and those shown in the actual figures. vii viii Preface tions and can be omitted by those wanting to progress quickly to practical applications.
At the end of each chapter, a concise summary of the R com- mands that were used is given followed by exercises. All data sets used in the book, and solutions to the odd numbered exercises, are available on the website http://www.nz/∼pscowper/ts. We thank John Kimmel of Springer and the anonymous referees for their helpful guidance and suggestions, Brian Webby for careful reading of the text and valuable comments, and John Xie for useful comments on an earlier draft. The Institute of Information and Mathematical Sciences at Massey Univer- sity and the School of Mathematical Sciences, University of Adelaide, are acknowledged for support and funding that made our collaboration possible.
Paul thanks his wife, Sarah, for her continual encouragement and support during the writing of this book, and our son, Daniel, and daughters, Lydia and Louise, for the joy they bring to our lives. Andrew thanks Natalie for providing inspiration and her enthusiasm for the project. Paul Cowpertwait and Andrew Metcalfe Massey University, Auckland, New Zealand University of Adelaide, Australia December 2008 Contents Preface. vii 1 Time Series Data .4 Plots, trends, and seasonal variation .1 A flying start: Air passenger bookings .3 Multiple time series: Electricity, beer and chocolate data 10 1.4 Quarterly exchange rate: GBP to NZ dollar .5 Global temperature series .5 Decomposition of series .3 Estimating trends and seasonal effects .6 Summary of commands used in examples .2 Expectation and the ensemble .2 The ensemble and stationarity .2 Example based on air passenger series .3 Example based on the Font Reservoir series .4 Covariance of sums of random variables .5 Summary of commands used in examples .2 Leading variables and associated variables .2 Building approvals publication .3 Interpretation of the Bass model* .4 Exponential smoothing and the Holt-Winters method .2 Holt-Winters method .3 Four-year-ahead forecasts for the air passenger data .5 Summary of commands used in examples.
64 4 Basic Stochastic Models .4 Second-order properties and the correlogram .5 Fitting a white noise model .3 The backward shift operator .4 Random walk: Second-order properties .5 Derivation of second-order properties* .6 The difference operator .4 Fitted models and diagnostic plots .1 Simulated random walk series .2 Exchange rate series .3 Random walk with drift .2 Stationary and non-stationary AR processes .3 Second-order properties of an AR(1) model .4 Derivation of second-order properties for an AR(1) process* .5 Correlogram of an AR(1) process .1 Model fitted to simulated series .2 Exchange rate series: Fitted AR model .3 Global temperature series: Fitted AR model .7 Summary of R commands .1 Model fitted to simulated data .2 Model fitted to the temperature series (1970–2005) .3 Autocorrelation and the estimation of sample statistics* 96 5.4 Generalised least squares .1 GLS fit to simulated series .2 Confidence interval for the trend in the temperature series .5 Linear models with seasonal variables .2 Additive seasonal indicator variables .3 Example: Seasonal model for the temperature series .6 Harmonic seasonal models .2 Fit to simulated series .3 Harmonic model fitted to temperature series (1970–2005)105 5.2 Example using the air passenger series .8 Non-linear models .2 Example of a simulated and fitted non-linear series .9 Forecasting from regression .10 Inverse transform and bias correction .1 Log-normal residual errors .2 Empirical correction factor for forecasting means .3 Example using the air passenger data .11 Summary of R commands .2 Strictly stationary series .3 Moving average models .1 MA(q) process: Definition and properties .2 R examples: Correlogram and simulation .4 Fitted MA models .1 Model fitted to simulated series .2 Exchange rate series: Fitted MA model .5 Mixed models: The ARMA process .2 Derivation of second-order properties* .6 ARMA models: Empirical analysis .1 Simulation and fitting .2 Exchange rate series .3 Electricity production series .4 Wave tank data .7 Summary of R commands. 135 7 Non-stationary Models .2 Non-seasonal ARIMA models .1 Differencing and the electricity series .3 Definition and examples .4 Simulation and fitting .5 IMA(1, 1) model fitted to the beer production series .3 Seasonal ARIMA models .2 Modelling volatility: Definition of the ARCH model .3 Extensions and GARCH models .4 Simulation and fitted GARCH model .5 Fit to S&P500 series .6 Volatility in climate series .7 GARCH in forecasts and simulations .5 Summary of R commands. 155 8 Long-Memory Processes .3 Fitting to simulated data .4 Assessing evidence of long-term dependence .2 Bellcore Ethernet data .3 Bank loan rate .6 Summary of additional commands used .2 Unit of measurement of frequency .1 Fitting sine waves .4 Spectra of simulated series .2 AR(1): Positive coefficient .3 AR(1): Negative coefficient .5 Sampling interval and record length .1 Wave tank data .2 Fault detection on electric motors .3 Measurement of vibration dose .5 Bank loan rate .7 Discrete Fourier transform (DFT)* .8 The spectrum of a random process* .1 Discrete white noise .3 Derivation of spectrum .9 Autoregressive spectrum estimation .6 Spectral analysis compared with wavelets .11 Summary of additional commands used .2 Identifying the gain of a linear system .3 Estimator of the gain function .3 Spectrum of an AR(p) process .4 Simulated single mode of vibration system .5 Ocean-going tugboat .3 Tests for unit roots .2 Exchange rate series .5 Bivariate and multivariate white noise .6 Vector autoregressive models .1 VAR model fitted to US economic series .7 Summary of R commands. 227 12 State Space Models .2 Linear state space models .1 Dynamic linear model .3 Fitting to simulated univariate time series .1 Random walk plus noise model .2 Regression model with time-varying coefficients .4 Fitting to univariate time series .5 Bivariate time series – river salinity .6 Estimating the variance matrices .8 Summary of additional commands used.
249 1 Time Series Data 1.1 Purpose Time series are analysed to understand the past and to predict the future, enabling managers or policy makers to make properly informed decisions. A time series analysis quantifies the main features in data and the random variation. These reasons, combined with improved computing power, have made time series methods widely applicable in government, industry, and commerce. The Kyoto Protocol is an amendment to the United Nations Framework Convention on Climate Change.
It opened for signature in December 1997 and came into force on February 16, 2005. The arguments for reducing greenhouse gas emissions rely on a combination of science, economics, and time series analysis. Decisions made in the next few years will affect the future of the planet. During 2006, Singapore Airlines placed an initial order for twenty Boeing 787-9s and signed an order of intent to buy twenty-nine new Airbus planes, twenty A350s, and nine A380s (superjumbos).
The airline’s decision to expand its fleet relied on a combination of time series analysis of airline passenger trends and corporate plans for maintaining or increasing its market share. Time series methods are used in everyday operational decisions. For exam- ple, gas suppliers in the United Kingdom have to place orders for gas from the offshore fields one day ahead of the supply. Variation about the average for the time of year depends on temperature and, to some extent, the wind speed.
Time series analysis is used to forecast demand from the seasonal average with adjustments based on one-day-ahead weather forecasts. Time series models often form the basis of computer simulations. Some examples are assessing different strategies for control of inventory using a simulated time series of demand; comparing designs of wave power devices us- ing a simulated series of sea states; and simulating daily rainfall to investigate the long-term environmental effects of proposed water management policies. Metcalfe, Introductory Time Series with R, 1 Use R, DOI 10.1007/978-0-387-88698-5 1, © Springer Science+Business Media, LLC 2009 2 1 Time Series Data 1.2 Time series In most branches of science, engineering, and commerce, there are variables measured sequentially in time.
Reserve banks record interest rates and ex- change rates each day. The government statistics department will compute the country’s gross domestic product on a yearly basis. Newspapers publish yesterday’s noon temperatures for capital cities from around the world.