TIMESERIES ANALYSIS USING R.doc

上传人:文库蛋蛋多 文档编号:3025496 上传时间:2023-03-09 格式:DOC 页数:20 大小:435KB
返回 下载 相关 举报
TIMESERIES ANALYSIS USING R.doc_第1页
第1页 / 共20页
TIMESERIES ANALYSIS USING R.doc_第2页
第2页 / 共20页
TIMESERIES ANALYSIS USING R.doc_第3页
第3页 / 共20页
TIMESERIES ANALYSIS USING R.doc_第4页
第4页 / 共20页
TIMESERIES ANALYSIS USING R.doc_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《TIMESERIES ANALYSIS USING R.doc》由会员分享,可在线阅读,更多相关《TIMESERIES ANALYSIS USING R.doc(20页珍藏版)》请在三一办公上搜索。

1、Time Series Analysis Using R by Steve Raper and Chris ChatfieldThis document is a free appendix to the 6th edition ofThe Analysis of Time SeriesBy Chris Chatfield, published in 2004 by Chapman & Hall/CRC in the Texts in Statistical Science series.The examples in this time-series text were mostly car

2、ried out using Minitab and S-Plus (see Chapter 14 and Appendix D). Since the time the book was written, a software package called R has become the software choice of many students and staff and is now used extensively by the academic statistical community for statistical modelling, including time-se

3、ries modelling. R is a free updated version of S-Plus. Of course, many statistical software packages, including SPSS, SAS and Minitab, also contain Time Series Analysis modules which allow the analyst to model time series as they see fit. Microsoft Excel even contains a command within its Data Analy

4、sis add-in to carry out exponential smoothing! However, the R language and environment arguably provide greater depth and flexibility in many situations. Since R is used within a command-line interface, this may impose a steeper learning-curve for the new user, but the range of time series analysis

5、packages available in R, together with its publication-quality graphical capabilities, mean that it is increasingly the favoured package amongst serious time series analysts. We assume the reader is familiar with the basic use of R and indicate how to extend this to time-series analysis. The key ext

6、ension is that the time-series commands act on data called a ts object which are not just a set of numbers but have an order and a position in a cycle. For example, if the series is monthly, we may know that a particular observation is say the value in the 4th month of the second year.What is R?R is

7、 an open-source (i.e. free!) statistical software package, maintained by the user community themselves. It is distributed by CRAN (Comprehensive R Archive Network) and is available for download for Linux, MACOS X and Windows from the CRAN web-site at http:/cran.r-project.org. R uses the S language a

8、nd environment, developed at Bell Laboratories (now Lucent Technologies) by John Chambers and colleagues, and much of the code written for S can run under R.Resources in R for Time Series AnalysisA lot of resources for time series analysis are available to the R community including: several useful i

9、ndividual functions (such as plotting the sample autocorrelation and sample partial autocorrelation functions, fitting an ARIMA Model etc. for regularly spaced time series) included with the base R infrastructure additional packages for more extensive time series analysis, and for state-space models

10、 and spectral analysis time series datasets available directly in base R and in other time series packages books, on-line tutorials, and other on-line resources Time-series functions available in the base R packageSeveral commonly-used analysis tools for time series are available within the base R p

11、ackage, including (amongst others):acfproduces the sample autocorrelation function. User can specify maximum lags, or a vector of required lags. Can also produce sample autocovariance function and sample partial autocorrelation function. pacfas acf above, but produces just the sample partial autocor

12、relation functionarimafits a SARIMA model of order (p,d,q)x(P,D,Q), with period s. Method can be chosen from:MLMaximum LikelihoodCSSMinimising conditional sum of squaresCSS-MLUsing conditional sum of squares to find starting values, then maximum likelihood to fit the modelpredictpredicts n steps ahe

13、ad, from any fitted model, including a time series fitted using the command arima (see above)arima.simsimulates an ARIMA model of stated length of the order (p,d,q), with innovations having a stated variancetsdiagproduces 3 standard diagnostic charts for a fitted ARIMA model: plot of residuals from

14、the model sample autocorrelation function of the residuals from the fitted model Ljung-Box portmanteau statistic for stated maximum number of lags.spectrumproduces a spectral density using one of two methods: periodogram using Fast Fourier transforms, optionally smoothed with Daniell Smoothers to be

15、 specified autoregressive fits an AR model, and computes the spectral density of the fitted model.Alternatively, the command spec.prgrm can be used.Time series packages in RIn addition to the functions in base R, several time series analysis packages are available for specialised models, including:z

16、ooinfrastructure for both regularly- and irregularly-spaced time seriestseriescontains many specialised time series functions e.g. GARCH (Generalised AutoRegressive Conditional Heteroscedastic) model fittingctscontinuous-time AutoRegressive modelsdseDynamic Systems Estimation tools for multivariate,

17、 time-invariant models including state-space representationsdlmBayesian and likelihood analysis of Dynamic Linear Modelssspirtools for the specification of formulae to define and fit state-space models DatasetsThere are several datasets available with base R for time series analysis, including some

18、already featured in this text. Within base R, such datasets have often already been formulated into a ts (time series) class object, and the data can be recalled simply by inputting the dataset name. For example, the Box-Jenkins Monthly Airline Passenger Numbers (1949-1960) can be invoked as follows

19、: AirPassengers Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec1949 112 118 132 129 121 135 148 148 136 119 104 1181950 115 126 141 135 125 149 170 170 158 133 114 1401951 145 150 178 163 172 178 199 199 184 162 146 1661952 171 180 193 181 183 218 230 242 209 191 172 1941953 196 196 236 235 229 243

20、264 272 237 211 180 2011954 204 188 235 227 234 264 302 293 259 229 203 2291955 242 233 267 269 270 315 364 347 312 274 237 2781956 284 277 317 313 318 374 413 405 355 306 271 3061957 315 301 356 348 355 422 465 467 404 347 305 3361958 340 318 362 348 363 435 491 505 404 359 310 3371959 360 342 406

21、396 420 472 548 559 463 407 362 4051960 417 391 419 461 472 535 622 606 508 461 390 432As can be seen, the data has been referenced by month and by year.Similarly, Monthly Sunspot data from 1749 to 1997 can be invoked using: sunspot.month Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec1749 58.0 62.6

22、 70.0 55.7 85.0 83.5 94.8 66.3 75.9 75.5 158.6 85.21750 73.3 75.9 89.2 88.3 90.0 100.0 85.4 103.0 91.2 65.7 63.3 75.41751 70.0 43.5 45.3 56.4 60.7 50.7 66.3 59.8 23.5 23.2 28.5 44.01752 35.0 50.0 71.0 59.3 59.7 39.6 78.4 29.3 27.1 46.6 37.6 40.0 1993 59.3 91.0 69.8 62.2 61.3 49.8 57.9 42.2 22.4 56.4

23、 35.6 48.91994 57.8 35.5 31.7 16.1 17.8 28.0 35.1 22.5 25.7 44.0 18.0 26.21995 24.2 29.9 31.1 14.0 14.5 15.6 14.5 14.3 11.8 21.1 9.0 10.01996 11.5 4.4 9.2 4.8 5.5 11.8 8.2 14.4 1.6 0.9 17.9 13.31997 5.7 7.6 8.7 15.5 18.5 12.7 10.4 24.4 51.3 22.8 39.0 41.2Other regular time series can be read into R

24、and turned into a ts object using the ts command for example:ts(x, frequency = 4, start = c(1959, 2) transforms the vector x into a quarterly time series object (frequency = 4), starting in Quarter 2 of the year 1959.Time series packages have their own inherent datasets and time series -objects.On-l

25、ine and off-line resourcesSeveral Springer Texts in Statistics cover Time Series Analysis using R, including:Time Series Analysis & Its Applications: With R Examples (3rd ed) Robert Shumway and David Stoffer, Springer (2011)(In addition Stoffers own web-site includes a useful R Time Series Tutorial

26、at http:/www.stat.pitt.edu/stoffer/tsa2/R_time_series_quick_fix.htm)Introductory Time Series with R Examples Paul Cowpertwait and Andrew Metcalfe, Springer (2009)Time Series Analysis in R Examples (2nd ed) Jonathan Cryer and Kung-Sik Chan, Springer (2009)Three ExamplesThree examples have been select

27、ed to illustrate the use of R in time series analysis and to provide further guidance for the reader. They correspond to Examples 14.1, 14.2, and 14.3 in the book. This appendix concentrates on the R commands. For further discussion of the modelling process, see the book.Example 1.Monthly Air Temper

28、ature at RecifeTable 14.1 in the book shows the air temperature at Recife in Brazil in successive months over a 10-year period. The objective of our analysis is to describe and understand the data.The first step is to import the data into R and to produce an object of class ts:recife=ts(read.csv(E:R

29、ecife.csv,header=FALSE),start=1953,frequency=12)recife Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec1953 26.8 27.2 27.1 26.3 25.4 23.9 23.8 23.6 25.3 25.8 26.4 26.91954 27.1 27.5 27.4 26.4 24.8 24.3 23.4 23.4 24.6 25.4 25.8 26.71955 26.9 26.3 25.7 25.7 24.8 24.0 23.4 23.5 24.8 25.6 26.2 26.51956 2

30、6.8 26.9 26.7 26.1 26.2 24.7 23.9 23.7 24.7 25.8 26.1 26.51957 26.3 27.1 26.2 25.7 25.5 24.9 24.2 24.6 25.5 25.9 26.4 26.91958 27.1 27.1 27.4 26.4 25.5 24.7 24.3 24.4 24.8 26.2 26.3 27.01959 27.1 27.5 26.2 28.2 27.1 25.4 25.6 24.5 24.7 26.0 26.5 26.81960 26.3 26.7 26.6 25.8 25.2 25.1 23.3 23.8 25.2

31、25.5 26.4 26.71961 27.0 27.4 27.0 26.3 25.9 24.6 24.1 24.3 25.2 26.3 26.4 26.7Table 14.1:We now plot the data, as seen below:plot(recife,ylab=Temperature (degree C), xlab=Year,main=Recife, Brazil Temperature Data)The plot exhibits regular seasonal variation with little or no trend, as we would expec

32、t a priori. The correlogram is produced using the R command: acf(ts(recife,freq=1),lag.max=40,main=Autocorrelation Function for Recife Data,ylim=c(-1,1)(Note that the R function acfs default plot gives an X-axis in terms of the frequency (in this case 12) of the time series object recife. The autoco

33、rrelation function for lag 12 months is then labelled 1 year. Re-stating the frequency as 1 allows a plot of the autocorrelation function as we would expect it)The correlogram identifies the obvious seasonal variation, with high positive autocorrelations at lags 12, 24, We can remove the seasonality

34、 in the data by calculating monthly averages (see table below) and subtracting them from the raw data:MonthAv. Temp. 1953-161 (oC)January26.82February27.08March26.70April26.32May25.60June24.62July24.00August23.98September24.98October25.83November26.28December26.74The resulting, deseasonalised data i

35、s shown below:with the above (plot of the) sample ac.f (ignore the word periodogram in the title)The raw periodogram for the deseasonalised data is given below followed by three examples of smoothed periodograms using different Daniell filters (3,3), (5,5), and (7,7) to illustrate the smoothing effe

36、ct:Example 2Yield on short-term government securitiesWe first import and plot the data:yield1-read.csv(F:MA30085 50085 Time SeriesChatfield Data Filesyield.csv,header=FALSE)yield yield Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec1950 2.22 2.23 2.22 2.20 2.09 1.97 2.03 1.98 1.94 1.79 1.74 1.861951

37、 1.78 1.72 1.79 1.82 1.89 1.99 1.89 1.83 1.71 1.70 1.97 2.211952 2.36 2.41 2.92 3.15 3.26 3.51 3.48 3.16 3.01 2.97 2.88 2.911953 3.45 3.29 3.17 3.09 3.02 2.99 2.97 2.94 2.84 2.85 2.86 2.891954 2.93 2.93 2.87 2.82 2.63 2.33 2.22 2.15 2.28 2.28 2.06 2.541955 2.29 2.66 3.03 3.17 3.83 3.99 4.11 4.51 4.6

38、6 4.37 4.45 4.581956 4.58 4.76 4.89 4.65 4.51 4.65 4.52 4.52 4.57 4.65 4.74 5.101957 5.00 4.74 4.79 4.83 4.80 4.83 4.77 4.80 5.38 6.18 6.02 5.911958 5.66 5.42 5.06 4.70 4.73 4.64 4.62 4.48 4.43 4.33 4.32 4.301959 4.26 4.02 4.06 4.08 4.09 4.14 4.15 4.20 4.30 4.26 4.15 4.271960 4.69 4.72 4.92 5.10 5.2

39、0 5.56 6.08 6.13 6.09 5.99 5.58 5.591961 5.42 5.30 5.44 5.32 5.21 5.47 5.96 6.50 6.48 6.00 5.83 5.911962 5.98 5.91 5.64 5.49 5.43 5.33 5.22 5.03 4.74 4.55 4.68 4.531963 4.67 4.81 4.98 5.00 4.94 4.84 4.76 4.67 4.51 4.42 4.53 4.701964 4.75 4.90 5.06 4.99 4.96 5.03 5.22 5.47 5.45 5.48 5.57 6.331965 6.6

40、7 6.52 6.60 6.78 6.79 6.83 6.91 6.93 6.65 6.53 6.50 6.691966 6.58 6.42 6.79 6.82 6.76 6.88 7.22 7.41 7.27 7.03 7.09 7.181967 6.69 6.50 6.46 6.35 6.31 6.41 6.60 6.57 6.59 6.80 7.16 7.511968 7.52 7.40 7.48 7.42 7.53 7.75 7.80 7.63 7.51 7.49 7.64 7.921969 8.10 8.18 8.52 8.56 9.00 9.34 9.04 9.08 9.14 8.

41、99 8.96 8.861970 8.79 8.62 8.29 8.05 8.00 7.89 7.48 7.31 7.42 7.51 7.71 7.99The sample ac.f is produced using the command:yieldacf-acf(yield1,lag.max=50,main=Correlogram for Monthly Yield from British Short-Term Government Securities,ylim=c(-1,1)yieldacfValues of the ac.f are listed below, because t

42、hey differ from the values quoted in Chatfield 6th edition, namelylagChatfieldThis analysis10.970.9920.940.9730.920.9540.890.9350.850.91240.340.52: 1, 1.0000000 2, 0.9855462 3, 0.9680912 4, 0.9506049 5, 0.9317940 6, 0.9126326 7, 0.8922248 8, 0.8707009 9, 0.849594610, 0.829579211, 0.809158512, 0.7883

43、94513, 0.767441814, 0.744867815, 0.722140816, 0.700751117, 0.679276218, 0.658558219, 0.639071920, 0.617811721, 0.596046422, 0.574864523, 0.554477224, 0.536623625, 0.519586426, 0.504567827, 0.492092128, 0.483704029, 0.477261530, 0.471433531, 0.466190832, 0.460994633, 0.455473534, 0.450016935, 0.44401

44、4736, 0.437533337, 0.4302790These differences are being investigated by the author and are thought to be caused by an error in the data as originally listed. The correlogram is plotted below and shows the slow decay typical of a non-stationary series.We now take first differences of the data and plo

45、t the differenced series:diffyield=diff(yield, lag=1)plot(diffyield,main=Differenced Monthly Yield from British Short-Term Government Securities,ylab=Percent,xlab=Year,ylim=c(-1,1)The sample ac.f of the first differences is produced via:diffyieldacf diffyieldacf$acf, , 1 1, 1.000000e+00 2, 3.424154e-01 3, 4.511240e-02 4, 1

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 教育教学 > 成人教育


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号