Diethelm Würtz

Yohan Chalabi
Andrew Ellis

A Discussion of
Time Series Objects
for R in Finance
September 24, 2009

Rmetrics Association & Finance Online
Zurich

Series Editors:
PD Dr. Diethelm Würtz
Institute of Theoretical Physics and
Curriculum for Computational Science
Swiss Federal Institute of Technology
Hönggerberg, HIT K 32.2
8093 Zurich

Dr. Martin Hanf
Finance Online GmbH
Weinbergstrasse 41
8006 Zurich

Contact Address:
Rmetrics Association
Weinbergstrasse 41
8006 Zurich
info@rmetrics.org

Publisher:
Finance Online GmbH
Swiss Information Technologies
Weinbergstrasse 41
8006 Zurich

Authors:
Yohan Chalabi, ETH Zurich
Andrew Ellis, Finance Online GmbH Zurich
Diethelm Würtz, ETH Zurich

ISBN:
eISBN:
DOI:

© 2009, Finance Online GmbH, Zurich
Permission is granted to make and distribute verbatim copies of this manual provided
the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under
the conditions for verbatim copying, provided that the entire resulting derived work is
distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another
language, under the above conditions for modified versions, except that this permission
notice may be stated in a translation approved by the Rmetrics Association, Zurich.
Limit of Liability/Disclaimer of Warranty: While the publisher and authors have used
their best efforts in preparing this book, they make no representations or warranties
with respect to the accuracy or completeness of the contents of this book and specifically
disclaim any implied warranties of merchantability or fitness for a particular purpose. No
warranty may be created or extended by sales representatives or written sales materials.
The advice and strategies contained herein may not be suitable for your situation. You

vi
should consult with a professional where appropriate. Neither the publisher nor authors
shall be liable for any loss of profit or any other commercial damages, including but not
limited to special, incidental, consequential, or other damages.
Trademark notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation, without intent to infringe.

© 2009, Finance Online – Please do not distribute

vii Revision History May 2009: 1st edition September 2009: 1st edition Several small fixes in the packages zoo. © 2009. xts and timeSeries have been considered in this 2nd edition. Finance Online – Please do not distribute . the discussion of this will go to the 3rd edition. zoo has recently added a print style argument to its print function.

.

We are counting on your support to keep this FAQ up-to-date. or even missing. For each question you will find an answer together with a generic example. we not yet found a typical example. the answers are still incomplete.Preface About this Book Are you working with R and Rmetrics in the field of finance? Then you will usually use either xts/zoo or timeSeries/timeDate as your preferred time series package of functions and methods to create and manage financial time series objects. for inclusion in this ebook. what are the specific differences. We would also very much appreciate it if users and developers of the packages under discussion would send us further FAQs together with the answer. This ebook tries to answer any questions you might have about these issues. Often you may have asked yourself how certain functions in one time series package compare to their counterparts in the other package. In some cases. The ebook does not recommend or give any preferences to one of the time series classes. In which sense do functions behave differently when you work with the xts/zoo package or with the timeSeries/timeDate package. In other cases. The answers are intended to be unbiased and if you are ix . Does a given function from one package have an equivalent one in the other packages and if yes.

This FAQ is a Sweave document and we will keep it up-to-date with the most recent packages available on R. datasets. graphics.x Preface unsatisfied with any answer. methods.gnu. utils It can be downloaded from the CRAN2 (Comprehensive R Archive Network) web site. The goal is also that you learn to interpret and to better understand the output of the R date and time series management functions. timeDate and timeSeries are also downloadable from this site. We hope that the ebook can also serve as a source to generate (common) efforts of the developers to align the design and concepts used in zoo.1 (2009-06-26).9. xts. Most importantly.org website. The FAQs are published under the GNU GPL License1 and can be downloaded from the www. Note that the book is not only intended as a FAQ. the authors assume that the reader is interested in analyzing and modelling financial data sets with R and Rmetrics. the most recent copy of R was: • R version 2. Finance Online – Please do not distribute .org © 2009. please let us know and give us a suggestion how to improve it. grDevices. Audience Background It is assumed that the reader has a basic familiarity with the R statistical environment and knows the packages xts/zoo and timeSeries/timeDate. A background in finance will be helpful but not necessary. Getting Started When this ebook was written. packages can be installed directly in 1 2 http://www.rmetrics.html http://cran-r-project. i386-pc-mingw32 • Base packages: base. Alternatively. xts and timeSeries. stats. The contributed R packages zoo.org/licenses/licenses. we do not use development versions from the r-forge repository.

Bernhard Pfaff. May 2009. Stephan Joeri. and the participants of the RinFinance Workshop in Chicago April 2009. Acknowledgements This book would not be possible without the R environment developed by the R Development Core Team (2009). helping us to test the software. Martin Hanf. Dirk Eddelbuettel. September 2009. Kurt Hornik. David Lüthi. This book is the second in a series of Rmetrics ebooks. Dominik Lutz. from managing chronological objects to dealing with risk and managing portfolios.Preface xi the R environment. The R packages used in this ebook are described in the appendix where you can also find a copy of the description file. Dominik Locher. Martin Mächler. Alexios Ghalanos. Finance Online GmbH in Zurich. Finance Online – Please do not distribute . David Scott. Thanks also to those who contribute to the R-sig-finance mailing list. 2008 and 2009. Mahendra Mehta. Jeff Ryan. Stefan Theussl. Achim Zeileis. Francisco Gochez. Brian Petersen. 2nd Edition © 2009. 1st Edition Zurich. Oliver Greshake. Enjoy it! Diethelm Würtz Zurich. Peter Carl. These ebooks will cover the whole spectrum of Rmetrics and related packages. and the participants of the R/Rmetrics Meielisalp Workshops 2007. We are also grateful to many people who have read and commented on draft material and on previous manuscripts. We cannot name all who have helped us but we would to thank Enrique Bengoechea. William Chen. the Institute for Theoretical Physics at ETH Zurich. Elmar Heeb.

2 Regular Time Series Objects . . . . . . 165 xii . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Using R Functions 131 3. . . .5 Binding and Merging of Time Series . .4 Ordering and Overlapping of Time Series . . . . . . . . .1 Creating Time Series Objects . . . . . . . . . . 146 3. . . . . . .2 Plotting Time Series Objects . . . . . 12 1. . . . .8 Missing Data Handling . . . . . . 32 1. . . . . . . . . . . .7 Group Generics for Time Series Objects . . . . . . . . .6 Subsetting Time Series Objects . 132 3. . . 112 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Printing Time Series Objects . . . . . . . . . . . . . . . . . . . . . 43 1. . . . . . .2 Functions and Methods from stats R . . . . . . . . . . 77 1. . . . . . . . 63 1. . . . . . . . . . 2 1. . . .3 Functions and Methods from utils R . . . . . . . . . .1 Functions and Methods from base R . . 20 1. . . .Contents Preface ix Contents 1 2 3 xii Time Series Basics 1 1. . . . . . . . . . . . 92 Reporting 111 2. . . . . . .3 Time Zone and Daylight Saving Time . . . . . . . . . . . . . . . . .

2 Performance of Time Series subsetting . . . . . . . . . . . . . . . . . . Finance Online – Please do not distribute . . . 176 A Packages Required for this Ebook 183 B 187 Function Listings Bibliography 199 Index 201 About the Authors 205 © 2009. .1 Performance of Time Series Creation . . . . . 170 4. .Contents xiii 4 169 Performance Measurements 4. . . . . . . . .

.

Chapter 1 Time Series Basics 1 .

information and POSIXct when work with intraday data records and when time zones and daylight saving times. become relevant. zoo What time stamp index classes are usually used to create timeSeries objects? For zoo and xts these are Date for the use with daily data records where we don’t care about time zone.2 1. Creating Time Series Objects 1. timeSeries objects are independent of the type of time stamps used for its creation.1 Creating Time Series Objects Required R package(s): > library(zoo) > library(xts) > library(timeSeries) Which are the preferred time series classes in R for financial applications? These are zoo from the R package zoo. Time stamps in timeSeries objects are always of class numeric. its extension xts from R’s xts package. DST. © 2009. What are the major differences concerning the time stamps of these three time series classes ? and xts are time series classes with time stamp indices depending on the time stamp index class which we used for their creation.1. and timeSeries from the R/Rmetrics package timeSeries. TZ. Finance Online – Please do not distribute . which uses the timeDate package for its time stamps. The time stamps used for timeSeries objects are timeDate objects with internal Olsen time zone data base information.

tz = "". TZ. Using timeSeries objects you get always the same result on any computer.) NULL > args(as. are relevant.. For this cases the result depends on the internal implementation of time zone and daylight saving time rules which may be different for different operating systems.POSIXct) function (x. order. What influence does your current time zone environment have on the creation of time series when you don’t care about time zone settings of your environment? Using for zoo and xts the function as. order. © 2009.by = index(x). The reason is that timeSeries objects are handled internally with timeDate objects which always operate on POSIXct objects in "GMT" and save the TZ and DST information separate as delivered from Olsens time zone database made available by Rmetrics.) NULL Thus.1.POSIXct with default settings as index class creates time stamps with respect to the setting of the time zone in your local environment. users in London.. unique = TRUE. information and POSIXct when working with intraday data records and when time zones and daylight saving times. For zoo and xts these are Date for the use with daily data records where we don’t care about time zone. frequency = NULL) NULL > args(xts) function (x = NULL. The same holds if we create the index from the function ISOdatetime..by = index(x). Time Series Basics 3 Do time series depend on the way they were created? This may be the case for zoo and xts time series which were created in different time zones under different operating systems. The time stamps used for timeSeries objects are timeDate objects which rely on Olsen’s time zone data base information. Finance Online – Please do not distribute . New York or Tokyo will get different results.. frequency = NULL. DST. . > args(zoo) function (x = NULL. .

timezone() [1] "GMT" How should I create time series objects with daily time stamps when they are given as character formatted time stamps? For zoo and xts time series object the best index class to create a series with daily time stamps is to use Date. units = NULL.4 1. documentation = NULL. FinCenter = "". tz = "") NULL Be careful in using the function ISOdate. day.frame(). format = NULL. tz = "GMT") NULL By default the function generates an index in GMT and an additional offset of 12 hours. min. title = NULL. > args(timeSeries) function (data.. hour.1. month. FinCenter = "") NULL What is my current time zone environment? Use the function Sys. sec.. © 2009. recordIDs = data. month. sec = 0. min = 0. Creating Time Series Objects > args(ISOdatetime) function (year. > args(ISOdate) function (year. Finance Online – Please do not distribute . format = NULL. zone = "". > Sys. day. Note that all three functions return an index of class POSIXct.) NULL > args(timeDate) function (charvec.timezone() to find out your current time zone settings. charvec. . For timeSeries objects this considerations are obsolete when you create timeSeries objects from character strings and the appropriate time zone or in our notation financial centre information. zone = "". hour = 12.

Common Data: > set. 1:6.rnorm(6) > charvec <.413238 0.186621 2009-05-01 0. Time Series Basics 5 > args(as. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > zoo(data.230818 0. For timeSeries object the preferred option is to use ans input argument for the time stamps the character vector.1.seed(1953) > data <.021925 -0.1] 2009-01-01 0. as.904325 2009-03-01 0.235680 © 2009. they are handled like objects with TZ "GMT".904325 0. .413238 2009-04-01 0.paste("2009-0".186621 0.) NULL Date objects don’t care about time zone information. Finance Online – Please do not distribute .235680 xts: > xts(data.021925 2009-02-01 -0. as.Date(charvec)) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0..Date(charvec)) [. "-01".230818 2009-06-01 0..Date) function (x.

Finance Online – Please do not distribute .330700 2009-01-04 -0.504959 2008-12-30 1.201590 2008-12-28 -0.1326078 0.4211978 0.021925 2009-02-01 -0.Date(now)-0:10) © 2009.4837389 0.473622 2008-12-27 -1.1866212 -0.matrix(rnorm(22).1 2009-01-01 0. Creating Time Series Objects timeSeries: > timeSeries(data.259976 2008-12-29 1.235680 How can I create time series objects with daily time stamps for the previous 50 days? Common Data: > set.2308177 -1.4132378 -2.0994627 2.186621 2009-05-01 0.9043255 -0. charvec) GMT TS.791782 2009-01-01 0.2356802 0. ncol = 2) > now <."2009-01-05" zoo: > zoo(data.517750 2009-01-02 0.6 1.0046855 -0.075514 2009-01-05 0.1.413238 2009-04-01 0.523212 2009-01-03 0.904325 2009-03-01 0.0219246 0. as.570239 2008-12-31 0.seed(1953) > data <.164796 xts: > xts(data. as.230818 2009-06-01 0.Date(now)-0:10) 2008-12-26 -0.

"-01".164796 timeSeries: > timeSeries(data. Time Series Basics 7 [.1326078 0.2308177 -1.0994627 2.Date(now)-0:10) GMT 2009-01-05 TS.517750 2009-01-02 0.473622 2008-12-27 -1.164796 2009-01-04 -0.4837389 0.2 0.473622 The timeSeries function can also handle R’s Date objects.523212 2009-01-03 0.4837389 0.2356802 0.0046855 -0.9043255 -0.4132378 -2. Finance Online – Please do not distribute . sep = "") > charvec © 2009.1326078 0.seed(1953) > data <.1866212 -0.1 TS.791782 2009-01-01 0.0219246 0.330700 2009-01-04 -0.504959 2008-12-30 1.075514 2009-01-05 0.4211978 0.523212 2009-01-01 0.517750 2008-12-31 0.259976 2008-12-29 1.570239 2008-12-31 0.4211978 0.075514 2009-01-03 0. as.2356802 0.330700 2009-01-02 0.0994627 2.259976 2008-12-27 -1.4132378 -2.791782 2008-12-30 1.1866212 -0.0219246 0.2] 2008-12-26 -0.201590 2008-12-28 -0.1. What can go wrong when I create time series objects from POSIXct indices? Common Data: > set.paste("2009-0".rnorm(6) > charvec <.1] [. 1:6.504959 2008-12-28 -0.201590 2008-12-26 -0.9043255 -0.570239 2008-12-29 1.2308177 -1.0046855 -0.

1:6.235680 Note that all three examples for zoo time series objects deliver the same output.413238 0.904325 0. 1:6. ISOdate(2009.021925 [.POSIXct(charvec)) > z2 <. ISOdatetime(2009. 0)) > z3 <. z2.1] 2009-01-01 0. Finance Online – Please do not distribute .021925 2009-02-01 -0.413238 0. 0)) > x3 <. xts: > x1 <.413238 2009-04-01 0.230818 0.021925 -0.021925 -0.186621 0.230818 0. 1:6. 0.186621 0.235680 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0. as.904325 0. Creating Time Series Objects [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > z1 <.904325 2009-03-01 0.zoo(data.186621 0.230818 2009-06-01 0. ISOdate(2009.904325 0.235680 2009-01-01 0.xts(data.413238 0.zoo(data.186621 2009-05-01 0. z3 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0. x3 [.235680 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0. 1.186621 © 2009.904325 2009-03-01 0. as. 1. 0.zoo(data. 1. 0)) > x1.413238 2009-04-01 0.1] 2009-02-01 -0.1.xts(data. 0. 1:6.xts(data. nothing can bee seen from the output about the conditions under which the the time stamps were created. 1. x2.021925 -0. 0)) > z1. ISOdatetime(2009.230818 0.8 1.POSIXct(charvec)) > x2 <. 0.

235680 timeSeries: > s1 <.1 2009-01-01 0.230818 2009-06-01 0. 0.904325 2009-03-01 0.904325 2009-03-01 0. s2.timeSeries(data.235680 GMT TS.235680 GMT TS.021925 2009-02-01 -0. ISOdate(2009.230818 2009-06-01 0. 1. 1:6. 0.413238 2009-04-01 0.904325 2009-03-01 0. 0)) > s1.413238 2009-04-01 0.235680 © 2009.235680 2009-01-01 0.1.186621 2009-05-01 0.021925 2009-02-01 -0.1] 2009-02-01 -0.230818 2009-06-01 0.413238 2009-04-01 0.1 2009-01-01 0.230818 2009-06-01 0.1 2009-01-01 0. s3 GMT TS.timeSeries(data. Time Series Basics 2009-05-01 0.186621 2009-05-01 0. Finance Online – Please do not distribute . 1.timeSeries(data. charvec) > s2 <. ISOdatetime(2009.021925 2009-02-01 -0. 0)) > s3 <.186621 2009-05-01 0.186621 2009-05-01 0.413238 2009-04-01 0.904325 2009-03-01 0.230818 2009-06-01 0.021925 9 [. 1:6.

Finance Online – Please do not distribute . zoo: > class(index(z1)) [1] "POSIXt" "POSIXct" > class(index(z2)) [1] "POSIXt" "POSIXct" > class(index(z3)) [1] "POSIXt" "POSIXct" xts: > class(index(x1)) [1] "POSIXt" "POSIXct" > class(index(x2)) [1] "POSIXt" "POSIXct" > class(index(x3)) [1] "POSIXt" "POSIXct" timeSeries: > class(time(s1)) [1] "timeDate" attr(.10 1."package") [1] "timeDate" > class(time(s2)) © 2009. Creating Time Series Objects Does the index/time of a time series object depend on how the object was created? Common Data: We take the time series from the previous FAQ.1.

"package") [1] "timeDate" © 2009.1. Time Series Basics 11 [1] "timeDate" attr(."package") [1] "timeDate" > class(time(s3)) [1] "timeDate" attr(. Finance Online – Please do not distribute .

For example. names = if (!is.null(dimnames(data))) colnames(data) else paste("Series". high. the data arrives predictably at predefined intervals.eps"). deltat = 1. So the definition here is quite different to that what a R user or Programmer has in mind. end = numeric(0). start = 1.2 Regular Time Series Objects Required R package(s): > library(zoo) > library(xts) > library(timeSeries) First find out what is a regular time series? The oracle data base management manual tells us: A time series can be regular or irregular. ts.eps = getOption("ts. "ts") else "ts". low. In R we understand a regular time series as a time series with fixed equidistant units in a calendarical book keeping define a ∆ for the distances between two time stamps or frequency of incoming data points. Here are some examples: The most widely used regular time series include monthly and quarterly data points neglecting day and time stamps.2. © 2009. frequency = 1. For example.12 1. their frequency (per year) is then 12 for monthly data. Finance Online – Please do not distribute . Regular Time Series Objects 1. daily summaries of stock market data form a regular time series. The calendarical counter are then the years (and months). and one such time series might be the set of trade volumes and opening. An irregular time series may have long periods with no data or short periods with bursts of data. and closing prices for stock XYZ for the year 1997. and 4 for quarterly data. depending on how predictably data points arrive or occur: • In a regular time series. unpredictable bursts of data arrive at unspecified points in time. This is very restrictive. or most timestamps cannot be characterized by a repeating pattern. class = if (nseries > 1) c("mts". account deposits and withdrawals from a bank automated teller machine (ATM) form an irregular time series. > args(ts) function (data = NA. • In an irregular time series.

frequency = 4) Qtr3 Qtr4 2008 Qtr1 0.3644 0.4968 -1. 3). 3).round(rnorm(24). start = c(2008.4938 -0.3214 1. start = c(2008.7233 -0.0906 2.1193 0.0271 Jul Aug 0.4471 -0. 3).0841 0.4640 -1.8969 0.2801 0. start = c(2008.6240 -1.7276 May Jun 0.9402 -1.0174 0.7276 0. frequency = 12) > tm Jan Feb 2008 2009 Mar Apr May Jul Aug 0.8801 2009 -0.1219 2013 -0.2446 1.2801 Mar Apr 0.0438 0.4968 -1. Finance Online – Please do not distribute .1268 -0.8979 1.0209 -0.8061 -1. frequency = 12) Jan Feb 2008 2009 0.round(rnorm(24).7286 -0.8061 -1. Time Series Basics 13 seq(nseries))) NULL > data <.0659 2010 > ts(data.7233 -0.0174 2011 Qtr2 0.1219 2010 -0.0517 -0.2446 1.0841 0.0209 -0.0101 How can I create a regular monthly time series object? Common Data: > data <.0271 2009 0.1597 -0. 4) ts: > tm <.1307 0.1193 0.8969 0.2742 Jun 2010 -0.3392 0.8801 0.4900 0.0621 -0.ts(data.0101 Sep Oct Nov Dec 2008 -1.5464 © 2009.6240 -1.1.1597 2012 -0.9318 -0.0648 -1.1085 Nov Dec 0.0906 2.7621 -0.0517 -0.0648 -1.8979 1.1795 Sep Oct 2008 -1.9318 -0.3644 2010 -1.4798 -0.7091 -0.2274 0.0659 2014 -0. 4) > ts(data.

5464 Jan 2009 0.7621 0.1] Mar 2008 1.2742 May 2009 0.4471 -0.14 1.1307 0.1268 Jul 2008 -0.7714 -0.1268 -0.4900 0.7091 2008(11) 2008(12) 2009(1) 2009(2) 2009(3) 2009(4) 2009(5) 2009(6) 0.0438 Jun 2008 0.4900 Aug 2009 0.4798 Jun 2009 -0.4640 Apr 2008 -1.3214 Sep 2009 -1.3392 Mar 2009 0.4798 -0.0146 xts: > xm <.7714 Oct 2009 -0.3392 0.3214 2008(9) 2008(10) -0.xts(tm) > xm [.4471 Apr 2009 -0. frequency = 12) > zm 2008(3) 2008(4) 2008(5) 2008(6) 2008(7) 2008(8) 1. Regular Time Series Objects 2009 -1.2.3695 0.3695 © 2009.4938 -0.9545 Nov 2009 -0.zooreg(data. 3).5464 2009(7) 2009(8) -0.as.7286 -0.7091 Oct 2008 -0.0438 0.2274 Sep 2008 -1.9545 -0.4640 -1. Finance Online – Please do not distribute .1307 Aug 2008 0.2742 0.3695 0.0146 2010 zoo: > zm <. start = c(2008.4938 Dec 2008 -0.0621 -0.1085 Nov 2008 0.7286 May 2008 -0.1085 2009(9) 2009(10) 2009(11) 2009(12) 2010(1) 2010(2) -1.2274 -1.1795 -0.9402 Feb 2009 -1.9545 -0.7621 Jul 2009 -0.9402 -1.7714 -0.

3214 2009-09-30 -1.0146 Jan 2010 -0.1795 timeSeries: > sm <.2274 2008-09-30 -1.3695 2009-12-31 0.7286 -0.1307 2008-08-31 0.4640 -1.4938 2008-12-31 -0.0438 0.1307 0.7621 2009-07-31 -0.9402 2009-02-28 -1.1268 -0.1268 2008-07-31 -0.5464 0.1085 2008-11-30 0.3392 2009-03-31 0.9402 -1. Finance Online – Please do not distribute .4798 2009-06-30 -0.7091 -0.9545 2009-11-30 -0.4938 -0.1. Time Series Basics Dec 2009 15 0.4471 2009-04-30 -0.1085 0. style = "h") 2008-03-31 2008-04-30 2008-05-31 2008-06-30 2008-07-31 2008-08-31 1.0621 2010-02-28 -0.0146 2010-01-31 -0.0438 2008-06-30 0.4640 2008-04-30 -1.as.7091 2008-10-31 -0. > print(sm.7286 2008-05-31 -0.3392 © 2009.1 2008-03-31 1.0621 Feb 2010 -0.timeSeries(tm) > sm GMT TS.2742 2009-05-31 0.2274 2008-09-30 2008-10-31 2008-11-30 2008-12-31 2009-01-31 2009-02-28 -1.7714 2009-10-31 -0.4900 2009-08-31 0.5464 2009-01-31 0.1795 Can timeSeries objects be printed in a regular time series style? Yes.

4471 -0.0146 -0.0438 0.16 1.8574 -0.1268 -0.3695 0.1795 > print(sm.5507 -0.1795 Can timeSeries objects be printed in customized format? Yes. style = "h".3721 0.1795 How can I create a regular quarterly time series object? Common Data: > data <.4053 -1.1085 2008(11) 2008(12) 2009(01) 2009(02) 2009(03) 2009(04) 2009(05) 2009(06) 0.0146 -0.4640 -1.5491 1.7714 -0.7286 -0. Finance Online – Please do not distribute .4938 -0.4968 2010 0.1307 0.2274 -1.1268 -0. format = "%Y(%m)") 2008(03) 2008(04) 2008(05) 2008(06) 2008(07) 2008(08) 2008(09) 2008(10) 1.4640 -1.7621 2009(07) 2009(08) 2009(09) 2009(10) 2009(11) 2009(12) 2010(01) 2010(02) -0.2742 0.7091 -0. format = "%Y %b") 2008 Mar 2008 Apr 2008 May 2008 Jun 2008 Jul 2008 Aug 2008 Sep 2008 Oct 1.round(rnorm(24).0621 -0.0621 -0.5464 0.5464 0. 4) ts: > tq <.7621 2009 Jul 2009 Aug 2009 Sep 2009 Oct 2009 Nov 2009 Dec 2010 Jan 2010 Feb -0.3695 0.8821 2014 -0. 3).5100 0.4938 -0.ts(data.1863 2009 -0.3214 -1.4471 -0.1085 2008 Nov 2008 Dec 2009 Jan 2009 Feb 2009 Mar 2009 Apr 2009 May 2009 Jun 0. frequency = 4) > tq Qtr1 Qtr2 2008 Qtr3 Qtr4 -0. > print(sm.3991 1.4471 -0.7286 -0.0093 0.9402 -1. Regular Time Series Objects 2009-03-31 2009-04-30 2009-05-31 2009-06-30 2009-07-31 2009-08-31 0.7091 -0.5040 -0.2742 0.7714 -0.9402 -1.7621 -0. start = c(2008.0169 0.1971 -1.9545 -0.7714 -0.0621 -0.4798 -0.9545 -0.4900 0.0146 -0.3392 0.7443 0.4900 0.2274 -1.3544 0.3214 -1.0438 0.3695 0.4373 -2.3214 2009-09-30 2009-10-31 2009-11-30 2009-12-31 2010-01-31 2010-02-28 -1.1298 2011 0.5955 2013 0.2742 0.1307 0.2629 2012 0.9545 -0.8292 -0.9128 © 2009.4900 0. style = "h".2355 1.4798 -0.4798 -0.3392 0.2.3288 2.

0093 0.4373 -2.0093 2009 Q2 0.5100 0.4968 0.1863 2009-03-31 -0.5040 -0.8292 -0.4053 -1.5507 -0.1863 -0.8574 2009 Q4 -0.xts(tq) > head(xq) [.0169 2008 Q4 0.4968 timeSeries: > sq <. style = "h") © 2009.3544 0.1863 2009 Q1 -0.5491 1. Time Series Basics 17 zoo: > zq <.1.8821 -0.as.8574 2009-12-31 -0.5507 2009-09-30 -0.1971 -1.2355 1.3721 0.0093 2009-06-30 0. start = c(2008.1298 2010(4) 2011(1) 2011(2) 2011(3) 2011(4) 2012(1) 2012(2) 2012(3) 2012(4) 0.1] 2008 Q3 -0.1 2008-09-30 -0.4968 Can timeSeries objects be printed in a regular time series style? Yes.zooreg(data. frequency = 4) > zq 2008(3) 2008(4) 2009(1) 2009(2) 2009(3) 2009(4) 2010(1) 2010(2) 2010(3) -0.5955 2013(1) 2013(2) 2013(3) 2013(4) 2014(1) 2014(2) 0.timeSeries(tq) > head(sq) GMT TS. 3). Finance Online – Please do not distribute .0169 0.3288 2.5507 2009 Q3 -0.0169 2008-12-31 0.2629 0.7443 0.8574 -0.9128 xts: > xq <. > print(sq.3991 1.as.

0169 0.1863 -0.4968 0.5507 -0.4053 -1. > print(sq.3288 2.3544 2012 Sep 2012 Dec 2013 Mar 2013 Jun 2013 Sep 2013 Dec 2014 Mar 2014 Jun 0.7443 0. Finance Online – Please do not distribute .regular(z) [1] TRUE © 2009.5507 -0.0093 0.5491 1.8292 -0.5040 -0.18 1.8292 -0.2629 0.5507 -0.1863 -0.9128 Can timeSeries objects be printed in customized format? Yes.2355 1.5100 0.0093 0.1298 2010 Q4 2011 Q1 2011 Q2 2011 Q3 2011 Q4 2012 Q1 2012 Q2 2012 Q3 2012 Q4 0.3288 2.5100 0.zooreg(data.8574 -0.7443 0.4373 -2.4968 2010-03-31 2010-06-30 2010-09-30 2010-12-31 2011-03-31 2011-06-30 0.5100 0.2355 1.5955 2013 Q1 2013 Q2 2013 Q3 2013 Q4 2014 Q1 2014 Q2 0.3721 0.1971 -1.4373 2010 Sep 2010 Dec 2011 Mar 2011 Jun 2011 Sep 2011 Dec 2012 Mar 2012 Jun -2.5040 -0.2629 0.5955 2013-03-31 2013-06-30 2013-09-30 2013-12-31 2014-03-31 2014-06-30 0. style = "h". start = c(2008.7443 0.5955 0.3991 1.8821 -0.5491 1.8821 -0.1298 0.8292 -0.0093 0.4373 -2.3721 0.1971 -1.0169 0. frequency = 4) > is.2355 1.1971 -1.4053 -1.3721 0.9128 How I can find out if a time series is a regular time series? zoo: > z <.0169 0.8574 -0.1298 0.5040 2011-09-30 2011-12-31 2012-03-31 2012-06-30 2012-09-30 2012-12-31 -0.4053 -1.8574 -0. format = "%Q") 2008 Q3 2008 Q4 2009 Q1 2009 Q2 2009 Q3 2009 Q4 2010 Q1 2010 Q2 2010 Q3 -0. style = "h".9128 > print(sq.3288 2.5491 1. 3).3544 0.2.3544 0.2629 0.8821 -0.3991 1. Regular Time Series Objects 2008-09-30 2008-12-31 2009-03-31 2009-06-30 2009-09-30 2009-12-31 -0.3991 1.1863 -0.4968 0. format = "%Y %b") 2008 Sep 2008 Dec 2009 Mar 2009 Jun 2009 Sep 2009 Dec 2010 Mar 2010 Jun -0.

Time Series Basics 19 1. Finance Online – Please do not distribute .1.zrh <.xts(data. "-01".POSIXct(charvec.zoo(data.zrh <. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > z1.zrh 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 1 2 3 4 5 6 xts: > x1. tz = "CET")) > z1.paste("2009-0". as. Common Data: > data <. tz = "CET")) > x1.3 Time Zone and Daylight Saving Time Required R package(s): > library(zoo) > library(xts) > library(timeSeries) How can I create a time series object which takes care of time zone settings? Let us create a time series in Zurich which belongs to the “Central European Time" zone. 1:6.POSIXct(charvec. as.zrh © 2009.1:6 > charvec <. CET.

timeSeries: > s1.3.) NULL Have a look at the output of the possible four options. Finance Online – Please do not distribute . zone = "". FinCenter = "". charvec.. . > timeSeries(data. documentation = NULL.zrh. units = "s1 . charvec. recordIDs = data.1] 2008-12-31 23:00:00 1 2009-01-31 23:00:00 2 2009-02-28 23:00:00 3 2009-03-31 22:00:00 4 2009-04-30 22:00:00 5 2009-05-31 22:00:00 6 With the newest version of xts this cannot any longer be done.zrh Zurich TS. title = NULL.frame().1 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 Note that the timeSeries function has two time zone relevant inputs. units = NULL. To our understanding xts now forces always the time stamps to GMT time zone. format = NULL. zone = "Zurich". FinCenter = "Zurich") > s1. FinCenter = "Zurich". zone = "Zurich".zrh <.20 1. The argument zone holds the information to which time zone or to be more specific to which financial centre the charvec of time stamps belongs. Time Zone and Daylight Saving Time [. charvec. > args(timeSeries) function (data.zrh") © 2009. and the second argument FinCenter tells us in which time zone or at which financial centre we want to display and use the time series data..timeSeries(data.

gmt 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 © 2009. zone = "GMT". gmt.zrh.gmt. FinCenter = "GMT".gmt. units = "s1. Finance Online – Please do not distribute .zrh 2009-01-01 01:00:00 1 2009-02-01 01:00:00 2 2009-03-01 01:00:00 3 2009-04-01 02:00:00 4 2009-05-01 02:00:00 5 2009-06-01 02:00:00 6 > timeSeries(data. charvec. Time Series Basics 21 Zurich s1. zone = "Zurich". zone = "GMT".zrh.1. units = "s1. units = "s1.gmt") GMT s1. FinCenter = "Zurich".gmt 2008-12-31 23:00:00 1 2009-01-31 23:00:00 2 2009-02-28 23:00:00 3 2009-03-31 22:00:00 4 2009-04-30 22:00:00 5 2009-05-31 22:00:00 6 > timeSeries(data. charvec. charvec. gmt") GMT s1.zrh") Zurich s1.gmt. zrh. FinCenter = "GMT".zrh 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 > timeSeries(data.

sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: Note. Finance Online – Please do not distribute .xts(data.1:6 > charvec <. e. 1:6. tz = "CET")) > x1.paste("2009-0".zrh <. as. > x1.zoo(data.1] 2008-12-31 23:00:00 1 2009-01-31 23:00:00 2 2009-02-28 23:00:00 3 2009-03-31 22:00:00 4 2009-04-30 22:00:00 5 2009-05-31 22:00:00 6 © 2009.22 1. "-01".zrh [. objects of class POSIX.zrh <.g. xts: Also xts objects must be indexed by an index which supports time zones. > z1. the zoo object must be indexed by an index which supports time zones. Time Zone and Daylight Saving Time When I print a time series can I see what time zone the series belongs to? Common Data: > data <. as. tz = "CET")) > z1.POSIXct(charvec.POSIXct(charvec.zrh 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 1 2 3 4 5 6 For zoo time series objects the time zone cannot be seen from printing the series.3.

zoo(data.paste("2009-0". "-01".1:6 > charvec <. The information is taken from the time stamps which are objects of class timeDate.1:6 > charvec <.1 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 Note that timeSeries objects show on top to which time zone (or more specific financial centre) they belong.zrh Zurich TS. sep = "") > z1. as.zrh) © 2009. "-01". Time Series Basics 23 timeSeries: > s1. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > data <.POSIXct(charvec. zone = "Zurich".timeSeries(data. 1:6. 1:6. tz = "CET")) > index(z1.zrh <. charvec.zrh <.1. Finance Online – Please do not distribute . FinCenter = "Zurich") > s1. How can I find out to what time zone a time series belongs? To find out the time zone information we can use the functions index for zoo and xts objects and the function time for timeSeries objects to display the zone information Common Data: > data <.paste("2009-0".

zone = "Zurich". FinCenter = "Zurich") > time(s1.zrh <.zrh) Zurich [1] [2009-01-01] [2009-02-01] [2009-03-01] [2009-04-01] [2009-05-01] [6] [2009-06-01] Note that for timeSeries objects we can also retrieve and save zone information using the accessor function finCenter().frame of DST rules. To shorten the output we subset it here to some records © 2009. > currentCenter <.POSIXct(charvec. timeSeries: For timeSeries objects you can look in the data.zrh) > currentCenter [1] "Zurich" How can I display the DST rules used for the creation of time series objects? zoo and xts: There is no obvious way to do it.24 1. as. charvec.finCenter(s1.zrh <.3. tz = "CET")) > index(x1.timeSeries(data. Time Zone and Daylight Saving Time [1] "2009-01-01 CET" "2009-02-01 CET" "2009-03-01 CET" "2009-04-01 CEST" [5] "2009-05-01 CEST" "2009-06-01 CEST" xts: > x1. Finance Online – Please do not distribute .xts(data.zrh) [1] "2008-12-31 23:00:00 GMT" "2009-01-31 23:00:00 GMT" [3] "2009-02-28 23:00:00 GMT" "2009-03-31 22:00:00 GMT" [5] "2009-04-30 22:00:00 GMT" "2009-05-31 22:00:00 GMT" timeSeries: > s1.

Time Series Basics 25 > Zurich()[54:64. a flag which tells us if DST is in effect or not. on Unix systems the list is in /usr/share/tzone timeSeries: For timeSeries objects we can print the list of supported financial centres using the functions listFinCenter > length(listFinCenter()) [1] 397 © 2009. and a integer value in seconds belonging to the time stamp when the clock was changed. the time zone abbreviation.1. the offset in seconds with respect to GMT. Finance Online – Please do not distribute . Can you please quickly tell us to which time zone the stock exchange in Mumbai belongs? This is operating-system dependent. Which time zones are supported for the creation of time series objects? zoo and xts: We do not know how to create a list of time zones available in R’s POSIXt implementation and thus which time zones are available to use to create zoo and xts time series objects. ] Zurich offSet isdst TimeZone numeric 54 2005-03-27 01:00:00 7200 1 CEST 1111885200 55 2005-10-30 01:00:00 3600 0 CET 1130634000 56 2006-03-26 01:00:00 7200 1 CEST 1143334800 57 2006-10-29 01:00:00 3600 0 CET 1162083600 58 2007-03-25 01:00:00 7200 1 CEST 1174784400 59 2007-10-28 01:00:00 3600 0 CET 1193533200 60 2008-03-30 01:00:00 7200 1 CEST 1206838800 61 2008-10-26 01:00:00 3600 0 CET 1224982800 62 2009-03-29 01:00:00 7200 1 CEST 1238288400 63 2009-10-25 01:00:00 3600 0 CET 1256432400 64 2010-03-28 01:00:00 7200 1 CEST 1269738000 The table shows when the clock was changed in Zurich.

9546555 2009-04-01 0.26 1. and cities starting with "L" > listFinCenter("Pacific") [1] "Pacific/Apia" "Pacific/Auckland" "Pacific/Chatham" [4] "Pacific/Efate" "Pacific/Enderbury" "Pacific/Fakaofo" [7] "Pacific/Fiji" "Pacific/Funafuti" "Pacific/Galapagos" [10] "Pacific/Gambier" "Pacific/Guadalcanal" "Pacific/Guam" [13] "Pacific/Honolulu" "Pacific/Johnston" "Pacific/Kiritimati" [16] "Pacific/Kosrae" "Pacific/Kwajalein" "Pacific/Majuro" [19] "Pacific/Marquesas" "Pacific/Midway" "Pacific/Nauru" [22] "Pacific/Niue" [25] "Pacific/Pago_Pago" "Pacific/Norfolk" "Pacific/Noumea" [28] "Pacific/Ponape" "Pacific/Palau" "Pacific/Pitcairn" "Pacific/Port_Moresby" "Pacific/Rarotonga" [31] "Pacific/Saipan" "Pacific/Tahiti" "Pacific/Tarawa" [34] "Pacific/Tongatapu" "Pacific/Truk" "Pacific/Wake" [37] "Pacific/Wallis" > listFinCenter(".7571371 2009-02-01 0.1146348 © 2009. zone = "Frankfurt".1 2009-01-01 0.0056799 2009-03-01 0.*/L") [1] "Africa/Lagos" "Africa/Libreville" [3] "Africa/Lome" "Africa/Luanda" [5] "Africa/Lubumbashi" "Africa/Lusaka" [7] "America/Argentina/La_Rioja" [9] "America/La_Paz" "America/Kentucky/Louisville" [11] "America/Los_Angeles" "America/Lima" [13] "Australia/Lindeman" "Arctic/Longyearbyen" "Australia/Lord_Howe" [15] "Europe/Lisbon" "Europe/Ljubljana" [17] "Europe/London" "Europe/Luxembourg" Yo can even create your own financial centres. timeCalendar(). FinCenter = " Frankfurt") Frankfurt TS. Finance Online – Please do not distribute . > Frankfurt <. financial centres in the Pacific region. Here are some examples.3. Time Zone and Daylight Saving Time These are too many to get printed here. For example the DST rules for Germany are in "Germany/Berlin".Berlin > timeSeries(runif(1:12). as a banker you may prefer Frankfurt. To display a limited number you can select them by greped pattern. then just create your own table.

timeSeries: In timeSeries we use the assignment function finCenter<.1.32346 2009-11-01 01:00:00 0.3436831 2009-12-01 0.1 2009-07-01 01:00:00 1.7857066 2009-06-01 0. then to add the time shift between the new and old timezone.timeSeries(rnorm(6). For example to express a time series recorded in London for use in Zurich in time stamps of local time in New York proceed as follows > ZRH <.to assign a new financial centre to an already existing timeSeries object. How can I change the time zone representation for an existing time series? zoo and xts: We don’t know how to change time zone in a direct way.2741616 2009-08-01 0.8832456 2009-11-01 0. Finance Online – Please do not distribute . and finally to reassign the new index to the zoo or xts object.51925 2009-12-01 01:00:00 3. As a workaround we recommend to extract the index from the time series. FinCenter = "Zurich") > ZRH Zurich TS.06632 © 2009.6653792 Note that timeCalendar is a function from the package timeDate and creates monthly time stamps for the current year. zone = "London". timeCalendar(2009)[7:12].3301645 2009-09-01 0.91393 2009-09-01 01:00:00 0.5268424 2009-10-01 0.37900 2009-10-01 01:00:00 0.51711 2009-08-01 01:00:00 0.9515243 2009-07-01 0. Time Series Basics 27 2009-05-01 0.

e October and November respectively! Have a look in the DST tables to confirm this > Zurich()[63.37900 2009-09-30 19:00:00 0.] © 2009.] Zurich offSet isdst TimeZone 63 2009-10-25 01:00:00 3600 0 numeric CET 1256432400 > New_York()[179. Have a look on the table with the DST rules. timeSeries: This happened for example in Germany and Switzerland several times in the years between 1940 and 1985.51925 2009-11-30 19:00:00 3. Time Zone and Daylight Saving Time > finCenter(ZRH) <.1 2009-06-30 19:00:00 1.3.28 1. > Berlin()[8:38.06632 This example reflects also the fact that Europe and USA changed from summer time to winter time in different months.32346 2009-10-31 20:00:00 0.91393 2009-08-31 19:00:00 0.] New_York offSet isdst TimeZone 179 2009-11-01 06:00:00 -18000 0 numeric EST 1257055200 How can I handle data recorded in two cities which belong to the same time zone but have different DST rules? zoo and xts: Not supported by zoo and xts objects.51711 2009-07-31 19:00:00 0. Finance Online – Please do not distribute ."New_York" > ZRH New_York TS. i.

Time Series Basics 29 Berlin offSet isdst TimeZone 8 1940-04-01 01:00:00 9 7200 1 numeric CEST -938905200 1942-11-02 01:00:00 3600 0 CET -857257200 10 1943-03-29 01:00:00 7200 1 CEST -844556400 11 1943-10-04 01:00:00 3600 0 CET -828226800 12 1944-04-03 01:00:00 7200 1 CEST -812502000 13 1944-10-02 01:00:00 3600 0 CET -796777200 14 1945-04-02 01:00:00 7200 1 CEST -781052400 15 1945-05-24 00:00:00 10800 1 CEMT -776563200 16 1945-09-24 00:00:00 7200 1 CEST -765936000 17 1945-11-18 01:00:00 3600 0 CET -761180400 18 1946-04-14 01:00:00 7200 1 CEST -748479600 19 1946-10-07 01:00:00 3600 0 CET -733273200 20 1947-04-06 02:00:00 7200 1 CEST -717631200 21 1947-05-11 01:00:00 10800 1 CEMT -714610800 22 1947-06-29 00:00:00 7200 1 CEST -710380800 23 1947-10-05 01:00:00 3600 0 CET -701910000 24 1948-04-18 01:00:00 7200 1 CEST -684975600 25 1948-10-03 01:00:00 3600 0 CET -670460400 26 1949-04-10 01:00:00 7200 1 CEST -654130800 27 1949-10-02 01:00:00 3600 0 28 1980-04-06 01:00:00 7200 1 CEST 323830800 29 1980-09-28 01:00:00 3600 0 CET 338950800 30 1981-03-29 01:00:00 7200 1 CEST 354675600 31 1981-09-27 01:00:00 3600 0 CET 370400400 32 1982-03-28 01:00:00 7200 1 CEST 386125200 33 1982-09-26 01:00:00 3600 0 CET 401850000 34 1983-03-27 01:00:00 7200 1 CEST 417574800 35 1983-09-25 01:00:00 3600 0 CET 433299600 36 1984-03-25 01:00:00 7200 1 CEST 449024400 37 1984-09-30 01:00:00 3600 0 CET 465354000 38 1985-03-31 01:00:00 7200 1 CEST 481078800 Zurich offSet isdst TimeZone numeric CET -639010800 > Zurich()[2:15.1.] 2 1941-05-05 00:00:00 7200 1 CEST -904435200 3 1941-10-06 00:00:00 3600 0 CET -891129600 4 1942-05-04 00:00:00 7200 1 CEST -872985600 5 1942-10-05 00:00:00 3600 0 6 1981-03-29 01:00:00 7200 1 CEST 354675600 7 1981-09-27 01:00:00 3600 0 CET 370400400 8 1982-03-28 01:00:00 7200 1 CEST 386125200 9 1982-09-26 01:00:00 3600 0 CET 401850000 10 1983-03-27 01:00:00 7200 1 CEST 417574800 11 1983-09-25 01:00:00 3600 0 CET 433299600 12 1984-03-25 01:00:00 7200 1 CEST 449024400 CET -859680000 © 2009. Finance Online – Please do not distribute .

The question now is: what is the earliest time we can start with the investigation of the data at local Berlin time. Time Zone and Daylight Saving Time 13 1984-09-30 01:00:00 3600 0 CET 465354000 14 1985-03-31 01:00:00 7200 1 CEST 481078800 15 1985-09-29 01:00:00 3600 0 CET 496803600 We have end-of-day data 16:00 recorded in Zurich in local time.79543 So in February and March we can start our investigation in Berlin at the same time as in Zurich 16:00. Finance Online – Please do not distribute .99422 1980-05-15 17:00:00 0. zone = "Zurich". charvec.3. sep = "") > charvec [1] "1980-02-15 16:00:00" "1980-03-15 16:00:00" "1980-04-15 16:00:00" [4] "1980-05-15 16:00:00" > timeSeries(runif(4). 2:5. © 2009. but in April and May we can start with our investigation one hour later at 17:00 due to the time difference to Zurich. > charvec <. "-15 16:00:00". FinCenter = "Berlin". units = "fromZurich") Berlin fromZurich 1980-02-15 16:00:00 0.30 1.paste("1980-0".90749 1980-04-15 17:00:00 0. but now we want to use them in Berlin.74680 1980-03-15 16:00:00 0.

rev(paste("2009-0".024625 0.049799 2009-06-01 -0.716483 -0. "-01".640329 -0.Date(charvec)) [.716483 2009-04-01 -0.535918 -0.049799 -0. Time Series Basics 31 1. as.1. Finance Online – Please do not distribute .640329 2009-02-01 -0.seed <.Date(charvec)) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 1.535918 2009-05-01 -0. zoo and xts: > zoo(data. as.rnorm(6) > charvec <. sep = "")) > charvec [1] "2009-06-01" "2009-05-01" "2009-04-01" "2009-03-01" "2009-02-01" [6] "2009-01-01" Note the character vector charvec is in reverse order.128710 > xts(data.128710 © 2009.1953 > data <. 1:6.024625 2009-03-01 0.1] 2009-01-01 1.4 Ordering and Overlapping of Time Series Required R package(s): > library(zoo) > library(xts) > library(timeSeries) How can I create a time series with time stamps in reverse order? Common Data: > set.

> tS <.640329 2009-02-01 -0.1 2009-06-01 -0.535918 2009-03-01 0.4. charvec) > tS GMT TS. Finance Online – Please do not distribute .024625 2009-01-01 1.049799 2009-01-01 1.049799 2009-04-01 -0.1 2009-01-01 1.024625 2009-03-01 0.049799 2009-06-01 -0.716483 2009-04-01 -0. It is not possible to create a zoo or xts object in reverse time order.128710 Sample the time series in arbitray time order > sample(tS) GMT TS.1 2009-06-01 -0.128710 2009-05-01 -0. The same holds for xts time series objects.640329 Reverse the time order > rev(tS) GMT TS.128710 2009-04-01 -0.535918 2009-05-01 -0.535918 2009-03-01 0.32 1. timeSeries: timeSeries objects can be created in increasing and decreasing order.timeSeries(data.024625 © 2009.716483 2009-02-01 -0. and they can even be sampled arbitrarily. Ordering and Overlapping of Time Series Time series objects of class zoo are always forced to be ordered in time.640329 2009-02-01 -0.716483 2009-05-01 -0.

as. stating that possibly not all zoo() methods will work. Common Data: > data1 <. as. 1:6. 0) > charvec1 <. "-01". Finance Online – Please do not distribute . Time Series Basics 33 How can I create a time series with overlapping time stamps? You proceed in the same way as with any other unique time series. © 2009. "-01". paste("2009-0". sep = ""). "2009-04-01") > charvec1 [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" "2009-04-01" > data2 <.Date(charvec2)) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-04-01 2009-05-01 1 2 3 0 4 5 2009-06-01 6 zoo() will return a warning message. sep = "")) > charvec2 [1] "2009-04-01" "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" [6] "2009-05-01" "2009-06-01" zoo: > zoo(data1.c(1:6. 1:6.Date(charvec1)) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-04-01 2009-05-01 1 2 3 4 0 5 2009-06-01 6 > zoo(data2.c("2009-04-01".c(paste("2009-0".1.0:6 > charvec2 <.

charvec2) © 2009.1] 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-04-01 0 2009-05-01 5 2009-06-01 6 > xts(data2.34 1. charvec1) GMT TS.4.1] 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 0 2009-04-01 4 2009-05-01 5 2009-06-01 6 timeSeries: timeSeries objects allow for overlapping time stamps > timeSeries(data1. Ordering and Overlapping of Time Series xts: xts() has extended this point in zoo(). as. and overlapping time stamps can be handled. > xts(data1.1 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 2009-04-01 0 > timeSeries(data2.Date(charvec1)) [. as. Finance Online – Please do not distribute .Date(charvec2)) [.

To sort the series use the function sort() > sort(timeSeries(data1.1 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 0 2009-04-01 4 2009-05-01 5 2009-06-01 6 Note that you can also sort a timeSeries object in decreasing order. decreasing = TRUE) GMT TS. Finance Online – Please do not distribute . charvec1)) GMT TS. charvec2)) GMT TS.1.1 2009-06-01 6 2009-05-01 5 © 2009. charvec1).1 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-04-01 0 2009-05-01 5 2009-06-01 6 > sort(timeSeries(data2. > sort(timeSeries(data1. This is a useful feature since it can reflect the order in which you received the data records. Time Series Basics 35 GMT TS.1 2009-04-01 0 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 but the order is the same as that provided by the charvec time stamps.

frame().01 2009-02-01 0. Ordering and Overlapping of Time Series 2009-04-01 4 2009-04-01 0 2009-03-01 3 2009-02-01 2 2009-01-01 1 If you want to keep the information of the original order. documentation = NULL. charvec.sample(charvec1) > tS <.13 7 2009-05-01 -0..round(rnorm(7).sort(timeSeries(data3. .7* 5 2009-01-01 1. FinCenter = "". you can save it in the @recordIDs slot of the timeSeries object. title = NULL.frame(1:7))) > tS GMT TS. charvec3. recordIDs = data. > args(timeSeries) function (data. recordIDs = data.22 2 2009-03-01 0..4.36 1.29 6 Now retrieve the order in the same way as for the information > tS@recordIDs X1. zone = "". The following example shows you how to keep and retrieve this information. Finance Online – Please do not distribute .18 3 2009-06-01 -2.27 4 2009-04-01 1. 2) > charvec3 <. format = NULL.16 1 2009-04-01 0.1 X1.7 5 5 2 2 4 4 1 1 7 7 3 3 6 6 © 2009.) NULL > data3 <. units = NULL.

3.27 4 2009-04-01 1. which are rated. times = 3)) > ratingOfOffer © 2009.22 2 2009-03-01 0.01 -2. offeredPrice.01 5 2009-02-01 0.1.1. 4.29 6 > data3 [1] 1. times = 3).100 * c(3. > offeredPrice <. 4:6). times = 3)) > providerCompany [1] "UISA Ltd" "UISA Ltd" [6] "UISA Ltd" "UISA Ltd" "UISA Ltd" "HK Company" "UISA Ltd" > ratingOfOffer <. price offers. "BBB".c(rep("UISA Ltd". We have at given dates. provided by different companies.4.16 0.9) > offeredPrice [1] 340 320 400 400 410 350 290 > dateOfOffer <.5.7 2009-01-01 1. "HK Company". sep = "") > dateOfOffer [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-03-01" "2009-04-01" [6] "2009-05-01" "2009-06-01" > providerCompany <.paste("2009-0". 3.1 X1. rep("UISA Ltd ". 4. dateOfOffer.16 1 2009-04-01 0.frame named priceInfo. providerCompany. 4. 3.22 -0.2. 2.18 0.29 0.13 How can I handle additional attributes of a time series object? Let us consider the following (slightly more complex) time series example. rep("AAB". as.matrix(tS@recordIDs)) TS. ratingOfOffer. times = 3).13 7 2009-05-01 -0. > cbind(series(tS).18 3 2009-06-01 -2. c(1:3. Time Series Basics 37 or you can print it in the form of a direct comparison report.c(rep("AAA". Finance Online – Please do not distribute . The information about the providers and ratings is saved in a data. "-01".27 1.

The additional information on providers and the ratings can be added by an attribute named "info". Finance Online – Please do not distribute . "info") = priceInfo > zp 2009-01-01 2009-02-01 2009-03-01 2009-03-01 2009-04-01 2009-05-01 340 320 400 400 410 350 2009-06-01 290 > zp 2009-01-01 2009-02-01 2009-03-01 2009-03-01 2009-04-01 2009-05-01 340 320 400 400 410 350 2009-06-01 290 > attr(zp. "info") providerCompany ratingOfOffer 1 UISA Ltd AAA 2 UISA Ltd AAA 3 UISA Ltd AAA 4 HK Company BBB 5 UISA Ltd AAB 6 UISA Ltd AAB 7 UISA Ltd AAB © 2009.data. ratingOfOffer) > priceInfo providerCompany ratingOfOffer 1 UISA Ltd AAA 2 UISA Ltd AAA 3 UISA Ltd AAA 4 HK Company BBB 5 UISA Ltd AAB 6 UISA Ltd AAB 7 UISA Ltd AAB zoo: We create a zoo time series object from dates and prices.frame(providerCompany. Ordering and Overlapping of Time Series [1] "AAA" "AAA" "AAA" "BBB" "AAB" "AAB" "AAB" > priceInfo <.zoo(offeredPrice.4.38 1. > zp <. as.Date(dateOfOffer)) > attr(zp.

Date(dateOfOffer)) > attr(xp. Finance Online – Please do not distribute . "info") providerCompany ratingOfOffer 1 UISA Ltd AAA 2 UISA Ltd AAA 3 UISA Ltd AAA 4 HK Company BBB 5 UISA Ltd AAB 6 UISA Ltd AAB 7 UISA Ltd AAB timeSeries: > tS <.timeSeries(offeredPrice. > xp <.1. as. Time Series Basics 39 xts: For xts we proceed it in the same way. dateOfOffer. recordIDs = priceInfo) > tS GMT TS.xts(offeredPrice.1] 2009-01-01 340 2009-02-01 320 2009-03-01 400 2009-03-01 400 2009-04-01 410 2009-05-01 350 2009-06-01 290 > attr(xp. "info") = priceInfo > xp [.1 2009-01-01 340 2009-02-01 320 2009-03-01 400 2009-03-01 400 2009-04-01 410 2009-05-01 350 2009-06-01 290 > tS@recordIDs © 2009.

priceInfo[-4.40 1. > zx 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 340 320 400 410 350 290 > attr(zx. Ordering and Overlapping of Time Series providerCompany ratingOfOffer 1 UISA Ltd AAA 2 UISA Ltd AAA 3 UISA Ltd AAA 4 HK Company BBB 5 UISA Ltd AAB 6 UISA Ltd AAB 7 UISA Ltd AAB For timeSeries objects most attributes can be handled as a data frame through the @recordsIDs slot.] > zx 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 340 320 400 410 350 290 © 2009.zp[-4] > zx 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 340 320 400 410 350 290 > attr(zx. What happens with attributes when I modify the time series? Let us consider the example from the previous FAQ. zoo: > zx <. "info") <. 4 from the time series.4. We want to remove offer No. "info") NULL The attribute has been lost from the zoo time series object. A workaround is to check and re-add the subsetted attribute. Finance Online – Please do not distribute .

> attr(xx. However.1 2009-01-01 340 2009-02-01 320 2009-03-01 400 2009-04-01 410 2009-05-01 350 2009-06-01 290 > tX@recordIDs © 2009.xp[-4] > xx [. A workaround is to delete the record in the attribute.1.attr(xx. they are not subsetted. We delete the fourth offer by subsetting and print the result again.tS[-4. > xx <. ] > xx [. "info")[-4. "info") <. ] > tX GMT TS.1] 2009-01-01 340 2009-02-01 320 2009-03-01 400 2009-04-01 410 2009-05-01 350 2009-06-01 290 timeSeries: > tX <. Time Series Basics 41 Now let us inspect what happens with xts. Finance Online – Please do not distribute .1] 2009-01-01 340 2009-02-01 320 2009-03-01 400 2009-04-01 410 2009-05-01 350 2009-06-01 290 In contrast to zoo. xts time series objects keep attributes.

4. Ordering and Overlapping of Time Series providerCompany ratingOfOffer 1 UISA Ltd AAA 2 UISA Ltd AAA 3 UISA Ltd AAA 5 UISA Ltd AAB 6 UISA Ltd AAB 7 UISA Ltd AAB Note that timeSeries functions also handle attributes that are saved in the slot. @recordIDs © 2009. This should work without further workarounds. Finance Online – Please do not distribute .42 1.

c(paste("2009-0".paste("2009-0".c(1:6) > charvec1 <.zoo(data+6.zoo(data. sep = "") > charvec1 [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" > charvec2 <. 7:9. paste("2009-".5 Binding and Merging of Time Series Required R package(s): > library(zoo) > library(xts) > library(timeSeries) How can I bind two time series objects by row? To bind a time series row by row use the function rbind.1.Date(charvec1)) > z2 <. "-01".Date(charvec2)) > rbind(z1. sep = "")) > charvec2 [1] "2009-07-01" "2009-08-01" "2009-09-01" "2009-10-01" "2009-11-01" [6] "2009-12-01" zoo: > z1 <. 10:12. z2) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 1 2 3 4 5 6 2009-07-01 2009-08-01 2009-09-01 2009-10-01 2009-11-01 2009-12-01 7 8 9 10 11 12 © 2009. as. "-01". Time Series Basics 43 1. Finance Online – Please do not distribute . 1:6. sep = ""). Common Data: > data <. as. "-01".

Binding and Merging of Time Series > rbind(z2. x1) [. Finance Online – Please do not distribute .xts(data+6.Date(charvec2)) > rbind(x1.5.xts(data. z1) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 1 2 3 4 5 6 2009-07-01 2009-08-01 2009-09-01 2009-10-01 2009-11-01 2009-12-01 7 8 9 10 11 12 Note that the bound series do not depend on the order of the arguments in the function rbind.1] 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 2009-07-01 7 2009-08-01 8 2009-09-01 9 2009-10-01 10 2009-11-01 11 2009-12-01 12 © 2009. as.44 1.Date(charvec1)) > x2 <.1] 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 2009-07-01 7 2009-08-01 8 2009-09-01 9 2009-10-01 10 2009-11-01 11 2009-12-01 12 > rbind(x2. as. xts: > x1 <. x2) [.

It is important to note that this is not a bug but a feature. s1) GMT TS.1 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 2009-07-01 7 2009-08-01 8 2009-09-01 9 2009-10-01 10 2009-11-01 11 2009-12-01 12 > rbind(s2.1 2009-07-01 7 2009-08-01 8 2009-09-01 9 2009-10-01 10 2009-11-01 11 2009-12-01 12 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 Note that the result depends on the ordering of the two arguments in the function rbind().1_TS. Time Series Basics 45 The result is again an ordered (bound) time series. charvec1) > s2 <. charvec2) > rbind(s1.1_TS. timeSeries: > s1 <. Finance Online – Please do not distribute . If you want to obtain the same result. then just sort the series © 2009.timeSeries(data.timeSeries(data+6. s2) GMT TS.1.

Binding and Merging of Time Series > sort(rbind(s2. s1)) GMT TS. sep = "") > charvec2 [1] "2009-03-01" "2009-04-01" "2009-05-01" "2009-06-01" "2009-07-01" [6] "2009-08-01" "2009-09-01" zoo: > z1 <.3:9 > charvec1 <.46 1.1:6 > data2 <.Date(charvec1)) > z2 <.1_TS.) : indexes overlap\n" attr(. 3:9. Finance Online – Please do not distribute .Date(charvec2)) > print(try(rbind(z1..paste("2009-0". as. 1:6..zoo(data1. "-01".level. "-01". z2))) [1] "Error in rbind(deparse.5. as. sep = "") > charvec1 [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" > charvec2 <. ."class") [1] "try-error" © 2009.zoo(data2.1 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 2009-07-01 7 2009-08-01 8 2009-09-01 9 2009-10-01 10 2009-11-01 11 2009-12-01 12 Can overlapping time series be bound by row? Common Data: > data1 <.paste("2009-0".

as. x2) [..1] 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-03-01 3 2009-04-01 4 2009-04-01 4 2009-05-01 5 2009-05-01 5 2009-06-01 6 2009-06-01 6 2009-07-01 7 2009-08-01 8 2009-09-01 9 © 2009.xts(data1.Date(charvec1)) > x2 <. z1))) [1] "Error in rbind(deparse. as.Date(charvec2)) > rbind(x1..xts(data2.1. xts: > x1 <.1] 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-03-01 3 2009-04-01 4 2009-04-01 4 2009-05-01 5 2009-05-01 5 2009-06-01 6 2009-06-01 6 2009-07-01 7 2009-08-01 8 2009-09-01 9 > rbind(x2."class") [1] "try-error" Time series with overlapping indices cannot be bound in zoo.level.) : indexes overlap\n" attr(. x1) [. . Time Series Basics 47 > print(try(rbind(z2. Finance Online – Please do not distribute .

and the original ordering will not be preserved.xts(data2.5. The time order of the records is fully preserved.Date(charvec1)) > s2 <. s2) [.48 1. timeSeries: > s1 <. s1) [. i.Date(charvec2)) > rbind(s1.1] 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-03-01 3 2009-04-01 4 2009-04-01 4 2009-05-01 5 2009-05-01 5 2009-06-01 6 2009-06-01 6 2009-07-01 7 2009-08-01 8 2009-09-01 9 > rbind(s2. Finance Online – Please do not distribute . the series are sorted. as. Binding and Merging of Time Series Time series with overlapping indices cannot be bound row by row in xts.e.1] 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-03-01 3 2009-04-01 4 2009-04-01 4 2009-05-01 5 2009-05-01 5 2009-06-01 6 2009-06-01 6 2009-07-01 7 2009-08-01 8 2009-09-01 9 Binding of overlapping timeSeries objects is fully supported by the timeclass.xts(data1. as. Series © 2009.

Date(charvec)) > z2 <. as.zoo(data2. "-01".Date(charvec)) > x2 <.xts(data2. xts: > x1 <.zoo(data1.1:6 > data2 <. z2) z1 z2 2009-01-01 1 7 2009-02-01 2 8 2009-03-01 3 9 2009-04-01 4 10 2009-05-01 5 11 2009-06-01 6 12 > cbind(z2. 1:6. as.paste("2009-0".data1 + 6 > charvec <. Time Series Basics 49 How can I bind two time series objects by column? Common Data: > data1 <.Date(charvec)) > cbind(z1. as.1.Date(charvec)) © 2009. z1) z2 z1 2009-01-01 7 1 2009-02-01 8 2 2009-03-01 9 3 2009-04-01 10 4 2009-05-01 11 5 2009-06-01 12 6 Note that the ordering of the columns depends on the order in which the two arguments are to the function cbind. as. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > z1 <.xts(data1. Finance Online – Please do not distribute .

50 1. x2) . s1) GMT TS.1 ... Finance Online – Please do not distribute ..1.5.1 .Date(charvec)) > s2 <.2 2009-01-01 1 7 2009-02-01 2 8 2009-03-01 3 9 2009-04-01 4 10 2009-05-01 5 11 2009-06-01 6 12 > cbind(x2. as.2 2009-01-01 7 1 2009-02-01 8 2 2009-03-01 9 3 2009-04-01 10 4 2009-05-01 11 5 2009-06-01 12 6 © 2009.Date(charvec)) > cbind(s1. as.1 TS.timeSeries(data2. s2) GMT TS.1.2 2009-01-01 7 1 2009-02-01 8 2 2009-03-01 9 3 2009-04-01 10 4 2009-05-01 11 5 2009-06-01 12 6 timeSeries: > s1 <.timeSeries(data1. Binding and Merging of Time Series > cbind(x1.2 2009-01-01 1 7 2009-02-01 2 8 2009-03-01 3 9 2009-04-01 4 10 2009-05-01 5 11 2009-06-01 6 12 > cbind(s2..1.1 TS.1. x1) .

Date(charvec1)) > z2 <.paste("2009-0". Time Series Basics 51 As in the case of zoo and xts time series objects. sep = "") > charvec2 [1] "2009-04-01" "2009-05-01" "2009-06-01" "2009-07-01" "2009-08-01" zoo: > z1 <.Date(charvec2)) > cbind(z1. 1:6. "-01".paste("2009-0".4:8 > charvec1 <. sep = "") > charvec1 [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" > charvec2 <. as. z2) z1 z2 2009-01-01 1 NA 2009-02-01 2 NA 2009-03-01 3 NA 2009-04-01 4 4 2009-05-01 5 5 2009-06-01 6 6 2009-07-01 NA 7 2009-08-01 NA 8 zoo() can bind overlapping time series. as. missing values are substituted by NAs. the ordering of the columns depends on which of the two arguments is first passed to the function cbind(). Can overlapping time series be bound by column? Common Data: > data1 <.1. Finance Online – Please do not distribute .zoo(data2.zoo(data1. 4:8.1:6 > data2 <. © 2009. "-01".

Date(charvec1).52 1. NAs.1 . Note that the column names of the series are lost.Date(charvec2)) > cbind(x1. s2) GMT s1 s2 2009-01-01 1 NA 2009-02-01 2 NA 2009-03-01 3 NA 2009-04-01 4 4 2009-05-01 5 5 2009-06-01 6 6 2009-07-01 NA 7 2009-08-01 NA 8 Binding of overlapping timeSeries objects is fully supported by the timeSeries class. Finance Online – Please do not distribute .2 2009-01-01 1 NA 2009-02-01 2 NA 2009-03-01 3 NA 2009-04-01 4 4 2009-05-01 5 5 2009-06-01 6 6 2009-07-01 NA 7 2009-08-01 NA 8 xts() can bind overlapping time series. © 2009.Date(charvec2).xts(data1. as is the case for zoo and xts time series objects. timeSeries: > s1 <.xts(data2. as. units = "s1") > s2 <. as. Binding and Merging of Time Series xts: > x1 <. timeSeries also substitutes missing values by NAs.. units = "s2") > cbind(s1.timeSeries(data2.5. x2) . missing values are substituted by The behaviour is the same as with the column names..Date(charvec1)) > x2 <.timeSeries(data1. as. as.

1. Time Series Basics

53

How can I merge two time series objects and what is the
difference to binding time series objects?
The base package of R has a function merge which can merge two data
frames.
> args(merge.data.frame)
function (x, y, by = intersect(names(x), names(y)), by.x = by,
by.y = by, all = FALSE, all.x = all, all.y = all, sort = TRUE,
suffixes = c(".x", ".y"), incomparables = NULL, ...)
NULL

In the description subsection of the help page we can read: Merge two data
frames by common columns or row names, or do other versions of database "join"
operations. In our sense the merge of two time series object would work in
the same way as for data frames.
Note that a natural implementation for time series would mean that merging
behaves in a similar manner as for data frames.

What happens when I merge two identical univariate time
series objects?
Common Data:
> data <- 1:6
> charvec <- paste("2009-0", 1:6, "-01", sep = "")
> charvec
[1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01"
[6] "2009-06-01"

zoo:
> z <- zoo(data, as.Date(charvec))
> merge(z, z)
z z.1
2009-01-01 1

1

2009-02-01 2

2

© 2009, Finance Online – Please do not distribute

54

1.5. Binding and Merging of Time Series
2009-03-01 3

3

2009-04-01 4

4

2009-05-01 5

5

2009-06-01 6

6

zoo()

returns a bivariate time series object.

xts:
> x <- xts(data, as.Date(charvec))
> merge(x, x)
x x.1
2009-01-01 1

1

2009-02-01 2

2

2009-03-01 3

3

2009-04-01 4

4

2009-05-01 5

5

2009-06-01 6

6

xts()

also returns a bivariate time series object.

timeSeries:
> s <- timeSeries(data, charvec)
> merge(s, s)
GMT
TS.1
2009-01-01

1

2009-02-01

2

2009-03-01

3

2009-04-01

4

2009-05-01

5

2009-06-01

6

> merge(as.data.frame(s), as.data.frame(s))

© 2009, Finance Online – Please do not distribute

1. Time Series Basics

55

TS.1
1

1

2

2

3

3

4

4

5

5

6

6

timeSeries objects operate differently, in that they show the same behaviour
as we would expect from data.frame objects.

How are two different univariate time series merged?
Common Data:
> data1 <- 1:6
> data2 <- data1 + 3
> charvec1 <- paste("2009-0", 1:6, "-01", sep = "")
> charvec1
[1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01"
[6] "2009-06-01"
> charvec2 <- paste("2009-0", 4:9, "-01", sep = "")
> charvec2
[1] "2009-04-01" "2009-05-01" "2009-06-01" "2009-07-01" "2009-08-01"
[6] "2009-09-01"

zoo:
> z1 <- zoo(data1, as.Date(charvec1))
> z2 <- zoo(data2, as.Date(charvec2))
> merge(z1, z2)
z1 z2
2009-01-01

1 NA

2009-02-01

2 NA

2009-03-01

3 NA

2009-04-01

4

4

2009-05-01

5

5

2009-06-01

6

6

© 2009, Finance Online – Please do not distribute

56

1.5. Binding and Merging of Time Series
2009-07-01 NA

7

2009-08-01 NA

8

2009-09-01 NA

9

xts:
> x1 <- xts(data1, as.Date(charvec1))
> x2 <- xts(data2, as.Date(charvec2))
> merge(x1, x2)
x1 x2
2009-01-01

1 NA

2009-02-01

2 NA

2009-03-01

3 NA

2009-04-01

4

4

2009-05-01

5

5

2009-06-01

6

6

2009-07-01 NA

7

2009-08-01 NA

8

2009-09-01 NA

9

timeSeries:
> s1 <- timeSeries(data1, as.Date(charvec1), units = "s1")
> s2 <- timeSeries(data2, as.Date(charvec2), units = "s2")
> merge(s1, s2)
GMT
s1 s2
2009-01-01

1 NA

2009-02-01

2 NA

2009-03-01

3 NA

2009-04-01

4

4

2009-05-01

5

5

2009-06-01

6

6

2009-07-01 NA

7

2009-08-01 NA

8

2009-09-01 NA

9

All three time series classes work in the same way.

© 2009, Finance Online – Please do not distribute

sep = "") > charvec2 [1] "2009-04-01" "2009-05-01" "2009-06-01" "2009-07-01" "2009-08-01" [6] "2009-09-01" zoo: We don’t know how to do this with univariate zoo time series objects. "-01".data1 + 3 > charvec1 <.1 2009-01-01 1 NA 2009-02-01 2 NA 2009-03-01 3 NA 2009-04-01 4 4 2009-05-01 5 5 2009-06-01 6 6 2009-07-01 NA 7 © 2009."x" > z2 <.xts(data2.Date(charvec2)) > colnames(x2) <. as. sep = "") > charvec1 [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" > charvec2 <.1:6 > data2 <. as.xts(data1. Finance Online – Please do not distribute . 1:6.1."x" > merge(x1. x2) x x. Time Series Basics 57 What happens if I merge two univariate time series with the same underlying information set? Common Data: > data1 <. xts: For xts time series objects we can set identical column names and then merge the two series: > x1 <.Date(charvec1)) > colnames(x1) <.paste("2009-0". "-01". 4:9.paste("2009-0".

charvec1. charvec2. since both series are from the same information set “s". "-01". units = "s") > merge(s1.1 timeSeries: > s1 <. s2) GMT s 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 2009-07-01 7 2009-08-01 8 2009-09-01 9 timeSeries() returns a different result. 1:6.timeSeries(data2.paste("2009-0". Finance Online – Please do not distribute .58 1.5.3.1:6 > charvec <.timeSeries(data1. units = "s") > s2 <. Can I merge a time series object with a numeric value? Common Data: > data <.4 © 2009. Binding and Merging of Time Series 2009-08-01 NA 8 2009-09-01 NA 9 xts() returns a bivariate time series with column names x and x. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" > const <. We obtain a univariate series.

xts(data.4 2009-06-01 6 3.zoo(data. Time Series Basics 59 zoo: > z <. as.1.1 s 2009-01-01 1 3.4 2009-06-01 6 3.4 2009-02-01 2 3. as.4 2009-05-01 5 3. const) x const 2009-01-01 1 3.timeSeries(data.4 2009-04-01 4 3.4 2009-04-01 4 3. Finance Online – Please do not distribute .4 xts: > x <.4 2009-06-01 6 3.4 2009-03-01 3 3.4 2009-05-01 5 3.4 © 2009. const) GMT TS.4 2009-03-01 3 3.Date(charvec)) > merge(x.4 2009-05-01 5 3.4 timeSeries: > s <.Date(charvec)) > merge(z.4 2009-03-01 3 3.4 2009-02-01 2 3.4 2009-04-01 4 3. const) z const 2009-01-01 1 3.4 2009-02-01 2 3. charvec) > merge(s.

4 . vec) z vec 2009-01-01 1 2.4 2009-02-01 2 1.4 0.6 -2.zoo(data.1:6 > vec [1] 2. "-01". vec) x vec 2009-01-01 1 2.1:6 > data [1] 1 2 3 4 5 6 > charvec <.4 © 2009.60 1.6 xts: > x <.4 -0.4 2009-02-01 2 1.paste("2009-0".6 -1. as. Binding and Merging of Time Series Can I merge a time series object with a numeric vector? Common Data: > data <.6 2009-05-01 5 -1.3. Finance Online – Please do not distribute .4 2009-03-01 3 0. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" > vec <.Date(charvec)) > merge(x.xts(data.4 1.Date(charvec)) > merge(z. as.4 2009-04-01 4 -0. 1:6.6 zoo: > z <.5.6 2009-06-01 6 -2.4 2009-03-01 3 0.

] -6. 1:6.3.1:6 > charvec <.1 s 2009-01-01 1 2.6 2009-06-01 6 -2.6 [6. ncol = 2) . Time Series Basics 61 2009-04-01 4 -0.6 2009-05-01 5 -1.paste("2009-0".2] [1.6 [5.4 2.6 timeSeries: > s <.4 2009-03-01 3 0. Finance Online – Please do not distribute .6 2009-06-01 6 -2.4 2009-04-01 4 -0.4 2009-02-01 2 1. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" > mat <.4 1. vec) GMT TS. charvec) > merge(s.] -3.4 [3.4 > mat [.4 0.] -5.matrix((1:12)-6.6 2009-05-01 5 -1.] -7.4 -2.] -4.4 -0.4 [2.1] [.timeSeries(data. "-01".] -8.4 [4.1.4 -1.6 © 2009.6 Can I merge a time series object with a numeric matrix? Common Data: > data <.

4 2.2 2009-01-01 1 -8.2 2009-01-01 1 -8.4 -2.4 -0. as.4 2009-04-01 4 -5.1 mat.4 1.6 2009-06-01 6 -3.xts(data.4 2009-02-01 2 -7.4 0.6 2009-05-01 5 -4.1 2009-01-01 1 -8. mat) GMT TS. charvec) > merge(s.4 -2.4 -0.4 2009-03-01 3 -6.6 2009-06-01 6 -3.62 1.6 2009-06-01 6 -3.1 mat. mat) z mat.4 -0. as.4 2.4 -2.6 xts: > x <.4 2009-04-01 4 -5.4 2009-03-01 3 -6.zoo(data.4 2009-02-01 2 -7.4 2009-03-01 3 -6.4 2.4 0.Date(charvec)) > merge(z.4 0.6 © 2009.4 2009-04-01 4 -5.4 -1.4 -1.1 mat.4 2009-02-01 2 -7.4 1.4 -1. Finance Online – Please do not distribute .5.Date(charvec)) > merge(x.6 2009-05-01 5 -4.timeSeries(data. mat) x mat mat.6 timeSeries: > s <.6 2009-05-01 5 -4. Binding and Merging of Time Series zoo: > z <.4 1.

> vec <.2] [.] 1. thus we need only one index to subset it. Time Series Basics 63 1.3881734 -0.020904 -0.59615 0. and its length is the number of its elements.1627861 0.19046 0. > dim(vec) NULL > length(vec) [1] 6 A matrix is a rectangular object which requires a pair of indices to subset it.35675 [4.5432789 1. Finance Online – Please do not distribute .0019142 0.2315970 -0.75169 2.] 0.1] [.774526 -0.18090 -0.82036 [6.rnorm(6) > vec [1] 0.13801 -0.75169 2.] 0.] 0.1.90531 0.29875 [3.071777 © 2009.27887 [2.matrix(rnorm(18). ncol = 3) > mat [.6 Subsetting Time Series Objects Required R package(s): > library(zoo) > library(xts) > library(timeSeries) How can I subset a vector and a matrix? A vector is a linear "object".59615 A vector has a dimension NULL.10594 [5.3] [1.705969 0.496030 0.] -1.57736 > vec[3:4] [1] 0.] -0. > mat <.344935 0. one to choose the columns and another one to choose the rows.0763961 1.

2] [.1] [1.mat[3.5432789 1.0019142 0.] [. What happens when we subset a single column or a single row of matrix? > mat[3.076396 1.3881734 0.35675 > mat[.90531 > mat[3:4.35675 [4.0763961 1. the result is a vector.0019142 0.] [1] 1. .3881734 -0.162786 0.] 1.] 0.10594 For a matrix we have > dim(mat) [1] 6 3 > length(mat) [1] 18 Thus the function dim returns the number of rows and the number of columns of the matrix and the function length the total number of elements of the matrix.2315970 Then the rectangular object becomes linear.70597 [.2315970 -0.] -0.82036 [6.5432789 -0.10594 > mat[.6.10594 [5.1] [1.1] [.16279 0.drop = FALSE] > colmat <.] 0.] -0.77453 -0.3] 0.] 0. To prevent this we have to take care that we do not drop the redundant extent information.mat[.1627861 0. 2:3] [1. 2:3] [.29875 [3.076396 1.70597 0.2] 0.] -0.] [. Subsetting Time Series Objects > mat[3:4. > rowmat <.] [. Finance Online – Please do not distribute .64 1. 2] [1] 0.162786 0.35675 [2.27887 [2.0763961 0.1627861 -0. 2. drop = FALSE] © 2009.] 0.35675 [2.2] 0.

it depends what time series class we use.Date(charvec)) > Z[3:4. as.zoo(data2.67869 -0.paste("2009-0". as. "-01".1. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: A univariate time series > z <.6105 0.Date(charvec)) > z[3:4] 2009-03-01 2009-04-01 -0.577469 2009-04-01 2.20898 A multivariate time series > Z <.matrix(rnorm(18).61052 0.40434 -0.76079 -0.4912 0. Finance Online – Please do not distribute .257301 2009-05-01 2.045377 2009-06-01 -0. Common Data: > data1 <.rnorm(1:6) > data2 <.25730 > Z[.] A B C 2009-03-01 -0.57747 2009-04-01 -0.LETTERS[1:3] > charvec <.zoo(data1.164478 © 2009. 1:6.99734 1.52654 -1.34507 -1.31823 2. ncol = 3) > colnames(data2) <. Time Series Basics 65 When I am subsetting a time series. does it behave like subsetting a vector and a matrix? yes and no. 2:3] B C 2009-01-01 0.688973 2009-02-01 0.49123 0.114796 2009-03-01 -1.

4912 0.66 1.257301 2009-05-01 2.4912 0.Date(charvec)) > X[3:4. Extracting single rows or columns drops an index. 2:3] B C 2009-03-01 -1.1] 2009-03-01 -0.xts(data1.114796 2009-03-01 -1.76079 2009-04-01 -0.34507 -1.6105 0.6105 0. 2:3] B C 2009-01-01 0. Subsetting Time Series Objects > Z[3:4.Date(charvec)) > x[3:4] [.xts(data2. xts: A univariate time series > x <.164478 > Z[3:4.4912 0.99734 1.25730 We keep in mind. as.57747 2009-04-01 2. Finance Online – Please do not distribute .20898 A multivariate time series > X <. 2:3] B C 2009-03-01 -1.045377 2009-06-01 -0.40434 -0.25730 © 2009.577469 2009-04-01 2.688973 2009-02-01 0.6.67869 -0.57747 2009-04-01 -0.61052 0.] A B C 2009-03-01 -0. and a multivariate zoo time series object behaves like a matrix.52654 -1.57747 2009-04-01 2.6105 0.31823 2.25730 > Z[. as.49123 0. a univariate zoo time series object behaves like a vector.

49123 0.99734 1. 2:3] B C 2009-03-01 -1.67869 -0.timeSeries(data1.76079 -0.25730 Time series objects in xts are always consider as rectangular objects even in the univariate case. This uniqueness is introduced to make life easier for subsetting financial time series timeSeries: A univariate time series > s <.99734 1.6105 0.20898 > s[3:4.114796 2009-03-01 -1. Finance Online – Please do not distribute .045377 2009-06-01 -0.49123 0.61052 0.6105 0.577469 2009-04-01 2.40434 -0.57747 2009-04-01 2.40434 -0.045377 2009-06-01 -0. 2:3] B C 2009-01-01 0.257301 2009-05-01 2.688973 2009-02-01 0.1.164478 > X[3:4. Time Series Basics 67 > X[.164478 > X[3:4. ] © 2009.34507 -1.114796 2009-03-01 -1.57747 2009-04-01 2. charvec) > s[3:4] [1] -0.25730 > X[.61052 0. 2:3] B C 2009-03-01 -1.34507 -1.257301 2009-05-01 2.688973 2009-02-01 0.4912 0.67869 -0. 2:3] B C 2009-01-01 0.577469 2009-04-01 2.4912 0.

49123 0.688973 2009-02-01 0.67869 -0.99734 1.49123 0.6105 0.688973 2009-02-01 0.045377 2009-06-01 -0.34507 -1.61052 0.76079 2009-04-01 -0.67869 -0.] GMT A B C 2009-03-01 -0. This a timeSeries feature not a bug.577469 2009-04-01 2.1 2009-03-01 -0.6.34507 -1. 2:3] GMT B C 2009-03-01 -1.57747 2009-04-01 2.257301 2009-05-01 2.045377 2009-06-01 -0.20898 A multivariate time series > S <.4912 0.99734 1.52654 -1.) > S[.577469 2009-04-01 2. 2:3] GMT B C 2009-01-01 0.61052 0.25730 > S[.164478 > S[3:4. 2:3] GMT B C 2009-01-01 0.timeSeries(data2.57747 2009-04-01 -0.31823 2.40434 -0.164478 > S[3:4.68 1.25730 Note that a timeSeries object has always to be subsetted by a pair of indices.114796 2009-03-01 -1. Subsetting Time Series Objects GMT TS.114796 2009-03-01 -1.4912 0.40434 -0.257301 2009-05-01 2. 2:3] © 2009. Finance Online – Please do not distribute .6105 0. charvec) > S[3:4.

sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > Z <. Finance Online – Please do not distribute . as. "A"] 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 1. The behave in the same way.57747 2009-04-01 2.4912 0.paste("2009-0".22988 2009-04-01 0.22988 0.40826 xts: > X <. timeSeries Can I subset a multivariate time series by column names? Common Data: > data <.58880 1.07909 0. "-01".32385 2009-02-01 -1.58880 2009-03-01 1. 1:6.22244 2009-05-01 -1.25730 objects are like xts objects rectangular objects. ncol = 3) > colnames(data) <.07909 2009-06-01 0.40826 © 2009. as. "A"] A 2009-01-01 1.zoo(data. Time Series Basics 69 GMT B C 2009-03-01 -1.LETTERS[1:3] > charvec <.Date(charvec)) > X[.xts(data.Date(charvec)) > Z[.6105 0.1.matrix(rnorm(18).22244 -1.32385 -1.

"-01".zoo(data. as.22988 2009-04-01 0. "A"] GMT A 2009-01-01 1.40826 Can I subset a multivariate time series by column using the $operator? Common Data: > data <.84425 -1.paste("2009-0". charvec) > S[.xts(data.13289 0.Date(charvec)) > X$B © 2009.37841 -1.matrix(rnorm(18).58880 2009-03-01 1.70 1. 1:6.Date(charvec)) > Z$B 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 1.32385 2009-02-01 -1.22244 2009-05-01 -1. ncol = 3) > colnames(data) <.6.timeSeries(data.07909 2009-06-01 0. Finance Online – Please do not distribute .08238 -1.13910 0. as.49833 xts: > X <. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > Z <. Subsetting Time Series Objects timeSeries: > S <.LETTERS[1:3] > charvec <.

84425 2009-02-01 -1.Date(charvec)) > charvec[3:4] [1] "2009-03-01" "2009-04-01" > Z[charvec[3:4]. ncol = 3) > colnames(data) <.08238 -1.37841 2009-04-01 -1. Time Series Basics 71 B 2009-01-01 1.13289 2009-06-01 0.49833 timeSeries: > S <. Finance Online – Please do not distribute . charvec) > S$B [1] 1.13910 2009-05-01 0.84425 -1.08238 2009-03-01 -1. ] A B C © 2009.1.matrix(rnorm(18).13910 0.timeSeries(data. "-01". 1:6. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > Z <. as.49833 Can I subset a multivariate time series by character time stamps? Common Data: > data <.paste("2009-0".zoo(data.LETTERS[1:3] > charvec <.37841 -1.13289 0.

1:6.timeSeries(data. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: © 2009. ] GMT A B C 2009-03-01 1.72 1.01366 2009-04-01 0.41269 timeSeries: > S <.Date(charvec)) > charvec[3:4] [1] "2009-03-01" "2009-04-01" > X[charvec[3:4].280866 0.79083 1.matrix(rnorm(18).xts(data. "-01".79083 1. as.93683 0.079842 0. ] A B C 2009-03-01 1. ncol = 3) > colnames(data) <.93683 0. charvec) > charvec[3:4] [1] "2009-03-01" "2009-04-01" > S[charvec[3:4].LETTERS[1:3] > charvec <.280866 0.paste("2009-0". Subsetting Time Series Objects xts: > X <. Finance Online – Please do not distribute .41269 Can I subset a multivariate time series by its intrinsic time objects? Common Data: > data <.6.079842 0.01366 2009-04-01 0.

Date(charvec)) > timeStamp <.zoo(data.14760 0.index(X)[3:4] > timeStamp [1] "2009-03-01" "2009-04-01" > X[timeStamp.xts(data.24991 0.51329 0.time(S)[3:4] > timeStamp GMT [1] [2009-03-01] [2009-04-01] > S[timeStamp.30254 -2.39797 © 2009.Date(charvec)) > timeStamp <. as. as.39797 xts: > X <.14760 0.1. Finance Online – Please do not distribute . ] A B C 2009-03-01 2.timeSeries(data.21588 2009-04-01 0.51329 0. charvec) > timeStamp <.51329 0.24991 0.30254 -2.24991 0.21588 2009-04-01 0.14760 0. ] A B C 2009-03-01 2.39797 timeSeries: > S <.30254 -2.21588 2009-04-01 0. ] GMT A B C 2009-03-01 2. Time Series Basics 73 > Z <.index(Z)[3:4] > timeStamp [1] "2009-03-01" "2009-04-01" > Z[timeStamp.

065068 -0.6. Finance Online – Please do not distribute .2661 2009-05-01 0.065068 -0.matrix(rnorm(18).2661 2009-05-01 0. as.LETTERS[1:3] > charvec <. ncol = 3) > colnames(data) <.11104 -1. ] A B C 2009-04-01 0.489019 -0.069340 -2.54277 3.12725 1.069340 -2.Date(charvec)) > timeStamp <.12725 1.5996 xts: > X <.zoo(data.0568 2009-06-01 -0.5996 © 2009.index(Z) > index(Z)[3] > timeStamp [1] FALSE FALSE FALSE TRUE TRUE TRUE > Z[timeStamp.xts(data.Date(charvec)) > timeStamp <.index(X) > index(X)[3] > timeStamp [1] FALSE FALSE FALSE TRUE TRUE TRUE > X[timeStamp.paste("2009-0". 1:6.11104 -1. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > Z <. ] A B C 2009-04-01 0.54277 3. "-01". Subsetting Time Series Objects Can I subset a multivariate time series by logical predicates? Common Data: > data <.489019 -0.0568 2009-06-01 -0. as.74 1.

time(S) > time(S)[3] > timeStamp [1] FALSE FALSE FALSE TRUE TRUE TRUE > S[timeStamp. end(x)) [1] "2009-01-01" "2009-06-01" xts: > x <.54277 3.timeSeries(data. charvec) > timeStamp <.1.Date(charvec)) > c(start(x). ] GMT A B C 2009-04-01 0.5996 How to extract the start and end date of a series? Common Data: > data <. Time Series Basics 75 timeSeries: > S <. as. "-01". 1:6. Finance Online – Please do not distribute .1:6 > charvec <.zoo(data.11104 -1. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > z <.065068 -0.xts(data.Date(charvec)) > c(start(x).2661 2009-05-01 0.489019 -0.paste("2009-0". end(x)) [1] "2009-01-01" "2009-06-01" © 2009.12725 1.0568 2009-06-01 -0. as.069340 -2.

time(s[6. ]). see > s <. Finance Online – Please do not distribute . end(s)) GMT [1] [2009-01-01] [2009-06-01] Since timeSeries also support non-sorted time stamps keep in mind.timeSeries(data. ])) GMT [1] [2009-03-01] [2009-05-01] © 2009.timeSeries(data. that the first and last entry of the series ar not necessarily the start and end values. sample(charvec)) > c(start(s).76 1. charvec) > c(start(s). Subsetting Time Series Objects timeSeries: > s <.6. end(s)) GMT [1] [2009-01-01] [2009-06-01] > c(time(s[1.

. . We test them on multivariate time series objects © 2009. textttCompare. e2) Ops(e1. • • • • • • • • Arith(e1. Math. e2) Complex(z) Summary(. Summary and Complex. digits) Summary(x. Finance Online – Please do not distribute . e2) Math(x) Math2(x. Ops... Ops.. na. e2) Compare(e1. textttArith. Math.rm = FALSE) S4 Group Generic Functions: Group generic methods can be defined for four pre-specified groups of functions.. Math2. Sumary. Time Series Basics 77 1. e2) Logic(e1.1. . • • • • Math(x..) Ops(e1. na.rm = FALSE) Complex(z) In the following we test some selected functions which are relevant for use in financial time series analysis..7 Group Generics for Time Series Objects Required R package(s): > library(zoo) > library(xts) > library(timeSeries) First inspect the help pages for groupGeneric() in R’s base package and the help page for groupGeneric() in R’s methods package S3 Group Generic Functions: Group generic methods can be defined for four pre-specified groups of functions.. and Complex. Logic.

as.78 1.060902 2009-04-01 0.319320 0.688525 0.") > X X.paste("S".956925 0.matrix(runif(18).58708 0.3 2009-01-01 0.Date(charvec)) > colnames(Z) <.67986 0.799286 2009-05-01 0.956925 0.661693 2009-02-01 0.2 Z. Finance Online – Please do not distribute . ncol = 3) > charvec <.paste("X".250198 xts: > X <.Date(charvec)) > colnames(X) <.42011 0.99161 0. "-01".paste("2009-0".zoo(data.41569 0.780628 0. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > Z <.780628 0.58708 0.99161 0.337101 0.319320 0.timeSeries(data.1 Z. charvec) > colnames(S) <.37923 0.188548 2009-06-01 0.67986 0. sep = ".37923 0.") > Z Z.188548 2009-06-01 0.060902 2009-04-01 0.326017 2009-03-01 0.250198 timeSeries: > S <.2 X.") > S © 2009.41569 0. 1:3.1 X.paste("Z".337101 0.020039 0.799286 2009-05-01 0. 1:3.42011 0.661693 2009-02-01 0. sep = ". 1:3.020039 0.3 2009-01-01 0. as. 1:6. Group Generics for Time Series Objects Common Data: > data <.xts(data.688525 0.7.326017 2009-03-01 0. sep = ".

1. Time Series Basics

79

GMT
S.1

S.2

S.3

2009-01-01 0.99161 0.020039 0.661693
2009-02-01 0.42011 0.319320 0.326017
2009-03-01 0.37923 0.337101 0.060902
2009-04-01 0.67986 0.956925 0.799286
2009-05-01 0.41569 0.688525 0.188548
2009-06-01 0.58708 0.780628 0.250198

And we test them on univariate time series objects

zoo:
> z <- Z[, 1]
> z
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01
0.99161

0.42011

0.37923

0.67986

0.41569

0.58708

xts:
> x <- X[, 1]
> x
X.1
2009-01-01 0.99161
2009-02-01 0.42011
2009-03-01 0.37923
2009-04-01 0.67986
2009-05-01 0.41569
2009-06-01 0.58708

timeSeries:
> s <- S[, 1]
> s
GMT
S.1
2009-01-01 0.99161

© 2009, Finance Online – Please do not distribute

80

1.7. Group Generics for Time Series Objects
2009-02-01 0.42011
2009-03-01 0.37923
2009-04-01 0.67986
2009-05-01 0.41569
2009-06-01 0.58708

Which “Arithmetic" operations are supported by time series
objects?
Arith:
"+", "-", "*", "ˆ
", "%%", "%/%", "/"

e.g.

zoo:
> Z[, 1] + Z[, 2]
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01
1.01165

0.73943

0.71633

1.63678

1.10422

1.36771

xts:
> X[, 1] + X[, 2]
X.1
2009-01-01 1.01165
2009-02-01 0.73943
2009-03-01 0.71633
2009-04-01 1.63678
2009-05-01 1.10422
2009-06-01 1.36771

timeSeries:
> S[, 1] + S[, 2]

© 2009, Finance Online – Please do not distribute

1. Time Series Basics

81

GMT
S.1
2009-01-01 1.01165
2009-02-01 0.73943
2009-03-01 0.71633
2009-04-01 1.63678
2009-05-01 1.10422
2009-06-01 1.36771

Which "Compare" operations are supported by time series
objects?
Compare:
"==", ">", "<", "!=", "<=", ">="

e.g.

zoo:
> Z[, 1] > Z[, 2]
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01
TRUE

TRUE

TRUE

FALSE

FALSE

FALSE

xts:
> X[, 1] > X[, 2]
X.1
2009-01-01

TRUE

2009-02-01

TRUE

2009-03-01

TRUE

2009-04-01 FALSE
2009-05-01 FALSE
2009-06-01 FALSE

© 2009, Finance Online – Please do not distribute

82

1.7. Group Generics for Time Series Objects

timeSeries:
> S[, 1] > S[, 2]
GMT
S.1
2009-01-01

TRUE

2009-02-01

TRUE

2009-03-01

TRUE

2009-04-01 FALSE
2009-05-01 FALSE
2009-06-01 FALSE

Which "Logic" operations are supported by time series
objects?
Logic:
"&", "|"

e.g.

zoo:
> (Z[, 1] > Z[, 2]) & (Z[, 2] < Z[, 3])
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01
TRUE

TRUE

FALSE

FALSE

FALSE

FALSE

xts:
> (X[, 1] > X[, 2]) & (X[, 2] < X[, 3])
X.1
2009-01-01

TRUE

2009-02-01

TRUE

2009-03-01 FALSE
2009-04-01 FALSE
2009-05-01 FALSE
2009-06-01 FALSE

© 2009, Finance Online – Please do not distribute

Finance Online – Please do not distribute . "trigamma" e. "lgamma".22404 © 2009. "tanh". "trunc". "cumsum".1 Z. "sign". "log2". "cos". "Logic" Which "Math" operations are supported by time series objects? Math: "abs".3 2009-01-01 -0.8672350 -1.12080 2009-03-01 -0. "asin".3858696 -0. Time Series Basics 83 timeSeries: > (S[. "exp". "tan".91009 -0.9696104 -1. "atanh". "log1p". "acosh". "gamma". "floor".14156 -1.g. "sin". "sinh". "ceiling".0084213 -3.2 Z. "asinh". "atan". "cumprod". "expm1". "acos". 2]) & (S[. zoo: > log(abs(Z)) Z. "log10".08737 -2. 1] > S[. "Compare".04403 -0.1 2009-01-01 TRUE 2009-02-01 TRUE 2009-03-01 FALSE 2009-04-01 FALSE 2009-05-01 FALSE 2009-06-01 FALSE Which "Ops" operations are supported by time series objects? Ops: "Arith". "sqrt". 3]) GMT S. "cosh".1. "cummax".41295 2009-02-01 -0.79850 2009-04-01 -0. "log". "digamma". "cummin". 2] < S[.

14156 -1.0084213 -3.9696104 -1.5325981 -0.9696104 -1.66840 2009-06-01 -0.38550 timeSeries: > log(abs(S)) GMT S.66840 2009-06-01 -0. Group Generics for Time Series Objects 2009-05-01 -0.22404 2009-05-01 -0.66840 2009-06-01 -0.41295 2009-02-01 -0.38550 xts: > log(abs(X)) X.8672350 -1. "signif" e. Finance Online – Please do not distribute .38550 Which "Math2" operations are supported by time series objects? Math2: "round".7.91009 -0.8672350 -1. © 2009.24766 -1.3858696 -0.8778063 -0.3858696 -0.2 X.2 S.91009 -0.79850 2009-04-01 -0.3 2009-01-01 -0.1 S.37320 -1.84 1.g.8778063 -0.12080 2009-03-01 -0.5325981 -0.8778063 -0.12080 2009-03-01 -0.04403 -0.24766 -1.1 X.3 2009-01-01 -0.79850 2009-04-01 -0.08737 -2.41295 2009-02-01 -0.08737 -2.0084213 -3.22404 2009-05-01 -0.04403 -0.14156 -1.24766 -1.37320 -1.5325981 -0.37320 -1.

3 2009-01-01 0.38 0.69 0.68 0.42 0.42 0. 2) Z.3 2009-01-01 0.38 0.80 2009-05-01 0.2 Z.1 X.02 0.06 2009-04-01 0.33 2009-03-01 0.69 0.66 2009-02-01 0.96 0.02 0.66 2009-02-01 0.19 2009-06-01 0.68 0.78 0.99 0.1 Z.190 2009-06-01 0.68 0.42 0.19 2009-06-01 0. 2) X.42 0.250 xts: > round(X.800 2009-05-01 0. Finance Online – Please do not distribute .96 0.80 2009-05-01 0.061 2009-04-01 0.32 0.99 0.78 0.660 2009-02-01 0.061 2009-04-01 0.42 0.69 0.42 0.3 2009-01-01 0.42 0.1 Z.42 0.2 X.96 0.59 0.330 2009-03-01 0.32 0. 2) X.1 X.06 2009-04-01 0.96 0. 2) Z.02 0.2 Z.59 0.25 > signif(X.3 2009-01-01 0.02 0.250 © 2009.1.190 2009-06-01 0.99 0.34 0.59 0.33 2009-03-01 0.32 0.78 0.34 0.38 0.25 > signif(Z.34 0.660 2009-02-01 0.32 0. Time Series Basics 85 zoo: > round(Z.34 0.2 X.69 0.38 0.68 0.800 2009-05-01 0.78 0.99 0.330 2009-03-01 0.59 0.

3 2009-01-01 0.25 > signif(S.59 0. 2) GMT S.250 Which "Summary" operations are supported by time series objects? Summary: "max".33 2009-03-01 0. Group Generics for Time Series Objects timeSeries: > round(S.3 2009-01-01 0.78 0. 2) GMT S.80 2009-05-01 0.66 2009-02-01 0.2 S.96 0. "range".g.32 0.061 2009-04-01 0. "min".34 0.42 0.99 0.68 0.59 0.38 0.1 S.800 2009-05-01 0.1 S. "prod".02 0. "any". Finance Online – Please do not distribute .99 0. "all" e.96 0.99161 > min(z) [1] 0.34 0. "sum".68 0.69 0.37923 > range(z) © 2009.19 2009-06-01 0.2 S.42 0.7.86 1.78 0.42 0.38 0.190 2009-06-01 0.660 2009-02-01 0.330 2009-03-01 0.32 0.42 0.02 0.69 0.06 2009-04-01 0. zoo: > max(z) [1] 0.

026212 > sum(s) [1] 3. Finance Online – Please do not distribute .99161 > prod(s) [1] 0.37923 0.37923 0.37923 > range(s) [1] 0.026212 > sum(z) [1] 3. Time Series Basics 87 [1] 0.4736 © 2009.37923 0.99161 > prod(x) [1] 0.37923 > range(x) [1] 0.99161 > prod(z) [1] 0.4736 timeSeries: > max(s) [1] 0.99161 > min(s) [1] 0.026212 > sum(x) [1] 3.4736 xts: > max(x) [1] 0.1.99161 > min(x) [1] 0.

c(0.0000 1. with > u <.0000 1. "Im". "Mod".58708-2i > Im(z + u) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0 2 0 2 0 2 > Mod(z + u) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0.67986 0.58708 xts: > Arg(x + u) © 2009.41569 2. "Conj".37923 0.42011 0. Finance Online – Please do not distribute . 2i) > u [1] 0+0i 0+2i zoo: > Arg(z + u) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0.7.67986-2i 0.37923+0i 0.11239 0.3638 0.08438 > Re(z + u) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0.37923 2.88 1.99161 0.04365 0.42011-2i 0.2853 > Conj(z + u) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0.2431 0. "Re" e.0000 1.41569 0.99161 2. Group Generics for Time Series Objects Which "Complex" operations are supported by time series objects? Complex: "Arg".41569+0i 0.g.99161+0i 0.

58708-2i > Im(x + u) X.37923 2009-04-01 2.42011 2009-03-01 0.2431 2009-05-01 0.08438 > Re(x + u) X.1 2009-01-01 0.1 2009-01-01 0.37923 2009-04-01 0.67986 2009-05-01 0.1 2009-01-01 0.2853 > Conj(x + u) X.1 2009-01-01 0 2009-02-01 2 2009-03-01 0 2009-04-01 2 2009-05-01 0 2009-06-01 2 > Mod(x + u) X.1.04365 2009-03-01 0.67986-2i 2009-05-01 0.41569 2009-06-01 2.0000 2009-02-01 1. Time Series Basics 89 X.37923+0i 2009-04-01 0.99161 2009-02-01 2.3638 2009-03-01 0.1 2009-01-01 0.0000 2009-04-01 1.99161+0i 2009-02-01 0.42011-2i 2009-03-01 0.0000 2009-06-01 1.11239 2009-05-01 0.41569+0i 2009-06-01 0. Finance Online – Please do not distribute .99161 2009-02-01 0.41569 2009-06-01 0.58708 © 2009.

08438 > Re(s + u) GMT S.1 2009-01-01 0 2009-02-01 2 2009-03-01 0 2009-04-01 2 2009-05-01 0 2009-06-01 2 > Mod(s + u) GMT S.1 2009-01-01 0.2431 2009-05-01 0.0000 2009-06-01 1.90 1.1 2009-01-01 0.0000 2009-04-01 1.99161 2009-02-01 2.99161+0i 2009-02-01 0.2853 > Conj(s + u) GMT S.67986-2i 2009-05-01 0.0000 2009-02-01 1.41569 2009-06-01 2.1 2009-01-01 0.3638 2009-03-01 0.41569+0i 2009-06-01 0.11239 2009-05-01 0. Finance Online – Please do not distribute .7.04365 2009-03-01 0.37923+0i 2009-04-01 0.37923 2009-04-01 2.58708-2i > Im(s + u) GMT S. Group Generics for Time Series Objects timeSeries: > Arg(s + u) GMT S.1 © 2009.42011-2i 2009-03-01 0.

67986 2009-05-01 0. Finance Online – Please do not distribute .58708 © 2009.41569 2009-06-01 0.99161 2009-02-01 0.37923 2009-04-01 0.42011 2009-03-01 0. Time Series Basics 91 2009-01-01 0.1.

90058 0. Common Data: > data <.86024 0.96776 0.Date(charvec)) > na.41014 -0.90058 0.92 1. as. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" "2009-07-01" "2009-08-01" "2009-09-01" zoo: > z <.32398 0.86024 0. 1:9.32398 0. Finance Online – Please do not distribute .8 Missing Data Handling Required R package(s): > library(zoo) > library(xts) > library(timeSeries) How can I omit missing values in a univariate time series? Note that omit can interpreted in several ways.58655 0. By default we mean that we remove the record wit NAs from the data set.96776 0.14042 2009-09-01 2.zoo(data.rnorm(9) > data[c(1. Missing Data Handling 1.41014 NA -0. Later we will extend this meaning by replacing and/or interpolating missing values in a time series object.omit(z) 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-08-01 0. 7)] <.paste("2009-0".58655 © 2009.8. "-01".14042 > charvec <. Now let us remove NAs from a time series.NA > data [1] NA [9] 2.

58655 How can I omit missing values in a multivariate time series? Common Data: > data <.] [.86024 2009-06-01 0.] -0.08348 [3.matrix(rnorm(7*3).09214 -1.3] NA -0.1] <.omit(s) GMT TS.96776 2009-05-01 0.NA > data[4.14042 2009-09-01 2.1] [1. Finance Online – Please do not distribute .Date(charvec)) > na.2] [2.NA > data[3.32398 2009-04-01 0. as.timeSeries(data.298535 NA -0.32398 2009-04-01 0.047918 © 2009.96967 -0.1] 2009-02-01 0.1.90058 2009-03-01 0.96776 2009-05-01 0.41014 2009-08-01 -0.xts(data.14042 2009-09-01 2.1 2009-02-01 0.2] <.] [. charvec) > na. ncol = 3) > data[1.588760 NA 0.58655 timeSeries: > s <.1:2] <. Time Series Basics 93 xts: > x <.90058 2009-03-01 0.86024 2009-06-01 0.41014 2009-08-01 -0.NA > data [.omit(x) [.

77068 -0.08348 0.29853 2009-05-01 -0. as.3] 2009-02-01 -0.911978 [7.30157 0.65321 -0.65321 -0.omit(s) © 2009.82323 1.timeSeries(data.94 1.42253 attr(.77068 -0.09214 -1.08348 [. 1:7.65321 -0.93308 -0.422525 > charvec <. Missing Data Handling [4.100902 [5.xts(data.omit(Z) 2009-02-01 -0.] 1.omit(X) [.52529 2009-06-01 -0.zoo(data. Finance Online – Please do not distribute .27576 0.27576 0.1] 0.525291 [6.paste("2009-0". sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" "2009-07-01" zoo: > Z <.] 0.42253 timeSeries: > s <.Date(charvec)) > na. "-01".82323 1.8.] -0.91198 2009-07-01 0.93308 -0.30157 0.91198 2009-07-01 0.09214 -1.52529 2009-06-01 -0.action") [1] 1 3 4 attr(.82323 1.03234 NA -1.Date(charvec)) > na.93308 -0.] -0.2] [. as.29853 2009-05-01 -0."class") [1] "omit" xts: > X <.27576 0."na.77068 -0.30157 0. charvec) > na.

na.00000 -0. > z <. 7)] <. zoo: Replace for example missing values in a series of financial returns by zero values. the mean or the median object? Common Data: > data <.91198 2009-07-01 0.mean(z.23671 2009-07-01 2009-08-01 2009-09-01 0. the mean.93308 -0.09026 -0.0 > z 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0.Date(charvec)) > z[is. as.00000 0.09214 -1.g.30157 0.2 TS.27576 0.29853 2009-05-01 -0.zoo(data.na(z)] <. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" "2009-07-01" "2009-08-01" "2009-09-01" It may be of interest to replace missing values in a financial return series by a constant value. Time Series Basics 95 GMT TS.65321 -0.74019 -1.zoo(data.na(z)] <.1. "-01".NA > charvec <.82323 1.rnorm(9) > data[c(1.44108 -1.42253 How can I replace missing values in a univariate time series for example by zeros.08348 TS. e.77068 -0.52529 2009-06-01 -0.1 0. > z <. as. or the median.Date(charvec)) > z[is.23273 0.29271 1. Finance Online – Please do not distribute .rm = TRUE) > z © 2009.3 2009-02-01 -0. 1:9. zero.05783 Or Replace for example missing values in a series of financial returns by their sample mean.paste("2009-0".

median(z.xts(data.0 > x [. na.05783 > x <.20620 -0. as.05783 Or Replace for example missing values in a series of financial returns by a robust estimate of the mean.23273 0.29271 2009-04-01 1. na.00000 2009-08-01 0.74019 0. Missing Data Handling 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 -0.74019 -0.74019 -1.96 1.74019 2009-06-01 -1.8.23273 2009-03-01 0.09026 2009-05-01 -0.Date(charvec)) > x[is.1] 2009-01-01 0.na(z)] <.Date(charvec)) > x[is.29271 2009-04-01 1.xts(data. using the median.zoo(data.23273 2009-03-01 0.09026 -0. > z <.44108 2009-09-01 -1.mean(x.na(x)] <.05783 xts: > x <.29271 1. as.74019 -1.29271 1.na(x)] <.44108 -1.23671 2009-07-01 2009-08-01 2009-09-01 -0.74019 © 2009.09026 -0.44108 -1.20620 0.23273 0.09026 2009-05-01 -0.rm = TRUE) > z 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 -0.Date(charvec)) > z[is.20620 2009-02-01 -0. as. Finance Online – Please do not distribute .23671 2009-07-01 2009-08-01 2009-09-01 -0.00000 2009-02-01 -0.1] 2009-01-01 -0.23671 2009-07-01 0.rm = TRUE) > x [.

44108 2009-09-01 -1.23273 2009-03-01 0.1] 2009-01-01 -0. Finance Online – Please do not distribute .mean(s.na(s)] <.1.09026 2009-05-01 -0.23671 2009-07-01 -0.23671 2009-07-01 0.00000 2009-08-01 0. na.Date(charvec)) > x[is.na(s)] <.74019 2009-06-01 -1.rm = TRUE) > x [.xts(data. as.05783 timeSeries: > s <.23273 2009-03-01 0.09026 2009-05-01 -0.74019 2009-08-01 0.44108 2009-09-01 -1.74019 2009-06-01 -1.timeSeries(data. charvec) > s[is. na.1 2009-01-01 -0.00000 2009-02-01 -0.29271 2009-04-01 1.23671 2009-07-01 -0.timeSeries(data.44108 2009-09-01 -1.05783 > x <.20620 2009-08-01 0. Time Series Basics 97 2009-06-01 -1. charvec) > s[is.0 > s GMT TS.1 2009-01-01 0.74019 2009-02-01 -0.29271 2009-04-01 1.median(x.05783 > s <.na(x)] <.20620 © 2009.rm = TRUE) > s GMT TS.

na. the (column) mean.8.05783 > s <.29271 2009-04-01 1.23671 2009-07-01 -0.NA > data [.44108 2009-09-01 -1.74019 2009-06-01 -1.74019 2009-06-01 -1.29271 2009-04-01 1. zero.na(s)] <.09026 2009-05-01 -0. Finance Online – Please do not distribute .70410 © 2009. charvec) > s[is.23273 2009-03-01 0.NA > data[4.median(s.2] <.44108 2009-09-01 -1.rm = TRUE) > s GMT TS.1] <. ncol = 3) > data[1.23273 2009-08-01 0.20620 2009-08-01 0. Common Data: > data <. or the robust (column) median.g.matrix(rnorm(7*3). e. Missing Data Handling 2009-02-01 -0.NA > data[3.1:2] <.98 1.23671 2009-07-01 -0.3] NA 0.timeSeries(data.] [.05783 How can I replace missing values in a multivariate time series object? It may be of interest to replace missing values in a series of financial returns by a constant value.23273 2009-02-01 -0.23273 2009-03-01 0.09026 2009-05-01 -0.1 2009-01-01 -0.96519 0.1] [1.2] [.

000000 0. as.79447 2009-05-01 -0.44003 [3. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" "2009-07-01" zoo: > Z <.1.na(Z)] <.71239 2009-07-01 -0.zoo(data.70410 2009-02-01 -0.paste("2009-0".Date(charvec)) > Z 2009-01-01 NA 0.96519 0.629991 1.] -1.79603 1. 1:7.89683 -0.xts(data.96519 0.35576 NA 0.71239 [7.629991 1.35576 2009-04-01 -1.03604 2009-06-01 -0.629991 1.79603 1.03604 [6.89683 -0.71239 2009-07-01 -0.760570 0. Time Series Basics 99 [2.0 > Z 2009-01-01 0. as.00000 -0.] -0.760570 0.79603 1.89683 -0.359353 0.70410 2009-02-01 -0.3] NA 0.029310 0.359353 0. Finance Online – Please do not distribute .44003 0.] -0.000000 0.1] 2009-01-01 [.35576 [4.] -0.74984 > charvec <.Date(charvec)) > X [.359353 0.760570 0.70410 © 2009.22897 -0.] NA 0.22897 -0.55432 2009-03-01 0.74984 > Z[is.029310 0.55432 NA -0.029310 0.00000 0.] -0.79447 [5.74984 xts: > X <.349331 0.96519 0.44003 0.349331 NA 0.2] [. "-01".55432 2009-03-01 NA 2009-04-01 -1.03604 2009-06-01 -0.79447 2009-05-01 -0.22897 -0.349331 NA -0.

2 TS.359353 0.359353 0.00000 -0.760570 0.71239 2009-07-01 -0.1 TS.79603 1.89683 -0.629991 1.79447 2009-05-01 -0.629991 1. Finance Online – Please do not distribute .3] 0.629991 1.000000 0.2 TS.00000 -0.na(X)] <.349331 NA -0.na(S)] <.74984 timeSeries: > S <.03604 2009-06-01 -0.03604 2009-06-01 -0.0 > S GMT TS.55432 2009-01-01 2009-03-01 NA 2009-04-01 -1.44003 0.74984 © 2009.00000 0.3 NA 0.029310 0.35576 2009-04-01 -1.89683 -0.timeSeries(data.71239 2009-07-01 -0.349331 0.79447 2009-05-01 -0.89683 -0. Missing Data Handling 2009-02-01 -0.70410 2009-02-01 -0.74984 > X[is.35576 2009-04-01 -1.22897 -0.96519 [.3 0. as.1] 0.74984 > S[is.79603 1.44003 0.03604 2009-06-01 -0.79603 1.349331 0.760570 0.79603 1.8.349331 0.55432 2009-01-01 2009-03-01 0.100 1.79447 2009-05-01 -0.00000 0.35576 NA 0.89683 -0.029310 0.629991 1.55432 NA -0.71239 2009-07-01 -0.44003 2009-03-01 NA 2009-04-01 -1.22897 -0.760570 0.55432 2009-01-01 2009-03-01 0.000000 0.359353 0.2] [.029310 0.0 > X [.22897 -0.03604 2009-06-01 -0.44003 0.79447 2009-05-01 -0.1 0.22897 -0.029310 0.70410 2009-02-01 -0.70410 2009-02-01 -0.71239 2009-07-01 -0.000000 0.359353 0.760570 0.96519 0.Date(charvec)) > S GMT TS.96519 TS.35576 NA 0.000000 0.

omit with the argument method for selecting interpolation.1:9 > data[c(1. 7)] <.9836 2009-08-01 2009-09-01 8. Series Common Data: > data <.spline.0000 9. "-01".0000 5. Finance Online – Please do not distribute .1. 1:9.paste("2009-0". sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" "2009-07-01" "2009-08-01" "2009-09-01" zoo: Linear Interpolation using the function approx() > z <.0000 6. timehas the function na.zoo(data. as.Date(charvec)) > z 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 NA 2 3 4 5 6 2009-07-01 2009-08-01 2009-09-01 NA 8 9 > na.NA > data [1] NA 2 3 4 5 6 NA 8 9 > charvec <. Time Series Basics 101 How can I interpolate missing values in a univariate time series object? For this zoo and xts have the functions na.0000 Spline Interpolation using the function spline() > z © 2009.0000 3.approx(z) 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-01 2.approx and na.0000 4.0000 6.

trim(z.spline(z) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0.102 1. as.8.00000 9. Missing Data Handling 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 NA 2 3 4 5 6 2009-07-01 2009-08-01 2009-09-01 NA 8 9 > na.00000 5.Date(charvec)) > xts © 2009.97966 8.63057 2.locf(z) 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-01 2 3 4 5 6 6 2009-08-01 2009-09-01 8 9 Trim Leading/Trailing Missing Observations > z 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 NA 2 3 4 5 6 2009-07-01 2009-08-01 2009-09-01 NA 8 9 > na. Finance Online – Please do not distribute .00000 2009-07-01 2009-08-01 2009-09-01 6.00000 4.xts(data. sides = "left") 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-01 2 3 4 5 6 NA 2009-08-01 2009-09-01 8 9 xts: > xts <.00000 6.00000 Last Observation Carried Forward > z 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 NA 2 3 4 5 6 2009-07-01 2009-08-01 2009-09-01 NA 8 9 > na.00000 3.

Time Series Basics 103 [.1] 2009-02-01 2.05783 © 2009.0000 2009-06-01 6.approx(xts) [.spline(x) [.0000 2009-05-01 5.1] 2009-01-01 -0.1] 2009-01-01 NA 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 2009-07-01 NA 2009-08-01 8 2009-09-01 9 > na.23671 2009-07-01 -0.0000 2009-04-01 4.09026 2009-05-01 -0.05783 > na.23273 2009-03-01 0.9836 2009-08-01 8.44108 2009-09-01 -1.74019 2009-08-01 0.23273 2009-03-01 0.74019 2009-08-01 0.0000 2009-09-01 9.29271 2009-04-01 1.1.0000 > x [.29271 2009-04-01 1. Finance Online – Please do not distribute .23671 2009-07-01 -0.74019 2009-02-01 -0.09026 2009-05-01 -0.0000 2009-03-01 3.44108 2009-09-01 -1.74019 2009-06-01 -1.0000 2009-07-01 6.1] 2009-01-01 -0.74019 2009-06-01 -1.74019 2009-02-01 -0.

Finance Online – Please do not distribute .74019 2009-06-01 -1.1] 2009-01-01 -0.74019 2009-08-01 0.44108 2009-09-01 -1.23273 © 2009.74019 2009-08-01 0. sides = "left") [.23273 2009-03-01 0.09026 2009-05-01 -0.23273 2009-03-01 0.29271 2009-04-01 1.23671 2009-07-01 -0.23671 2009-07-01 -0. Missing Data Handling Last Observation Carried Forward > x [.44108 2009-09-01 -1.74019 2009-06-01 -1.74019 2009-08-01 0.trim(x.09026 2009-05-01 -0.74019 2009-02-01 -0.1] 2009-01-01 -0.74019 2009-02-01 -0.1] 2009-01-01 -0.8.23273 2009-03-01 0.09026 2009-05-01 -0.05783 > na.29271 2009-04-01 1.74019 2009-02-01 -0.05783 Trim Leading/Trailing Missing Observations > x [.104 1.74019 2009-02-01 -0.05783 > na.29271 2009-04-01 1.44108 2009-09-01 -1.23671 2009-07-01 -0.locf(x) [.1] 2009-01-01 -0.74019 2009-06-01 -1.

23671 2009-07-01 -0.05783 timeSeries: Interpolation of time series is done by calling internally the function approx whoch provides linear interpolation.09026 105 2009-05-01 -0. Finance Online – Please do not distribute .29271 2009-04-01 1.1 2009-01-01 0 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 2009-07-01 6 © 2009.44108 2009-09-01 -1.timeSeries(data.1. charvec) > na. Time Series Basics 2009-03-01 0.1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 2009-07-01 6 2009-08-01 8 2009-09-01 9 Interpolate and replace NAs at the beginning and end of the series with zeros > s <. Interpolate and remove (trim) NAs at the beginning and end of the series > s <.omit(s.74019 2009-06-01 -1. charvec) > na.timeSeries(data.74019 2009-08-01 0. "iz") GMT TS.omit(s. "ir") GMT TS.

f = 0. interp = "linear") > sn[is. yleft.na. n = 50.timeSeries(data.omit(s.locf).omit(s.8. Missing Data Handling 2009-08-01 8 2009-09-01 9 Interpolate and extrapolate NAs at the beginning and end of the series > s <. "after") we can select how to interpolate. method = "iz".na(s)] [1] 0 6 > sn <. method = "linear". In the case of "before" we carry the last observation forward (see zoo’s na. and in the case of "after" we carry the next observation backward. method = "iz". "linear". y = NULL. > args(approx) function (x. Finance Online – Please do not distribute .1 2009-01-01 2 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 2009-07-01 6 2009-08-01 8 2009-09-01 9 Through the argument interp=c("before".na. xout.106 1.na(s)] [1] 0 7 > sn <. interp = "before") > sn[is. interp = "after") > sn[is. charvec) > na. "ie") GMT TS.omit(s. rule = 1. yright. > sn <.na.na(s)] [1] 0 8 Note that the way how to perform the linear interpolation can be modified by changing the defaults settings of the arguments in the function approx. method = "iz".omit(s. ties = mean) NULL © 2009.

33651 zoo: > z <. Common Data: > data <. 8)] = NA > data [1] -0.Date(charvec)) > z © 2009. this will be the first such stretch.75352 Aug 2009 NA Sep Mar Apr NA -0. Note that in the event of a tie.1.67703 May 0.65252 -0. as.contiguous() finds the longest consecutive stretch of nonmissing values in a time series object.42813 Oct Nov Jun Jul 1.78623 -0.contiguous(t) Apr 2009 -0.33651 0.15183 -1.ts(data.rnorm(12) > data[c(3.78623 -0.24610 > charvec <.13887 -0.75352 [8] NA NA -0.62797 -0.42813 Jun Jul 1.13887 -0.67703 0.character(timeCalendar(2009)) > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" "2009-07-01" "2009-08-01" "2009-09-01" "2009-10-01" [11] "2009-11-01" "2009-12-01" ts: > t <.contiguous() work for a univariate time series objects? The function na. frequency = 12) > t Jan Feb 2009 -0.24610 > na. Time Series Basics 107 Does the function na.65252 -0.as.62797 -0.67703 May 0. Finance Online – Please do not distribute .zoo(data.15183 -1.33651 Dec 0.15183 -1.42813 1. start = 2009.

67703 2009-07-01 2009-08-01 2009-09-01 2009-10-01 2009-11-01 2009-12-01 0.1] 2009-04-01 -0. Finance Online – Please do not distribute .42813 2009-05-01 1.42813 1.33651 2009-08-01 NA 2009-09-01 0.42813 1.24610 > na.75352 NA -0.15183 2009-06-01 -1.33651 xts: > x <.42813 2009-05-01 1.24610 > na.Date(charvec)) > x [.62797 -0.65252 2009-10-01 -0.1 © 2009.67703 0.13887 2009-02-01 -0. as.65252 -0.1] 2009-01-01 -0.67703 2009-07-01 0.8.contiguous(x) [.33651 timeSeries: > s <.33651 NA 0.75352 2009-03-01 NA 2009-04-01 -0. charvec) > s GMT TS.13887 -0.contiguous(z) 2009-04-01 2009-05-01 2009-06-01 2009-07-01 -0.timeSeries(data.15183 -1.67703 2009-07-01 0. Missing Data Handling 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 -0.xts(data.108 1.62797 2009-11-01 -0.15183 2009-06-01 -1.15183 -1.78623 2009-12-01 -0.78623 -0.

78623 2009-12-01 -0. Finance Online – Please do not distribute .42813 2009-05-01 1.contiguous(s) GMT TS.75352 2009-03-01 NA 2009-04-01 -0.15183 2009-06-01 -1. Time Series Basics 109 2009-01-01 -0.42813 2009-05-01 1.67703 2009-07-01 0.13887 2009-02-01 -0.33651 2009-08-01 NA 2009-09-01 0.67703 2009-07-01 0.33651 © 2009.62797 2009-11-01 -0.1 2009-04-01 -0.1.65252 2009-10-01 -0.15183 2009-06-01 -1.24610 > na.

.

Chapter 2 Reporting 111 .

as. 1:6. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" xts: > zoo(data.Date(charvec)) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 1 2 3 4 5 6 xts: > xts(data.1:6 > charvec <. Printing Time Series Objects 2.Date(charvec)) [. Finance Online – Please do not distribute .1.paste("2009-0".1] 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 © 2009.112 2. "-01". as.1 Printing Time Series Objects Required R package(s): > library(zoo) > library(xts) > library(timeSeries) How can I print a time series object? Time series objects are displayed in the usual way as other data objects in R > data <.

Date(charvec))) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 1 2 3 4 5 6 xts: > print(xts(data. Finance Online – Please do not distribute .Date(charvec))) [. as. Reporting 113 timeSeries: > timeSeries(data. as.2.1:6 > charvec <.1] 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 © 2009. 1:6.paste("2009-0". charvec) GMT TS.1 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 Alternatively we can use explicitely the generic print function print() > data <. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" xts: > print(zoo(data. "-01".

Printing Time Series Objects timeSeries: > print(timeSeries(data. 1:6. charvec)) GMT TS. Finance Online – Please do not distribute .paste("2009-0". "-01". © 2009.114 2.1:6 > charvec <.1. charvec)) GMT TS.1 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 Since a timeSeries object is represented by a S4 class we can also use > show(timeSeries(data. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: Using the print function univariate zoo time series are always displayed in an horizontal style.1 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 How can I select the style for printing an univariate times series? Common Data: > data <.

Finance Online – Please do not distribute .Date(charvec))) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 1 2 3 4 5 6 xts: Using the print function univariate xts time series are always displayed in an vertical style. as. Reporting 115 > print(zoo(data.2. > print(xts(data. as. as.xts(xts(data.1] 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 If you want to have printed zoo objects vertically and xts horizontically type > print(as. as.Date(charvec)))) [.Date(charvec)))) 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 > print(as.Date(charvec))) [.zoo(xts(data.1] 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 © 2009.

116 2. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: To our knowledge.1:6 > charvec <. Finance Online – Please do not distribute . © 2009.1. Printing Time Series Objects timeSeries: The timesSeries class comes with a generic print function which allows to customize the printout in several ways.timeSeries(data.paste("2009-0". no. charvec) > print(s) GMT TS. 1:6. For example an univariate timeSeries object is printed by default in vertical style > s <. style = "h") 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 1 2 3 4 5 6 Is it possible to display beside the ISO date/time format other formats when we print a a time series ? Common Data: > data <.1 2009-01-01 1 2009-02-01 2 2009-03-01 3 2009-04-01 4 2009-05-01 5 2009-06-01 6 but it can also be printed in horizontal style > print(s. "-01".

1416 > print(s.1416 > print(s. timeSeries: The generic print function for the timeSeries class allows to customize the printing format.timeSeries(pi. format = "DayNo %j Hour: %H") GMT s DayNo 128 Hour: 19 3. format = "%m/%d/%y %H:%M") GMT s 05/08/09 19:26 3.1416 The first example prints in default ISO format. otherwise not. units = "s") > print(s) GMT s 2009-05-08 19:26:22 3. "2009-05-08 19:26:22".2. Here are some examples > s <. format = "%m/%d/%y %A") GMT s 05/08/09 Friday 3.1416 > print(s. Reporting 117 xts: To our knowledge. Finance Online – Please do not distribute . and the last example prints the day of the year together the truncated hour of the day. no. © 2009. Can time series Objects be printed in the style of R’s ts objects? zoo: In the case of a regular zoo time series yes. the second in month-day-year format with the full name of the weekday.

"06". 1:6. "09". by = "quarter") Qtr1 Qtr2 Qtr3 Qtr4 2009 1 2 2010 3 4 1 2 2011 3 4 1 2 How can we print a time series with respect to another time zone? zoo: There is no direct way to do it. Finance Online – Please do not distribute . xts: There is no direct way to do it.1:6 > charvec <.paste("2009-0". © 2009. charvec) > print(s. sep = "") > s <. "-01". style = "ts") Jan Feb Mar Apr May Jun 2009 1 2 3 4 5 6 and in the case of quarterly records > data <.1. charvec) > print(s.timeSeries(data.1:4 > charvec <.timeSeries(data. "-01". no. style = "ts".paste("2009-". timeSeries: The print method for timeSeries objects has a style="ts" option. c("03". "12"). sep = "") > s <.118 2. For example if you have monthly records > data <. Printing Time Series Objects xts: To our knowledge.

45696 2009-05-01 23:00:00 -0.47390 > print(s.1 2009-01-02 07:00:00 0. FinCenter = "Zurich") > print(s.49877 2009-06-01 16:00:00 -0. For example if you have monthly records > data <.45696 2009-05-01 16:00:00 -0. Reporting 119 timeSeries: The print method for timeSeries objects has a FinCenter=NULL option. FinCenter = "Zurich") Zurich TS. zone = "Chicago".47390 © 2009.62115 2009-03-01 23:00:00 -0. charvec.61180 2009-02-01 16:00:00 -0.49877 2009-06-02 06:00:00 -0.49877 2009-06-01 23:00:00 -0. FinCenter = "Chicago") Chicago TS.62115 2009-03-02 07:00:00 -0.47390 > print(s.1 2009-01-01 23:00:00 0. 1:6.2. Finance Online – Please do not distribute .25072 2009-04-02 06:00:00 -0.rnorm(6) > charvec <.62115 2009-03-01 16:00:00 -0.25072 2009-04-01 16:00:00 -0. FinCenter = "Tokyo") Tokyo TS.61180 2009-02-01 23:00:00 -0.61180 2009-02-02 07:00:00 -0.paste("2009-0".25072 2009-04-01 23:00:00 -0.timeSeries(data. sep = "") > s <.45696 2009-05-02 06:00:00 -0. "-01 16:00:00".1 2009-01-01 16:00:00 0.

widths = 1. 2.labels = FALSE.. ylim = NULL. xts: > args(plot.seed(1953) > data <. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > args(plot. 0).xts) © 2009.. lty = 1.type. mar = c(0. 5. oma = c(6. plot. nc. "-01". main = NULL.POSIXct(charvec.1. xlim = NULL. 0. panel = lines. Plotting Time Series Objects 2. ylab = NULL.2.runif(6) > charvec <. 0. as.1). heights = 1.zoo(data. 5. 1:6.120 2. screens.) NULL > z <.2 Plotting Time Series Objects Required R package(s): > library(zoo) > library(xts) > library(timeSeries) How can I plot a univariate time series using the generic plot function? Common Data: > set.lines = NULL. type = "l". .paste("2009-0". Finance Online – Please do not distribute . pch = 1.zoo) function (x. xy. tz = "GMT")) > plot(z) Note for this example the labels on the x axis have no year numbers. col = 1. y = NULL. xy. xlab = "Index".

col = "white". Finance Online – Please do not distribute . as.) NULL > x <.xts(data. charvec) > plot(s) How can I plot a multivariate time series using the generic plot function? Common Data: > set. .xts(data. y = NULL. ann = TRUE. major. Reporting 121 function (x.2.zoo(data..matrix(runif(12).timeSeries(data. ncol = 2) > charvec <.POSIXct(charvec.col = "grey".POSIXct(charvec. type = "l".grid = TRUE. tz = "GMT")) > plot(Z) xts: > X <.paste("2009-0". candle. as. tz = "GMT")) > plot(X) © 2009. tz = "GMT")) > plot(x) timeSeries: timeSeries has an S4 generic method for plotting. minor.format = TRUE. axes = TRUE. "-01". sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > Z <. as. > s <. major.seed(1953) > data <. bar. auto.POSIXct(charvec. 1:6..ticks = TRUE.ticks = "auto".

type = "single") © 2009. timeSeries: > S <.paste("2009-0".timeSeries(data. as. "-01". FinCenter = "GMT")) > plot(S.2. timeSeries: > S <.zoo(data. plot.type = "single") xts: xts does not support multivariate time series plots in in single charts. as. charvec) > plot(S) How can I plot a multivariate time series in a single chart using the generic plot function? Common Data: > set. 1:6.matrix(runif(18). sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > Z <. tz = "GMT")) > plot(Z.POSIXct(charvec. Finance Online – Please do not distribute .122 xts 2.POSIXct(charvec.seed(1953) > data <. ncol = 3) > charvec <. plot. Plotting Time Series Objects does not support multivariate time series plots in in multiple charts.timeSeries(data.

paste("2009-0".paste("2009-0". tz = "GMT")) > z2 <. charvec. as.zoo(data2.rnorm(9) > charvec <.zoo(data1.seed(1953) > data1 <. FinCenter = "GMT") > plot(s1) > lines(s2.timeSeries(data2. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" "2009-07-01" "2009-08-01" "2009-09-01" zoo: > z1 <. "-01". FinCenter = "GMT") > s2 <.POSIXct(charvec. Reporting 123 How can I add a line to an existing plot? Common Data: > set. as. col = 2) timeSeries: > s1 <.timeSeries(data1. "-01". sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" "2009-07-01" "2009-08-01" "2009-09-01" © 2009.2) > charvec <.2. charvec.rnorm(9. Finance Online – Please do not distribute .POSIXct(charvec. col = 2) How can I add points to an existing plot? Common Data: > set. sd = 0. tz = "GMT")) > plot(z1) > lines(z2. 1:9. 1:9.seed(1953) > data <.rnorm(9) > data2 <.

tz = "GMT")) > plot(z) > abline(v = index(z). as. "-01". charvec. col = "darkgrey". sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" "2009-07-01" "2009-08-01" "2009-09-01" zoo: > z <. pch = 19) timeSeries: > s <. col = "darkgrey". tz = "GMT")) > plot(z) > points(z.timeSeries(data.seed(1953) > data <.rnorm(9) > charvec <. pch = 19) Can I use abline for time series plots? Common Data: > set.POSIXct(charvec. charvec. FinCenter = "GMT") > plot(s) > points(s. lty = 3) timeSeries: > s <.zoo(data.zoo(data. lty = 3) © 2009.POSIXct(charvec. 1:9.paste("2009-0". FinCenter = "GMT") > plot(s) > abline(v = time(s). as. Plotting Time Series Objects zoo: > z <.2. Finance Online – Please do not distribute . col = 2.124 2. col = 2.timeSeries(data.

Finance Online – Please do not distribute .1 0.Example of a single panel plot from zoo.4 0.2 0.2 0. xts and timeSeries © 2009.6 2.1: Plot 1 .2 TS.6 Jan 2009 2009−03−02 Time Figure 2.4 0.6 x Feb 2009 2009−01−01 2009−01−31 Mar 2009 Apr 2009 May 2009 Jun 2009 2009−04−01 2009−05−01 2009−06−01 0.125 z 0.4 0. Reporting Jan Mar May Index 0.

6 0.2: Plot 2a .4 Series 2 0.126 2.2 Series 1 0.2. Finance Online – Please do not distribute .Multivariate Time Series plots in multiple graphs © 2009.4 0. Plotting Time Series Objects 0.6 Z Jan Mar May Index Figure 2.8 0.

3: Plot 2b .2 0.6 0.8 0.2. Reporting 127 0.2 TS.6 x 2009−01−01 2009−03−02 2009−05−01 Time Figure 2.4 TS.1 0.Multivariate Time Series plots in multiple graphs © 2009.4 0. Finance Online – Please do not distribute .

6 Z Jan Mar May Index Figure 2.128 2.4 Series 2 0. Finance Online – Please do not distribute .4: Plot3a .6 0.2.8 0.8 0.4 0.4 0. Plotting Time Series Objects 0.6 0.2 Series 3 0.2 0.Example of a single panel plot from zoo © 2009.5 0.3 Series 1 0.

6 0.2 0. Finance Online – Please do not distribute .8 0.3 TS.6 0.8 0.4 0.5: Plot 3b .4 TS.1 0.2 0.4 TS. Reporting 129 0.6 x 2009−01−01 2009−01−31 2009−03−02 2009−04−01 2009−05−01 2009−06−01 Time Figure 2.Example of a single panel plot from timeSeries © 2009.2 0.2.5 0.3 0.

Chapter 3

Using R Functions

131

132

3.1. Functions and Methods from base R

3.1 Functions and Methods from base R
Required R package(s):
> library(zoo)
> library(xts)
> library(timeSeries)

In the following we test some selected functions that are relevant for use
in financial time series analysis. We test them on multivariate time series
objects
> set.seed(1953)
> data <- matrix(runif(18), ncol = 3)
> charvec <- rev(paste("2009-0", 1:6, "-01", sep = ""))
> charvec
[1] "2009-06-01" "2009-05-01" "2009-04-01" "2009-03-01" "2009-02-01"
[6] "2009-01-01"

zoo:
> Z <- zoo(data, as.Date(charvec))
> colnames(Z) = paste("Z", 1:3, sep = ".")

xts:
> X <- xts(data, as.Date(charvec))
> colnames(X) = paste("X", 1:3, sep = ".")

timeSeries:
> S <- timeSeries(data, charvec)
> colnames(S) = paste("S", 1:3, sep = ".")

and/or univariate time series objects.

© 2009, Finance Online – Please do not distribute

3. Using R Functions

133

zoo:
> z <- Z[, 1]

xts:
> x <- X[, 1]

timeSeries:
> s <- S[, 1]

Can I use apply like in R’s base package?
The function apply() returns a vector or array or list of values obtained by
applying a function to margins of an array. How does the function works
together with time Series objects?

zoo:
> apply(Z, 2, mean)
Z.1

Z.2

Z.3

0.41652 0.58821 0.58572

xts:
> apply(X, 2, mean)
X.1

X.2

X.3

0.41652 0.58821 0.58572

timeSeries:
> apply(S, 2, mean)

© 2009, Finance Online – Please do not distribute

2 S."class") [1] "try-error" xts: > print(try(attach(X))) [1] "Error in attach(X) : \n 'attach' only works for lists.23193 © 2009. Finance Online – Please do not distribute . data frames and environments\n" attr(."class") [1] "try-error" timeSeries: > attach(S) > colnames(S) [1] "S.3 0.58572 Can I use attach like in R’s base package? The database is attached to the R search path.58821 0.3" > S.96524 0.1 S. zoo: > print(try(attach(Z))) [1] "Error in attach(Z) : \n 'attach' only works for lists.57402 0. Functions and Methods from base R S.59127 0. data frames and environments\n" attr(.1" "S.134 3.2 [1] 0. so objects in the database can be accessed by simply giving their names.59316 0. This means that the database is searched by R when evaluating a variable.2" "S.41652 0.1.57365 0.

294113 -0.28980 2009-03-01 -0.46521 2009-06-01 0.212986 -0.264387 -0.212986 0.031721 0.46521 2009-06-01 0.031721 0.36122800 0.212986 2009-04-01 -0.294113 0.212986 0.01761714 -0.294113 2009-06-01 0.3.1 Z. zoo: > diff(Z) Z.437452 0.3 0.00036655 0.11545 2009-04-01 -0.437452 2009-03-01 -0.2 Z.25063 2009-02-01 2009-05-01 0.264387 2009-05-01 0.031721 xts: > diff(X) X.37207930 0.36122800 0.01761714 -0.25063 2009-05-01 0.37396709 0.28980 2009-03-01 -0.031721 © 2009.264387 -0.264387 0.437452 0.437452 -0.2 2009-01-01 NA NA X.37396709 0. Using R Functions 135 Can I use diff like in R’s base package? The function diff() returns suitably lagged and iterated differences.53205 > diff(z) 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0. Finance Online – Please do not distribute .3 NA 2009-02-01 0.53205 > diff(x) X.00036655 0.294113 -0.1 2009-01-01 NA 2009-02-01 0.37207930 0.11545 2009-04-01 -0.1 X.

00036655 -0.1 2009-06-01 NA 2009-05-01 -0.437452 Can I use dim like in R’s base package? The function dim() retrieves or sets the dimension of an object.25063 0.53205 2009-04-01 -0.28980 > diff(s) GMT S.3 NA NA NA 2009-05-01 -0.294113 0.37396709 -0.212986 2009-01-01 -0.212986 -0.2 S.294113 2009-03-01 0.1 S.11545 2009-01-01 -0.36122800 -0. Finance Online – Please do not distribute . Functions and Methods from base R timeSeries: > diff(S) GMT 2009-06-01 S.37207930 -0.01761714 2009-03-01 0.264387 0.1.264387 2009-02-01 0.46521 2009-02-01 0.031721 2009-04-01 -0.437452 -0. zoo: > dim(Z) [1] 6 3 > dim(z) NULL xts: > dim(X) [1] 6 3 © 2009.031721 -0.136 3.

equal values) and missing values can be handled in several ways. zoo: > print(try(rank(Z))) [1] "Error in names(y) <."class") [1] "try-error" > print(try(rank(z))) [1] "Error in if (xi == xj) 0L else if (xi > xj) 1L else -1L : \n argument is of length zero\n" attr(.nm[!nas] : \n 'names' attribute [18] must be the same length as the vector [0]\n" attr(.xts`(x. Finance Online – Please do not distribute . !nas) : i is out of range\n\n" attr(. Ties (i. Using R Functions 137 > dim(x) [1] 6 1 timeSeries: > dim(S) [1] 6 3 > dim(s) [1] 6 1 Can I use rank like in R’s base package? The function rank() returns the sample ranks of the values in a vector.e."class") [1] "try-error" © 2009."class") [1] "try-error" xts: > print(try(rank(X))) [1] "Error in `[..3.

Finance Online – Please do not distribute . zoo: > print(try(rev(Z))) [1] "Error in `[."class") [1] "try-error" timeSeries: > rank(S) GMT S.1 S. Functions and Methods from base R > print(try(rank(x))) [1] "Error in if (xi == xj) 0L else if (xi > xj) 1L else -1L : \n argument is of length zero\n" attr(.2 S.zoo`(x.1 2009-06-01 5 2009-05-01 4 2009-04-01 1 2009-03-01 3 2009-02-01 6 2009-01-01 2 Can I use rev like in R’s base package? The function rev() provides a reversed version of its argument. length(x):1L) : subscript out of bounds\n" attr(.3 2009-06-01 5 3 6 2009-05-01 4 2 2 2009-04-01 1 4 5 2009-03-01 3 6 4 2009-02-01 6 5 3 2009-01-01 2 1 1 > rank(s) GMT S."class") [1] "try-error" © 2009.138 3.1.

3. Using R Functions

139

> rev(z)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01
0.22283

0.66028

0.44730

0.18291

0.47702

0.50875

xts:
> print(try(rev(X)))
[1] "Error in `[.xts`(x, length(x):1L) : subscript out of bounds\n"
attr(,"class")
[1] "try-error"
> rev(x)
X.1
2009-01-01 0.22283
2009-02-01 0.66028
2009-03-01 0.44730
2009-04-01 0.18291
2009-05-01 0.47702
2009-06-01 0.50875

timeSeries:
> rev(S)
GMT
S.1

S.2

S.3

2009-01-01 0.22283 0.23193 0.20833
2009-02-01 0.66028 0.59316 0.49813
2009-03-01 0.44730 0.96524 0.61359
2009-04-01 0.18291 0.59127 0.86422
2009-05-01 0.47702 0.57365 0.39901
2009-06-01 0.50875 0.57402 0.93106
> rev(s)
GMT
S.1
2009-01-01 0.22283
2009-02-01 0.66028
2009-03-01 0.44730
2009-04-01 0.18291

© 2009, Finance Online – Please do not distribute

140

3.1. Functions and Methods from base R

2009-05-01 0.47702
2009-06-01 0.50875

Can I use sample like in R’s base package?
The function sample() takes a sample of the specified size from the elements
of x using either with or without replacement.

zoo:
> print(try(sample(Z)))
[1] "Error in `[.zoo`(x, .Internal(sample(length(x), size, replace, prob))) :
\n subscript out of bounds\n"
attr(,"class")
[1] "try-error"
> sample(z)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01
0.22283

0.66028

0.44730

0.18291

0.47702

0.50875

xts:
> print(try(sample(X)))
[1] "Error in `[.xts`(x, .Internal(sample(length(x), size, replace, prob))) :
\n subscript out of bounds\n"
attr(,"class")
[1] "try-error"
> sample(x)
X.1
2009-01-01 0.22283
2009-02-01 0.66028
2009-03-01 0.44730
2009-04-01 0.18291
2009-05-01 0.47702
2009-06-01 0.50875

© 2009, Finance Online – Please do not distribute

3. Using R Functions

141

timeSeries:
> sample(S)
GMT
S.1

S.2

S.3

2009-03-01 0.44730 0.96524 0.61359
2009-02-01 0.66028 0.59316 0.49813
2009-04-01 0.18291 0.59127 0.86422
2009-05-01 0.47702 0.57365 0.39901
2009-06-01 0.50875 0.57402 0.93106
2009-01-01 0.22283 0.23193 0.20833
> sample(s)
GMT
S.1
2009-04-01 0.18291
2009-05-01 0.47702
2009-06-01 0.50875
2009-02-01 0.66028
2009-01-01 0.22283
2009-03-01 0.44730

Can I use scale like in R’s base package?
The function scale() is generic function whose default method centers
and/or scales the columns of a numeric matrix. How does it work together
with time serie sobjects?

zoo:
> scale(Z)
Z.1

Z.2

Z.3

2009-01-01 -1.06742 -1.534521 -1.36437
2009-02-01

1.34343

0.021305 -0.31667

2009-03-01

0.16964

1.623869

0.10073

2009-04-01 -1.28742

0.013175

1.00683

2009-05-01

0.33347 -0.062703 -0.67501

2009-06-01

0.50829 -0.061125

1.24849

attr(,"scaled:center")
Z.1

Z.2

Z.3

© 2009, Finance Online – Please do not distribute

142

3.1. Functions and Methods from base R

0.41652 0.58821 0.58572
attr(,"scaled:scale")
Z.1

Z.2

Z.3

0.18145 0.23218 0.27660
> scale(z)
2009-01-01 -1.06742
2009-02-01

1.34343

2009-03-01

0.16964

2009-04-01 -1.28742
2009-05-01

0.33347

2009-06-01

0.50829

attr(,"scaled:center")
[1] 0.41652
attr(,"scaled:scale")
[1] 0.18145

xts:
> scale(X)
X.1

X.2

X.3

2009-01-01 -1.06742 -1.534521 -1.36437
2009-02-01

1.34343

0.021305 -0.31667

2009-03-01

0.16964

1.623869

0.10073

2009-04-01 -1.28742

0.013175

1.00683

2009-05-01

0.33347 -0.062703 -0.67501

2009-06-01

0.50829 -0.061125

1.24849

> scale(x)
X.1
2009-01-01 -1.06742
2009-02-01

1.34343

2009-03-01

0.16964

2009-04-01 -1.28742
2009-05-01

0.33347

2009-06-01

0.50829

timeSeries:
> scale(S)

© 2009, Finance Online – Please do not distribute

18291 0.06742 Can I use sort like in R’s base package? The function sort (or order) sorts a vector or factor (partially) into ascending (or descending) order.062703 -0.50829 -0.g.013175 1.28742 2009-03-01 0.33347 2009-04-01 -1.623869 0.47702 0.50829 2009-05-01 0.zoo`(x. Finance Online – Please do not distribute . For ordering along more than one variable.28742 0. order(x.1 1. Using R Functions 143 GMT S.33347 -0.3..00683 2009-03-01 0.36437 > scale(s) GMT S.last = na.061125 S.16964 1. na. e.10073 2009-02-01 1.021305 -0.66028 0.06742 -1."class") [1] "try-error" > sort(z) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0.34343 2009-01-01 -1.16964 2009-02-01 1.1 2009-06-01 0.31667 2009-01-01 -1.last.44730 0.2 S.534521 -1. for sorting data frames.67501 2009-04-01 -1.50875 xts: > print(try(sort(X))) © 2009.3 2009-06-01 0. zoo: > print(try(sort(Z))) [1] "Error in `[. see order.22283 0.24849 2009-05-01 0. decreasing = decreasing)) : \n subscript out of bounds\n" attr(.34343 0.

50875 timeSeries: > sort(S) GMT S.47702 2009-06-01 0.3 2009-01-01 0.last.50875 Can I use start and end like in R’s base package? The functions start() and end() extract and encode the times the first and last observations were taken.66028 2009-03-01 0. Functions and Methods from base R [1] "Error in `[.2 S.18291 0.last = na. order(x.18291 2009-05-01 0.22283 0.47702 0. na.59316 0.1. Finance Online – Please do not distribute .22283 2009-02-01 0.23193 0.xts`(x.66028 0. decreasing = decreasing)) : \n subscript out of bounds\n" attr(.1 2009-01-01 0.22283 2009-02-01 0.39901 2009-06-01 0."class") [1] "try-error" > sort(x) X.44730 2009-04-01 0.1 2009-01-01 0.86422 2009-05-01 0.66028 2009-03-01 0.18291 2009-05-01 0.1 S.49813 2009-03-01 0.93106 > sort(s) GMT S.96524 0.20833 2009-02-01 0.57402 0.61359 2009-04-01 0.59127 0.44730 0.44730 2009-04-01 0.144 3.57365 0.47702 2009-06-01 0. © 2009.50875 0.

3. end(z) [1] "2009-01-01" [1] "2009-06-01" xts: > start(X). end(Z) [1] "2009-01-01" [1] "2009-06-01" > start(z). end(s) GMT [1] [2009-01-01] GMT [1] [2009-06-01] © 2009. Using R Functions 145 zoo: > start(Z). end(S) GMT [1] [2009-01-01] GMT [1] [2009-06-01] > start(s). end(X) [1] "2009-01-01" [1] "2009-06-01" > start(x). Finance Online – Please do not distribute . end(x) [1] "2009-01-01" [1] "2009-06-01" timeSeries: > start(S).

59127 0.66028 0. 1:3.Date(charvec)) > colnames(Z) = paste("Z".50875 0.96524 0.1 Z.57365 0.61359 2009-05-01 0. 1:6. ncol = 3) > charvec <.2 Functions and Methods from stats R Required R package(s): > library(zoo) > library(xts) > library(timeSeries) In the following we test selected functions which are relevant for use in financial time series analysis. Common Data: We test them on multivariate > set.93106 2009-02-01 0. as.3 2009-01-01 0.seed(1953) > data <.22283 0.57402 0.zoo(data.") > Z Z.49813 2009-06-01 0.86422 2009-04-01 0. sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > Z <.59316 0.23193 0.39901 2009-03-01 0. Finance Online – Please do not distribute .47702 0.matrix(runif(18).146 3. "-01".2 Z.paste("2009-0".44730 0. sep = ".18291 0.2.20833 xts: © 2009. Functions and Methods from stats R 3.

1 X.Date(charvec)) > colnames(X) = paste("X".96524 0. charvec) > colnames(S) = paste("S".96524 0. sep = ".18291 0.93106 2009-02-01 0. 1:3.44730 0.44730 0.39901 2009-03-01 0.50875 0.57365 0.59127 0.3 2009-01-01 0.57365 0.50875 0.2 X.xts(data.57402 0.50875 0.59316 0.X[. as.timeSeries(data.20833 timeSeries: > S <.93106 2009-02-01 0. Finance Online – Please do not distribute .59316 0.86422 2009-04-01 0. sep = ".Z[.61359 2009-05-01 0. 1] > z 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0.66028 0.44730 0.47702 0.1 S.66028 0.66028 0.3.47702 0.49813 2009-06-01 0.20833 and/or univariate time series objects. 1] > x © 2009.3 2009-01-01 0.49813 2009-06-01 0.18291 0.23193 0.61359 2009-05-01 0.23193 0.22283 0. 1:3.39901 2009-03-01 0.22283 0. zoo: > z <.18291 0.") > S GMT S.86422 2009-04-01 0.59127 0.") > X X. Using R Functions 147 > X <.57402 0.2 S.47702 0.22283 xts: > x <.

068 sigma^2 estimated as 0.18291 2009-04-01 0.148 3.47702 2009-03-01 0.50875 2009-02-01 0.66028 2009-06-01 0.2. aic = -0.1 2009-01-01 0.1 2009-01-01 0.e.S[. zoo: > arima(z) Call: arima(x = z) Coefficients: intercept 0.0274: log likelihood = 2.18291 2009-04-01 0.27. Finance Online – Please do not distribute .44730 2009-05-01 0. 0.47702 2009-03-01 0. 1] > s GMT S.22283 timeSeries: > s <. Functions and Methods from stats R X.22283 Can I use arima like in R’s base package? The function arima() fits an ARIMA model to a univariate time series.55 © 2009.50875 2009-02-01 0.417 s.66028 2009-06-01 0.44730 2009-05-01 0.

068 sigma^2 estimated as 0.ts(x)) : missing values in object\n" attr(.fail.27. aic = -0. 0. aic = -0. 0.27.default(as.0274: Can I use acf like in R’s stats package? The function acf() computes (and by default plots) estimates of the autocovariance or autocorrelation function.417 s. Function ccf() computes the cross-correlation or cross-covariance of two univariate series.e."class") [1] "try-error" © 2009.3.55 timeSeries: > arima(s) Call: arima(x = s) Coefficients: intercept 0. zoo: > print(try(acf(z))) [1] "Error in na.417 s.0274: log likelihood = 2. Using R Functions 149 xts: > arima(x) Call: arima(x = x) Coefficients: intercept 0. Function pacf() is the function used for the partial autocorrelations.068 sigma^2 estimated as 0.55 log likelihood = 2. Finance Online – Please do not distribute .e.

502 0.337 -0. Finance Online – Please do not distribute .0016191 Z. and timeSeries objects these functions behave in the following way.000 -0.0329245 0.1667 0.337 -0.2 Z.1 0. by lag 0. by lag 2 3 1.382 0.065 -0.502 0 1 0. If x and y are matrices then the covariances (or correlations) between the columns of x and the columns of y are computed.2500 0. Functions and Methods from stats R xts: > print(try(acf(x))) Autocorrelations of series 'x'.015783 0.150 3.2 0.0016191 0. xts.4167 1.0833 0.028640 0.053906 0.065 -0.382 4 5 0.3 0.0286396 Z.2. cov() and cor() compute the variance of x and the covariance or correlation of x and y if these are vectors. zoo: > var(z) [1] 0.3 Z.000 -0.0157826 0.1 Z.3333 0.0765103 > cov(Z) © 2009.109 timeSeries: > print(acf(s)) Autocorrelations of series 's'.0000 0.032925 > var(Z) Z. Applied to zoo.109 Can I use cov like in R’s stats package? The functions var().

Using R Functions Z.0157826 0.1 151 Z.0765103 > cor(Z) Z.032925 > var(X) X.3 0.0765103 > cov(X) X.032925 © 2009.3 Z.000000 0.3 0.015783 0.3 0.00000 0.032259 0.0157826 0.000000 xts: > var(x) X.374627 1.0157826 0.1 1.445951 Z.2 X.1 Z.053906 0.032259 Z.37463 0.1 1.1 X.032259 0.00000 0.1 S. Finance Online – Please do not distribute .2 0.0016191 0.445951 X.1 X.053906 0.015783 0.2 0.3 0.0286396 X.0016191 0.0016191 X.1 X.032259 X.3.2 0.3 Z.0329245 0.2 X.0329245 0.1 0.0765103 > cor(X) X.1 0.1 X.1 0.0286396 X.2 X.1 0.000000 0.0286396 Z.374627 1.015783 0.2 0.2 Z.3 0.3 X.1 0.3 X.44595 1.053906 0.37463 0.028640 0.0016191 Z.028640 0.2 Z.3 X.028640 0.0329245 0.44595 1.0016191 X.2 0.0016191 0.000000 timeSeries: > var(s) S.

152 3.46663 0.3 S.44400 2009-06-01 0.1 1.2 0. Finance Online – Please do not distribute .374627 1.000000 0.0016191 S.1 Z.2 0.1 0.00000 0.45908 0.0765103 > cov(S) S.0286396 S.0765103 > cor(S) S.67321 Z.60159 0.2 0.74894 0.445951 S.3 0.053906 0.55066 2009-04-01 0.60475 © 2009.0157826 0.3 S.3 0.0016191 0.015783 0.44595 1.032259 0.028640 0.86738 0.0329245 0.0016191 S.83831 0.2 0.0286396 S.1 0.2 Z.053906 0.52208 2009-05-01 0.1 S.2 S.20926 0.1 S.84918 0.3 0.33307 0.2 S.0016191 0.000000 Can I use dist like in R’s stats package? This function dist() computes and returns the distance matrix computed by using the specified distance measure to compute the distances between the rows of a data matrix.015783 0.2.032259 S.0329245 0.37463 0. Functions and Methods from stats R > var(S) S.63705 > dist(t(Z)) Z.2 S.028640 0.1 S.44751 0.3 S.50756 0. zoo: > dist(Z) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-02-01 0.3 0.53300 2009-03-01 0.0157826 0.

60475 Can I use dnorm like in R’s stats package? The function dnorm() computes the density for the normal distribution with mean equal to mean and standard deviation equal to sd.2 0.1 S.2 S.52208 2009-05-01 0. sd = 1) © 2009.20926 0.83831 0.53300 2009-03-01 0.74894 0.67321 S.60159 0.44400 2009-06-01 0.63705 > dist(t(X)) X.50756 0.1 X.3. mean = 0.84918 0.44751 0.44751 0.3 0.55066 2009-04-01 0.45908 0.50756 0.63705 > dist(t(S)) S.2 0.45908 0.84918 0.46663 0.86738 0.46663 0.86738 0. Using R Functions 153 xts: > dist(X) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-02-01 0. Finance Online – Please do not distribute .3 0.33307 0.60475 timeSeries: > dist(S) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-02-01 0.74894 0. zoo: > dnorm(z.67321 X.53300 2009-03-01 0.33307 0.44400 2009-06-01 0.20926 0.55066 2009-04-01 0.52208 2009-05-01 0.83831 0.2 X.60159 0.

38916 xts: > dnorm(x.35052 2009-02-01 0.35052 2009-02-01 0. Finance Online – Please do not distribute . Functions and Methods from stats R 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0. mean = 0.35604 0.35052 0.38916 timeSeries: > dnorm(s.38916 Can I use filter like in R’s stats package? The function filter() applies linear filtering to a univariate time series or to each series separately of a multivariate time series.39232 2009-04-01 0.32080 2009-06-01 0.35604 2009-03-01 0.1 2009-01-01 0.39232 2009-04-01 0. sd = 1) X.32080 0.35604 2009-03-01 0.154 3.2.39232 0.1 2009-01-01 0. sd = 1) GMT S.36096 2009-05-01 0. zoo: > print(try(filter(z))) © 2009. mean = 0.32080 2009-06-01 0.36096 0.36096 2009-05-01 0.

47702 0.\n 'length' being evaluated was:\n attr(. rep(1.1072 2009-04-01 1. lower-hinge.66028 0.3304 2009-06-01 NA Can I use fivenum like in R’s stats package? The function fivenum returns Tukey’s five number summary (minimum.22283 © 2009. maximum) for the input data. median."class") the part of the args list of (filter)\n" [1] "try-error" timeSeries: > filter(s. Using R Functions 155 [1] "Error in filter(z) : element 1 is empty."class") the part of the args list of (filter)\n" [1] "try-error" xts: > print(try(filter(x))) [1] "Error in filter(x) : element 1 is empty.50875 0.\n 'length' being evaluated was:\n attr(. zoo: > fivenum(z) 2009-01-01 2009-02-01 2009-05-01 2009-06-01 0.1 2009-01-01 NA 2009-02-01 1. upper-hinge. Finance Online – Please do not distribute .1687 2009-03-01 1.2905 2009-05-01 1.3.3)) GMT S.

6667 0.0000 3.66028 2009-06-01 0.3333 1.6667 1.6667 1.3333 1.6667 1.6667 © 2009.50875 0.22283 0.18291 0. If plot=TRUE.3333 1.22283 timeSeries: > fivenum(s) [1] 0.156 3.66028 Can I use hist like in R’s stats package? The generic function hist() computes a histogram of the given data values.47702 2009-05-01 0. Functions and Methods from stats R xts: > fivenum(x) X. Finance Online – Please do not distribute .6667 1.2. the resulting object of class "histogram" is plotted by plot.6667 timeSeries: > hist(s)$density [1] 1.6667 xts: > hist(x)$density [1] 1.6667 0.0000 3.6667 1.0000 3.1 2009-01-01 0.6667 1. before it is returned. zoo: > hist(z)$density [1] 1.6667 0.50875 2009-02-01 0.histogram.46216 0.

49813 © 2009.96524 0.22283 0.18291 0.1 Z.44730 0.86422 2009-05-01 0.96524 0.2[1] S.49813 2009-05-01 0.39901 2009-04-01 0.61359 2009-06-01 0.2 Z.61359 2009-06-01 0.86422 2009-05-01 0.18291 0.96524 0.20833 xts: > lag(X) 2009-01-01 X.18291 0.50875 0.59316 0.49813 timeSeries: > lag(S) GMT 2009-01-01 S.93106 2009-03-01 0.3[1] NA NA NA 2009-02-01 0. Using R Functions 157 Can I use lag like in R’s stats package? The function lag() computes a lagged version of a time series.39901 2009-02-01 0.3 NA NA NA 2009-02-01 0.2 X.23193 0.61359 2009-04-01 0.86422 2009-03-01 0.39901 2009-04-01 0.59127 0.44730 0.59316 0.57402 0. shifting the time base back by a given number of observations.1[1] S. zoo: > lag(Z) Z.59316 0. Finance Online – Please do not distribute .47702 0.59127 0.1 X.66028 0.50875 0.59127 0.47702 0.3 2009-01-01 0.66028 0.44730 0.57402 0.66028 0.93106 2009-03-01 0.57365 0.47702 0.57365 0.57365 0.3.

110 > resid(fit) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0.203938 -0.2 + X.110 > resid(fit) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0.158 3.351 -0.2 + Z. It can be used to carry out regression.1 ~ X. single stratum analysis of variance and analysis of covariance (although aov may provide a more convenient interface for these).lm( X.2 Z.2 + Z.045015 -0.135337 0.135337 0.3 0.3.098638 0. zoo: > fit <.045015 -0.2 + X.232361 -0.3.lm( Z. data = Z) > fit Call: lm(formula = Z.3.098638 0.232361 -0.2.110136 xts: > fit <.2 X.203938 -0.1 ~ Z.1 ~ Z. data = X) Coefficients: (Intercept) X.274 0. Functions and Methods from stats R Can I use lm like in R’s stats package? The function lm() is used to fit linear models. data = Z) Coefficients: (Intercept) Z.1 ~ X. data = X) > fit Call: lm(formula = X.274 0. Finance Online – Please do not distribute .110136 timeSeries: © 2009.3 0.3.351 -0.

26854 © 2009.53355 0.203938 -0.3 0.098638 0.40950 0.40950 0.135337 0.43720 0.110136 Can I use lowess like in R’s stats package? The function lowess() performs the computations for the LOWESS smoother which uses locally-weighted polynomial regression.2 + S.3.351 -0.045015 -0.lm( S.37472 0.41244 0.41244 0.1 ~ S. data = S) Coefficients: (Intercept) S.2 + S. data = S) > fit Call: lm(formula = S.37472 0.26854 xts: > lowess(x) $x [1] 1 2 3 4 5 6 $y [1] 0.110 > resid(fit) 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0. Using R Functions 159 > fit <.43720 0. Finance Online – Please do not distribute .274 0.3.1 ~ S.3.232361 -0.2 S. zoo: > lowess(z) $x [1] 1 2 3 4 5 6 $y [1] 0.53355 0.

Functions and Methods from stats R timeSeries: > lowess(s) $x [1] 1 2 3 4 5 6 $y [1] 0. i.e.37472 0.43720 0.53355 0.. © 2009.19599 Can I use median like in R’s stats package? The function median() computes the sample median.41244 0. Finance Online – Please do not distribute .26854 Can I use mad like in R’s stats package? The function mad() computes the median absolute deviation.160 3. zoo: > mad(z) [1] 0.19599 xts: > mad(x) [1] 0. and (by default) adjust by a factor for asymptotically normal consistency.2. the (lo/hi-) median of the absolute deviations from the median.19599 timeSeries: > mad(s) [1] 0.40950 0.

zoo: > print(qqnorm(z)) $x [1] 0.64335 0.66028 0. Finance Online – Please do not distribute .3.44730 0.18291 0.50875 0.31510 xts: > median(x) [1] 0.20189 1.28155 -0.64335 $y 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0.22283 © 2009.31510 Can I use qqnorm like in R’s stats package? The function qqnorm() is a generic function the default method of which produces a normal QQ plot of the values in y.47702 0. qqline() adds a line to a normal quantile-quantile plot which passes through the first and third quartiles.31510 timeSeries: > median(s) [1] 0.20189 -1. Using R Functions 161 zoo: > median(x) [1] 0.28155 -0.

3RS3R. Finance Online – Please do not distribute .28155 -0.20189 -1.20189 -1.44730 2009-05-01 0.66028 2009-06-01 0.28155 -0. The function implements Tukey’s smoothers.22283 Can I use smooth like in R’s stats package? The functions smooth() performs running median smoothing.2.47702 2009-03-01 0.18291 2009-04-01 0. etc.1 2009-01-01 0.20189 1.50875 2009-02-01 0. 3RSS. Functions and Methods from stats R xts: > print(qqnorm(x)) $x [1] 0.50875 2009-02-01 0.22283 timeSeries: > print(qqnorm(s)) $x [1] 0.64335 0. zoo: > smooth(z) © 2009.1 2009-01-01 0.64335 $y X.47702 2009-03-01 0.44730 2009-05-01 0.28155 -0.66028 2009-06-01 0.162 3.20189 1.64335 $y GMT S.28155 -0. 3R.64335 0.18291 2009-04-01 0.

44730 0.3. Using R Functions 163 3RS3R Tukey smoother resulting from smooth(x = z) used 1 iterations [1] 0. Finance Online – Please do not distribute .50875 0.50875 0.44730 0.ts(x)) : missing values in object\n" attr(.47702 0.50875 0.44730 0.44730 timeSeries: > smooth(s) 3RS3R Tukey smoother resulting from smooth(x = s) used 1 iterations [1] 0.fail.44730 Can I use spectrum like in R’s stats package? The function spectrum() estimates the spectral density of a time series.44730 0. zoo: > print(try(spectrum(z)[1:2])) [1] "Error in na.44730 0.44730 0.44730 0.47702 0.44730 0.47702 0.default(as.44730 0.44730 xts: > smooth(x) 3RS3R Tukey smoother resulting from smooth(x = x) used 1 iterations [1] 0."class") [1] "try-error" xts: > print(spectrum(x)[1:2]) © 2009.

00134900 0.00036691 © 2009.164 3.0161880 0.33333 0.2.0044029 timeSeries: > print(spectrum(s)[1:2]) $freq [1] 2 4 6 $spec [1] 0. Functions and Methods from stats R $freq [1] 0.00604908 0.16667 0.0725889 0. Finance Online – Please do not distribute .50000 $spec [1] 0.

66028 0.59127 0.57365 0. Common Data: We test them on multivariate > set.96524 0.50875 0.18291 0.93106 2009-02-01 0.2 Z.matrix(runif(18). "-01". Using R Functions 165 3.20833 xts: © 2009.23193 0.86422 2009-04-01 0.61359 2009-05-01 0. ncol = 3) > charvec <. 1:3.57402 0.47702 0.22283 0.3.59316 0.zoo(data. as. 1:6.1 Z.39901 2009-03-01 0.44730 0.Date(charvec)) > colnames(Z) = paste("Z".") > Z Z.3 2009-01-01 0.49813 2009-06-01 0.seed(1953) > data <.paste("2009-0".3 Functions and Methods from utils R Required R package(s): > library(zoo) > library(xts) > library(timeSeries) In the following we test selected functions which are relevant for use in financial time series analysis. Finance Online – Please do not distribute . sep = ". sep = "") > charvec [1] "2009-01-01" "2009-02-01" "2009-03-01" "2009-04-01" "2009-05-01" [6] "2009-06-01" zoo: > Z <.

20833 and/or univariate time series objects. charvec) > colnames(S) = paste("S".96524 0.61359 2009-05-01 0.1 X.23193 0.Date(charvec)) > colnames(X) = paste("X". 1:3.57365 0. sep = ".2 X.20833 timeSeries: > S <.50875 0.49813 2009-06-01 0.57402 0.57402 0.1 S. 1] > x © 2009.86422 2009-04-01 0.57365 0. zoo: > z <.X[.18291 0.18291 0.39901 2009-03-01 0.59316 0. 1] > z 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 0.93106 2009-02-01 0.47702 0.39901 2009-03-01 0.50875 0. Functions and Methods from utils R > X <.22283 0.44730 0.23193 0.22283 xts: > x <.") > X X.59127 0.66028 0.xts(data.61359 2009-05-01 0.166 3. Finance Online – Please do not distribute .3 2009-01-01 0.86422 2009-04-01 0.timeSeries(data.3 2009-01-01 0.47702 0. as.96524 0.") > S GMT S.Z[.66028 0.44730 0.3. 1:3.93106 2009-02-01 0.50875 0.22283 0.59316 0.44730 0.47702 0. sep = ".66028 0.59127 0.18291 0.2 S.49813 2009-06-01 0.

1 Z.S[.44730 2009-05-01 0.1 2009-01-01 0.50875 0.18291 2009-04-01 0.50875 2009-02-01 0.3 2009-01-01 0. ts: «head.50875 2009-02-01 0. 3) Z.57402 0.2 Z.1 2009-01-01 0.22283 timeSeries: > s <. zoo: > head(Z. 1] > s GMT S.47702 2009-03-01 0.44730 2009-05-01 0.3.ts» ts <. Using R Functions 167 X.93106 © 2009.47702 2009-03-01 0. the attributes are lost.18291 2009-04-01 0. matrix. Finance Online – Please do not distribute .66028 2009-06-01 0. table.66028 2009-06-01 0.22283 Can I use head like in R’s utils package? The function head() returns the first or last parts of a vector. data frame or function.rnorm(ts(rnorm(12))) ts head(ts) For a regular time series of class ts.

47702 0.1 X.18291 0.39901 2009-03-01 0.2 X.86422 > head(s.1 2009-01-01 0. 3) GMT S.57402 0.2 S.39901 2009-03-01 0.86422 > head(x.18291 timeSeries: > head(S.1 2009-01-01 0.50875 2009-02-01 0.57365 0.59127 0.47702 2009-03-01 0. 3) X.57402 0.50875 0.59127 0.18291 0.47702 0.57365 0.18291 0.50875 0. 3) 2009-01-01 2009-02-01 2009-03-01 0. 3) X. Finance Online – Please do not distribute .86422 > head(z.3. Functions and Methods from utils R 2009-02-01 0.18291 xts: > head(X.18291 © 2009. 3) GMT S.3 2009-01-01 0.93106 2009-02-01 0.3 2009-01-01 0.57365 0.47702 2009-03-01 0.47702 0.1 S.93106 2009-02-01 0.47702 0.59127 0.50875 0.39901 2009-03-01 0.50875 2009-02-01 0.168 3.

Chapter 4 Performance Measurements 169 .

one has to transform the character time stamps in an appropriate index.. gcFirst = TRUE. Performance of Time Series Creation 4.170 4.format(seq(from = as. Common Data: > charvec <.time(expr.00 0.parent(substitute(function(. class = "proc_time") } How long does it take to create a one hundred year 5-column series with daily records from character time stamps? If we read time series information from an ASCII. median). to = as.25 © 2009.Date("1901-01-01").1 Performance of Time Series Creation Required R package(s): > library(zoo) > library(xts) > library(timeSeries) We define the following function to measure the performance of the different functions with > systemTime <. xts or timeSeries object. by = "day")) > data <. eval. charvec)) user 0.sapply(integer(n). To convert this to a zoo.1.25 system elapsed 0. or extract it from a downloaded html file from the Web. n = 20) { time <.Date("1999-12-31"). or import it from a xls or csv file then we have the time usually as character strings. gcFirst = gcFirst)))) structure(apply(time. Finance Online – Please do not distribute .. Here we consider abot 35’000 daily records which represent about 100 year of daily data.function(expr. 1.) system. ncol = 5) zoo: > systemTime(zoo(data.matrix(rnorm(length(charvec)*5).

230 system elapsed 0.03 system elapsed 0.format(seq(from = as. charvec))) [1] "Error in xts(data.Date("1999-12-31"). to = as.as.00 0. charvec)) user 0. charvec) : \n order. © 2009. index)) user 0.Date("1901-01-01").000 0. Finance Online – Please do not distribute ."class") [1] "try-error" timeSeries: > systemTime(timeSeries(data.03 This is the generic case to create daily recorded time series with zoo.Date(charvec) > class(index) [1] "Date" zoo: > systemTime(zoo(data. by = "day")) > data <. ncol = 5) > index <.matrix(rnorm(length(charvec)*5).by requires an appropriate time- based object\n" attr(.4. Performance Measurements 171 xts: > print(try(xts(data.235 How long does it take to create a one hundred year 5-column series with daily records from Date time stamps? Common Data: > charvec <.

Performance of Time Series Creation xts: > systemTime(xts(data.matrix(rnorm(length(charvec)*5).00 0. by = "day")) > data <. Finance Online – Please do not distribute .03 © 2009.00 0.03 system elapsed 0. ncol = 5) > index <.format(seq(from = as. timeSeries: > systemTime(timeSeries(data.as. index)) user system elapsed 0. the following test is the proper benchmark.00 0.172 4. index)) user 0. tz = "GMT") > class(index) [1] "POSIXt" "POSIXct" zoo: > systemTime(zoo(data.Date("1901-01-01").POSIXct(charvec.03 system elapsed 0.Date("1999-12-31").03 Since we are always working with timeDate object. index)) user 0.1.42 0.43 This is the generic case to create daily recorded time series with xts. How long does it take to create a one hundred year 5-column series with daily records from GMT POSIXct time stamps? Common Data: > charvec <. to = as.

020 system elapsed 0. to = as.Date("1901-01-01"). index)) user 0. Finance Online – Please do not distribute . index)) user system elapsed 0 0 0 timeSeries: > systemTime(timeSeries(data.POSIXct(charvec.03 system elapsed 0. Performance Measurements 173 xts: > systemTime(xts(data.format(seq(from = as.03 xts: > systemTime(xts(data. tz = "CET") > class(index) [1] "POSIXt" "POSIXct" zoo: > systemTime(zoo(data. by = "day")) > data <.025 How long does it take to create a one hundred year 5-column series with daily records from non-GMT POSIXct time stamps? Common Data: > charvec <. index)) user 0.4.matrix(rnorm(length(charvec)*5).as.Date("1999-12-31"). index)) user 0 system elapsed 0 0 © 2009.00 0.000 0. ncol = 5) > index <.

00 0.000 0."package") [1] "timeDate" zoo: > systemTime(zoo(data. Performance of Time Series Creation timeSeries: > systemTime(timeSeries(data. by = "day")) > data <.Date("1901-01-01").01 timeSeries: > systemTime(timeSeries(data. index)) user system elapsed 0.025 0.00 0.03 0.030 How long does it take to create a one hundred year 5-column series with daily records from timeDate time stamps? Common Data: > charvec <. index)) © 2009. ncol = 5) > index <.174 4.03 xts: > systemTime(xts(data.Date("1999-12-31"). index)) user system elapsed 0.1. to = as.matrix(rnorm(length(charvec)*5).format(seq(from = as. Finance Online – Please do not distribute . index)) user system elapsed 0.01 0.timeDate(charvec) > class(index) [1] "timeDate" attr(.

Finance Online – Please do not distribute . Performance Measurements user 0 175 system elapsed 0 0 © 2009.4.

median).2. by = "day")) > data <. to = as. class = "proc_time") } How long does it take to subset by integers a one hundred year 5-column series with daily records? Common data: > charvec <.format(seq(from = as.2 Performance of Time Series subsetting Required R package(s): > library(zoo) > library(xts) > library(timeSeries) We define the following function to measure the performance of the different functions with > systemTime <.) system.sample(charvec)[1:length] zoo: > z <.. ]) user 0. Finance Online – Please do not distribute .Date("1999-12-31").time(expr. gcFirst = TRUE.function(expr. Performance of Time Series subsetting 4.matrix(rnorm(length(charvec)*5).Date("1901-01-01").00 0.sapply(integer(n).14 xts: © 2009. eval. n = 20) { time <.176 4.zoo(data.charvec > length <.14 system elapsed 0. 1.Date(charvec)) > systemTime(z[subset.floor(length(charvec)/2) > subset <.. ncol = 5) > index <. as.parent(substitute(function(. gcFirst = gcFirst)))) structure(apply(time.

format(seq(from = as.00 0. ]) user 0.Date("1999-12-31").4.xts(data. ]) user 0.13 system elapsed 0.as.as.Date("1901-01-01"). index) > systemTime(z[subset.Date(charvec) > length <. Performance Measurements 177 > x <.03 xts: > x <.timeSeries(data.00 0.13 How long does it take to subset by Date objects a one hundred year 5-column series with daily records? Common data: > charvec <.03 system elapsed 0. charvec) > systemTime(s[subset. Finance Online – Please do not distribute . as.xts(data.matrix(rnorm(length(charvec)*5). to = as.floor(length(charvec)/2) > subset <.Date(sample(charvec)[1:length]) zoo: > z <.Date(charvec)) This cannot be done in a reasonable amount of time! timeSeries: > s <.zoo(data. ncol = 5) > index <. by = "day")) > data <. index) This cannot be done in a reasonable amount of time! © 2009.

to = as. Performance of Time Series subsetting timeSeries: > s <. index) > systemTime(z[subset. tz = "GMT") > length <. ]) user system elapsed 0.03 xts: > x <.format(seq(from = as.02 0.00 0. ]) © 2009. ]) user system elapsed 0.03 0. Finance Online – Please do not distribute . index) This cannot be done in a reasonable amount of time! timeSeries: > s <.floor(length(charvec)/2) > subset <. ncol = 5) > index <. index) > systemTime(s[subset.as. index) > systemTime(s[subset.timeSeries(data.xts(data.00 0.Date("1999-12-31"). tz = "GMT") zoo: > z <. by = "day")) > data <.matrix(rnorm(length(charvec)*5).POSIXct(sample(charvec)[1:length].zoo(data.178 4.Date("1901-01-01").as.2.POSIXct(charvec.timeSeries(data.02 How long does it take to subset by Date objects a one hundred year 5-column series with GMT POSIXct records? Common data: > charvec <.

by = "day")) > data <.floor(length(charvec)/2) > subset <.02 How long does it take to subset by Date objects a one hundred year 5-column series with non-GMT POSIXct records? Common data: > charvec <. index) Here is a problem ? © 2009.01 0. Finance Online – Please do not distribute .as.02 0.01 xts: > x <.00 0.POSIXct(charvec. tz = "CET") > length <. ]) user system elapsed 0. tz = "GMT") zoo: > z <.as.matrix(rnorm(length(charvec)*5).xts(data.00 0.format(seq(from = as. index) This cannot be done in a reasonable amount of time! timeSeries: > s <. Performance Measurements user 179 system elapsed 0. ncol = 5) > index <.zoo(data.POSIXct(sample(charvec)[1:length].Date("1999-12-31"). index) > systemTime(z[subset.4.Date("1901-01-01").timeSeries(data. to = as.

00 0. index) > systemTime(z[subset. Finance Online – Please do not distribute . index) > systemTime(s[subset. xts: This cannot be done in a reasonable amount of time! timeSeries: > s <.zoo(data.Date("1901-01-01").00 0.2. by = "day")) > data <.format(seq(from = as.timeDate(sample(charvec)[1:length]) zoo: > z <. to = as.180 4.timeDate(charvec) > length <.matrix(rnorm(length(charvec)*5).03 > try(detach("package:fCalendar")) Note that zoo attaches old version timeDate() from package fCalendar.Date("1999-12-31").03 0. ncol = 5) > index <. ]) user system elapsed 0. ]) user 0.timeSeries(data. Performance of Time Series subsetting How long does it take to subset by Date objects a one hundred year 5-column series with GMT timeDate records? Common data: > charvec <.floor(length(charvec)/2) > subset <.02 system elapsed 0.01 How long does it take to subset by Date objects a one hundred year 5-column series with non-GMT timeDate records? © 2009.

floor(length(charvec)/2) > subset <.00 0. ]) > NA [1] NA timeSeries: > s <. FinCenter = " Zurich") zoo: > z <. ]) user 0.zoo(data. index) > #systemTime(x[subset.matrix(rnorm(length(charvec)*5).timeSeries(data.4. FinCenter = "Zurich") > length <. ]) user system elapsed 0.Date("1901-01-01"). to = as.xts(data.03 0. index) > systemTime(z[subset.01 © 2009.timeDate(charvec. zone = "Zurich".03 xts: > x <. zone = "Zurich". Finance Online – Please do not distribute .Date("1999-12-31").timeDate(sample(charvec)[1:length]. ncol = 5) > index <.00 0. by = "day")) > data <. index) > systemTime(s[subset.format(seq(from = as. Performance Measurements 181 Common data: > charvec <.02 system elapsed 0.

.

1).4. It is particularly aimed at irregular time series of numeric vectors/matrices and factors.5-8 Date: 2009-07-22 Title: Z's ordered observations Author: Achim Zeileis.org> Description: An S3 class with methods for totally ordered indexed observations. stats 183 .Zeileis@R-project. zoo's key design goals are independence of a particular index/date/time class and consistency with ts and base R by providing methods to extend standard generics.Appendix A Packages Required for this Ebook Required R package(s): > library(fBasics) Contributed R Package: zoo The description file of the zoo package used in this version of the ebook. Gabor Grothendieck Maintainer: Achim Zeileis <Achim. Depends: R (>= 2. > listDescription(zoo) zoo Description: Package: zoo Version: 1.

jryan Repository: CRAN Date/Publication: 2009-07-22 04:55:21 Built: R 2. DAAG. timeDate.tis LazyLoad: yes Maintainer: Jeffrey A. zeileis Repository: CRAN Date/Publication: 2009-07-22 19:20:13 Built: R 2.r-project. its. Ryan. > listDescription(xts) xts Description: Package: xts Type: Package Title: Extensible Time Series Version: 0.9. fame. lattice.tseries.1. fts. .fts.org/projects/zoo/ Packaged: 2009-07-22 17:21:04 UTC. License: GPL-3 URL: http://r-forge. xts Imports: stats. windows Contributed R Package: xts The description file of the xts package used in this version of the ebook. strucchange. windows © 2009.6-7 Date: 2009-07-21 Author: Jeffrey A.timeDate. while simplifying cross-class interoperability.a.org/projects/xts/ Packaged: 2009-07-21 20:27:23 UTC. chron. utils. Ryan <jeff. Finance Online – Please do not distribute . fCalendar. maximizing native format information preservation and allowing for user level customization and extension.9. Ulrich Depends: zoo Suggests: timeSeries.chron. 2009-08-23 16:38:13 UTC. Josh M. graphics. fSeries.1.its. i386-pc-mingw32.ryan@gmail.184 Suggests: coda. tseries. grDevices. 2009-08-23 16:39:28 UTC. timeSeries.com> Description: Provide for uniform handling of R's different time-based data classes by extending zoo.R-project. lattice LazyLoad: yes License: GPL-2 URL: http://R-Forge.

AS WELL AS DEFAULTS FOR ARGUMENTS AND RETURN VALUES. > listDescription(timeSeries) timeSeries Description: Package: timeSeries Version: 2100.rmetrics.9. and others Depends: R (>= 2.org> Description: Environment for teaching "Financial Engineering and NOTE: SEVERAL PARTS ARE STILL PRELIMINARY AND MAY BE Computational Finance" CHANGED IN THE FUTURE. Joe W. methods Suggests: RUnit Maintainer: Rmetrics Core Team <Rmetrics-core@r-project. windows Rmetrics Package: timeSeries The description file of the timeSeries package used in this version of the ebook.Chronological and Calendarical Objects Author: Diethelm Wuertz and Yohan Chalabi with contributions from Martin Maechler. 2009-08-21 13:24:27 UTC. > listDescription(timeDate) timeDate Description: Package: timeDate Version: 2100.6.84 Revision: 4093 Date: 2009-04-19 © 2009. THIS TYPICALLY INCLUDES FUNCTION AND ARGUMENT NAMES. Byers.86 Revision: 4054 Date: 2009-04-15 Title: Rmetrics . LazyLoad: yes LazyData: yes License: GPL (>= 2) URL: http://www. utils. Finance Online – Please do not distribute .org Built: R 2. graphics. Packages Required for this Ebook 185 Rmetrics Package: timeDate The description file of the timeDate package used in this version of the ebook. stats.A. .1.0).

org Built: R 2. stats. 2009-08-21 13:29:41 UTC. methods.1. AS WELL AS DEFAULTS FOR ARGUMENTS AND RETURN VALUES. graphics.86) Computational Finance" CHANGED IN THE FUTURE.9.Financial Time Series Objects Author: Diethelm Wuertz and Yohan Chalabi Depends: R (>= 2. Finance Online – Please do not distribute . windows © 2009. . RUnit Maintainer: Rmetrics Core Team <Rmetrics-core@r-project. THIS TYPICALLY INCLUDES FUNCTION AND ARGUMENT NAMES.rmetrics. grDevices.0).6. LazyLoad: yes LazyData: yes License: GPL (>= 2) URL: http://www.org> Description: Environment for teaching "Financial Engineering and NOTE: SEVERAL PARTS ARE STILL PRELIMINARY AND MAY BE utils. Suggests: robustbase. timeDate (>= 2100.186 Title: Rmetrics .

yearmon" [4] "as.tis" "panel.yearmon.yearqtr" "as.its" [37] "panel.default" [7] "as.approx" "na.arrows. > listFunctions(zoo) [1] "as.zoo" "as.zooreg.yearqtr.trim.default" [28] "na.default" "as.ts" "panel.zoo" "coredata" [13] "coredata.regular" "is.zoo" "na.default" "na.default" "cbind.yearqtr" [16] "frequency<-" "index" "index<-" [19] "index2char" "is.default" "panel.zoo" [22] "make.default" "as.numeric" "as.list" "MATCH" "MATCH.default" "na.zooreg" [10] "as.Date.arrows.arrows.locf.Appendix B Function Listings Required R package(s): > library(zoo) > library(xts) > library(timeSeries) > library(fBasics) Contributed R Package: zoo The functions available from the zoo package used in this version of the ebook.default" "coredata<-" "format.default" [25] "merge.yearqtr" "as.par.trim" "na.default" [34] "ORDER" "ORDER.spline" [31] "na.locf" "na.zoo" 187 .approx.yearqtr.spline.zoo.arrows.

xts" [43] "isOrdered" "lag.188 [40] "panel.index<-" ".default" "rollmean" [73] "rollmean.max" "period.custom" "panel.xts" "apply.tis" "panel.plot.indexmon" [10] ".ts" "panel.xts" [25] "c.polygon.default" [46] "panel.zoo" [67] "rbind.prod" [61] "period.zoo" "rollapply" [70] "rollmax" "rollmax.xts" [31] "dimnames.default" "rollmedian" "rollmedian.points.xts" [16] ".indexwday" ".sum" "periodicity" "plot.xts" [49] "ndays" "nhours" "nminutes" [52] "nmonths" "nquarters" "nseconds" [55] "nweeks" "nyears" "period.indexisdst" [7] ".default" [76] "Sys.points.ts" "panel.text.polygon.lines.plot.tis" [64] "panel.timeBased" "is.text.xts" "merge.monthly" [19] "apply.tis" [52] "panel.points.xts" "CLASS" [28] "CLASS<-" "convertIndex" "diff.indexyear" ".lines.its" [55] "panel.xts" "axTicksByTime" "as.tis" "panel.its" "panel.index" ".indexhour" ".indexDate" [4] ".tis" "panel.fts.its" "panel.yearmon" "Sys.indexmin" ".xts" "last" [46] "lastof" "lines.segments.lines.zoo" "panel.rect.ts" [61] "panel.indexmday" ".text.segments.rect.daily" "apply.polygon.its" "panel.zoo" "panel.zoo" "yearmon" [82] "yearqtr" "zoo" "zooreg" Contributed R Package: xts The functions available from the xts package used in this version of the ebook.zoo" "read.xts" © 2009.xts" "endpoints" [34] "first" "firstof" "indexClass" [37] "indexClass<-" "indexFormat" "indexFormat<-" [40] "indexTZ" "is.segments.apply" [58] "period.rect.zoo" "panel.lines.xts" "cbind.ts" [49] "panel.its" "panel.ts" [43] "panel.text.indexday" ".indexsec" ".indexweek" [13] ".subset.tis" "panel.xts" "dimnames<-.its" "panel.ts" "panel.quarterly" "apply.zoo" [58] "panel.points.indexyday" ". Finance Online – Please do not distribute .rect.yearqtr" "time<-" [79] "write.weekly" "apply.polygon.zoo" "panel. > listFunctions(xts) [1] ".yearly" [22] "as.zoo" "xtfrm.min" "period.segments.zoo" "plot.

nday" ".monthly" "to.daily" "to.minutes10" "to.periodicallyRolling" [21] ".of.xts" "xcoredata" [88] "xcoredata<-" "xts" "xtsAttributes" [91] "xtsAttributes<-" "xtsible" Rmetrics Package: timeDate The functions available from the timeDate package used in this version of the ebook.nth.last.julian" ".day.reclass" "use. Finance Online – Please do not distribute .fjulian" [7] ".minutes15" [76] "to.JULIAN" [13] ".subsetByPython" [27] ".sjulian" [25] ".yearly" "try.day.weekly" "to.on.by2seconds" ".genDaylightSavingTime" ".subsetBySpan" ".hourly" [73] "to. > listFunctions(timeDate) [1] ".period" "to.before" ".endpoints" ".on.or.whichFormat" "Abidjan" [31] "abline" [33] "Adak" "Accra" "Addis_Ababa" [35] "Adelaide" "Aden" [37] "Advent1st" "Advent2nd" [39] "Advent3rd" "Advent4th" [41] "Algiers" "align" [43] "AllSaints" "AllSouls" [45] "Almaty" "Amman" [47] "Amsterdam" "Anadyr" [49] "Anchorage" "Andorra" [51] "Anguilla" "Annunciation" © 2009.xts" "reclass" "Reclass" [67] "split.subsetCode" [29] ".sdate" [23] ".easter" ".week" [3] ".of.isPOSIX" [11] ".of.sleap.minutes30" "to.month.sday.xts" "timeBased" "timeBasedRange" [70] "timeBasedSeq" "to.or. Function Listings 189 [64] "rbind.year" ".midnightStandard" [15] ".after" [19] ".B.minutes" "to.minutes5" [79] "to.easterSunday" [5] ".xts" [85] "use.formatFinCenter" ".week" ".formatFinCenterNum" [9] ".minutes3" "to.monthlyRolling" [17] ".periods" ".year" ".of.nday" ".quarterly" [82] "to.

timeDate" "Azores" [73] "Baghdad" "Bahia" [75] "Bahrain" "Baku" [77] "Bamako" "Bangkok" [79] "Bangui" "Banjul" [81] "Barbados" "Beirut" [83] "Belem" "Belgrade" [85] "Belize" "Berlin" [87] "Bermuda" "BirthOfVirginMary" [89] "Bishkek" "Bissau" [91] "Blanc-Sablon" "Blantyre" [93] "blockEnd" [95] "Boa_Vista" "blockStart" [97] "Boise" "BoxingDay" [99] "Bratislava" [101] "Brisbane" "Brazzaville" "Broken_Hill" [103] "Brunei" "Brussels" [105] "Bucharest" [107] "Buenos_Aires" "Budapest" [109] "Bujumbura" "CACanadaDay" "Bogota" "BuenosAires" [111] "CACivicProvincialHoliday" "CAFamilyDay" [113] "Cairo" [115] "Calcutta" [117] "Campo_Grande" "CALabourDay" "Cambridge_Bay" [119] "Cancun" "Canary" "Cape_Verde" [121] "Caracas" "CaRemembranceDay" [123] "Casablanca" "Casey" [125] "Catamarca" "CAThanksgivingDay" [127] "CAVictoriaDay" "Cayenne" [129] "Cayman" "CelebrationOfHolyCross" [131] "Center" "Ceuta" [133] "Chagos" "CHAscension" [135] "Chatham" "CHBerchtoldsDay" [137] "CHConfederationDay" "Chicago" [139] "Chihuahua" "Chisinau" [141] "CHKnabenschiessen" "Choibalsan" [143] "Chongqing" "Christmas" © 2009.190 [53] "Antananarivo" "Antigua" [55] "Apia" "Aqtau" [57] "Aqtobe" "Araguaina" [59] "Aruba" "as. Finance Online – Please do not distribute .timeDate" [61] "Ascension" "Ashgabat" [63] "AshWednesday" "Asmara" [65] "AssumptionOfMary" "Asuncion" [67] "Athens" "Atikokan" [69] "atoms" "Auckland" [71] "axis.

Function Listings [145] "ChristmasDay" 191 "ChristmasEve" [147] "ChristTheKing" "CHSechselaeuten" [149] "Cocos" "Colombo" [151] "Comoro" "Conakry" [153] "Copenhagen" [155] "CorpusChristi" "Cordoba" "Costa_Rica" [157] "Cuiaba" "Curacao" [159] "Currie" "Dakar" [161] "Damascus" [163] "Dar_es_Salaam" "Danmarkshavn" [165] "Davis" [167] "Dawson_Creek" "Dawson" [169] "dayOfYear" "DEAscension" "Darwin" "dayOfWeek" [171] "DEChristmasEve" "DECorpusChristi" [173] "DEGermanUnity" "DENewYearsEve" [175] "Denver" "Detroit" [177] "Dhaka" "difftimeDate" [179] "Dili" "Djibouti" [181] "Dominica" "Douala" [183] "Dubai" "Dublin" [185] "DumontDUrville" "Dushanbe" [187] "Easter" "EasterMonday" [189] "Eastern" "EasterSunday" [191] "Edmonton" "Efate" "El_Aaiun" [193] "Eirunepe" [195] "El_Salvador" [197] "Epiphany" "Enderbury" "Eucla" [199] "Fakaofo" "Faroe" [201] "Fiji" "finCenter" [203] "finCenter<-" "Fortaleza" [205] "FRAllSaints" "Frankfurt" [207] "FRArmisticeDay" "FRAscension" [209] "FRAssumptionVirginMary" "FRBastilleDay" [211] "Freetown" "FRFetDeLaVictoire1945" [213] "Funafuti" "Gaborone" [215] "Galapagos" "Gambier" [217] "Gaza" "GBBankHoliday" [219] "GBMayDay" "GBMilleniumDay" [221] "GBSummerBankHoliday" [223] "Gibraltar" "getRmetricsOptions" "Glace_Bay" [225] "Godthab" [227] "Goose_Bay" "GoodFriday" "Grand_Turk" [229] "Grenada" "Guadalcanal" [231] "Guadeloupe" "Guam" [233] "Guatemala" "Guayaquil" [235] "Guernsey" "Guyana" © 2009.B. Finance Online – Please do not distribute .

Finance Online – Please do not distribute .192 [237] "Halifax" "Harare" [239] "Harbin" "Havana" [241] "Helsinki" "Hermosillo" [243] "Hobart" "holiday" [245] "holidayNERC" "holidayNYSE" [247] "holidayTSX" [249] "Hong_Kong" "holidayZURICH" [251] "Honolulu" "Hovd" "HongKong" [253] "Indianapolis" "Inuvik" [255] "Iqaluit" "Irkutsk" [257] "isBizday" [259] "Isle_of_Man" "isHoliday" [261] "isWeekday" "isWeekend" "Istanbul" [263] "ITAllSaints" "ITAssumptionOfVirginMary" [265] "ITEpiphany" "ITImmaculateConception" [267] "ITLiberationDay" "ITStAmrose" [269] "Jakarta" "Jamaica" [271] "Jayapura" "Jersey" [273] "Jerusalem" "Johannesburg" [275] "Johnston" "JPAutumnalEquinox" [277] "JPBankHolidayDec31" "JPBankHolidayJan2" [279] "JPBankHolidayJan3" "JPBunkaNoHi" [281] "JPChildrensDay" "JPComingOfAgeDay" [283] "JPConstitutionDay" "JPEmperorsBirthday" [285] "JPGantan" "JPGreeneryDay" [287] "JPHealthandSportsDay" "JPKeirouNOhi" [289] "JPKenkokuKinenNoHi" "JPKenpouKinenBi" [291] "JPKinrouKanshaNoHi" "JPKodomoNoHi" [293] "JPKokuminNoKyujitu" "JPMarineDay" [295] "JPMidoriNoHi" "JPNatFoundationDay" [297] "JPNationalCultureDay" "JPNationHoliday" [299] "JPNewYearsDay" "JPRespectForTheAgedDay" [301] "JPSeijinNoHi" "JPShuubunNoHi" [303] "JPTaiikuNoHi" "JPTennouTanjyouBi" [305] "JPThanksgivingDay" "JPUmiNoHi" [307] "JPVernalEquinox" "Jujuy" [309] "julian" "Juneau" [311] "Kabul" "Kaliningrad" [313] "Kamchatka" "Kampala" [315] "Karachi" "Kashgar" [317] "Katmandu" "Kerguelen" [319] "Khartoum" "Kiev" [321] "Kigali" "Kinshasa" [323] "Kiritimati" "Knox" [325] "Kosrae" [327] "Kuala_Lumpur" "Krasnoyarsk" "KualaLumpur" © 2009.

Finance Online – Please do not distribute . Function Listings 193 [329] "Kuching" "kurtosis" [331] "Kuwait" [333] "La_Paz" "Kwajalein" "La_Rioja" [335] "LaborDay" "Lagos" [337] "Libreville" "Lima" [339] "Lindeman" "lines" [341] "Lisbon" "listFinCenter" [343] "listHolidays" "Ljubljana" [345] "Lome" "London" "Lord_Howe" [347] "Longyearbyen" [349] "Los_Angeles" "LosAngeles" [351] "Louisville" "Luanda" [353] "Lubumbashi" "Lusaka" [355] "Luxembourg" "Macau" [357] "Maceio" "Madeira" [359] "Madrid" "Magadan" [361] "Mahe" "Majuro" [363] "Makassar" "Malabo" [365] "Maldives" "Malta" [367] "Managua" "Manaus" [369] "Manila" "Maputo" [371] "Marengo" "Mariehamn" [373] "Marigot" "Marquesas" [375] "Martinique" "Maseru" [377] "MassOfArchangels" "Mauritius" [379] "Mawson" "Mayotte" [381] "Mazatlan" "Mbabane" [383] "McMurdo" "Melbourne" [385] "Mendoza" "Menominee" "Mexico_City" [387] "Merida" [389] "MexicoCity" "midnightStandard" [391] "Midway" "Minsk" [393] "Miquelon" "Mogadishu" [395] "Monaco" "Moncton" [397] "Monrovia" "Monterrey" [399] "Montevideo" "monthlyRolling" [401] "months" "Monticello" [403] "Montreal" "Montserrat" [405] "Moscow" "Muscat" [407] "Nairobi" "Nassau" [409] "Nauru" [411] "New_Salem" "Ndjamena" "New_York" [413] "NewYearsDay" "NewYork" [415] "Niamey" "Nicosia" [417] "Nipigon" "Niue" [419] "Nome" "Norfolk" © 2009.B.

194 [421] "Noronha" "Nouakchott" [423] "Noumea" "Novosibirsk" [425] "Omsk" "Oral" [427] "Oslo" [429] "Pacific" "Ouagadougou" "Pago_Pago" [431] "Palau" "Palmer" [433] "PalmSunday" "Panama" [435] "Pangnirtung" "Paramaribo" [437] "Paris" "Pentecost" [439] "PentecostMonday" "periodicallyRolling" [441] "periods" [443] "Petersburg" "Perth" "Phnom_Penh" [445] "Phoenix" "Pitcairn" [447] "plot" "Podgorica" [449] "points" "Ponape" [451] "Pontianak" [453] "Port_Moresby" "Port-au-Prince" "Port_of_Spain" [455] "Porto-Novo" "Porto_Velho" [457] "Prague" [459] "Puerto_Rico" "PresentationOfLord" [461] "Qatar" "Quinquagesima" "Rainy_River" [463] "Qyzylorda" "Pyongyang" [465] "Rangoon" "Rankin_Inlet" [467] "Rarotonga" "Recife" [469] "Regina" "Resolute" [471] "Reunion" "Reykjavik" "Rio_Branco" [473] "Riga" [475] "Rio_Gallegos" "Riyadh" [477] "RogationSunday" "Rome" [479] "Rothera" "rulesFinCenter" [481] "Saigon" "Saipan" [483] "Sakhalin" "Samara" [485] "Samarkand" [487] "San_Juan" "sample" "San_Marino" [489] "Santiago" [491] "Sao_Paulo" "Santo_Domingo" "Sao_Tome" [493] "Sarajevo" "Scoresbysund" [495] "Seoul" "Septuagesima" [497] "setRmetricsOptions" "Shanghai" [499] "Shiprock" "Simferopol" [501] "Singapore" "skewness" [503] "Skopje" "Sofia" "South_Georgia" [505] "SolemnityOfMary" [507] "South_Pole" [509] "St_Helena" [511] "St_Kitts" "St_Barthelemy" "St_Johns" "St_Lucia" © 2009. Finance Online – Please do not distribute .

B. Function Listings 195 [513] "St_Thomas" "St_Vincent" [515] "Stanley" [517] "strptimeDate" "Stockholm" "Swift_Current" [519] "Sydney" "Syowa" [521] "Sys.timeDate" "Tahiti" [523] "Taipei" "Tallinn" [525] "Tarawa" "Tashkent" [527] "Tbilisi" "Tegucigalpa" "Tell_City" [529] "Tehran" [531] "Thimphu" [533] "Thunder_Bay" "Thule" [535] "timeCalendar" "timeDate" "Tijuana" [537] "timeFirstDayInMonth" "timeFirstDayInQuarter" [539] "timeLastDayInMonth" "timeLastDayInQuarter" [541] "timeLastNdayInMonth" "timeNdayOnOrAfter" [543] "timeNdayOnOrBefore" "timeNthNdayInMonth" [545] "timeSequence" "Tirane" [547] "Tokyo" "Tongatapu" [549] "Toronto" "Tortola" [551] "TransfigurationOfLord" "TrinitySunday" [553] "Tripoli" "Truk" [555] "Tucuman" "Tunis" [557] "Ulaanbaatar" "Urumqi" [559] "USChristmasDay" "USColumbusDay" [561] "USCPulaskisBirthday" "USDecorationMemorialDay" [563] "USElectionDay" "USGoodFriday" [565] "Ushuaia" "USInaugurationDay" [567] "USIndependenceDay" "USLaborDay" [569] "USLincolnsBirthday" "USMemorialDay" [571] "USMLKingsBirthday" "USNewYearsDay" [573] "USPresidentsDay" "USThanksgivingDay" [575] "USVeteransDay" "USWashingtonsBirthday" [577] "Uzhgorod" "Vaduz" [579] "Vancouver" "Vatican" [581] "Vevay" "Vienna" [583] "Vientiane" "Vilnius" [585] "Vincennes" "Vladivostok" [587] "Volgograd" "Vostok" [589] "Wake" "Wallis" [591] "Warsaw" "whichFormat" [593] "Whitehorse" "Winamac" [595] "Windhoek" "Winnipeg" [597] "Yakutat" "Yakutsk" [599] "Yekaterinburg" "Yellowknife" [601] "Yerevan" "Zagreb" [603] "Zaporozhye" "Zurich" © 2009. Finance Online – Please do not distribute .

timeSeries" ".turnpointsPastecs" [45] ".196 Rmetrics Package: timeSeries The functions available from the timeSeries package used in this version of the ebook.applySeries" ".turnpointsPastecs" [61] ".isOHLC" [31] ".list.list" [75] "as.isMonthly.timeDate" ".timeSeries" [7] ".isDaily" [23] ".points.frame" "as.matrix" [77] "as.print.dollar_assign" ".as.turnpointsPastecs" [65] ".tail. > listFunctions(timeSeries) [1] ".isOHLCV" ".signalCounts" ".start.isMonthly" [27] ".plotOHLC" ".isQuarterly.timeSeries" [3] ".data.ts.head.na.timeSeries" ".lowessSmoother" ".timeSeries" ".fapply" ".omit.naOmitMatrix" [41] ".timeSeries" [59] ".frequency.data.timeSeries" ".old2newTimeSeries" [43] ".timeSeries" "as.isDaily.time.as.list.diff.default" ".splineSmoother" [55] ".description" [11] ".timeSeries" [21] ".timeSeries" ".timeSeries" ".turnpointsStats" [67] ".matrix.as.plot.lines.as.plotTimeSeries" [47] ".cut.timeSeries" ".timeSeries" ".aggregate.old2newRda" ".timeSeries" ".extract.timeSeries" [15] ".isMonthly.isDaily.turnpointsPastecs" [17] ".rev.endOfPeriodBenchmarks" ".turnpoints2" ".isQuarterly.timeSeries" ".timeSeries" ".findIndex" [19] ".timeSeries" ".align.isMonthly.timeSeries" ".subset_timeSeries" ".timeSeries" [49] ".endOfPeriodSeries" ".timeSeries" ".timeSeries" "aggregate" [69] "aggregate.turnpointsSeries" ".signalSeries" [53] ".end.ts" © 2009.timeSeries" [39] ".scale.timeSeries" [37] ".timeSeries" [9] ".frequency.timeSeries" "as.isQuarterly.timeSeries" [57] ".timeDate" [35] ".timeSeries" [13] ".timeDate" [25] ".isQuarterly" [33] ".default" ".frame.summary.timeSeries" [51] ". Finance Online – Please do not distribute .timeSeries" "alignDailySeries" [71] "apply" "applySeries" [73] "as.endOfPeriodStats" ".plot.sort.timeDate" [29] ".timeSeries" [63] ".supsmuSmoother" ".window.isDaily.default" ".merge.str.timeSeries" [5] ".

unsorted" "isMonthly" [129] "isMultivariate" "isUnivariate" [131] "lag" "lag.timeSeries" "attach" [81] "colAvgs" "colCummaxs" [83] "colCummins" "colCumprods" [85] "colCumreturns" "colCumsums" [87] "colKurtosis" "colMaxs" [89] "colMeans" "colMins" [91] "colnames" "colnames<-" [93] "colProds" "colQuantiles" [95] "colSds" "colSkewness" [97] "colStats" "colStdevs" [99] "colSums" "colVars" [101] "comment" "comment<-" [103] "countMonthlyRecords" "cumulated" [105] "cut" "cut.signalSeries" "is.timeSeries" [139] "newPositions<-" "ohlcDailyPlot" [141] "orderColnames" "orderStatistics" [143] "outlier" "pcaColnames" [145] "print" "quantile" [147] "rank" "readSeries" [149] "removeNA" "returns" [151] "returnSeries" "rev" [153] "rev.timeSeries" [135] "midquotes" "midquoteSeries" [137] "na.timeSeries" [133] "merge" "merge.timeSeries" "interpNA" [125] "is.timeSeries" "fapply" [119] "getDataPart" "getReturns" [121] "hclustColnames" "head" [123] "head.timeSeries" [127] "is.timeSeries" [107] "description" "diff" [109] "diff.timeSeries" [163] "series" "series<-" [165] "seriesData" "seriesPositions" [167] "setDataPart" "sort" [169] "sort. Function Listings 197 [79] "as.timeSeries" "sortColnames" © 2009.omit" "na.ts. Finance Online – Please do not distribute .omit.timeSeries" "rollDailySeries" [155] "rollMonthlySeries" "rollMonthlyWindows" [157] "rowCumsums" "rownames" [159] "rownames<-" "sampleColnames" [161] "scale" "scale.timeSeries" "drawdowns" [111] "drawdownsStats" "dummyDailySeries" [113] "dummySeries" "durations" [115] "durationSeries" "end" [117] "end.B.

198 [171] "spreads" "spreadSeries" [173] "start" "start. Finance Online – Please do not distribute .timeSeries" © 2009.timeSeries" "time<-" [183] "timeSeries" "window" [185] "window.timeSeries" [175] "statsColnames" "str" [177] "substituteNA" "tail" [179] "tail.timeSeries" "time" [181] "time.

r-project. Gabor Grothendieck http://cran.org/web/packages/timeDate/timeDate. Vignette Achim Zeileis.r-project.pdf xts: Extensible Time Series.r-project. Achim Zeileis.r-project.pdf 199 .r-project.pdf timeDate Reference Manual Diethelm Wuertz.org/web/packages/xts/xts. Vignette Ajay Shah.pdf zoo FAQ. Yohan Chalabi http://cran. Vignette Jeff Ryan. Gabor Grothendieck http://cran. Achim Zeileis http://cran. Josh Ulrich http://cran.org/web/packages/zoo/vignettes/zoo-quickref. Gabor Grothendieck http://cran.org/web/packages/zoo/vignettes/zoo.r-project.org/web/packages/zoo/zoo.org/web/packages/zoo/vignettes/zoo-faq.pdf zoo: An S3 Class and Methods for Indexed Totally Ordered Observations. Vignette Gabor Grothendieck.Bibliography zoo Reference Manual Achim Zeileis.pdf xts Reference Manual Jeff Ryan.pdf zoo Quick Reference.r-project. Josh Ulrich http://cran.org/web/packages/xts/vignettes/xts.

r-project.200 Bibliography timeDate in Rnews Yohan Chalabi.pdf © 2009. Diethelm Wuertz timeSeries Reference Manual Diethelm Wuertz. Yohan Chalabi http://cran.org/web/packages/timeSeries/timeSeries. Finance Online – Please do not distribute . Martin Maechler.

132. 62. 170–174. 107. 90 args. 118– 124. 12 . 74. 37. 16. 107 class. 78. 14. 54. 53. 39–42. 116. 166 Conj. 170– 174. 15. 60. 93.Index keywords abline. 112–114. 109. 134. 149. 61. 32. 3. 53 apply. 170. 38. 134. 44. 3. 65. 132. 53. 176 approx. 123. 158. 183 deltat. 137–143. 17. 43. 69–72. 60. 133. 148. 113. 10–12. 147. 88. 13. 150–152 CRAN. 57. 106. 21. 99–102. 53. 17. 163. 166. 146–149. 3–5. 4–7. 170–174. 46. 38–40. 146. 176–180 date. 88–90 cor. 12. 150. 16. 132. 24. 37. 149. 118. 12–14. 120. 78. 65. 35. 118. 183 col. 151. 20. 23–25. 146. 159 cbind. 108. 43. 106 Arg. 58. 165. 47. 101. 88. 5–8. 71–75 201 c. 176–180 attach. 11. 95. 69–75. 32. 101. 66. 165. 23. 84 all. 183 C. 103. 95. 12. 92–97. 120–124. 92. 74. 165. 176– 180. 78. 174 by. 34. 107. 152 cov. 43–45. 75. 94. 120 Call. 38. 75. 20. 49–52 character. 184 data. 134 attr. 59. 76. 170–174. 65–69. 19. 124 abs. 115. 121. 53. 34. 158. 124 colnames. 155. 92. 101. 98. 12. 107. 159. 21. 112. 46–57. 153. 69–72. 176. 39. 120 as. 83.

88–90 index. 122. 51. 132. 53. 92. 64. 46. 43. 49. 95–100. 38. 10. 46. 86. 133. 160 mat. 12. 16. 3. 23. 78. 47. 120 length. 146. 93. 121 hist. 75.202 Index density. 161–164 prod. 76. 101. 77. 17. 55. 12 Im. 107 function. 106. 159 log. 146. 109. 133. 176 fivenum. 98. 37. 124 print. 120. 92. 84. 149. 6. 146. 25. 12. 132. 57. 20. 121. 58. 26. 4. 176 intersect. 161. 3–5. 74. 54 frequency. 136 dim. 187 lines. 146. 120–124 points. 159. 18. 158. 53 ncol. 165. 21. Finance Online – Please do not distribute . 61. 153–155. 132. 183 integer. 185 min. 170–174. 12. 78. 144. 156 if. 69–72. 134. 184–186 format. 61–64 matrix. 121. 19. 136. 120 parent. 158. 170. 31. 63. 3 panel. 99. 95–97. 3. 112–114. 4. 7. 137 dimnames. 120. 176–180 for. 87 qqnorm. 74. 166 plot. 106. 64 else. 65. 176. 137–140. 106 order. 87 mean. 12. 87 © 2009. 86. 74. 86. 156 floor. 159 frame. 12 dnorm. 12 grid. 93. 147. 170. 12. 176–180 null. 98. 165. 16. 12. 145 eval. 5. 153. 156 detach. 176–180 formula. 132. 165. 105. 96–98. 16. 117. 18. 53. 63. 12 numeric. 19. 120. 64. 153. 32. 143. 170. 171– 180. 65. 118–124. 24. 95. 121. 69–72. 94. 170–174. 4. 73. 75. 2. 170–174. 38. 32. 176 merge. 60. 183–186 range. 37. 176 getOption. 165. 61. 83. 20. 12 end. 43. 183. 113– 119. 150. 116. 34. 26. 37. 69–72. 180 diff. 63. 21. 183. 154 drop. 123 lm. 74. 155. 112. 24. 135. 154 median. 170. 53–62 methods. 176 paste. 63. 160 mad. 63. 176–180 library. 170–174. 121. 21. 176–180 max. 92–109 names. 162 R. 161. 53 is. 106 labels. 149 lowess. 88–90 na. 12–14. 6. 15. 29. 65. 122. 124. 170. 148. 30 omit. 87 Mod. 170. 61. 78. 53. 92–94. 12. 39.

lag. head. 150 start. 69 180 zoo. 43–48 unique. 107. 48. 176–180 approx. 51. 15. 170. 133 seq. 11. 120–124. 183 hist. 48. 152. 158. 156 title. 27. 146. 142 acf. 32. 51 signif. 33. 73.frame. 120 rev. 143. 159 xy. 157 143. Finance Online – Please do not distribute . 55 rnorm. 150–152 rep. 95–98 data. 153 groupGeneric. 37. 132. 78. diff. 31. 167 sample. 41. 139 R classes rm. POSIX. 107. 19. 135 144. 21.Index 203 rank. 29. 137–140. 170–174. 123. 133. 76. 138. 37. 172 round. 13. 152 subset. 52 sapply. 146. 150 spline. 183–185 resid. 17. 25 t. 141. 176 dist. 86 ccf. 42. 102. 103 cov. 74. 98. 149 sd. 109. 85. 95. 13. 123. 16. 33. 18. 75. 23. 87 filter. 36. 144 sum. 149. 3 Re. 46. 86 timeSeries. 153. 16. 33. 154 system. 158 ts. 16. 5–7. 92. 12–14. 2 65. 153 substitute. 24. 140. 85. 136 structure. 154 apply. 38 Version. 176–180 dnorm. 148 show. 137. 52. 52. 28. 88–90 var. 21. 180 lm. 183 © 2009. 46. 149 sort. 33. 13. 165 arima. 132. 63. 76. 93. 12. 121 165 ts. 53. 32. 176– xts. runif. 144 cor. 69. 107. 170–180 finCenter. 170. 77 time. 28. 176–180 timeDate. 109 rbind. 25. numeric. 32. 33. 124. 163. 170. 150. 75. 13. 176 end. 138 tsp. 134. 47. 76. 16. 167 176. 55. 176 R functions scale. 107. 4. 69–72. 37. 153–155. 9 try. 170–174. 145 dim. 37. 141. 29. 51. 5–7. 23 119. 16. 170. 37 ISOdate. 132. 124. 42. 101 set. 114 cbind. 120–122.

58 zoo. 11. 4 timeSeries. 107 pacf. x © 2009. 162 sort. 35. Finance Online – Please do not distribute . 161 rank.contiguous. 150 xts. 138 sample. x xts. 149 print. 159 mad. 45 rev. 137 rbind. 163 spline. 144 Sys. x zoo. x xts/zoo. 160 median.timezone. 139 scale. 161 qqnorm. 101 start. 51. x timeSeries/timeDate. 58 var. 10. 113 qqline. 35. x timeSeries. 54. 34. 36 spectrum. 141 smooth. 160 na.204 Index lowess. 54 R packages timeDate. 52.

He is working for Finance Online and is currently doing a 6 month Student Internship in the Econophysics group at ETH Zurich at the Institute for Theoretical Physics. CSE. 205 . at the Swiss Federal Institute of Technology in Zurich. an ETH spin-off company in Zurich. Yohan is a co-maintainer of the Rmetrics packages. He teaches Econophysics at ITP and supervises seminars in Financial Engineering at CSE. and co-founder of the Rmetrics Association.About the Authors Diethelm Würtz is private lecturer at the Institute for Theoretical Physics. ITP. Diethelm is senior partner of Finance Online. Andrew Ellis read neuroscience and mathematics at the University in Zurich. He is now a PhD student in the Econophysics group at ETH Zurich at the Institute for Theoretical Physics. Yohan Chalabi has a master in Physics from the Swiss Federal Institute of Technology in Lausanne. Andrew is working on the Rmetrics documentation project. and for the Curriculum Computational Science and Engineering.

Sign up to vote on this title
UsefulNot useful