Computational Finance and Financial Engineering: The R/Rmetrics
Rmetrics User Guides · 2018. 11. 4. · R/Rmetrics eBook Series R/Rmetrics eBooks is a series of...
Transcript of Rmetrics User Guides · 2018. 11. 4. · R/Rmetrics eBook Series R/Rmetrics eBooks is a series of...
A Discussion of Time Series Objects for R in Finance
Diethelm Würtz
Andrew Ellis
Yohan Chalabi
Rmetrics Association & Finance Online
R/Rmetrics eBook Series
R/Rmetrics eBooks is a series of electronic books and user guides aimedat students and practitioner who use R/Rmetrics to analyze financialmarkets.
A Discussion of Time Series Objects for R in Finance (2009)Diethelm Würtz, Yohan Chalabi, Andrew Ellis
Portfolio Optimization with R/Rmetrics (2010),Diethelm Würtz, William Chen, Yohan Chalabi, Andrew Ellis
Basic R for Finance (2010),Diethelm Würtz, Yohan Chalabi, Longhow Lam, Andrew EllisEarly Bird Edition
Financial Market Data for R/Rmetrics (2010)Diethelm Würtz, Andrew Ellis, Yohan ChalabiEarly Bird Edition
Indian Financial Market Data for R/Rmetrics (2010)Diethelm Würtz, Mahendra Mehta, Andrew Ellis, Yohan Chalabi
A DISCUSSION OF TIME SERIES
OBJECTS
FOR R IN FINANCE
DIETHELM WÜRTZ
ANDREW ELLIS
YOHAN CHALABI
RMETRICS ASSOCIATION & FINANCE ONLINE
Series Editors:Prof. Dr. Diethelm WürtzInstitute of Theoretical Physics andCurriculum for Computational ScienceSwiss Federal Institute of TechnologyHönggerberg, HIT K 32.28093 Zurich
Dr. Martin HanfFinance Online GmbHZeltweg 78032 Zurich
Contact Address:Rmetrics AssociationZeltweg 78032 [email protected]
Publisher:Finance Online GmbHSwiss Information TechnologiesZeltweg 78032 Zurich
Authors:Yohan Chalabi, ETH ZurichAndrew Ellis, Finance Online GmbH ZurichDiethelm Würtz, ETH Zurich
ISBN: 978-3-906041-00-1
© 2009, Finance Online GmbH, ZurichPermission is granted to make and distribute verbatim copies of this manual provided thecopyright notice and this permission notice are preserved on all copies.Permission is granted to copy and distribute modified versions of this manual under the con-ditions for verbatim copying, provided that the entire resulting derived work is distributedunder the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another lan-guage, under the above conditions for modified versions, except that this permission noticemay be stated in a translation approved by the Rmetrics Association, Zurich.
Limit of Liability/Disclaimer of Warranty: While the publisher and authors have used theirbest efforts in preparing this book, they make no representations or warranties with respectto the accuracy or completeness of the contents of this book and specifically disclaim anyimplied warranties of merchantability or fitness for a particular purpose. No warranty maybe created or extended by sales representatives or written sales materials. The advice andstrategies contained herein may not be suitable for your situation. You should consult with aprofessional where appropriate. Neither the publisher nor authors shall be liable for any lossof 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.
DEDICATION
This book is dedicated to all those whohave helped make Rmetrics what it is today:
The leading open source software environment incomputational finance and financial engineering.
CONTENTS
DEDICATION III
CONTENTS V
LIST OF FIGURES VII
I Time Series Basics 1
1 CREATING TIME SERIES OBJECTS 2
2 REGULAR TIME SERIES OBJECTS 10
3 TIME ZONE AND DAYLIGHT SAVING TIME 16
4 ORDERING AND OVERLAPPING OF TIME SERIES 25
5 BINDING AND MERGING OF TIME SERIES 34
6 SUBSETTING TIME SERIES OBJECTS 50
7 GROUP GENERICS FOR TIME SERIES OBJECTS 61
8 MISSING DATA HANDLING 73
II Reporting 87
9 PRINTING TIME SERIES OBJECTS 88
10 PLOTTING TIME SERIES OBJECTS 94
III Using R Functions 102
11 FUNCTIONS AND METHODS FROM BASE R 103
v
12 FUNCTIONS AND METHODS FROM STATS R 114
13 FUNCTIONS AND METHODS FROM UTILS R 128
IV Performance Measurements 132
14 PERFORMANCE OF TIME SERIES CREATION 133
V Appendix 137
A PACKAGES REQUIRED FOR THIS EBOOK 138
B FUNCTION LISTINGS 141
BIBLIOGRAPHY 151
INDEX 152
ABOUT THE AUTHORS 153
LIST OF FIGURES
10.1 Plot 1 - Example of a single panel plot from zoo, xts and time-Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
10.2 Plot 2a - Multivariate Time Series plots in multiple graphs . . . . 9710.3 Plot 2b - Multivariate Time Series plots in multiple graphs . . . 9810.4 Plot3a - Example of a single panel plot from zoo . . . . . . . . . . . 9910.5 Plot 3b - Example of a single panel plot from timeSeries . . . . . 100
vii
PART I
TIME SERIES BASICS
1
CHAPTER 1
CREATING TIME SERIES OBJECTS
> 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, its extension xts from R’s xts pack-age, and timeSeries from the R/Rmetrics package timeSeries, whichuses the timeDate package for its time stamps.
What are the major differences concerning the time stamps of these threetime series classes ?
zoo and xts are time series classes with time stamp indices depending onthe time stamp index class which we used for their creation. timeSeriesobjects are independent of the type of time stamps used for its creation.Time stamps in timeSeries objects are always of class numeric.
What time stamp index classes are usually used to create timeSeriesobjects?
For zoo and xts these are Date for the use with daily data records wherewe don’t care about time zone, TZ, information and POSIXct when workwith intraday data records and when time zones and daylight saving times,DST, become relevant. The time stamps used for timeSeries objects aretimeDate objects with internal Olsen time zone data base information.
2
Do time series depend on the way they were created?
This may be the case for zoo and xts time series which were created indifferent time zones under different operating systems. For this casesthe result depends on the internal implementation of time zone anddaylight saving time rules which may be different for different operatingsystems. Using timeSeries objects you get always the same result on anycomputer. The reason is that timeSeries objects are handled internallywith timeDate objects which always operate on POSIXct objects in "GMT"and save the TZ and DST information separate as delivered from Olsenstime zone database made available by Rmetrics.For zoo and xts these are Date for the use with daily data records wherewe don’t care about time zone, TZ, information and POSIXct when work-ing with intraday data records and when time zones and daylight savingtimes, DST, are relevant. The time stamps used for timeSeries objects aretimeDate objects which rely on Olsen’s time zone data base information.
What influence does your current time zone environment have on thecreation of time series when you don’t care about time zone settings of yourenvironment?
Using for zoo and xts the function as.POSIXct with default settings asindex class creates time stamps with respect to the setting of the timezone in your local environment.
> args(zoo)
function (x = NULL, order.by = index(x), frequency = NULL)NULL
> args(xts)
function (x = NULL, order.by = index(x), frequency = NULL, unique = TRUE,...)
NULL
> args(as.POSIXct)
function (x, tz = "", ...)NULL
Thus, users in London, New York or Tokyo will get different results. Thesame holds if we create the index from the function ISOdatetime.
> args(ISOdatetime)
function (year, month, day, hour, min, sec, tz = "")NULL
Be careful in using the function ISOdate.
> args(ISOdate)
function (year, month, day, hour = 12, min = 0, sec = 0, tz = "GMT")NULL
By default the function generates an index in GMT and an additionaloffset of 12 hours. Note that all three functions return an index of classPOSIXct. For timeSeries objects this considerations are obsolete whenyou create timeSeries objects from character strings and the appropriatetime zone or in our notation financial centre information.
> args(timeSeries)
function (data, charvec, units = NULL, format = NULL, zone = "",FinCenter = "", recordIDs = data.frame(), title = NULL, documentation = NULL,...)
NULL
> args(timeDate)
function (charvec, format = NULL, zone = "", FinCenter = "")NULL
What is my current time zone environment?
Use the function Sys.timezone() to find out your current time zone set-tings.
> Sys.timezone()
[1] "CEST"
How should I create time series objects with daily time stamps when theyare given as character formatted time stamps?
For zoo and xts time series object the best index class to create a serieswith daily time stamps is to use Date.
> args(as.Date)
function (x, ...)NULL
Date objects don’t care about time zone information, they are handledlike objects with TZ "GMT". For timeSeries object the preferred option isto use ans input argument for the time stamps the character vector.
Common Data:
> set.seed(1953)> data <- rnorm(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:
> zoo(data, as.Date(charvec))
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.021925 -0.904325 0.413238 0.186621 0.230818 0.235680
xts:
> xts(data, as.Date(charvec))
[,1]2009-01-01 0.0219252009-02-01 -0.9043252009-03-01 0.4132382009-04-01 0.1866212009-05-01 0.2308182009-06-01 0.235680
timeSeries:
> timeSeries(data, charvec)
GMTTS.1
2009-01-01 0.0219252009-02-01 -0.9043252009-03-01 0.4132382009-04-01 0.1866212009-05-01 0.2308182009-06-01 0.235680
How can I create time series objects with daily time stamps for the previous50 days?
Common Data:
> set.seed(1953)> data <- matrix(rnorm(22), ncol = 2)> now <- "2009-01-05"
zoo:
> zoo(data, as.Date(now) - 0:10)
2008-12-26 -0.1326078 0.4736222008-12-27 -1.4211978 0.2015902008-12-28 -0.0046855 -0.2599762008-12-29 1.0994627 2.5049592008-12-30 1.4837389 0.5702392008-12-31 0.2356802 0.7917822009-01-01 0.2308177 -1.5177502009-01-02 0.1866212 -0.5232122009-01-03 0.4132378 -2.3307002009-01-04 -0.9043255 -0.0755142009-01-05 0.0219246 0.164796
xts:
> xts(data, as.Date(now) - 0:10)
[,1] [,2]2008-12-26 -0.1326078 0.4736222008-12-27 -1.4211978 0.2015902008-12-28 -0.0046855 -0.2599762008-12-29 1.0994627 2.5049592008-12-30 1.4837389 0.5702392008-12-31 0.2356802 0.7917822009-01-01 0.2308177 -1.5177502009-01-02 0.1866212 -0.5232122009-01-03 0.4132378 -2.3307002009-01-04 -0.9043255 -0.0755142009-01-05 0.0219246 0.164796
timeSeries:
> timeSeries(data, as.Date(now) - 0:10)
GMTTS.1 TS.2
2009-01-05 0.0219246 0.1647962009-01-04 -0.9043255 -0.0755142009-01-03 0.4132378 -2.3307002009-01-02 0.1866212 -0.5232122009-01-01 0.2308177 -1.5177502008-12-31 0.2356802 0.7917822008-12-30 1.4837389 0.5702392008-12-29 1.0994627 2.5049592008-12-28 -0.0046855 -0.2599762008-12-27 -1.4211978 0.2015902008-12-26 -0.1326078 0.473622
The timeSeries function can also handle R’s Date objects.
What can go wrong when I create time series objects from POSIXct indices?
Common Data:
> set.seed(1953)> data <- rnorm(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:
> z1 <- zoo(data, as.POSIXct(charvec))> z2 <- zoo(data, ISOdatetime(2009, 1:6, 1, 0, 0, 0))> z3 <- zoo(data, ISOdate(2009, 1:6, 1, 0))> z1
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.021925 -0.904325 0.413238 0.186621 0.230818 0.235680
> z2
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.021925 -0.904325 0.413238 0.186621 0.230818 0.235680
> z3
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.021925 -0.904325 0.413238 0.186621 0.230818 0.235680
Note that all three examples for zoo time series objects deliver the sameoutput, nothing can bee seen from the output about the conditions underwhich the the time stamps were created.
xts:
> x1 <- xts(data, as.POSIXct(charvec))> x2 <- xts(data, ISOdatetime(2009, 1:6, 1, 0, 0, 0))> x3 <- xts(data, ISOdate(2009, 1:6, 1, 0))> x1
[,1]2009-01-01 0.0219252009-02-01 -0.9043252009-03-01 0.4132382009-04-01 0.1866212009-05-01 0.2308182009-06-01 0.235680
> x2
[,1]2009-01-01 0.0219252009-02-01 -0.9043252009-03-01 0.4132382009-04-01 0.1866212009-05-01 0.2308182009-06-01 0.235680
> x3
[,1]2009-01-01 01:00:00 0.0219252009-02-01 01:00:00 -0.9043252009-03-01 01:00:00 0.4132382009-04-01 02:00:00 0.1866212009-05-01 02:00:00 0.2308182009-06-01 02:00:00 0.235680
timeSeries:
> s1 <- timeSeries(data, charvec)> s2 <- timeSeries(data, ISOdatetime(2009, 1:6, 1, 0, 0, 0))> s3 <- timeSeries(data, ISOdate(2009, 1:6, 1, 0))> s1
GMTTS.1
2009-01-01 0.0219252009-02-01 -0.9043252009-03-01 0.4132382009-04-01 0.1866212009-05-01 0.2308182009-06-01 0.235680
> s2
GMTTS.1
2008-12-31 23:00:00 0.0219252009-01-31 23:00:00 -0.9043252009-02-28 23:00:00 0.4132382009-03-31 22:00:00 0.1866212009-04-30 22:00:00 0.2308182009-05-31 22:00:00 0.235680
> s3
GMTTS.1
2009-01-01 0.0219252009-02-01 -0.9043252009-03-01 0.4132382009-04-01 0.1866212009-05-01 0.2308182009-06-01 0.235680
Does the index/time of a time series object depend on how the object wascreated?
Common Data: We take the time series from the previous FAQ.
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(,"package")[1] "timeDate"
> class(time(s2))
[1] "timeDate"attr(,"package")[1] "timeDate"
> class(time(s3))
[1] "timeDate"attr(,"package")[1] "timeDate"
CHAPTER 2
REGULAR TIME SERIES OBJECTS
> library(zoo)> library(xts)> library(timeSeries)
First find out what is a regular time series? The oracle data base manage-ment manual tells us:A time series can be regular or irregular, depending on how predictablydata points arrive or occur:
• In a regular time series, the data arrives predictablyat predefined intervals. For example, daily summariesof stock market data form a regular time series, and onesuch time series might be the set of trade volumes andopening, high, low, and closing prices for stock XYZ forthe year 1997.
• In an irregular time series, unpredictable bursts of dataarrive at unspecified points in time, or most timestampscannot be characterized by a repeating pattern. For ex-ample, account deposits and withdrawals from a bankautomated teller machine (ATM) form an irregular timeseries. An irregular time series may have long periodswith no data or short periods with bursts of data.
So the definition here is quite different to that what a R user or Programmerhas in mind. In R we understand a regular time series as a time series withfixed equidistant units in a calendarical book keeping define a� for the
10
distances between two time stamps or frequency of incoming data points.This is very restrictive. Here are some examples:The most widely used regular time series include monthly and quarterlydata points neglecting day and time stamps. The calendarical counterare then the years (and months), their frequency (per year) is then 12 formonthly data, and 4 for quarterly data.
> args(ts)
function (data = NA, start = 1, end = numeric(0), frequency = 1,deltat = 1, ts.eps = getOption("ts.eps"), class = if (nseries >
1) c("mts", "ts") else "ts", names = if (!is.null(dimnames(data))) colnames(data) else paste("Series",seq(nseries)))
NULL
> data <- round(rnorm(24), 4)> ts(data, start = c(2008, 3), frequency = 12)
Jan Feb Mar Apr May Jun Jul Aug Sep2008 -0.2249 0.8451 -0.3527 1.5267 -0.8748 -0.6524 -0.91252009 0.1425 0.4336 -0.1590 -0.4773 -0.5119 -0.5514 -0.0628 3.2816 -0.82572010 0.0521 -0.8245
Oct Nov Dec2008 1.1236 0.8131 1.90202009 0.8579 -1.0184 -0.87002010
> ts(data, start = c(2008, 3), frequency = 4)
Qtr1 Qtr2 Qtr3 Qtr42008 -0.2249 0.84512009 -0.3527 1.5267 -0.8748 -0.65242010 -0.9125 1.1236 0.8131 1.90202011 0.1425 0.4336 -0.1590 -0.47732012 -0.5119 -0.5514 -0.0628 3.28162013 -0.8257 0.8579 -1.0184 -0.87002014 0.0521 -0.8245
How can I create a regular monthly time series object?
Common Data:
> data <- round(rnorm(24), 4)
ts:
> tm <- ts(data, start = c(2008, 3), frequency = 12)> tm
Jan Feb Mar Apr May Jun Jul Aug Sep2008 0.6350 0.0334 0.4925 -0.1001 -0.7288 0.0466 -0.15192009 -0.1059 -0.1520 -0.0508 -1.0952 -0.2912 0.3055 -0.7520 -1.1353 -0.22362010 -0.1734 1.5693
Oct Nov Dec
2008 0.1910 0.5528 -0.20892009 0.1725 1.3901 -0.35252010
zoo:
> zm <- zooreg(data, start = c(2008, 3), frequency = 12)> zm
2008(3) 2008(4) 2008(5) 2008(6) 2008(7) 2008(8) 2008(9) 2008(10)0.6350 0.0334 0.4925 -0.1001 -0.7288 0.0466 -0.1519 0.1910
2008(11) 2008(12) 2009(1) 2009(2) 2009(3) 2009(4) 2009(5) 2009(6)0.5528 -0.2089 -0.1059 -0.1520 -0.0508 -1.0952 -0.2912 0.3055
2009(7) 2009(8) 2009(9) 2009(10) 2009(11) 2009(12) 2010(1) 2010(2)-0.7520 -1.1353 -0.2236 0.1725 1.3901 -0.3525 -0.1734 1.5693
xts:
> xm <- as.xts(tm)> xm
[,1]Feb 2008 0.6350Mar 2008 0.0334Apr 2008 0.4925May 2008 -0.1001Jun 2008 -0.7288Jul 2008 0.0466Aug 2008 -0.1519Sep 2008 0.1910Oct 2008 0.5528Nov 2008 -0.2089Dec 2008 -0.1059Jan 2009 -0.1520Feb 2009 -0.0508Mar 2009 -1.0952Apr 2009 -0.2912May 2009 0.3055Jun 2009 -0.7520Jul 2009 -1.1353Aug 2009 -0.2236Sep 2009 0.1725Oct 2009 1.3901Nov 2009 -0.3525Dec 2009 -0.1734Jan 2010 1.5693
timeSeries:
> sm <- as.timeSeries(tm)> sm
GMTTS.1
2008-03-31 0.6350
2008-04-30 0.03342008-05-31 0.49252008-06-30 -0.10012008-07-31 -0.72882008-08-31 0.04662008-09-30 -0.15192008-10-31 0.19102008-11-30 0.55282008-12-31 -0.20892009-01-31 -0.10592009-02-28 -0.15202009-03-31 -0.05082009-04-30 -1.09522009-05-31 -0.29122009-06-30 0.30552009-07-31 -0.75202009-08-31 -1.13532009-09-30 -0.22362009-10-31 0.17252009-11-30 1.39012009-12-31 -0.35252010-01-31 -0.17342010-02-28 1.5693
Can timeSeries objects be printed in a regular time series style? Yes.
> print(sm, style = "h")
2008-03-31 2008-04-30 2008-05-31 2008-06-30 2008-07-31 2008-08-31 2008-09-300.6350 0.0334 0.4925 -0.1001 -0.7288 0.0466 -0.1519
2008-10-31 2008-11-30 2008-12-31 2009-01-31 2009-02-28 2009-03-31 2009-04-300.1910 0.5528 -0.2089 -0.1059 -0.1520 -0.0508 -1.0952
2009-05-31 2009-06-30 2009-07-31 2009-08-31 2009-09-30 2009-10-31 2009-11-30-0.2912 0.3055 -0.7520 -1.1353 -0.2236 0.1725 1.3901
2009-12-31 2010-01-31 2010-02-28-0.3525 -0.1734 1.5693
Can timeSeries objects be printed in customized format? Yes.
> print(sm, style = "h", format = "%Y %b")
2008 Mar 2008 Apr 2008 May 2008 Jun 2008 Jul 2008 Aug 2008 Sep 2008 Oct0.6350 0.0334 0.4925 -0.1001 -0.7288 0.0466 -0.1519 0.1910
2008 Nov 2008 Dec 2009 Jan 2009 Feb 2009 Mar 2009 Apr 2009 May 2009 Jun0.5528 -0.2089 -0.1059 -0.1520 -0.0508 -1.0952 -0.2912 0.3055
2009 Jul 2009 Aug 2009 Sep 2009 Oct 2009 Nov 2009 Dec 2010 Jan 2010 Feb-0.7520 -1.1353 -0.2236 0.1725 1.3901 -0.3525 -0.1734 1.5693
> print(sm, style = "h", format = "%Y(%m)")
2008(03) 2008(04) 2008(05) 2008(06) 2008(07) 2008(08) 2008(09) 2008(10)0.6350 0.0334 0.4925 -0.1001 -0.7288 0.0466 -0.1519 0.1910
2008(11) 2008(12) 2009(01) 2009(02) 2009(03) 2009(04) 2009(05) 2009(06)0.5528 -0.2089 -0.1059 -0.1520 -0.0508 -1.0952 -0.2912 0.3055
2009(07) 2009(08) 2009(09) 2009(10) 2009(11) 2009(12) 2010(01) 2010(02)-0.7520 -1.1353 -0.2236 0.1725 1.3901 -0.3525 -0.1734 1.5693
How can I create a regular quarterly time series object?
Common Data:
> data <- round(rnorm(24), 4)
ts:
> tq <- ts(data, start = c(2008, 3), frequency = 4)> tq
Qtr1 Qtr2 Qtr3 Qtr42008 0.0812 -0.52092009 0.5102 0.8066 0.3428 0.96782010 0.4929 1.1089 -2.0314 -0.06682011 1.2822 -0.9643 -0.3389 0.51232012 -0.0736 1.1874 1.4593 -1.31612013 0.3130 -2.0288 -0.7241 -0.65492014 0.5326 0.3174
zoo:
> zq <- zooreg(data, start = c(2008, 3), frequency = 4)> zq
2008(3) 2008(4) 2009(1) 2009(2) 2009(3) 2009(4) 2010(1) 2010(2) 2010(3) 2010(4)0.0812 -0.5209 0.5102 0.8066 0.3428 0.9678 0.4929 1.1089 -2.0314 -0.06682011(1) 2011(2) 2011(3) 2011(4) 2012(1) 2012(2) 2012(3) 2012(4) 2013(1) 2013(2)1.2822 -0.9643 -0.3389 0.5123 -0.0736 1.1874 1.4593 -1.3161 0.3130 -2.02882013(3) 2013(4) 2014(1) 2014(2)-0.7241 -0.6549 0.5326 0.3174
xts:
> xq <- as.xts(tq)> head(xq)
[,1]2008 Q3 0.08122008 Q4 -0.52092009 Q1 0.51022009 Q2 0.80662009 Q3 0.34282009 Q4 0.9678
timeSeries:
> sq <- as.timeSeries(tq)> head(sq)
GMTTS.1
2008-09-30 0.08122008-12-31 -0.52092009-03-31 0.5102
2009-06-30 0.80662009-09-30 0.34282009-12-31 0.9678
Can timeSeries objects be printed in a regular time series style? Yes.
> print(sq, style = "h")
2008-09-30 2008-12-31 2009-03-31 2009-06-30 2009-09-30 2009-12-31 2010-03-310.0812 -0.5209 0.5102 0.8066 0.3428 0.9678 0.4929
2010-06-30 2010-09-30 2010-12-31 2011-03-31 2011-06-30 2011-09-30 2011-12-311.1089 -2.0314 -0.0668 1.2822 -0.9643 -0.3389 0.5123
2012-03-31 2012-06-30 2012-09-30 2012-12-31 2013-03-31 2013-06-30 2013-09-30-0.0736 1.1874 1.4593 -1.3161 0.3130 -2.0288 -0.7241
2013-12-31 2014-03-31 2014-06-30-0.6549 0.5326 0.3174
Can timeSeries objects be printed in customized format? Yes.
> print(sq, style = "h", format = "%Y %b")
2008 Sep 2008 Dec 2009 Mar 2009 Jun 2009 Sep 2009 Dec 2010 Mar 2010 Jun0.0812 -0.5209 0.5102 0.8066 0.3428 0.9678 0.4929 1.1089
2010 Sep 2010 Dec 2011 Mar 2011 Jun 2011 Sep 2011 Dec 2012 Mar 2012 Jun-2.0314 -0.0668 1.2822 -0.9643 -0.3389 0.5123 -0.0736 1.18742012 Sep 2012 Dec 2013 Mar 2013 Jun 2013 Sep 2013 Dec 2014 Mar 2014 Jun
1.4593 -1.3161 0.3130 -2.0288 -0.7241 -0.6549 0.5326 0.3174
> print(sq, style = "h", format = "%Q")
2008 Q3 2008 Q4 2009 Q1 2009 Q2 2009 Q3 2009 Q4 2010 Q1 2010 Q2 2010 Q3 2010 Q40.0812 -0.5209 0.5102 0.8066 0.3428 0.9678 0.4929 1.1089 -2.0314 -0.06682011 Q1 2011 Q2 2011 Q3 2011 Q4 2012 Q1 2012 Q2 2012 Q3 2012 Q4 2013 Q1 2013 Q21.2822 -0.9643 -0.3389 0.5123 -0.0736 1.1874 1.4593 -1.3161 0.3130 -2.02882013 Q3 2013 Q4 2014 Q1 2014 Q2-0.7241 -0.6549 0.5326 0.3174
How I can find out if a time series is a regular time series?
zoo:
> z <- zooreg(data, start = c(2008, 3), frequency = 4)> is.regular(z)
[1] TRUE
CHAPTER 3
TIME ZONE AND DAYLIGHT SAVING TIME
> 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 Euro-pean Time" zone, CET.
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:
> z1.zrh <- zoo(data, as.POSIXct(charvec, tz = "CET"))> z1.zrh
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-011 2 3 4 5 6
xts:
> x1.zrh <- xts(data, as.POSIXct(charvec, tz = "CET"))> x1.zrh
16
[,1]2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
With the newest version of xts this cannot any longer be done. To ourunderstanding xts now forces always the time stamps to GMT time zone.
timeSeries:
> s1.zrh <- timeSeries(data, charvec, zone = "Zurich", FinCenter = "Zurich")> s1.zrh
ZurichTS.1
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
Note that the timeSeries function has two time zone relevant inputs. Theargument zone holds the information to which time zone or to be morespecific to which financial centre the charvec of time stamps belongs, andthe second argument FinCenter tells us in which time zone or at whichfinancial centre we want to display and use the time series data.
> args(timeSeries)
function (data, charvec, units = NULL, format = NULL, zone = "",FinCenter = "", recordIDs = data.frame(), title = NULL, documentation = NULL,...)
NULL
Have a look at the output of the possible four options.
> timeSeries(data, charvec, zone = "Zurich", FinCenter = "Zurich",units = "s1.zrh.zrh")
Zurichs1.zrh.zrh
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
> timeSeries(data, charvec, zone = "GMT", FinCenter = "Zurich",units = "s1.gmt.zrh")
Zurichs1.gmt.zrh
2009-01-01 01:00:00 12009-02-01 01:00:00 22009-03-01 01:00:00 32009-04-01 02:00:00 42009-05-01 02:00:00 52009-06-01 02:00:00 6
> timeSeries(data, charvec, zone = "Zurich", FinCenter = "GMT",units = "s1.zrh.gmt")
GMTs1.zrh.gmt
2008-12-31 23:00:00 12009-01-31 23:00:00 22009-02-28 23:00:00 32009-03-31 22:00:00 42009-04-30 22:00:00 52009-05-31 22:00:00 6
> timeSeries(data, charvec, zone = "GMT", FinCenter = "GMT", units = "s1.gmt.gmt")
GMTs1.gmt.gmt
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
When I print a time series can I see what time zone the series belongs to?
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: Note, the zoo object must be indexed by an index which supportstime zones, e.g. objects of class POSIX.
> z1.zrh <- zoo(data, as.POSIXct(charvec, tz = "CET"))> z1.zrh
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-011 2 3 4 5 6
For zoo time series objects the time zone cannot be seen from printingthe series.
xts: Also xts objects must be indexed by an index which supports timezones.
> x1.zrh <- xts(data, as.POSIXct(charvec, tz = "CET"))> x1.zrh
[,1]2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
timeSeries:
> s1.zrh <- timeSeries(data, charvec, zone = "Zurich", FinCenter = "Zurich")> s1.zrh
ZurichTS.1
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
Note that timeSeries objects show on top to which time zone (or morespecific financial centre) they belong. The information is taken from thetime stamps which are objects of class timeDate.
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 indexfor zoo and xts objects and the function time for timeSeries objects todisplay the zone information
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:
> data <- 1:6> charvec <- paste("2009-0", 1:6, "-01", sep = "")> z1.zrh <- zoo(data, as.POSIXct(charvec, tz = "CET"))> index(z1.zrh)
[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.zrh <- xts(data, as.POSIXct(charvec, tz = "CET"))> index(x1.zrh)
[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"
timeSeries:
> s1.zrh <- timeSeries(data, charvec, zone = "Zurich", FinCenter = "Zurich")> time(s1.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 infor-mation using the accessor function finCenter().
> currentCenter <- finCenter(s1.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.
timeSeries: For timeSeries objects you can look in the data.frame ofDST rules. To shorten the output we subset it here to some records
> Zurich()[54:64, ]
Zurich offSet isdst TimeZone numeric54 2005-03-27 01:00:00 7200 1 CEST 111188520055 2005-10-30 01:00:00 3600 0 CET 113063400056 2006-03-26 01:00:00 7200 1 CEST 114333480057 2006-10-29 01:00:00 3600 0 CET 116208360058 2007-03-25 01:00:00 7200 1 CEST 117478440059 2007-10-28 01:00:00 3600 0 CET 119353320060 2008-03-30 01:00:00 7200 1 CEST 120683880061 2008-10-26 01:00:00 3600 0 CET 122498280062 2009-03-29 01:00:00 7200 1 CEST 123828840063 2009-10-25 01:00:00 3600 0 CET 125643240064 2010-03-28 01:00:00 7200 1 CEST 1269738000
The table shows when the clock was changed in Zurich, the offset inseconds with respect to GMT, a flag which tells us if DST is in effect or not,the time zone abbreviation, and a integer value in seconds belonging tothe time stamp when the clock was changed.
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 availablein R’s POSIXt implementation and thus which time zones are available touse to create zoo and xts time series objects. Can you please quickly tellus to which time zone the stock exchange in Mumbai belongs?This is operating-system dependent; on Unix systems the list is in /us-r/share/tzone
timeSeries: For timeSeries objects we can print the list of supportedfinancial centres using the functions listFinCenter
> length(listFinCenter())
[1] 397
These are too many to get printed here. To display a limited number youcan select them by greped pattern. Here are some examples, financialcentres in the Pacific region, 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" "Pacific/Norfolk" "Pacific/Noumea"[25] "Pacific/Pago_Pago" "Pacific/Palau" "Pacific/Pitcairn"[28] "Pacific/Ponape" "Pacific/Port_Moresby" "Pacific/Rarotonga"[31] "Pacific/Saipan" "Pacific/Tahiti" "Pacific/Tarawa"[34] "Pacific/Tongatapu" "Pacific/Truk" "Pacific/Wake"[37] "Pacific/Wallis"
> listFinCenter(".*/L")
[1] "Africa/Lagos" "Africa/Libreville"[3] "Africa/Lome" "Africa/Luanda"[5] "Africa/Lubumbashi" "Africa/Lusaka"[7] "America/Argentina/La_Rioja" "America/Kentucky/Louisville"[9] "America/La_Paz" "America/Lima"[11] "America/Los_Angeles" "Arctic/Longyearbyen"[13] "Australia/Lindeman" "Australia/Lord_Howe"[15] "Europe/Lisbon" "Europe/Ljubljana"[17] "Europe/London" "Europe/Luxembourg"
Yo can even create your own financial centres. For example the DST rulesfor Germany are in "Germany/Berlin", as a banker you may prefer Frank-furt, then just create your own table.
> Frankfurt <- Berlin> timeSeries(runif(1:12), timeCalendar(), zone = "Frankfurt", FinCenter = "Frankfurt")
FrankfurtTS.1
2010-01-01 0.36058232010-02-01 0.80517542010-03-01 0.75566062010-04-01 0.12797502010-05-01 0.58967122010-06-01 0.65229432010-07-01 0.31534022010-08-01 0.45591332010-09-01 0.94327232010-10-01 0.57030222010-11-01 0.00241372010-12-01 0.6121905
Note that timeCalendar is a function from the package timeDate andcreates monthly time stamps for the current year.
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. Asa workaround we recommend to extract the index from the time series,then to add the time shift between the new and old timezone, and finallyto reassign the new index to the zoo or xts object.
timeSeries: In timeSerieswe use the assignment function finCenter<-to assign a new financial centre to an already existing timeSeries object.For example to express a time series recorded in London for use in Zurichin time stamps of local time in New York proceed as follows
> ZRH <- timeSeries(rnorm(6), timeCalendar(2009)[7:12], zone = "London",FinCenter = "Zurich")
> ZRH
ZurichTS.1
2009-07-01 01:00:00 0.524142009-08-01 01:00:00 0.555842009-09-01 01:00:00 -0.300572009-10-01 01:00:00 -0.679792009-11-01 01:00:00 1.237732009-12-01 01:00:00 -0.18627
> finCenter(ZRH) <- "New_York"> ZRH
New_YorkTS.1
2009-06-30 19:00:00 0.524142009-07-31 19:00:00 0.555842009-08-31 19:00:00 -0.300572009-09-30 19:00:00 -0.679792009-10-31 20:00:00 1.237732009-11-30 19:00:00 -0.18627
This example reflects also the fact that Europe and USA changed fromsummer time to winter time in different months, i.e October and Novem-ber respectively! Have a look in the DST tables to confirm this
> Zurich()[63, ]
Zurich offSet isdst TimeZone numeric63 2009-10-25 01:00:00 3600 0 CET 1256432400
> New_York()[179, ]
New_York offSet isdst TimeZone numeric179 2009-11-01 06:00:00 -18000 0 EST 1257055200
How can I handle data recorded in two cities which belong to the sametime zone but have different DST rules?
zoo and xts: Not supported by zoo and xts objects.
timeSeries: This happened for example in Germany and Switzerlandseveral times in the years between 1940 and 1985. Have a look on the tablewith the DST rules.
> Berlin()[8:38, ]
Berlin offSet isdst TimeZone numeric8 1940-04-01 01:00:00 7200 1 CEST -9389052009 1942-11-02 01:00:00 3600 0 CET -85725720010 1943-03-29 01:00:00 7200 1 CEST -84455640011 1943-10-04 01:00:00 3600 0 CET -82822680012 1944-04-03 01:00:00 7200 1 CEST -81250200013 1944-10-02 01:00:00 3600 0 CET -79677720014 1945-04-02 01:00:00 7200 1 CEST -78105240015 1945-05-24 00:00:00 10800 1 CEMT -77656320016 1945-09-24 00:00:00 7200 1 CEST -76593600017 1945-11-18 01:00:00 3600 0 CET -76118040018 1946-04-14 01:00:00 7200 1 CEST -74847960019 1946-10-07 01:00:00 3600 0 CET -73327320020 1947-04-06 02:00:00 7200 1 CEST -71763120021 1947-05-11 01:00:00 10800 1 CEMT -71461080022 1947-06-29 00:00:00 7200 1 CEST -71038080023 1947-10-05 01:00:00 3600 0 CET -70191000024 1948-04-18 01:00:00 7200 1 CEST -68497560025 1948-10-03 01:00:00 3600 0 CET -67046040026 1949-04-10 01:00:00 7200 1 CEST -65413080027 1949-10-02 01:00:00 3600 0 CET -63901080028 1980-04-06 01:00:00 7200 1 CEST 32383080029 1980-09-28 01:00:00 3600 0 CET 33895080030 1981-03-29 01:00:00 7200 1 CEST 35467560031 1981-09-27 01:00:00 3600 0 CET 37040040032 1982-03-28 01:00:00 7200 1 CEST 38612520033 1982-09-26 01:00:00 3600 0 CET 40185000034 1983-03-27 01:00:00 7200 1 CEST 41757480035 1983-09-25 01:00:00 3600 0 CET 43329960036 1984-03-25 01:00:00 7200 1 CEST 449024400
37 1984-09-30 01:00:00 3600 0 CET 46535400038 1985-03-31 01:00:00 7200 1 CEST 481078800
> Zurich()[2:15, ]
Zurich offSet isdst TimeZone numeric2 1941-05-05 00:00:00 7200 1 CEST -9044352003 1941-10-06 00:00:00 3600 0 CET -8911296004 1942-05-04 00:00:00 7200 1 CEST -8729856005 1942-10-05 00:00:00 3600 0 CET -8596800006 1981-03-29 01:00:00 7200 1 CEST 3546756007 1981-09-27 01:00:00 3600 0 CET 3704004008 1982-03-28 01:00:00 7200 1 CEST 3861252009 1982-09-26 01:00:00 3600 0 CET 40185000010 1983-03-27 01:00:00 7200 1 CEST 41757480011 1983-09-25 01:00:00 3600 0 CET 43329960012 1984-03-25 01:00:00 7200 1 CEST 44902440013 1984-09-30 01:00:00 3600 0 CET 46535400014 1985-03-31 01:00:00 7200 1 CEST 48107880015 1985-09-29 01:00:00 3600 0 CET 496803600
We have end-of-day data 16:00 recorded in Zurich in local time, but nowwe want to use them in Berlin. The question now is: what is the earliesttime we can start with the investigation of the data at local Berlin time.
> charvec <- paste("1980-0", 2:5, "-15 16:00:00", 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), charvec, zone = "Zurich", FinCenter = "Berlin",units = "fromZurich")
BerlinfromZurich
1980-02-15 16:00:00 0.382251980-03-15 16:00:00 0.830181980-04-15 17:00:00 0.896631980-05-15 17:00:00 0.69902
So in February and March we can start our investigation in Berlin at thesame time as in Zurich 16:00, but in April and May we can start with ourinvestigation one hour later at 17:00 due to the time difference to Zurich.
CHAPTER 4
ORDERING AND OVERLAPPING OF TIME SERIES
> library(zoo)> library(xts)> library(timeSeries)
How can I create a time series with time stamps in reverse order?
Common Data:
> set.seed <- 1953> data <- rnorm(6)> 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"
Note the character vector charvec is in reverse order.
zoo and xts:
> zoo(data, as.Date(charvec))
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.30449 -3.25273 0.74521 0.31609 -1.27466 -0.80742
> xts(data, as.Date(charvec))
[,1]2009-01-01 0.304492009-02-01 -3.252732009-03-01 0.745212009-04-01 0.316092009-05-01 -1.274662009-06-01 -0.80742
25
Time series objects of class zoo are always forced to be ordered in time.The same holds for xts time series objects. It is not possible to create azoo or xts object in reverse time order.
timeSeries: timeSeries objects can be created in increasing and de-creasing order, and they can even be sampled arbitrarily.
> tS <- timeSeries(data, charvec)> tS
GMTTS.1
2009-06-01 -0.807422009-05-01 -1.274662009-04-01 0.316092009-03-01 0.745212009-02-01 -3.252732009-01-01 0.30449
Reverse the time order
> rev(tS)
GMTTS.1
2009-01-01 0.304492009-02-01 -3.252732009-03-01 0.745212009-04-01 0.316092009-05-01 -1.274662009-06-01 -0.80742
Sample the time series in arbitray time order
> sample(tS)
GMTTS.1
2009-05-01 -1.274662009-04-01 0.316092009-03-01 0.745212009-06-01 -0.807422009-02-01 -3.252732009-01-01 0.30449
How can I create a time series with overlapping time stamps?
You proceed in the same way as with any other unique time series.
Common Data:
> data1 <- c(1:6, 0)> charvec1 <- c(paste("2009-0", 1:6, "-01", 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 <- 0:6> charvec2 <- c("2009-04-01", paste("2009-0", 1:6, "-01", 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, as.Date(charvec1))
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-04-01 2009-05-01 2009-06-011 2 3 4 0 5 6
> zoo(data2, as.Date(charvec2))
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-04-01 2009-05-01 2009-06-011 2 3 0 4 5 6
zoo() will return a warning message, stating that possibly not all zoo()methods will work.
xts: xts()has extended this point in zoo(), and overlapping time stampscan be handled.
> xts(data1, as.Date(charvec1))
[,1]2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-04-01 02009-05-01 52009-06-01 6
> xts(data2, as.Date(charvec2))
[,1]2009-01-01 12009-02-01 22009-03-01 32009-04-01 02009-04-01 42009-05-01 52009-06-01 6
timeSeries: timeSeries objects allow for overlapping time stamps
> timeSeries(data1, charvec1)
GMTTS.1
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 62009-04-01 0
> timeSeries(data2, charvec2)
GMTTS.1
2009-04-01 02009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
but the order is the same as that provided by the charvec time stamps.This is a useful feature since it can reflect the order in which you receivedthe data records. To sort the series use the function sort()
> sort(timeSeries(data1, charvec1))
GMTTS.1
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-04-01 02009-05-01 52009-06-01 6
> sort(timeSeries(data2, charvec2))
GMTTS.1
2009-01-01 12009-02-01 22009-03-01 32009-04-01 02009-04-01 42009-05-01 52009-06-01 6
Note that you can also sort a timeSeries object in decreasing order.
> sort(timeSeries(data1, charvec1), decreasing = TRUE)
GMTTS.1
2009-06-01 62009-05-01 52009-04-01 42009-04-01 02009-03-01 32009-02-01 22009-01-01 1
If you want to keep the information of the original order, you can save itin the @recordIDs slot of the timeSeries object. The following exampleshows you how to keep and retrieve this information.
> args(timeSeries)
function (data, charvec, units = NULL, format = NULL, zone = "",FinCenter = "", recordIDs = data.frame(), title = NULL, documentation = NULL,...)
NULL
> data3 <- round(rnorm(7), 2)> charvec3 <- sample(charvec1)> tS <- sort(timeSeries(data3, charvec3, recordIDs = data.frame(1:7)))> tS
GMTTS.1 X1.7*
2009-01-01 1.22 12009-02-01 -0.81 72009-03-01 -0.05 32009-04-01 -0.38 52009-04-01 0.55 62009-05-01 1.25 42009-06-01 -0.60 2
Now retrieve the order in the same way as for the information
> tS@recordIDs
X1.71 17 73 35 56 64 42 2
or you can print it in the form of a direct comparison report.
> cbind(series(tS), as.matrix(tS@recordIDs))
TS.1 X1.72009-01-01 1.22 12009-02-01 -0.81 72009-03-01 -0.05 32009-04-01 -0.38 5
2009-04-01 0.55 62009-05-01 1.25 42009-06-01 -0.60 2
> data3
[1] 1.22 -0.60 -0.05 1.25 -0.38 0.55 -0.81
How can I handle additional attributes of a time series object?
Let us consider the following (slightly more complex) time series example.We have at given dates, dateOfOffer, price offers, offeredPrice, pro-vided by different companies, providerCompany, which are rated, ratin-gOfOffer. The information about the providers and ratings is saved in adata.frame named priceInfo.
> offeredPrice <- 100 * c(3.4, 3.2, 4, 4, 4.1, 3.5, 2.9)> offeredPrice
[1] 340 320 400 400 410 350 290
> dateOfOffer <- paste("2009-0", c(1:3, 3, 4:6), "-01", 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 <- c(rep("UISA Ltd", times = 3), "HK Company",rep("UISA Ltd", times = 3))
> providerCompany
[1] "UISA Ltd" "UISA Ltd" "UISA Ltd" "HK Company" "UISA Ltd"[6] "UISA Ltd" "UISA Ltd"
> ratingOfOffer <- c(rep("AAA", times = 3), "BBB", rep("AAB", times = 3))> ratingOfOffer
[1] "AAA" "AAA" "AAA" "BBB" "AAB" "AAB" "AAB"
> priceInfo <- data.frame(providerCompany, ratingOfOffer)> priceInfo
providerCompany ratingOfOffer1 UISA Ltd AAA2 UISA Ltd AAA3 UISA Ltd AAA4 HK Company BBB5 UISA Ltd AAB6 UISA Ltd AAB7 UISA Ltd AAB
zoo: We create a zoo time series object from dates and prices. The ad-ditional information on providers and the ratings can be added by anattribute named "info".
> zp <- zoo(offeredPrice, as.Date(dateOfOffer))> attr(zp, "info") = priceInfo> zp
2009-01-01 2009-02-01 2009-03-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01340 320 400 400 410 350 290
> zp
2009-01-01 2009-02-01 2009-03-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01340 320 400 400 410 350 290
> attr(zp, "info")
providerCompany ratingOfOffer1 UISA Ltd AAA2 UISA Ltd AAA3 UISA Ltd AAA4 HK Company BBB5 UISA Ltd AAB6 UISA Ltd AAB7 UISA Ltd AAB
xts: For xts we proceed it in the same way.
> xp <- xts(offeredPrice, as.Date(dateOfOffer))> attr(xp, "info") = priceInfo> xp
[,1]2009-01-01 3402009-02-01 3202009-03-01 4002009-03-01 4002009-04-01 4102009-05-01 3502009-06-01 290
> attr(xp, "info")
providerCompany ratingOfOffer1 UISA Ltd AAA2 UISA Ltd AAA3 UISA Ltd AAA4 HK Company BBB5 UISA Ltd AAB6 UISA Ltd AAB7 UISA Ltd AAB
timeSeries:
> tS <- timeSeries(offeredPrice, dateOfOffer, recordIDs = priceInfo)> tS
GMTTS.1
2009-01-01 3402009-02-01 3202009-03-01 4002009-03-01 4002009-04-01 4102009-05-01 3502009-06-01 290
> tS@recordIDs
providerCompany ratingOfOffer1 UISA Ltd AAA2 UISA Ltd AAA3 UISA Ltd AAA4 HK Company BBB5 UISA Ltd AAB6 UISA Ltd AAB7 UISA Ltd AAB
For timeSeries objects most attributes can be handled as a data framethrough the @recordsIDs slot.
What happens with attributes when I modify the time series?
Let us consider the example from the previous FAQ. We want to removeoffer No. 4 from the time series.
zoo:
> zx <- zp[-4]> zx
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01340 320 400 410 350 290
> attr(zx, "info")
NULL
The attribute has been lost from the zoo time series object. A workaroundis to check and re-add the subsetted attribute.
> zx
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01340 320 400 410 350 290
> attr(zx, "info") <- priceInfo[-4, ]> zx
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01340 320 400 410 350 290
Now let us inspect what happens with xts. We delete the fourth offer bysubsetting and print the result again.
> xx <- xp[-4]> xx
[,1]2009-01-01 3402009-02-01 3202009-03-01 4002009-04-01 4102009-05-01 3502009-06-01 290
In contrast to zoo, xts time series objects keep attributes. However, theyare not subsetted.A workaround is to delete the record in the attribute.
> attr(xx, "info") <- attr(xx, "info")[-4, ]> xx
[,1]2009-01-01 3402009-02-01 3202009-03-01 4002009-04-01 4102009-05-01 3502009-06-01 290
timeSeries:
> tX <- tS[-4, ]> tX
GMTTS.1
2009-01-01 3402009-02-01 3202009-03-01 4002009-04-01 4102009-05-01 3502009-06-01 290
> tX@recordIDs
providerCompany ratingOfOffer1 UISA Ltd AAA2 UISA Ltd AAA3 UISA Ltd AAA5 UISA Ltd AAB6 UISA Ltd AAB7 UISA Ltd AAB
Note that timeSeries functions also handle attributes that are saved inthe @recordIDs slot. This should work without further workarounds.
CHAPTER 5
BINDING AND MERGING OF TIME SERIES
> 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.
Common Data:
> data <- c(1:6)> 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 <- c(paste("2009-0", 7:9, "-01", sep = ""), paste("2009-",10:12, "-01", sep = ""))
> charvec2
[1] "2009-07-01" "2009-08-01" "2009-09-01" "2009-10-01" "2009-11-01"[6] "2009-12-01"
zoo:
> z1 <- zoo(data, as.Date(charvec1))> z2 <- zoo(data + 6, as.Date(charvec2))
> rbind(z1, z2)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-011 2 3 4 5 6 7
2009-08-01 2009-09-01 2009-10-01 2009-11-01 2009-12-018 9 10 11 12
34
> rbind(z2, z1)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-011 2 3 4 5 6 7
2009-08-01 2009-09-01 2009-10-01 2009-11-01 2009-12-018 9 10 11 12
Note that the bound series do not depend on the order of the argumentsin the function rbind.
xts:
> x1 <- xts(data, as.Date(charvec1))> x2 <- xts(data + 6, as.Date(charvec2))
> rbind(x1, x2)
[,1]2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 62009-07-01 72009-08-01 82009-09-01 92009-10-01 102009-11-01 112009-12-01 12
> rbind(x2, x1)
[,1]2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 62009-07-01 72009-08-01 82009-09-01 92009-10-01 102009-11-01 112009-12-01 12
The result is again an ordered (bound) time series.
timeSeries:
> s1 <- timeSeries(data, charvec1)> s2 <- timeSeries(data + 6, charvec2)
> rbind(s1, s2)
GMTTS.1_TS.1
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 62009-07-01 72009-08-01 82009-09-01 92009-10-01 102009-11-01 112009-12-01 12
> rbind(s2, s1)
GMTTS.1_TS.1
2009-07-01 72009-08-01 82009-09-01 92009-10-01 102009-11-01 112009-12-01 122009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
Note that the result depends on the ordering of the two arguments inthe function rbind(). It is important to note that this is not a bug but afeature. If you want to obtain the same result, then just sort the series
> sort(rbind(s2, s1))
GMTTS.1_TS.1
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 62009-07-01 72009-08-01 82009-09-01 92009-10-01 102009-11-01 112009-12-01 12
Can overlapping time series be bound by row?
Common Data:
> data1 <- 1:6> data2 <- 3:9> 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", 3:9, "-01", 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 <- zoo(data1, as.Date(charvec1))> z2 <- zoo(data2, as.Date(charvec2))
> print(try(rbind(z1, z2)))
[1] "Error in rbind(deparse.level, ...) : indexes overlap\n"attr(,"class")[1] "try-error"
> print(try(rbind(z2, z1)))
[1] "Error in rbind(deparse.level, ...) : indexes overlap\n"attr(,"class")[1] "try-error"
Time series with overlapping indices cannot be bound in zoo.
xts:
> x1 <- xts(data1, as.Date(charvec1))> x2 <- xts(data2, as.Date(charvec2))
> rbind(x1, x2)
[,1]2009-01-01 12009-02-01 22009-03-01 32009-03-01 32009-04-01 42009-04-01 42009-05-01 52009-05-01 52009-06-01 62009-06-01 62009-07-01 72009-08-01 82009-09-01 9
> rbind(x2, x1)
[,1]2009-01-01 12009-02-01 22009-03-01 32009-03-01 32009-04-01 42009-04-01 42009-05-01 52009-05-01 52009-06-01 62009-06-01 62009-07-01 72009-08-01 82009-09-01 9
Time series with overlapping indices cannot be bound row by row in xts,and the original ordering will not be preserved, i.e. the series are sorted.
timeSeries:
> s1 <- xts(data1, as.Date(charvec1))> s2 <- xts(data2, as.Date(charvec2))
> rbind(s1, s2)
[,1]2009-01-01 12009-02-01 22009-03-01 32009-03-01 32009-04-01 42009-04-01 42009-05-01 52009-05-01 52009-06-01 62009-06-01 62009-07-01 72009-08-01 82009-09-01 9
> rbind(s2, s1)
[,1]2009-01-01 12009-02-01 22009-03-01 32009-03-01 32009-04-01 42009-04-01 42009-05-01 52009-05-01 52009-06-01 62009-06-01 62009-07-01 72009-08-01 8
2009-09-01 9
Binding of overlapping timeSeries objects is fully supported by the time-Series class. The time order of the records is fully preserved.
How can I bind two time series objects by column?
Common Data:
> data1 <- 1:6> data2 <- data1 + 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:
> z1 <- zoo(data1, as.Date(charvec))> z2 <- zoo(data2, as.Date(charvec))
> cbind(z1, z2)
z1 z22009-01-01 1 72009-02-01 2 82009-03-01 3 92009-04-01 4 102009-05-01 5 112009-06-01 6 12
> cbind(z2, z1)
z2 z12009-01-01 7 12009-02-01 8 22009-03-01 9 32009-04-01 10 42009-05-01 11 52009-06-01 12 6
Note that the ordering of the columns depends on the order in which thetwo arguments are to the function cbind.
xts:
> x1 <- xts(data1, as.Date(charvec))> x2 <- xts(data2, as.Date(charvec))
> cbind(x1, x2)
..1 ..22009-01-01 1 72009-02-01 2 82009-03-01 3 9
2009-04-01 4 102009-05-01 5 112009-06-01 6 12
> cbind(x2, x1)
..1 ..22009-01-01 7 12009-02-01 8 22009-03-01 9 32009-04-01 10 42009-05-01 11 52009-06-01 12 6
timeSeries:
> s1 <- timeSeries(data1, as.Date(charvec))> s2 <- timeSeries(data2, as.Date(charvec))
> cbind(s1, s2)
GMTTS.1.1 TS.1.2
2009-01-01 1 72009-02-01 2 82009-03-01 3 92009-04-01 4 102009-05-01 5 112009-06-01 6 12
> cbind(s2, s1)
GMTTS.1.1 TS.1.2
2009-01-01 7 12009-02-01 8 22009-03-01 9 32009-04-01 10 42009-05-01 11 52009-06-01 12 6
As in the case of zoo and xts time series objects, the ordering of thecolumns depends on which of the two arguments is first passed to thefunction cbind().
Can overlapping time series be bound by column?
Common Data:
> data1 <- 1:6> data2 <- 4:8> 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:8, "-01", sep = "")> charvec2
[1] "2009-04-01" "2009-05-01" "2009-06-01" "2009-07-01" "2009-08-01"
zoo:
> z1 <- zoo(data1, as.Date(charvec1))> z2 <- zoo(data2, as.Date(charvec2))
> cbind(z1, z2)
z1 z22009-01-01 1 NA2009-02-01 2 NA2009-03-01 3 NA2009-04-01 4 42009-05-01 5 52009-06-01 6 62009-07-01 NA 72009-08-01 NA 8
zoo() can bind overlapping time series, missing values are substituted byNAs.
xts:
> x1 <- xts(data1, as.Date(charvec1))> x2 <- xts(data2, as.Date(charvec2))
> cbind(x1, x2)
..1 ..22009-01-01 1 NA2009-02-01 2 NA2009-03-01 3 NA2009-04-01 4 42009-05-01 5 52009-06-01 6 62009-07-01 NA 72009-08-01 NA 8
xts() can bind overlapping time series, missing values are substituted byNAs. The behaviour is the same as with the column names. Note that thecolumn names of the series are lost.
timeSeries:
> s1 <- timeSeries(data1, as.Date(charvec1), units = "s1")> s2 <- timeSeries(data2, as.Date(charvec2), units = "s2")
> cbind(s1, s2)
GMTs1 s2
2009-01-01 1 NA2009-02-01 2 NA2009-03-01 3 NA2009-04-01 4 42009-05-01 5 52009-06-01 6 62009-07-01 NA 72009-08-01 NA 8
Binding of overlapping timeSeries objects is fully supported by the time-Series class. timeSeries also substitutes missing values by NAs, as is thecase for zoo and xts time series objects.
How can I merge two time series objects and what is the difference tobinding time series objects?
The base package of R has a function merge which can merge two dataframes.
> 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 dataframes by common columns or row names, or do other versions of database"join" operations. In our sense the merge of two time series object wouldwork in the same way as for data frames.Note that a natural implementation for time series would mean that merg-ing 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.12009-01-01 1 12009-02-01 2 22009-03-01 3 32009-04-01 4 42009-05-01 5 52009-06-01 6 6
zoo() returns a bivariate time series object.
xts:
> x <- xts(data, as.Date(charvec))
> merge(x, x)
x x.12009-01-01 1 12009-02-01 2 22009-03-01 3 32009-04-01 4 42009-05-01 5 52009-06-01 6 6
xts() also returns a bivariate time series object.
timeSeries:
> s <- timeSeries(data, charvec)
> merge(s, s)
GMTTS.1
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
> merge(as.data.frame(s), as.data.frame(s))
TS.11 12 23 34 45 56 6
timeSeries objects operate differently, in that they show the same be-haviour 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 z22009-01-01 1 NA2009-02-01 2 NA2009-03-01 3 NA2009-04-01 4 42009-05-01 5 52009-06-01 6 62009-07-01 NA 72009-08-01 NA 82009-09-01 NA 9
xts:
> x1 <- xts(data1, as.Date(charvec1))> x2 <- xts(data2, as.Date(charvec2))
> merge(x1, x2)
x1 x22009-01-01 1 NA2009-02-01 2 NA2009-03-01 3 NA2009-04-01 4 42009-05-01 5 52009-06-01 6 62009-07-01 NA 72009-08-01 NA 82009-09-01 NA 9
timeSeries:
> s1 <- timeSeries(data1, as.Date(charvec1), units = "s1")> s2 <- timeSeries(data2, as.Date(charvec2), units = "s2")
> merge(s1, s2)
GMTs1 s2
2009-01-01 1 NA2009-02-01 2 NA2009-03-01 3 NA2009-04-01 4 42009-05-01 5 52009-06-01 6 62009-07-01 NA 72009-08-01 NA 82009-09-01 NA 9
All three time series classes work in the same way.
What happens if I merge two univariate time series with the sameunderlying information set?
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: We don’t know how to do this with univariate zoo time series ob-jects.
xts: For xts time series objects we can set identical column names andthen merge the two series:
> x1 <- xts(data1, as.Date(charvec1))> colnames(x1) <- "x"> z2 <- xts(data2, as.Date(charvec2))> colnames(x2) <- "x"
> merge(x1, x2)
x x.12009-01-01 1 NA2009-02-01 2 NA2009-03-01 3 NA2009-04-01 4 42009-05-01 5 52009-06-01 6 62009-07-01 NA 72009-08-01 NA 82009-09-01 NA 9
xts() returns a bivariate time series with column names x and x.1
timeSeries:
> s1 <- timeSeries(data1, charvec1, units = "s")> s2 <- timeSeries(data2, charvec2, units = "s")
> merge(s1, s2)
GMTs
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 62009-07-01 72009-08-01 82009-09-01 9
timeSeries() returns a different result. We obtain a univariate series,since both series are from the same information set “s".
Can I merge a time series object with a numeric value?
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"
> const <- 3.4
zoo:
> z <- zoo(data, as.Date(charvec))
> merge(z, const)
z const2009-01-01 1 3.42009-02-01 2 3.42009-03-01 3 3.42009-04-01 4 3.42009-05-01 5 3.42009-06-01 6 3.4
xts:
> x <- xts(data, as.Date(charvec))
> merge(x, const)
x const2009-01-01 1 3.42009-02-01 2 3.42009-03-01 3 3.42009-04-01 4 3.42009-05-01 5 3.42009-06-01 6 3.4
timeSeries:
> s <- timeSeries(data, charvec)
> merge(s, const)
GMTTS.1 s
2009-01-01 1 3.42009-02-01 2 3.42009-03-01 3 3.42009-04-01 4 3.42009-05-01 5 3.42009-06-01 6 3.4
Can I merge a time series object with a numeric vector?
Common Data:
> data <- 1:6> data
[1] 1 2 3 4 5 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"
> vec <- 3.4 - 1:6> vec
[1] 2.4 1.4 0.4 -0.6 -1.6 -2.6
zoo:
> z <- zoo(data, as.Date(charvec))
> merge(z, vec)
z vec2009-01-01 1 2.42009-02-01 2 1.42009-03-01 3 0.42009-04-01 4 -0.62009-05-01 5 -1.62009-06-01 6 -2.6
xts:
> x <- xts(data, as.Date(charvec))
> merge(x, vec)
x vec2009-01-01 1 2.42009-02-01 2 1.42009-03-01 3 0.42009-04-01 4 -0.62009-05-01 5 -1.62009-06-01 6 -2.6
timeSeries:
> s <- timeSeries(data, charvec)
> merge(s, vec)
GMTTS.1 s
2009-01-01 1 2.42009-02-01 2 1.42009-03-01 3 0.42009-04-01 4 -0.62009-05-01 5 -1.62009-06-01 6 -2.6
Can I merge a time series object with a numeric matrix?
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"
> mat <- matrix((1:12) - 6, ncol = 2) - 3.4> mat
[,1] [,2][1,] -8.4 -2.4[2,] -7.4 -1.4[3,] -6.4 -0.4[4,] -5.4 0.6[5,] -4.4 1.6[6,] -3.4 2.6
zoo:
> z <- zoo(data, as.Date(charvec))
> merge(z, mat)
z mat.1 mat.22009-01-01 1 -8.4 -2.42009-02-01 2 -7.4 -1.42009-03-01 3 -6.4 -0.42009-04-01 4 -5.4 0.62009-05-01 5 -4.4 1.62009-06-01 6 -3.4 2.6
xts:
> x <- xts(data, as.Date(charvec))
> merge(x, mat)
x mat mat.12009-01-01 1 -8.4 -2.42009-02-01 2 -7.4 -1.42009-03-01 3 -6.4 -0.42009-04-01 4 -5.4 0.62009-05-01 5 -4.4 1.62009-06-01 6 -3.4 2.6
timeSeries:
> s <- timeSeries(data, charvec)
> merge(s, mat)
GMTTS.1 mat.1 mat.2
2009-01-01 1 -8.4 -2.42009-02-01 2 -7.4 -1.42009-03-01 3 -6.4 -0.42009-04-01 4 -5.4 0.62009-05-01 5 -4.4 1.62009-06-01 6 -3.4 2.6
CHAPTER 6
SUBSETTING TIME SERIES OBJECTS
> library(zoo)> library(xts)> library(timeSeries)
How can I subset a vector and a matrix?
A vector is a linear "object", thus we need only one index to subset it.
> vec <- rnorm(6)> vec
[1] -0.681091 -0.797853 -0.068636 1.194639 0.043844 0.323764
> vec[3:4]
[1] -0.068636 1.194639
A vector has a dimension NULL, and its length is the number of its ele-ments.
> dim(vec)
NULL
> length(vec)
[1] 6
A matrix is a rectangular object which requires a pair of indices to subsetit, one to choose the columns and another one to choose the rows.
> mat <- matrix(rnorm(18), ncol = 3)> mat
50
[,1] [,2] [,3][1,] 0.583904 0.56393 -2.92107[2,] 1.544312 -0.84297 0.49891[3,] -0.070267 -1.91732 1.03852[4,] -1.052401 -0.56104 0.10326[5,] -1.480855 0.99502 -0.46399[6,] 0.432032 0.30460 0.58397
> mat[3:4, ]
[,1] [,2] [,3][1,] -0.070267 -1.91732 1.03852[2,] -1.052401 -0.56104 0.10326
> mat[, 2:3]
[,1] [,2][1,] 0.56393 -2.92107[2,] -0.84297 0.49891[3,] -1.91732 1.03852[4,] -0.56104 0.10326[5,] 0.99502 -0.46399[6,] 0.30460 0.58397
> mat[3:4, 2:3]
[,1] [,2][1,] -1.91732 1.03852[2,] -0.56104 0.10326
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 ofcolumns of the matrix and the function length the total number of ele-ments of the matrix.What happens when we subset a single column or a single row of matrix?
> mat[3, ]
[1] -0.070267 -1.917323 1.038519
> mat[, 2]
[1] 0.56393 -0.84297 -1.91732 -0.56104 0.99502 0.30460
Then the rectangular object becomes linear, the result is a vector. To pre-vent this we have to take care that we do not drop the redundant extentinformation.
> rowmat <- mat[3, , drop = FALSE]> colmat <- mat[, 2, drop = FALSE]
When I am subsetting a time series, does it behave like subsetting a vectorand a matrix?
yes and no, it depends what time series class we use.
Common Data:
> data1 <- rnorm(1:6)> data2 <- matrix(rnorm(18), ncol = 3)> colnames(data2) <- LETTERS[1:3]> 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: A univariate time series
> z <- zoo(data1, as.Date(charvec))> z[3:4]
2009-03-01 2009-04-01-0.45120 -0.65896
A multivariate time series
> Z <- zoo(data2, as.Date(charvec))> Z[3:4, ]
A B C2009-03-01 -1.77775 -1.12348 -0.5530032009-04-01 -0.67286 0.32582 -0.044203
> Z[, 2:3]
B C2009-01-01 -0.56601 1.4577822009-02-01 1.82684 -2.1150772009-03-01 -1.12348 -0.5530032009-04-01 0.32582 -0.0442032009-05-01 -2.02805 -1.0990612009-06-01 -1.27874 0.115879
> Z[3:4, 2:3]
B C2009-03-01 -1.12348 -0.5530032009-04-01 0.32582 -0.044203
> Z[, 2:3]
B C2009-01-01 -0.56601 1.4577822009-02-01 1.82684 -2.1150772009-03-01 -1.12348 -0.5530032009-04-01 0.32582 -0.0442032009-05-01 -2.02805 -1.0990612009-06-01 -1.27874 0.115879
> Z[3:4, 2:3]
B C2009-03-01 -1.12348 -0.5530032009-04-01 0.32582 -0.044203
We keep in mind, a univariate zoo time series object behaves like a vector,and a multivariate zoo time series object behaves like a matrix. Extractingsingle rows or columns drops an index.
xts: A univariate time series
> x <- xts(data1, as.Date(charvec))> x[3:4]
[,1]2009-03-01 -0.451202009-04-01 -0.65896
A multivariate time series
> X <- xts(data2, as.Date(charvec))> X[3:4, ]
A B C2009-03-01 -1.77775 -1.12348 -0.5530032009-04-01 -0.67286 0.32582 -0.044203
> X[, 2:3]
B C2009-01-01 -0.56601 1.4577822009-02-01 1.82684 -2.1150772009-03-01 -1.12348 -0.5530032009-04-01 0.32582 -0.0442032009-05-01 -2.02805 -1.0990612009-06-01 -1.27874 0.115879
> X[3:4, 2:3]
B C2009-03-01 -1.12348 -0.5530032009-04-01 0.32582 -0.044203
> X[, 2:3]
B C2009-01-01 -0.56601 1.4577822009-02-01 1.82684 -2.1150772009-03-01 -1.12348 -0.5530032009-04-01 0.32582 -0.0442032009-05-01 -2.02805 -1.0990612009-06-01 -1.27874 0.115879
> X[3:4, 2:3]
B C2009-03-01 -1.12348 -0.5530032009-04-01 0.32582 -0.044203
Time series objects in xts are always consider as rectangular objects evenin the univariate case. This uniqueness is introduced to make life easierfor subsetting financial time series
timeSeries: A univariate time series
> s <- timeSeries(data1, charvec)> s[3:4]
[1] -0.45120 -0.65896
> s[3:4, ]
GMTTS.1
2009-03-01 -0.451202009-04-01 -0.65896
A multivariate time series
> S <- timeSeries(data2, charvec)> S[3:4, ]
GMTA B C
2009-03-01 -1.77775 -1.12348 -0.5530032009-04-01 -0.67286 0.32582 -0.044203
> S[, 2:3]
GMTB C
2009-01-01 -0.56601 1.4577822009-02-01 1.82684 -2.1150772009-03-01 -1.12348 -0.5530032009-04-01 0.32582 -0.0442032009-05-01 -2.02805 -1.0990612009-06-01 -1.27874 0.115879
> S[3:4, 2:3]
GMTB C
2009-03-01 -1.12348 -0.5530032009-04-01 0.32582 -0.044203
Note that a timeSeries object has always to be subsetted by a pair ofindices. This a timeSeries feature not a bug.)
> S[, 2:3]
GMTB C
2009-01-01 -0.56601 1.4577822009-02-01 1.82684 -2.1150772009-03-01 -1.12348 -0.5530032009-04-01 0.32582 -0.0442032009-05-01 -2.02805 -1.0990612009-06-01 -1.27874 0.115879
> S[3:4, 2:3]
GMTB C
2009-03-01 -1.12348 -0.5530032009-04-01 0.32582 -0.044203
timeSeries objects are like xts objects rectangular objects. The behavein the same way.
Can I subset a multivariate time series by column names?
Common Data:
> data <- matrix(rnorm(18), ncol = 3)> colnames(data) <- LETTERS[1:3]> 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))> Z[, "A"]
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-011.29664 -0.39167 0.22615 0.39817 -3.31007 0.45847
xts:
> X <- xts(data, as.Date(charvec))> X[, "A"]
A2009-01-01 1.296642009-02-01 -0.391672009-03-01 0.226152009-04-01 0.398172009-05-01 -3.310072009-06-01 0.45847
timeSeries:
> S <- timeSeries(data, charvec)> S[, "A"]
GMTA
2009-01-01 1.296642009-02-01 -0.391672009-03-01 0.226152009-04-01 0.398172009-05-01 -3.310072009-06-01 0.45847
Can I subset a multivariate time series by column using the $operator?
Common Data:
> data <- matrix(rnorm(18), ncol = 3)> colnames(data) <- LETTERS[1:3]> 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))> Z$B
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.16881 0.89181 0.45846 0.16604 -1.18825 1.27125
xts:
> X <- xts(data, as.Date(charvec))> X$B
B2009-01-01 0.168812009-02-01 0.891812009-03-01 0.458462009-04-01 0.166042009-05-01 -1.188252009-06-01 1.27125
timeSeries:
> S <- timeSeries(data, charvec)> S$B
[1] 0.16881 0.89181 0.45846 0.16604 -1.18825 1.27125
Can I subset a multivariate time series by character time stamps?
Common Data:
> data <- matrix(rnorm(18), ncol = 3)> colnames(data) <- LETTERS[1:3]> 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))> charvec[3:4]
[1] "2009-03-01" "2009-04-01"
> Z[charvec[3:4], ]
A B C
xts:
> X <- xts(data, as.Date(charvec))> charvec[3:4]
[1] "2009-03-01" "2009-04-01"
> X[charvec[3:4], ]
A B C2009-03-01 -1.99563 -0.052661 -0.323042009-04-01 -0.54409 -0.522312 1.55504
timeSeries:
> S <- timeSeries(data, charvec)> charvec[3:4]
[1] "2009-03-01" "2009-04-01"
> S[charvec[3:4], ]
GMTA B C
2009-03-01 -1.99563 -0.052661 -0.323042009-04-01 -0.54409 -0.522312 1.55504
Can I subset a multivariate time series by its intrinsic time objects?
Common Data:
> data <- matrix(rnorm(18), ncol = 3)> colnames(data) <- LETTERS[1:3]> 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))> timeStamp <- index(Z)[3:4]> timeStamp
[1] "2009-03-01" "2009-04-01"
> Z[timeStamp, ]
A B C2009-03-01 -0.46300 -0.27254 0.321332009-04-01 0.99523 -1.71927 -0.22107
xts:
> X <- xts(data, as.Date(charvec))> timeStamp <- index(X)[3:4]> timeStamp
[1] "2009-03-01" "2009-04-01"
> X[timeStamp, ]
A B C2009-03-01 -0.46300 -0.27254 0.321332009-04-01 0.99523 -1.71927 -0.22107
timeSeries:
> S <- timeSeries(data, charvec)> timeStamp <- time(S)[3:4]> timeStamp
GMT[1] [2009-03-01] [2009-04-01]
> S[timeStamp, ]
GMTA B C
2009-03-01 -0.46300 -0.27254 0.321332009-04-01 0.99523 -1.71927 -0.22107
Can I subset a multivariate time series by logical predicates?
Common Data:
> data <- matrix(rnorm(18), ncol = 3)> colnames(data) <- LETTERS[1:3]> 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))> timeStamp <- index(Z) > index(Z)[3]> timeStamp
[1] FALSE FALSE FALSE TRUE TRUE TRUE
> Z[timeStamp, ]
A B C2009-04-01 0.80519 -0.85381 -0.410292009-05-01 0.88327 -0.42062 0.021672009-06-01 0.52345 0.43270 -1.16985
xts:
> X <- xts(data, as.Date(charvec))> timeStamp <- index(X) > index(X)[3]> timeStamp
[1] FALSE FALSE FALSE TRUE TRUE TRUE
> X[timeStamp, ]
A B C2009-04-01 0.80519 -0.85381 -0.410292009-05-01 0.88327 -0.42062 0.021672009-06-01 0.52345 0.43270 -1.16985
timeSeries:
> S <- timeSeries(data, charvec)> timeStamp <- time(S) > time(S)[3]> timeStamp
[1] FALSE FALSE FALSE TRUE TRUE TRUE
> S[timeStamp, ]
GMTA B C
2009-04-01 0.80519 -0.85381 -0.410292009-05-01 0.88327 -0.42062 0.021672009-06-01 0.52345 0.43270 -1.16985
How to extract the start and end date of a series?
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))> c(start(x), end(x))
[1] "2009-01-01" "2009-06-01"
xts:
> x <- xts(data, as.Date(charvec))> c(start(x), end(x))
[1] "2009-01-01" "2009-06-01"
timeSeries:
> s <- timeSeries(data, charvec)> c(start(s), end(s))
GMT[1] [2009-01-01] [2009-06-01]
Since timeSeries also support non-sorted time stamps keep in mind,that the first and last entry of the series ar not necessarily the start andend values, see
> s <- timeSeries(data, sample(charvec))> c(start(s), end(s))
GMT[1] [2009-01-01] [2009-06-01]
> c(time(s[1, ]), time(s[6, ]))
GMT[1] [2009-06-01] [2009-03-01]
CHAPTER 7
GROUP GENERICS FOR TIME SERIES OBJECTS
> library(zoo)> library(xts)> library(timeSeries)
First inspect the help pages for groupGeneric() in R’s base package andthe help page for groupGeneric() in R’s methods packageS3 Group Generic Functions:Group generic methods can be defined for four pre-specified groups offunctions, Math, Ops, Summary and Complex.
• Math(x, ...)
• Ops(e1, e2)
• Complex(z)
• Summary(..., na.rm = FALSE)
S4 Group Generic Functions:Group generic methods can be defined for four pre-specified groupsof functions, textttArith, textttCompare, Ops, Logic, Math, Math2, Sumary,and Complex.
• Arith(e1, e2)
• Compare(e1, e2)
• Ops(e1, e2)
• Logic(e1, e2)
• Math(x)
• Math2(x, digits)
61
• Summary(x, ..., na.rm = FALSE)
• Complex(z)
In the following we test some selected functions which are relevant foruse in financial time series analysis. We test them on multivariate timeseries objects
Common Data:
> data <- matrix(runif(18), ncol = 3)> 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))> colnames(Z) <- paste("Z", 1:3, sep = ".")> Z
Z.1 Z.2 Z.32009-01-01 0.369564 0.055042 0.788582009-02-01 0.638691 0.980744 0.968202009-03-01 0.049595 0.134349 0.440272009-04-01 0.538985 0.762447 0.948802009-05-01 0.776208 0.821583 0.879402009-06-01 0.045961 0.519558 0.52079
xts:
> X <- xts(data, as.Date(charvec))> colnames(X) <- paste("X", 1:3, sep = ".")> X
X.1 X.2 X.32009-01-01 0.369564 0.055042 0.788582009-02-01 0.638691 0.980744 0.968202009-03-01 0.049595 0.134349 0.440272009-04-01 0.538985 0.762447 0.948802009-05-01 0.776208 0.821583 0.879402009-06-01 0.045961 0.519558 0.52079
timeSeries:
> S <- timeSeries(data, charvec)> colnames(S) <- paste("S", 1:3, sep = ".")> S
GMTS.1 S.2 S.3
2009-01-01 0.369564 0.055042 0.788582009-02-01 0.638691 0.980744 0.96820
2009-03-01 0.049595 0.134349 0.440272009-04-01 0.538985 0.762447 0.948802009-05-01 0.776208 0.821583 0.879402009-06-01 0.045961 0.519558 0.52079
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-010.369564 0.638691 0.049595 0.538985 0.776208 0.045961
xts:
> x <- X[, 1]> x
X.12009-01-01 0.3695642009-02-01 0.6386912009-03-01 0.0495952009-04-01 0.5389852009-05-01 0.7762082009-06-01 0.045961
timeSeries:
> s <- S[, 1]> s
GMTS.1
2009-01-01 0.3695642009-02-01 0.6386912009-03-01 0.0495952009-04-01 0.5389852009-05-01 0.7762082009-06-01 0.045961
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-010.42461 1.61944 0.18394 1.30143 1.59779 0.56552
xts:
> X[, 1] + X[, 2]
X.12009-01-01 0.424612009-02-01 1.619442009-03-01 0.183942009-04-01 1.301432009-05-01 1.597792009-06-01 0.56552
timeSeries:
> S[, 1] + S[, 2]
GMTS.1
2009-01-01 0.424612009-02-01 1.619442009-03-01 0.183942009-04-01 1.301432009-05-01 1.597792009-06-01 0.56552
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-01TRUE FALSE FALSE FALSE FALSE FALSE
xts:
> X[, 1] > X[, 2]
X.12009-01-01 TRUE2009-02-01 FALSE2009-03-01 FALSE2009-04-01 FALSE2009-05-01 FALSE2009-06-01 FALSE
timeSeries:
> S[, 1] > S[, 2]
GMTS.1
2009-01-01 TRUE2009-02-01 FALSE2009-03-01 FALSE2009-04-01 FALSE2009-05-01 FALSE2009-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-01TRUE FALSE FALSE FALSE FALSE FALSE
xts:
> (X[, 1] > X[, 2]) & (X[, 2] < X[, 3])
X.12009-01-01 TRUE2009-02-01 FALSE2009-03-01 FALSE2009-04-01 FALSE2009-05-01 FALSE2009-06-01 FALSE
timeSeries:
> (S[, 1] > S[, 2]) & (S[, 2] < S[, 3])
GMTS.1
2009-01-01 TRUE2009-02-01 FALSE2009-03-01 FALSE2009-04-01 FALSE2009-05-01 FALSE2009-06-01 FALSE
Which "Ops" operations are supported by time series objects?
Ops:
"Arith", "Compare", "Logic"
Which "Math" operations are supported by time series objects?
Math:
"abs", "sign", "sqrt", "ceiling", "floor", "trunc","cummax", "cummin", "cumprod", "cumsum", "log", "log10","log2", "log1p", "acos", "acosh", "asin", "asinh", "atan","atanh", "exp", "expm1", "cos", "cosh", "sin", "sinh","tan", "tanh", "gamma", "lgamma", "digamma", "trigamma"
e.g.
zoo:
> log(abs(Z))
Z.1 Z.2 Z.32009-01-01 -0.99543 -2.899665 -0.2375232009-02-01 -0.44833 -0.019443 -0.0323202009-03-01 -3.00387 -2.007313 -0.8203632009-04-01 -0.61807 -0.271222 -0.0525582009-05-01 -0.25334 -0.196523 -0.1285112009-06-01 -3.07996 -0.654777 -0.652412
xts:
> log(abs(X))
X.1 X.2 X.32009-01-01 -0.99543 -2.899665 -0.2375232009-02-01 -0.44833 -0.019443 -0.0323202009-03-01 -3.00387 -2.007313 -0.8203632009-04-01 -0.61807 -0.271222 -0.0525582009-05-01 -0.25334 -0.196523 -0.1285112009-06-01 -3.07996 -0.654777 -0.652412
timeSeries:
> log(abs(S))
GMTS.1 S.2 S.3
2009-01-01 -0.99543 -2.899665 -0.2375232009-02-01 -0.44833 -0.019443 -0.0323202009-03-01 -3.00387 -2.007313 -0.8203632009-04-01 -0.61807 -0.271222 -0.0525582009-05-01 -0.25334 -0.196523 -0.1285112009-06-01 -3.07996 -0.654777 -0.652412
Which "Math2" operations are supported by time series objects?
Math2:
"round", "signif"
e.g.
zoo:
> round(Z, 2)
Z.1 Z.2 Z.32009-01-01 0.37 0.06 0.792009-02-01 0.64 0.98 0.972009-03-01 0.05 0.13 0.442009-04-01 0.54 0.76 0.952009-05-01 0.78 0.82 0.882009-06-01 0.05 0.52 0.52
> signif(Z, 2)
Z.1 Z.2 Z.32009-01-01 0.370 0.055 0.792009-02-01 0.640 0.980 0.972009-03-01 0.050 0.130 0.442009-04-01 0.540 0.760 0.952009-05-01 0.780 0.820 0.882009-06-01 0.046 0.520 0.52
xts:
> round(X, 2)
X.1 X.2 X.32009-01-01 0.37 0.06 0.792009-02-01 0.64 0.98 0.972009-03-01 0.05 0.13 0.442009-04-01 0.54 0.76 0.952009-05-01 0.78 0.82 0.882009-06-01 0.05 0.52 0.52
> signif(X, 2)
X.1 X.2 X.32009-01-01 0.370 0.055 0.792009-02-01 0.640 0.980 0.972009-03-01 0.050 0.130 0.442009-04-01 0.540 0.760 0.952009-05-01 0.780 0.820 0.882009-06-01 0.046 0.520 0.52
timeSeries:
> round(S, 2)
GMTS.1 S.2 S.3
2009-01-01 0.37 0.06 0.792009-02-01 0.64 0.98 0.972009-03-01 0.05 0.13 0.442009-04-01 0.54 0.76 0.952009-05-01 0.78 0.82 0.882009-06-01 0.05 0.52 0.52
> signif(S, 2)
GMTS.1 S.2 S.3
2009-01-01 0.370 0.055 0.792009-02-01 0.640 0.980 0.972009-03-01 0.050 0.130 0.442009-04-01 0.540 0.760 0.952009-05-01 0.780 0.820 0.882009-06-01 0.046 0.520 0.52
Which "Summary" operations are supported by time series objects?
Summary:
"max", "min", "range", "prod", "sum", "any", "all"
e.g.
zoo:
> max(z)
[1] 0.7762
> min(z)
[1] 0.045961
> range(z)
[1] 0.045961 0.776208
> prod(z)
[1] 0.00022509
> sum(z)
[1] 2.419
xts:
> max(x)
[1] 0.7762
> min(x)
[1] 0.045961
> range(x)
[1] 0.045961 0.776208
> prod(x)
[1] 0.00022509
> sum(x)
[1] 2.419
timeSeries:
> max(s)
[1] 0.7762
> min(s)
[1] 0.045961
> range(s)
[1] 0.045961 0.776208
> prod(s)
[1] 0.00022509
> sum(s)
[1] 2.419
Which "Complex" operations are supported by time series objects?
Complex:
"Arg", "Conj", "Im", "Mod", "Re"
e.g. with
> u <- c(0, 0+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-010.0000 1.2617 0.0000 1.3076 0.0000 1.5478
> Conj(z + u)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.369564+0i 0.638691-2i 0.049595+0i 0.538985-2i 0.776208+0i 0.045961-2i
> Im(z + u)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010 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-010.369564 2.099506 0.049595 2.071354 0.776208 2.000528
> Re(z + u)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.369564 0.638691 0.049595 0.538985 0.776208 0.045961
xts:
> Arg(x + u)
X.12009-01-01 0.00002009-02-01 1.26172009-03-01 0.00002009-04-01 1.30762009-05-01 0.00002009-06-01 1.5478
> Conj(x + u)
X.12009-01-01 0.369564+0i2009-02-01 0.638691-2i2009-03-01 0.049595+0i2009-04-01 0.538985-2i2009-05-01 0.776208+0i2009-06-01 0.045961-2i
> Im(x + u)
X.12009-01-01 02009-02-01 22009-03-01 02009-04-01 22009-05-01 02009-06-01 2
> Mod(x + u)
X.12009-01-01 0.3695642009-02-01 2.0995062009-03-01 0.0495952009-04-01 2.0713542009-05-01 0.7762082009-06-01 2.000528
> Re(x + u)
X.12009-01-01 0.3695642009-02-01 0.6386912009-03-01 0.0495952009-04-01 0.5389852009-05-01 0.7762082009-06-01 0.045961
timeSeries:
> Arg(s + u)
GMTS.1
2009-01-01 0.00002009-02-01 1.26172009-03-01 0.00002009-04-01 1.30762009-05-01 0.00002009-06-01 1.5478
> Conj(s + u)
GMTS.1
2009-01-01 0.369564+0i2009-02-01 0.638691-2i2009-03-01 0.049595+0i2009-04-01 0.538985-2i2009-05-01 0.776208+0i2009-06-01 0.045961-2i
> Im(s + u)
GMTS.1
2009-01-01 02009-02-01 22009-03-01 02009-04-01 22009-05-01 02009-06-01 2
> Mod(s + u)
GMTS.1
2009-01-01 0.3695642009-02-01 2.099506
2009-03-01 0.0495952009-04-01 2.0713542009-05-01 0.7762082009-06-01 2.000528
> Re(s + u)
GMTS.1
2009-01-01 0.3695642009-02-01 0.6386912009-03-01 0.0495952009-04-01 0.5389852009-05-01 0.7762082009-06-01 0.045961
CHAPTER 8
MISSING DATA HANDLING
> 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. By default we mean thatwe remove the record wit NAs from the data set. Later we will extend thismeaning by replacing and/or interpolating missing values in a time seriesobject. Now let us remove NAs from a time series.
Common Data:
> data <- rnorm(9)> data[c(1, 7)] <- NA> data
[1] NA 0.195236 1.244563 0.018836 -0.184340 0.169355 NA[8] 0.099439 0.904254
> charvec <- paste("2009-0", 1:9, "-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"
zoo:
> z <- zoo(data, as.Date(charvec))> na.omit(z)
2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-08-01 2009-09-010.195236 1.244563 0.018836 -0.184340 0.169355 0.099439 0.904254
73
xts:
> x <- xts(data, as.Date(charvec))> na.omit(x)
[,1]2009-02-01 0.1952362009-03-01 1.2445632009-04-01 0.0188362009-05-01 -0.1843402009-06-01 0.1693552009-08-01 0.0994392009-09-01 0.904254
timeSeries:
> s <- timeSeries(data, charvec)> na.omit(s)
GMTTS.1
2009-02-01 0.1952362009-03-01 1.2445632009-04-01 0.0188362009-05-01 -0.1843402009-06-01 0.1693552009-08-01 0.0994392009-09-01 0.904254
How can I omit missing values in a multivariate time series?
Common Data:
> data <- matrix(rnorm(7 * 3), ncol = 3)> data[1, 1] <- NA> data[3, 1:2] <- NA> data[4, 2] <- NA> data
[,1] [,2] [,3][1,] NA -0.68610 -0.289555[2,] 2.45684 1.13952 -0.653460[3,] NA NA 0.422332[4,] 0.37813 NA 0.201370[5,] 0.11385 1.87744 1.448888[6,] 1.33599 0.49034 0.179034[7,] 0.54805 -1.64434 -0.081939
> charvec <- paste("2009-0", 1:7, "-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"
zoo:
> Z <- zoo(data, as.Date(charvec))> na.omit(Z)
2009-02-01 2.45684 1.13952 -0.6534602009-05-01 0.11385 1.87744 1.4488882009-06-01 1.33599 0.49034 0.1790342009-07-01 0.54805 -1.64434 -0.081939attr(,"na.action")[1] 1 3 4attr(,"class")[1] "omit"
xts:
> X <- xts(data, as.Date(charvec))> na.omit(X)
[,1] [,2] [,3]2009-02-01 2.45684 1.13952 -0.6534602009-05-01 0.11385 1.87744 1.4488882009-06-01 1.33599 0.49034 0.1790342009-07-01 0.54805 -1.64434 -0.081939
timeSeries:
> s <- timeSeries(data, charvec)> na.omit(s)
GMTTS.1 TS.2 TS.3
2009-02-01 2.45684 1.13952 -0.6534602009-05-01 0.11385 1.87744 1.4488882009-06-01 1.33599 0.49034 0.1790342009-07-01 0.54805 -1.64434 -0.081939
How can I replace missing values in a univariate time series for example byzeros, the mean or the median object?
Common Data:
> data <- rnorm(9)> data[c(1, 7)] <- NA> charvec <- paste("2009-0", 1:9, "-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"
It may be of interest to replace missing values in a financial return seriesby a constant value, e.g. zero, the mean, or the median.
zoo: Replace for example missing values in a series of financial returnsby zero values.
> z <- zoo(data, as.Date(charvec))> z[is.na(z)] <- 0> z
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-010.00000 0.54898 0.66806 -1.62037 0.95402 -1.93451 0.00000
2009-08-01 2009-09-01-0.95558 -0.86442
Or Replace for example missing values in a series of financial returns bytheir sample mean.
> z <- zoo(data, as.Date(charvec))> z[is.na(z)] <- mean(z, na.rm = TRUE)> z
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-01-0.45769 0.54898 0.66806 -1.62037 0.95402 -1.93451 -0.45769
2009-08-01 2009-09-01-0.95558 -0.86442
Or Replace for example missing values in a series of financial returns by arobust estimate of the mean, using the median.
> z <- zoo(data, as.Date(charvec))> z[is.na(z)] <- median(z, na.rm = TRUE)> z
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-010.95402 0.54898 0.66806 -1.62037 0.95402 -1.93451 0.95402
2009-08-01 2009-09-01-0.95558 -0.86442
xts:
> x <- xts(data, as.Date(charvec))> x[is.na(x)] <- 0> x
[,1]2009-01-01 0.000002009-02-01 0.548982009-03-01 0.668062009-04-01 -1.620372009-05-01 0.954022009-06-01 -1.934512009-07-01 0.000002009-08-01 -0.955582009-09-01 -0.86442
> x <- xts(data, as.Date(charvec))> x[is.na(x)] <- mean(x, na.rm = TRUE)> x
[,1]2009-01-01 -0.457692009-02-01 0.548982009-03-01 0.668062009-04-01 -1.620372009-05-01 0.954022009-06-01 -1.934512009-07-01 -0.457692009-08-01 -0.955582009-09-01 -0.86442
> x <- xts(data, as.Date(charvec))> x[is.na(x)] <- median(x, na.rm = TRUE)> x
[,1]2009-01-01 0.954022009-02-01 0.548982009-03-01 0.668062009-04-01 -1.620372009-05-01 0.954022009-06-01 -1.934512009-07-01 0.954022009-08-01 -0.955582009-09-01 -0.86442
timeSeries:
> s <- timeSeries(data, charvec)> s[is.na(s)] <- 0> s
GMTTS.1
2009-01-01 0.000002009-02-01 0.548982009-03-01 0.668062009-04-01 -1.620372009-05-01 0.954022009-06-01 -1.934512009-07-01 0.000002009-08-01 -0.955582009-09-01 -0.86442
> s <- timeSeries(data, charvec)> s[is.na(s)] <- mean(s, na.rm = TRUE)> s
GMTTS.1
2009-01-01 -0.457692009-02-01 0.548982009-03-01 0.668062009-04-01 -1.620372009-05-01 0.95402
2009-06-01 -1.934512009-07-01 -0.457692009-08-01 -0.955582009-09-01 -0.86442
> s <- timeSeries(data, charvec)> s[is.na(s)] <- median(s, na.rm = TRUE)> s
GMTTS.1
2009-01-01 -0.864422009-02-01 0.548982009-03-01 0.668062009-04-01 -1.620372009-05-01 0.954022009-06-01 -1.934512009-07-01 -0.864422009-08-01 -0.955582009-09-01 -0.86442
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 financialreturns by a constant value, e.g. zero, the (column) mean, or the robust(column) median.
Common Data:
> data <- matrix(rnorm(7 * 3), ncol = 3)> data[1, 1] <- NA> data[3, 1:2] <- NA> data[4, 2] <- NA> data
[,1] [,2] [,3][1,] NA -0.985451 2.44375[2,] -0.7841994 -1.099405 -0.91241[3,] NA NA 0.27138[4,] 0.0503734 NA 1.22139[5,] -0.0059894 -0.040495 -0.49409[6,] -0.2791597 1.990596 1.27270[7,] -0.3769113 -1.103665 -0.33297
> charvec <- paste("2009-0", 1:7, "-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"
zoo:
> Z <- zoo(data, as.Date(charvec))> Z
2009-01-01 NA -0.985451 2.443752009-02-01 -0.7841994 -1.099405 -0.912412009-03-01 NA NA 0.271382009-04-01 0.0503734 NA 1.221392009-05-01 -0.0059894 -0.040495 -0.494092009-06-01 -0.2791597 1.990596 1.272702009-07-01 -0.3769113 -1.103665 -0.33297
> Z[is.na(Z)] <- 0> Z
2009-01-01 0.0000000 -0.985451 2.443752009-02-01 -0.7841994 -1.099405 -0.912412009-03-01 0.0000000 0.000000 0.271382009-04-01 0.0503734 0.000000 1.221392009-05-01 -0.0059894 -0.040495 -0.494092009-06-01 -0.2791597 1.990596 1.272702009-07-01 -0.3769113 -1.103665 -0.33297
xts:
> X <- xts(data, as.Date(charvec))> X
[,1] [,2] [,3]2009-01-01 NA -0.985451 2.443752009-02-01 -0.7841994 -1.099405 -0.912412009-03-01 NA NA 0.271382009-04-01 0.0503734 NA 1.221392009-05-01 -0.0059894 -0.040495 -0.494092009-06-01 -0.2791597 1.990596 1.272702009-07-01 -0.3769113 -1.103665 -0.33297
> X[is.na(X)] <- 0> X
[,1] [,2] [,3]2009-01-01 0.0000000 -0.985451 2.443752009-02-01 -0.7841994 -1.099405 -0.912412009-03-01 0.0000000 0.000000 0.271382009-04-01 0.0503734 0.000000 1.221392009-05-01 -0.0059894 -0.040495 -0.494092009-06-01 -0.2791597 1.990596 1.272702009-07-01 -0.3769113 -1.103665 -0.33297
timeSeries:
> S <- timeSeries(data, as.Date(charvec))> S
GMTTS.1 TS.2 TS.3
2009-01-01 NA -0.985451 2.443752009-02-01 -0.7841994 -1.099405 -0.91241
2009-03-01 NA NA 0.271382009-04-01 0.0503734 NA 1.221392009-05-01 -0.0059894 -0.040495 -0.494092009-06-01 -0.2791597 1.990596 1.272702009-07-01 -0.3769113 -1.103665 -0.33297
> S[is.na(S)] <- 0> S
GMTTS.1 TS.2 TS.3
2009-01-01 0.0000000 -0.985451 2.443752009-02-01 -0.7841994 -1.099405 -0.912412009-03-01 0.0000000 0.000000 0.271382009-04-01 0.0503734 0.000000 1.221392009-05-01 -0.0059894 -0.040495 -0.494092009-06-01 -0.2791597 1.990596 1.272702009-07-01 -0.3769113 -1.103665 -0.33297
How can I interpolate missing values in a univariate time series object?
For this zoo and xts have the functions na.approx and na.spline, time-Series has the function na.omit with the argument method for selectinginterpolation.
Common Data:
> data <- 1:9> data[c(1, 7)] <- NA> data
[1] NA 2 3 4 5 6 NA 8 9
> charvec <- paste("2009-0", 1:9, "-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"
zoo: Linear Interpolation using the function approx()
> z <- zoo(data, as.Date(charvec))> z
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-01NA 2 3 4 5 6 NA
2009-08-01 2009-09-018 9
> na.approx(z)
2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-01 2009-08-012.0000 3.0000 4.0000 5.0000 6.0000 6.9836 8.0000
2009-09-019.0000
Spline Interpolation using the function spline()
> z
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-01NA 2 3 4 5 6 NA
2009-08-01 2009-09-018 9
> na.spline(z)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-010.63057 2.00000 3.00000 4.00000 5.00000 6.00000 6.97966
2009-08-01 2009-09-018.00000 9.00000
Last Observation Carried Forward
> z
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-01NA 2 3 4 5 6 NA
2009-08-01 2009-09-018 9
> na.locf(z)
2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-01 2009-08-012 3 4 5 6 6 8
2009-09-019
Trim Leading/Trailing Missing Observations
> z
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-01NA 2 3 4 5 6 NA
2009-08-01 2009-09-018 9
> na.trim(z, sides = "left")
2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-01 2009-08-012 3 4 5 6 NA 8
2009-09-019
xts:
> xts <- xts(data, as.Date(charvec))> xts
[,1]2009-01-01 NA2009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 62009-07-01 NA2009-08-01 82009-09-01 9
> na.approx(xts)
[,1]2009-02-01 2.00002009-03-01 3.00002009-04-01 4.00002009-05-01 5.00002009-06-01 6.00002009-07-01 6.98362009-08-01 8.00002009-09-01 9.0000
> x
[,1]2009-01-01 0.954022009-02-01 0.548982009-03-01 0.668062009-04-01 -1.620372009-05-01 0.954022009-06-01 -1.934512009-07-01 0.954022009-08-01 -0.955582009-09-01 -0.86442
> na.spline(x)
[,1]2009-01-01 0.954022009-02-01 0.548982009-03-01 0.668062009-04-01 -1.620372009-05-01 0.954022009-06-01 -1.934512009-07-01 0.954022009-08-01 -0.955582009-09-01 -0.86442
Last Observation Carried Forward
> x
[,1]2009-01-01 0.954022009-02-01 0.548982009-03-01 0.668062009-04-01 -1.620372009-05-01 0.954022009-06-01 -1.934512009-07-01 0.954022009-08-01 -0.955582009-09-01 -0.86442
> na.locf(x)
[,1]2009-01-01 0.954022009-02-01 0.548982009-03-01 0.66806
2009-04-01 -1.620372009-05-01 0.954022009-06-01 -1.934512009-07-01 0.954022009-08-01 -0.955582009-09-01 -0.86442
Trim Leading/Trailing Missing Observations
> x
[,1]2009-01-01 0.954022009-02-01 0.548982009-03-01 0.668062009-04-01 -1.620372009-05-01 0.954022009-06-01 -1.934512009-07-01 0.954022009-08-01 -0.955582009-09-01 -0.86442
> na.trim(x, sides = "left")
[,1]2009-01-01 0.954022009-02-01 0.548982009-03-01 0.668062009-04-01 -1.620372009-05-01 0.954022009-06-01 -1.934512009-07-01 0.954022009-08-01 -0.955582009-09-01 -0.86442
timeSeries: Interpolation of time series is done by calling internally thefunction approx whoch provides linear interpolation.Interpolate and remove (trim) NAs at the beginning and end of the series
> s <- timeSeries(data, charvec)> na.omit(s, "ir")
GMTTS.1
2009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 62009-07-01 62009-08-01 82009-09-01 9
Interpolate and replace NAs at the beginning and end of the series withzeros
> s <- timeSeries(data, charvec)> na.omit(s, "iz")
GMTTS.1
2009-01-01 02009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 62009-07-01 62009-08-01 82009-09-01 9
Interpolate and extrapolate NAs at the beginning and end of the series
> s <- timeSeries(data, charvec)> na.omit(s, "ie")
GMTTS.1
2009-01-01 22009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 62009-07-01 62009-08-01 82009-09-01 9
Through the argument interp=c("before", "linear", "after") wecan select how to interpolate. In the case of "before" we carry the lastobservation forward (see zoo’s na.locf), and in the case of "after" wecarry the next observation backward.
> sn <- na.omit(s, method = "iz", interp = "before")> sn[is.na(s)]
[1] 0 6
> sn <- na.omit(s, method = "iz", interp = "linear")> sn[is.na(s)]
[1] 0 7
> sn <- na.omit(s, method = "iz", interp = "after")> sn[is.na(s)]
[1] 0 8
Note that the way how to perform the linear interpolation can be modifiedby changing the defaults settings of the arguments in the function approx.
> args(approx)
function (x, y = NULL, xout, method = "linear", n = 50, yleft,yright, rule = 1, f = 0, ties = mean)
NULL
Does the function na.contiguous() work for a univariate time seriesobjects?
The function na.contiguous() finds the longest consecutive stretch ofnon-missing values in a time series object. Note that in the event of a tie,this will be the first such stretch.
Common Data:
> data <- rnorm(12)> data[c(3, 8)] = NA> data
[1] 0.58975 -0.95992 NA 0.10815 1.57763 0.71092 -0.43372 NA[9] -0.32635 0.28415 0.91288 1.40203
> charvec <- as.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 <- ts(data, start = 2009, frequency = 12)> t
Jan Feb Mar Apr May Jun Jul Aug2009 0.58975 -0.95992 NA 0.10815 1.57763 0.71092 -0.43372 NA
Sep Oct Nov Dec2009 -0.32635 0.28415 0.91288 1.40203
> na.contiguous(t)
Apr May Jun Jul2009 0.10815 1.57763 0.71092 -0.43372
zoo:
> z <- zoo(data, as.Date(charvec))> z
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 2009-07-010.58975 -0.95992 NA 0.10815 1.57763 0.71092 -0.43372
2009-08-01 2009-09-01 2009-10-01 2009-11-01 2009-12-01NA -0.32635 0.28415 0.91288 1.40203
> na.contiguous(z)
2009-04-01 2009-05-01 2009-06-01 2009-07-010.10815 1.57763 0.71092 -0.43372
xts:
> x <- xts(data, as.Date(charvec))> x
[,1]2009-01-01 0.589752009-02-01 -0.959922009-03-01 NA2009-04-01 0.108152009-05-01 1.577632009-06-01 0.710922009-07-01 -0.433722009-08-01 NA2009-09-01 -0.326352009-10-01 0.284152009-11-01 0.912882009-12-01 1.40203
> na.contiguous(x)
[,1]2009-04-01 0.108152009-05-01 1.577632009-06-01 0.710922009-07-01 -0.43372
timeSeries:
> s <- timeSeries(data, charvec)> s
GMTTS.1
2009-01-01 0.589752009-02-01 -0.959922009-03-01 NA2009-04-01 0.108152009-05-01 1.577632009-06-01 0.710922009-07-01 -0.433722009-08-01 NA2009-09-01 -0.326352009-10-01 0.284152009-11-01 0.912882009-12-01 1.40203
> na.contiguous(s)
GMTTS.1
2009-04-01 0.108152009-05-01 1.577632009-06-01 0.710922009-07-01 -0.43372
PART II
REPORTING
87
CHAPTER 9
PRINTING TIME SERIES OBJECTS
> 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 inR
> 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:
> zoo(data, as.Date(charvec))
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-011 2 3 4 5 6
xts:
> xts(data, as.Date(charvec))
[,1]2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
88
timeSeries:
> timeSeries(data, charvec)
GMTTS.1
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
Alternatively we can use explicitely the generic print function print()
> 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:
> print(zoo(data, as.Date(charvec)))
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-011 2 3 4 5 6
xts:
> print(xts(data, as.Date(charvec)))
[,1]2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
timeSeries:
> print(timeSeries(data, charvec))
GMTTS.1
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
Since a timeSeries object is represented by a S4 class we can also use
> show(timeSeries(data, charvec))
GMTTS.1
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
How can I select the style for printing an univariate times series?
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: Using the print function univariate zoo time series are alwaysdisplayed in an horizontal style.
> print(zoo(data, as.Date(charvec)))
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-011 2 3 4 5 6
xts: Using the print function univariate xts time series are always dis-played in an vertical style.
> print(xts(data, as.Date(charvec)))
[,1]2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
If you want to have printed zoo objects vertically and xts horizonticallytype
> print(zoo(data, as.Date(charvec)), "vertical")
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
> print(as.zoo(xts(data, as.Date(charvec))))
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
timeSeries: The timesSeries class comes with a generic print functionwhich allows to customize the printout in several ways. For example anunivariate timeSeries object is printed by default in vertical style
> s <- timeSeries(data, charvec)> print(s)
GMTTS.1
2009-01-01 12009-02-01 22009-03-01 32009-04-01 42009-05-01 52009-06-01 6
but it can also be printed in horizontal style
> print(s, style = "h")
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-011 2 3 4 5 6
Is it possible to display beside the ISO date/time format other formatswhen we print a a time series ?
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: To our knowledge, no.
xts: To our knowledge, no.
timeSeries: The generic print function for the timeSeries class allowsto customize the printing format. Here are some examples
> s <- timeSeries(pi, "2009-05-08 19:26:22", units = "s")> print(s)
GMTs
2009-05-08 19:26:22 3.1416
> print(s, format = "%m/%d/%y %H:%M")
GMTs
05/08/09 19:26 3.1416
> print(s, format = "%m/%d/%y %A")
GMTs
05/08/09 Friday 3.1416
> print(s, format = "DayNo %j Hour: %H")
GMTs
DayNo 128 Hour: 19 3.1416
The first example prints in default ISO format, the second in month-day-year format with the full name of the weekday, and the last example printsthe day of the year together the truncated hour of the day.
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, otherwise not.
xts: To our knowledge, no.
timeSeries: The print method for timeSeries objects has a style="ts"option. For example if you have monthly records
> data <- 1:6> charvec <- paste("2009-0", 1:6, "-01", sep = "")> s <- timeSeries(data, charvec)> print(s, style = "ts")
Jan Feb Mar Apr May Jun2009 1 2 3 4 5 6
and in the case of quarterly records
> data <- 1:4> charvec <- paste("2009-", c("03", "06", "09", "12"), "-01", sep = "")> s <- timeSeries(data, charvec)> print(s, style = "ts", by = "quarter")
Qtr1 Qtr2 Qtr3 Qtr42009 1 22010 3 4 1 22011 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.
xts: There is no direct way to do it.
timeSeries: The print method for timeSeries objects has a FinCen-ter=NULL option. For example if you have monthly records
> data <- rnorm(6)> charvec <- paste("2009-0", 1:6, "-01 16:00:00", sep = "")> s <- timeSeries(data, charvec, zone = "Chicago", FinCenter = "Zurich")> print(s, FinCenter = "Chicago")
ChicagoTS.1
2009-01-01 16:00:00 -1.350862009-02-01 16:00:00 -2.496712009-03-01 16:00:00 0.909202009-04-01 16:00:00 -1.305622009-05-01 16:00:00 0.365562009-06-01 16:00:00 -0.58463
> print(s, FinCenter = "Zurich")
ZurichTS.1
2009-01-01 23:00:00 -1.350862009-02-01 23:00:00 -2.496712009-03-01 23:00:00 0.909202009-04-01 23:00:00 -1.305622009-05-01 23:00:00 0.365562009-06-01 23:00:00 -0.58463
> print(s, FinCenter = "Tokyo")
TokyoTS.1
2009-01-02 07:00:00 -1.350862009-02-02 07:00:00 -2.496712009-03-02 07:00:00 0.909202009-04-02 06:00:00 -1.305622009-05-02 06:00:00 0.365562009-06-02 06:00:00 -0.58463
CHAPTER 10
PLOTTING TIME SERIES OBJECTS
> library(zoo)> library(xts)> library(timeSeries)
How can I plot a univariate time series using the generic plot function?
Common Data:
> set.seed(1953)> data <- runif(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:
> args(plot.zoo)
function (x, y = NULL, screens, plot.type, panel = lines, xlab = "Index",ylab = NULL, main = NULL, xlim = NULL, ylim = NULL, xy.labels = FALSE,xy.lines = NULL, oma = c(6, 0, 5, 0), mar = c(0, 5.1, 0,
2.1), col = 1, lty = 1, lwd = 1, pch = 1, type = "l",nc, widths = 1, heights = 1, ...)
NULL
> z <- zoo(data, as.POSIXct(charvec, tz = "GMT"))> plot(z)
Note for this example the labels on the x axis have no year numbers.
94
xts:
> args(plot.xts)
function (x, y = NULL, type = "l", auto.grid = TRUE, major.ticks = "auto",minor.ticks = TRUE, major.format = TRUE, bar.col = "grey",candle.col = "white", ann = TRUE, axes = TRUE, ...)
NULL
> x <- xts(data, as.POSIXct(charvec, tz = "GMT"))> plot(x)
timeSeries: timeSeries has an S4 generic method for plotting.
> s <- timeSeries(data, charvec)> plot(s)
How can I plot a multivariate time series using the generic plot function?
Common Data:
> set.seed(1953)> data <- matrix(runif(12), ncol = 2)> 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.POSIXct(charvec, tz = "GMT"))> plot(Z)
xts:
> X <- xts(data, as.POSIXct(charvec, tz = "GMT"))> plot(X)
xts does not support multivariate time series plots in in multiple charts.
timeSeries:
> S <- timeSeries(data, charvec)> plot(S)
How can I plot a multivariate time series in a single chart using the genericplot function?
Common Data:
0.2
0.4
0.6
Index
z
Jan Mar May
Jan 2009 Feb 2009 Mar 2009 Apr 2009 May 2009 Jun 2009
0.2
0.4
0.6
x
Time
TS.1
2009−01−01 2009−01−31 2009−03−02 2009−04−01 2009−05−01 2009−06−01
0.2
0.4
0.6
FIGURE 10.1: Plot 1 - Example of a single panel plot from zoo, xts and timeSeries
> set.seed(1953)> data <- matrix(runif(18), ncol = 3)> 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:
0.2
0.4
0.6
Serie
s 1
0.4
0.6
0.8
Serie
s 2
Jan Mar May
Index
Z
FIGURE 10.2: Plot 2a - Multivariate Time Series plots in multiple graphs
> Z <- zoo(data, as.POSIXct(charvec, tz = "GMT"))> plot(Z, plot.type = "single")
xts: xts does not support multivariate time series plots in in singlecharts.
timeSeries:
0.2
0.4
0.6
TS.1
0.4
0.6
0.8
2009−01−01 2009−03−02 2009−05−01
TS.2
Time
x
FIGURE 10.3: Plot 2b - Multivariate Time Series plots in multiple graphs
> S <- timeSeries(data, as.POSIXct(charvec, FinCenter = "GMT"))> plot(S, plot.type = "single")
How can I add a line to an existing plot?
Common Data:
> set.seed(1953)> data1 <- rnorm(9)
0.2
0.3
0.4
0.5
0.6
Serie
s 1
0.4
0.6
0.8
Serie
s 2
0.2
0.4
0.6
0.8
Serie
s 3
Jan Mar May
Index
Z
FIGURE 10.4: Plot3a - Example of a single panel plot from zoo
> data2 <- rnorm(9, sd = 0.2)> charvec <- paste("2009-0", 1:9, "-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"
zoo:
> z1 <- zoo(data1, as.POSIXct(charvec, tz = "GMT"))
0.2
0.3
0.4
0.5
0.6
TS.1
0.4
0.6
0.8
TS.2
0.2
0.4
0.6
0.8
2008−12−31 23:00:00 2009−03−02 08:12:00 2009−05−01 17:24:00
TS.3
Time
x
FIGURE 10.5: Plot 3b - Example of a single panel plot from timeSeries
> z2 <- zoo(data2, as.POSIXct(charvec, tz = "GMT"))> plot(z1)> lines(z2, col = 2)
timeSeries:
> s1 <- timeSeries(data1, charvec, FinCenter = "GMT")> s2 <- timeSeries(data2, charvec, FinCenter = "GMT")> plot(s1)
> lines(s2, col = 2)
How can I add points to an existing plot?
Common Data:
> set.seed(1953)> data <- rnorm(9)> charvec <- paste("2009-0", 1:9, "-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"
zoo:
> z <- zoo(data, as.POSIXct(charvec, tz = "GMT"))> plot(z)> points(z, col = 2, pch = 19)
timeSeries:
> s <- timeSeries(data, charvec, FinCenter = "GMT")> plot(s)> points(s, col = 2, pch = 19)
Can I use abline for time series plots?
Common Data:
> set.seed(1953)> data <- rnorm(9)> charvec <- paste("2009-0", 1:9, "-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"
zoo:
> z <- zoo(data, as.POSIXct(charvec, tz = "GMT"))> plot(z)> abline(v = index(z), col = "darkgrey", lty = 3)
timeSeries:
> s <- timeSeries(data, charvec, FinCenter = "GMT")> plot(s)> abline(v = time(s), col = "darkgrey", lty = 3)
PART III
USING R FUNCTIONS
102
CHAPTER 11
FUNCTIONS AND METHODS FROM BASE R
> library(zoo)> library(xts)> library(timeSeries)
In the following we test some selected functions that are relevant for usein financial time series analysis. We test them on multivariate time seriesobjects
> 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.
103
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 byapplying a function to margins of an array. How does the function workstogether with time Series objects?
zoo:
> apply(Z, 2, mean)
Z.1 Z.2 Z.30.41652 0.58821 0.58572
xts:
> apply(X, 2, mean)
X.1 X.2 X.30.41652 0.58821 0.58572
timeSeries:
> apply(S, 2, mean)
S.1 S.2 S.30.41652 0.58821 0.58572
Can I use attach like in R’s base package?
The database is attached to the R search path. This means that the databaseis searched by R when evaluating a variable, so objects in the databasecan be accessed by simply giving their names.
zoo:
> print(try(attach(Z)))
[1] "Error in attach(Z) : \n 'attach' only works for lists, data frames and environments\n"attr(,"class")[1] "try-error"
xts:
> print(try(attach(X)))
[1] "Error in attach(X) : \n 'attach' only works for lists, data frames and environments\n"attr(,"class")[1] "try-error"
timeSeries:
> attach(S)> colnames(S)
[1] "S.1" "S.2" "S.3"
> S.2
[1] 0.57402 0.57365 0.59127 0.96524 0.59316 0.23193
Can I use diff like in R’s base package?
The function diff() returns suitably lagged and iterated differences.
zoo:
> diff(Z)
Z.1 Z.2 Z.32009-02-01 0.437452 0.36122800 0.289802009-03-01 -0.212986 0.37207930 0.115452009-04-01 -0.264387 -0.37396709 0.250632009-05-01 0.294113 -0.01761714 -0.465212009-06-01 0.031721 0.00036655 0.53205
> diff(z)
2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.437452 -0.212986 -0.264387 0.294113 0.031721
xts:
> diff(X)
X.1 X.2 X.32009-01-01 NA NA NA2009-02-01 0.437452 0.36122800 0.289802009-03-01 -0.212986 0.37207930 0.115452009-04-01 -0.264387 -0.37396709 0.250632009-05-01 0.294113 -0.01761714 -0.465212009-06-01 0.031721 0.00036655 0.53205
> diff(x)
X.12009-01-01 NA2009-02-01 0.4374522009-03-01 -0.2129862009-04-01 -0.2643872009-05-01 0.2941132009-06-01 0.031721
timeSeries:
> diff(S)
GMTS.1 S.2 S.3
2009-06-01 NA NA NA2009-05-01 -0.031721 -0.00036655 -0.532052009-04-01 -0.294113 0.01761714 0.465212009-03-01 0.264387 0.37396709 -0.250632009-02-01 0.212986 -0.37207930 -0.115452009-01-01 -0.437452 -0.36122800 -0.28980
> diff(s)
GMTS.1
2009-06-01 NA2009-05-01 -0.0317212009-04-01 -0.2941132009-03-01 0.2643872009-02-01 0.2129862009-01-01 -0.437452
Can I use dim like in R’s base package?
The function dim() retrieves or sets the dimension of an object.
zoo:
> dim(Z)
[1] 6 3
> dim(z)
NULL
xts:
> dim(X)
[1] 6 3
> 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.Ties (i.e., equal values) and missing values can be handled in several ways.
zoo:
> print(try(rank(Z)))
[1] "Error in names(y) <- nm[!nas] : \n 'names' attribute [18] must be the same length as the vector [0]\n"attr(,"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(,"class")[1] "try-error"
xts:
> print(try(rank(X)))
[1] "Error in `[.xts`(x, !nas) : i is out of range\n\n"attr(,"class")[1] "try-error"
> 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(,"class")[1] "try-error"
timeSeries:
> rank(S)
GMTS.1 S.2 S.3
2009-06-01 5 3 62009-05-01 4 2 22009-04-01 1 4 52009-03-01 3 6 42009-02-01 6 5 32009-01-01 2 1 1
> rank(s)
GMTS.1
2009-06-01 52009-05-01 42009-04-01 12009-03-01 32009-02-01 62009-01-01 2
Can I use rev like in R’s base package?
The function rev() provides a reversed version of its argument.
zoo:
> print(try(rev(Z)))
Z.1 Z.2 Z.32009-01-01 0.50875 0.57402 0.931062009-02-01 0.47702 0.57365 0.399012009-03-01 0.18291 0.59127 0.864222009-04-01 0.44730 0.96524 0.613592009-05-01 0.66028 0.59316 0.498132009-06-01 0.22283 0.23193 0.20833
> rev(z)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.50875 0.47702 0.18291 0.44730 0.66028 0.22283
xts:
> print(try(rev(X)))
X.1 X.2 X.32009-01-01 0.50875 0.57402 0.931062009-02-01 0.47702 0.57365 0.399012009-03-01 0.18291 0.59127 0.864222009-04-01 0.44730 0.96524 0.613592009-05-01 0.66028 0.59316 0.498132009-06-01 0.22283 0.23193 0.20833
> rev(x)
X.12009-01-01 0.508752009-02-01 0.477022009-03-01 0.182912009-04-01 0.447302009-05-01 0.660282009-06-01 0.22283
timeSeries:
> rev(S)
GMTS.1 S.2 S.3
2009-01-01 0.22283 0.23193 0.208332009-02-01 0.66028 0.59316 0.498132009-03-01 0.44730 0.96524 0.613592009-04-01 0.18291 0.59127 0.864222009-05-01 0.47702 0.57365 0.399012009-06-01 0.50875 0.57402 0.93106
> rev(s)
GMTS.1
2009-01-01 0.222832009-02-01 0.660282009-03-01 0.447302009-04-01 0.182912009-05-01 0.477022009-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 ele-ments 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-010.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.12009-01-01 0.222832009-02-01 0.660282009-03-01 0.447302009-04-01 0.182912009-05-01 0.477022009-06-01 0.50875
timeSeries:
> sample(S)
GMTS.1 S.2 S.3
2009-03-01 0.44730 0.96524 0.613592009-02-01 0.66028 0.59316 0.498132009-04-01 0.18291 0.59127 0.864222009-05-01 0.47702 0.57365 0.399012009-06-01 0.50875 0.57402 0.931062009-01-01 0.22283 0.23193 0.20833
> sample(s)
GMTS.1
2009-04-01 0.182912009-05-01 0.477022009-06-01 0.508752009-02-01 0.660282009-01-01 0.222832009-03-01 0.44730
Can I use scale like in R’s base package?
The function scale() is generic function whose default method centersand/or scales the columns of a numeric matrix. How does it work togetherwith time serie sobjects?
zoo:
> scale(Z)
Z.1 Z.2 Z.32009-01-01 -1.06742 -1.534521 -1.364372009-02-01 1.34343 0.021305 -0.316672009-03-01 0.16964 1.623869 0.100732009-04-01 -1.28742 0.013175 1.006832009-05-01 0.33347 -0.062703 -0.675012009-06-01 0.50829 -0.061125 1.24849attr(,"scaled:center")
Z.1 Z.2 Z.30.41652 0.58821 0.58572attr(,"scaled:scale")
Z.1 Z.2 Z.30.18145 0.23218 0.27660
> scale(z)
2009-01-01 -1.067422009-02-01 1.343432009-03-01 0.169642009-04-01 -1.287422009-05-01 0.333472009-06-01 0.50829attr(,"scaled:center")[1] 0.41652attr(,"scaled:scale")[1] 0.18145
xts:
> scale(X)
X.1 X.2 X.32009-01-01 -1.06742 -1.534521 -1.364372009-02-01 1.34343 0.021305 -0.31667
2009-03-01 0.16964 1.623869 0.100732009-04-01 -1.28742 0.013175 1.006832009-05-01 0.33347 -0.062703 -0.675012009-06-01 0.50829 -0.061125 1.24849
> scale(x)
X.12009-01-01 -1.067422009-02-01 1.343432009-03-01 0.169642009-04-01 -1.287422009-05-01 0.333472009-06-01 0.50829
timeSeries:
> scale(S)
GMTS.1 S.2 S.3
2009-06-01 0.50829 -0.061125 1.248492009-05-01 0.33347 -0.062703 -0.675012009-04-01 -1.28742 0.013175 1.006832009-03-01 0.16964 1.623869 0.100732009-02-01 1.34343 0.021305 -0.316672009-01-01 -1.06742 -1.534521 -1.36437
> scale(s)
GMTS.1
2009-06-01 0.508292009-05-01 0.333472009-04-01 -1.287422009-03-01 0.169642009-02-01 1.343432009-01-01 -1.06742
Can I use sort like in R’s base package?
The function sort (or order) sorts a vector or factor (partially) into as-cending (or descending) order. For ordering along more than one variable,e.g., for sorting data frames, see order.
zoo:
> print(try(sort(Z)))
[1] "Error in `[.zoo`(x, order(x, na.last = na.last, decreasing = decreasing)) : \n subscript out of bounds\n"attr(,"class")[1] "try-error"
> sort(z)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.22283 0.66028 0.44730 0.18291 0.47702 0.50875
xts:
> print(try(sort(X)))
[1] "Error in `[.xts`(x, order(x, na.last = na.last, decreasing = decreasing)) : \n subscript out of bounds\n"attr(,"class")[1] "try-error"
> sort(x)
X.12009-01-01 0.222832009-02-01 0.660282009-03-01 0.447302009-04-01 0.182912009-05-01 0.477022009-06-01 0.50875
timeSeries:
> sort(S)
GMTS.1 S.2 S.3
2009-01-01 0.22283 0.23193 0.208332009-02-01 0.66028 0.59316 0.498132009-03-01 0.44730 0.96524 0.613592009-04-01 0.18291 0.59127 0.864222009-05-01 0.47702 0.57365 0.399012009-06-01 0.50875 0.57402 0.93106
> sort(s)
GMTS.1
2009-01-01 0.222832009-02-01 0.660282009-03-01 0.447302009-04-01 0.182912009-05-01 0.477022009-06-01 0.50875
Can I use start and end like in R’s base package?
The functions start() and end() extract and encode the times the firstand last observations were taken.
zoo:
> start(Z)
[1] "2009-01-01"
> end(Z)
[1] "2009-06-01"
> start(z)
[1] "2009-01-01"
> end(z)
[1] "2009-06-01"
xts:
> start(X)
[1] "2009-01-01"
> end(X)
[1] "2009-06-01"
> start(x)
[1] "2009-01-01"
> end(x)
[1] "2009-06-01"
timeSeries:
> start(S)
GMT[1] [2009-01-01]
> end(S)
GMT[1] [2009-06-01]
> start(s)
GMT[1] [2009-01-01]
> end(s)
GMT[1] [2009-06-01]
CHAPTER 12
FUNCTIONS AND METHODS FROM STATS R
> library(zoo)> library(xts)> library(timeSeries)
In the following we test selected functions which are relevant for use infinancial time series analysis.
Common Data: We test them on multivariate
> set.seed(1953)> data <- matrix(runif(18), ncol = 3)> 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))> colnames(Z) = paste("Z", 1:3, sep = ".")> Z
Z.1 Z.2 Z.32009-01-01 0.50875 0.57402 0.931062009-02-01 0.47702 0.57365 0.399012009-03-01 0.18291 0.59127 0.864222009-04-01 0.44730 0.96524 0.613592009-05-01 0.66028 0.59316 0.498132009-06-01 0.22283 0.23193 0.20833
114
xts:
> X <- xts(data, as.Date(charvec))> colnames(X) = paste("X", 1:3, sep = ".")> X
X.1 X.2 X.32009-01-01 0.50875 0.57402 0.931062009-02-01 0.47702 0.57365 0.399012009-03-01 0.18291 0.59127 0.864222009-04-01 0.44730 0.96524 0.613592009-05-01 0.66028 0.59316 0.498132009-06-01 0.22283 0.23193 0.20833
timeSeries:
> S <- timeSeries(data, charvec)> colnames(S) = paste("S", 1:3, sep = ".")> S
GMTS.1 S.2 S.3
2009-01-01 0.50875 0.57402 0.931062009-02-01 0.47702 0.57365 0.399012009-03-01 0.18291 0.59127 0.864222009-04-01 0.44730 0.96524 0.613592009-05-01 0.66028 0.59316 0.498132009-06-01 0.22283 0.23193 0.20833
and/or 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-010.50875 0.47702 0.18291 0.44730 0.66028 0.22283
xts:
> x <- X[, 1]> x
X.12009-01-01 0.508752009-02-01 0.477022009-03-01 0.182912009-04-01 0.447302009-05-01 0.660282009-06-01 0.22283
timeSeries:
> s <- S[, 1]> s
GMTS.1
2009-01-01 0.508752009-02-01 0.477022009-03-01 0.182912009-04-01 0.447302009-05-01 0.660282009-06-01 0.22283
Can I use arima like in R’s base package?
The function arima() fits an ARIMA model to a univariate time series.
zoo:
> arima(z)
Call:arima(x = z)
Coefficients:intercept
0.417s.e. 0.068
sigma^2 estimated as 0.0274: log likelihood = 2.27, aic = -0.55
xts:
> arima(x)
Call:arima(x = x)
Coefficients:intercept
0.417s.e. 0.068
sigma^2 estimated as 0.0274: log likelihood = 2.27, aic = -0.55
timeSeries:
> arima(s)
Call:arima(x = s)
Coefficients:intercept
0.417s.e. 0.068
sigma^2 estimated as 0.0274: log likelihood = 2.27, aic = -0.55
Can I use acf like in R’s stats package?
The function acf() computes (and by default plots) estimates of the auto-covariance or autocorrelation function. Function pacf() is the functionused for the partial autocorrelations. Function ccf() computes the cross-correlation or cross-covariance of two univariate series.
zoo:
> print(try(acf(z)))
[1] "Error in na.fail.default(as.ts(x)) : missing values in object\n"attr(,"class")[1] "try-error"
xts:
> print(try(acf(x)))
Autocorrelations of series 'x', by lag
0 1 2 3 4 51.000 -0.337 -0.502 0.382 0.065 -0.109
timeSeries:
> print(acf(s))
Autocorrelations of series 's', by lag
0.0000 0.0833 0.1667 0.2500 0.3333 0.41671.000 -0.337 -0.502 0.382 0.065 -0.109
Can I use cov like in R’s stats package?
The functions var(), cov() and cor() compute the variance of x andthe covariance or correlation of x and y if these are vectors. If x and y arematrices then the covariances (or correlations) between the columns of xand the columns of y are computed. Applied to zoo, xts, and timeSeriesobjects these functions behave in the following way.
zoo:
> var(z)
[1] 0.032925
> var(Z)
Z.1 Z.2 Z.3Z.1 0.0329245 0.015783 0.0016191Z.2 0.0157826 0.053906 0.0286396Z.3 0.0016191 0.028640 0.0765103
> cov(Z)
Z.1 Z.2 Z.3Z.1 0.0329245 0.015783 0.0016191Z.2 0.0157826 0.053906 0.0286396Z.3 0.0016191 0.028640 0.0765103
> cor(Z)
Z.1 Z.2 Z.3Z.1 1.000000 0.37463 0.032259Z.2 0.374627 1.00000 0.445951Z.3 0.032259 0.44595 1.000000
xts:
> var(x)
X.1X.1 0.032925
> var(X)
X.1 X.2 X.3X.1 0.0329245 0.015783 0.0016191X.2 0.0157826 0.053906 0.0286396X.3 0.0016191 0.028640 0.0765103
> cov(X)
X.1 X.2 X.3X.1 0.0329245 0.015783 0.0016191X.2 0.0157826 0.053906 0.0286396X.3 0.0016191 0.028640 0.0765103
> cor(X)
X.1 X.2 X.3X.1 1.000000 0.37463 0.032259X.2 0.374627 1.00000 0.445951X.3 0.032259 0.44595 1.000000
timeSeries:
> var(s)
S.1S.1 0.032925
> var(S)
S.1 S.2 S.3S.1 0.0329245 0.015783 0.0016191S.2 0.0157826 0.053906 0.0286396S.3 0.0016191 0.028640 0.0765103
> cov(S)
S.1 S.2 S.3S.1 0.0329245 0.015783 0.0016191S.2 0.0157826 0.053906 0.0286396S.3 0.0016191 0.028640 0.0765103
> cor(S)
S.1 S.2 S.3S.1 1.000000 0.37463 0.032259S.2 0.374627 1.00000 0.445951S.3 0.032259 0.44595 1.000000
Can I use dist like in R’s stats package?
This function dist() computes and returns the distance matrix computedby using the specified distance measure to compute the distances betweenthe rows of a data matrix.
zoo:
> dist(Z)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-012009-02-01 0.533002009-03-01 0.33307 0.550662009-04-01 0.50756 0.44751 0.522082009-05-01 0.45908 0.20926 0.60159 0.444002009-06-01 0.84918 0.46663 0.74894 0.86738 0.63705
> dist(t(Z))
Z.1 Z.2Z.2 0.67321Z.3 0.83831 0.60475
xts:
> dist(X)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-012009-02-01 0.533002009-03-01 0.33307 0.550662009-04-01 0.50756 0.44751 0.522082009-05-01 0.45908 0.20926 0.60159 0.444002009-06-01 0.84918 0.46663 0.74894 0.86738 0.63705
> dist(t(X))
X.1 X.2X.2 0.67321X.3 0.83831 0.60475
timeSeries:
> dist(S)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-012009-02-01 0.533002009-03-01 0.33307 0.550662009-04-01 0.50756 0.44751 0.522082009-05-01 0.45908 0.20926 0.60159 0.444002009-06-01 0.84918 0.46663 0.74894 0.86738 0.63705
> dist(t(S))
S.1 S.2S.2 0.67321S.3 0.83831 0.60475
Can I use dnorm like in R’s stats package?
The function dnorm() computes the density for the normal distributionwith mean equal to mean and standard deviation equal to sd.
zoo:
> dnorm(z, mean = 0, sd = 1)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.35052 0.35604 0.39232 0.36096 0.32080 0.38916
xts:
> dnorm(x, mean = 0, sd = 1)
X.12009-01-01 0.350522009-02-01 0.356042009-03-01 0.392322009-04-01 0.360962009-05-01 0.320802009-06-01 0.38916
timeSeries:
> dnorm(s, mean = 0, sd = 1)
GMTS.1
2009-01-01 0.350522009-02-01 0.356042009-03-01 0.392322009-04-01 0.360962009-05-01 0.320802009-06-01 0.38916
Can I use filter like in R’s stats package?
The function filter() applies linear filtering to a univariate time seriesor to each series separately of a multivariate time series.
zoo:
> print(try(filter(z)))
[1] "Error in filter(z) : element 1 is empty;\n the part of the args list of 'length' being evaluated was:\n (filter)\n"attr(,"class")[1] "try-error"
xts:
> print(try(filter(x)))
[1] "Error in filter(x) : element 1 is empty;\n the part of the args list of 'length' being evaluated was:\n (filter)\n"attr(,"class")[1] "try-error"
timeSeries:
> filter(s, rep(1, 3))
GMTS.1
2009-01-01 NA2009-02-01 1.16872009-03-01 1.10722009-04-01 1.29052009-05-01 1.33042009-06-01 NA
Can I use fivenum like in R’s stats package?
The function fivenum returns Tukey’s five number summary (minimum,lower-hinge, median, upper-hinge, maximum) for the input data.
zoo:
> fivenum(z)
2009-01-01 2009-02-01 2009-05-01 2009-06-010.50875 0.47702 0.66028 0.22283
xts:
> fivenum(x)
X.12009-01-01 0.508752009-02-01 0.477022009-05-01 0.660282009-06-01 0.22283
timeSeries:
> fivenum(s)
[1] 0.18291 0.22283 0.46216 0.50875 0.66028
Can I use hist like in R’s stats package?
The generic function hist() computes a histogram of the given datavalues. If plot=TRUE, the resulting object of class "histogram" is plottedby plot.histogram, before it is returned.
zoo:
> hist(z)$density
[1] 1.6667 1.6667 0.0000 3.3333 1.6667 1.6667
xts:
> hist(x)$density
[1] 1.6667 1.6667 0.0000 3.3333 1.6667 1.6667
timeSeries:
> hist(s)$density
[1] 1.6667 1.6667 0.0000 3.3333 1.6667 1.6667
Can I use lag like in R’s stats package?
The function lag() computes a lagged version of a time series, shiftingthe time base back by a given number of observations.
zoo:
> lag(Z)
Z.1 Z.2 Z.32009-01-01 0.47702 0.57365 0.399012009-02-01 0.18291 0.59127 0.864222009-03-01 0.44730 0.96524 0.613592009-04-01 0.66028 0.59316 0.498132009-05-01 0.22283 0.23193 0.20833
xts:
> lag(X)
X.1 X.2 X.32009-01-01 NA NA NA2009-02-01 0.50875 0.57402 0.931062009-03-01 0.47702 0.57365 0.399012009-04-01 0.18291 0.59127 0.864222009-05-01 0.44730 0.96524 0.613592009-06-01 0.66028 0.59316 0.49813
timeSeries:
> lag(S)
GMTS.1[1] S.2[1] S.3[1]
2009-01-01 NA NA NA2009-02-01 0.50875 0.57402 0.931062009-03-01 0.47702 0.57365 0.399012009-04-01 0.18291 0.59127 0.864222009-05-01 0.44730 0.96524 0.613592009-06-01 0.66028 0.59316 0.49813
Can I use lm like in R’s stats package?
The function lm() is used to fit linear models. It can be used to carry outregression, single stratum analysis of variance and analysis of covariance(although aov may provide a more convenient interface for these).
zoo:
> fit <- lm(Z.1 ~ Z.2 + Z.3, data = Z)> fit
Call:lm(formula = Z.1 ~ Z.2 + Z.3, data = Z)
Coefficients:(Intercept) Z.2 Z.3
0.274 0.351 -0.110
> resid(fit)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.135337 0.045015 -0.203938 -0.098638 0.232361 -0.110136
xts:
> fit <- lm(X.1 ~ X.2 + X.3, data = X)> fit
Call:lm(formula = X.1 ~ X.2 + X.3, data = X)
Coefficients:(Intercept) X.2 X.3
0.274 0.351 -0.110
> resid(fit)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.135337 0.045015 -0.203938 -0.098638 0.232361 -0.110136
timeSeries:
> fit <- lm(S.1 ~ S.2 + S.3, data = S)> fit
Call:lm(formula = S.1 ~ S.2 + S.3, data = S)
Coefficients:(Intercept) S.2 S.3
0.274 0.351 -0.110
> resid(fit)
2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-010.135337 0.045015 -0.203938 -0.098638 0.232361 -0.110136
Can I use lowess like in R’s stats package?
The function lowess()performs the computations for the LOWESS smootherwhich uses locally-weighted polynomial regression.
zoo:
> lowess(z)
$x[1] 1 2 3 4 5 6
$y[1] 0.53355 0.40950 0.37472 0.43720 0.41244 0.26854
xts:
> lowess(x)
$x[1] 1 2 3 4 5 6
$y[1] 0.53355 0.40950 0.37472 0.43720 0.41244 0.26854
timeSeries:
> lowess(s)
$x[1] 1 2 3 4 5 6
$y[1] 0.53355 0.40950 0.37472 0.43720 0.41244 0.26854
Can I use mad like in R’s stats package?
The function mad() computes the median absolute deviation, i.e., the (lo-/hi-) median of the absolute deviations from the median, and (by default)adjust by a factor for asymptotically normal consistency.
zoo:
> mad(z)
[1] 0.19599
xts:
> mad(x)
[1] 0.19599
timeSeries:
> mad(s)
[1] 0.19599
Can I use median like in R’s stats package?
The function median() computes the sample median.
zoo:
> median(x)
[1] 0.31510
xts:
> median(x)
[1] 0.31510
timeSeries:
> median(s)
[1] 0.31510
Can I use qqnorm like in R’s stats package?
The function qqnorm() is a generic function the default method of whichproduces a normal QQ plot of the values in y. qqline() adds a line to anormal quantile-quantile plot which passes through the first and thirdquartiles.
zoo:
> print(qqnorm(z))
$x[1] 0.64335 0.20189 -1.28155 -0.20189 1.28155 -0.64335
$y2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01
0.50875 0.47702 0.18291 0.44730 0.66028 0.22283
xts:
> print(qqnorm(x))
$x[1] 0.64335 0.20189 -1.28155 -0.20189 1.28155 -0.64335
$yX.1
2009-01-01 0.508752009-02-01 0.477022009-03-01 0.182912009-04-01 0.447302009-05-01 0.660282009-06-01 0.22283
timeSeries:
> print(qqnorm(s))
$x[1] 0.64335 0.20189 -1.28155 -0.20189 1.28155 -0.64335
$yGMT
S.12009-01-01 0.508752009-02-01 0.477022009-03-01 0.182912009-04-01 0.447302009-05-01 0.660282009-06-01 0.22283
Can I use smooth like in R’s stats package?
The functions smooth() performs running median smoothing. The func-tion implements Tukey’s smoothers, 3RS3R, 3RSS, 3R, etc.
zoo:
> smooth(z)
3RS3R Tukey smoother resulting from smooth(x = z)used 1 iterations[1] 0.50875 0.47702 0.44730 0.44730 0.44730 0.44730
xts:
> smooth(x)
3RS3R Tukey smoother resulting from smooth(x = x)used 1 iterations[1] 0.50875 0.47702 0.44730 0.44730 0.44730 0.44730
timeSeries:
> smooth(s)
3RS3R Tukey smoother resulting from smooth(x = s)used 1 iterations[1] 0.50875 0.47702 0.44730 0.44730 0.44730 0.44730
Can I use spectrum like in R’s stats package?
The function spectrum() estimates the spectral density of a time series.
zoo:
> print(try(spectrum(z)[1:2]))
[1] "Error in na.fail.default(as.ts(x)) : missing values in object\n"attr(,"class")[1] "try-error"
xts:
> print(spectrum(x)[1:2])
$freq[1] 0.16667 0.33333 0.50000
$spec[1] 0.0161880 0.0725889 0.0044029
timeSeries:
> print(spectrum(s)[1:2])
$freq[1] 2 4 6
$spec[1] 0.00134900 0.00604908 0.00036691
CHAPTER 13
FUNCTIONS AND METHODS FROM UTILS R
> library(zoo)> library(xts)> library(timeSeries)
In the following we test selected functions which are relevant for use infinancial time series analysis.
Common Data: We test them on multivariate
> set.seed(1953)> data <- matrix(runif(18), ncol = 3)> 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))> colnames(Z) = paste("Z", 1:3, sep = ".")> Z
Z.1 Z.2 Z.32009-01-01 0.50875 0.57402 0.931062009-02-01 0.47702 0.57365 0.399012009-03-01 0.18291 0.59127 0.864222009-04-01 0.44730 0.96524 0.613592009-05-01 0.66028 0.59316 0.498132009-06-01 0.22283 0.23193 0.20833
128
xts:
> X <- xts(data, as.Date(charvec))> colnames(X) = paste("X", 1:3, sep = ".")> X
X.1 X.2 X.32009-01-01 0.50875 0.57402 0.931062009-02-01 0.47702 0.57365 0.399012009-03-01 0.18291 0.59127 0.864222009-04-01 0.44730 0.96524 0.613592009-05-01 0.66028 0.59316 0.498132009-06-01 0.22283 0.23193 0.20833
timeSeries:
> S <- timeSeries(data, charvec)> colnames(S) = paste("S", 1:3, sep = ".")> S
GMTS.1 S.2 S.3
2009-01-01 0.50875 0.57402 0.931062009-02-01 0.47702 0.57365 0.399012009-03-01 0.18291 0.59127 0.864222009-04-01 0.44730 0.96524 0.613592009-05-01 0.66028 0.59316 0.498132009-06-01 0.22283 0.23193 0.20833
and/or 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-010.50875 0.47702 0.18291 0.44730 0.66028 0.22283
xts:
> x <- X[, 1]> x
X.12009-01-01 0.508752009-02-01 0.477022009-03-01 0.182912009-04-01 0.447302009-05-01 0.660282009-06-01 0.22283
timeSeries:
> s <- S[, 1]> s
GMTS.1
2009-01-01 0.508752009-02-01 0.477022009-03-01 0.182912009-04-01 0.447302009-05-01 0.660282009-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, matrix, table,data frame or function.
ts: «head.ts» ts <- rnorm(ts(rnorm(12))) ts head(ts)For a regular time series of class ts, the attributes are lost.
zoo:
> head(Z, 3)
Z.1 Z.2 Z.32009-01-01 0.50875 0.57402 0.931062009-02-01 0.47702 0.57365 0.399012009-03-01 0.18291 0.59127 0.86422
> head(z, 3)
2009-01-01 2009-02-01 2009-03-010.50875 0.47702 0.18291
xts:
> head(X, 3)
X.1 X.2 X.32009-01-01 0.50875 0.57402 0.931062009-02-01 0.47702 0.57365 0.399012009-03-01 0.18291 0.59127 0.86422
> head(x, 3)
X.12009-01-01 0.508752009-02-01 0.477022009-03-01 0.18291
timeSeries:
> head(S, 3)
GMTS.1 S.2 S.3
2009-01-01 0.50875 0.57402 0.931062009-02-01 0.47702 0.57365 0.399012009-03-01 0.18291 0.59127 0.86422
> head(s, 3)
GMTS.1
2009-01-01 0.508752009-02-01 0.477022009-03-01 0.18291
PART IV
PERFORMANCE MEASUREMENTS
132
CHAPTER 14
PERFORMANCE OF TIME SERIES CREATION
> library(zoo)> library(xts)> library(timeSeries)
We define the following function to measure the performance of the dif-ferent functions with
> systemTime <- function(expr, gcFirst = TRUE, n = 20) {time <- sapply(integer(n), eval.parent(substitute(function(...) system.time(expr,
gcFirst = gcFirst))))structure(apply(time, 1, median), class = "proc_time")
}
How long does it take to create a one hundred year 5-column series withdaily records from character time stamps?
If we read time series information from an ASCII, or extract it from adownloaded html file from the Web, or import it from a xls or csv file thenwe have the time usually as character strings. To convert this to a zoo, xtsor timeSeries object, one has to transform the character time stamps inan appropriate index. Here we consider abot 35’000 daily records whichrepresent about 100 year of daily data.
Common Data:
> charvec <- format(seq(from = as.Date("1901-01-01"), to = as.Date("1999-12-31"),by = "day"))
> data <- matrix(rnorm(length(charvec) * 5), ncol = 5)
133
zoo:
> systemTime(zoo(data, charvec))
user system elapsed0.27 0.00 0.26
xts:
> print(try(xts(data, charvec)))
[1] "Error in xts(data, charvec) : \n order.by requires an appropriate time-based object\n"attr(,"class")[1] "try-error"
timeSeries:
> systemTime(timeSeries(data, charvec))
user system elapsed0.24 0.00 0.24
How long does it take to create a one hundred year 5-column series withdaily records from Date time stamps?
Common Data:
> charvec <- format(seq(from = as.Date("1901-01-01"), to = as.Date("1999-12-31"),by = "day"))
> data <- matrix(rnorm(length(charvec) * 5), ncol = 5)> index <- as.Date(charvec)> class(index)
[1] "Date"
zoo:
> systemTime(zoo(data, index))
user system elapsed0.03 0.00 0.03
This is the generic case to create daily recorded time series with zoo.
xts:
> systemTime(xts(data, index))
user system elapsed1.42 0.00 1.42
This is the generic case to create daily recorded time series with xts.
timeSeries:
> systemTime(timeSeries(data, index))
user system elapsed0.03 0.00 0.03
Since we are always working with timeDate object, the following test isthe proper benchmark.
How long does it take to create a one hundred year 5-column series withdaily records from GMT POSIXct time stamps?
Common Data:
> charvec <- format(seq(from = as.Date("1901-01-01"), to = as.Date("1999-12-31"),by = "day"))
> data <- matrix(rnorm(length(charvec) * 5), ncol = 5)> index <- as.POSIXct(charvec, tz = "GMT")> class(index)
[1] "POSIXt" "POSIXct"
zoo:
> systemTime(zoo(data, index))
user system elapsed0.03 0.00 0.03
xts:
> systemTime(xts(data, index))
user system elapsed0 0 0
timeSeries:
> systemTime(timeSeries(data, index))
user system elapsed0.03 0.00 0.03
How long does it take to create a one hundred year 5-column series withdaily records from non-GMT POSIXct time stamps?
Common Data:
> charvec <- format(seq(from = as.Date("1901-01-01"), to = as.Date("1999-12-31"),by = "day"))
> data <- matrix(rnorm(length(charvec) * 5), ncol = 5)> index <- as.POSIXct(charvec, tz = "CET")> class(index)
[1] "POSIXt" "POSIXct"
zoo:
> systemTime(zoo(data, index))
user system elapsed0.03 0.00 0.03
xts:
> systemTime(xts(data, index))
user system elapsed0 0 0
timeSeries:
> systemTime(timeSeries(data, index))
user system elapsed0.025 0.000 0.030
How long does it take to create a one hundred year 5-column series withdaily records from timeDate time stamps?
Common Data:
> charvec <- format(seq(from = as.Date("1901-01-01"), to = as.Date("1999-12-31"),by = "day"))
> data <- matrix(rnorm(length(charvec) * 5), ncol = 5)> index <- timeDate(charvec)> class(index)
[1] "timeDate"attr(,"package")[1] "timeDate"
zoo:
> systemTime(zoo(data, index))
user system elapsed0.03 0.00 0.03
xts:
> systemTime(xts(data, index))
user system elapsed0.01 0.00 0.01
timeSeries:
> systemTime(timeSeries(data, index))
user system elapsed0.01 0.00 0.01
PART V
APPENDIX
137
APPENDIX A
PACKAGES REQUIRED FOR THIS EBOOK
> library(fBasics)
Contributed R Package: zoo
The description file of the zoo package used in this version of the ebook.
> listDescription(zoo)
zoo Description:
Package: zooVersion: 1.6-2Date: 2009-11-23Title: Z's ordered observationsAuthor: Achim Zeileis, Gabor GrothendieckMaintainer: Achim Zeileis <[email protected]>Description: An S3 class with methods for totally ordered
indexed observations. It is particularly aimed atirregular time series of numeric vectors/matricesand factors. zoo's key design goals areindependence of a particular index/date/time classand consistency with ts and base R by providingmethods to extend standard generics.
Depends: R (>= 2.4.1), statsSuggests: coda, chron, DAAG, fCalendar, fSeries, fts, its,
lattice, strucchange, timeDate, timeSeries, tis,tseries, xts
Imports: stats, utils, graphics, grDevices, latticeLazyLoad: yesLicense: GPL-2URL: http://R-Forge.R-project.org/projects/zoo/Packaged: 2009-11-23 10:10:58 UTC; zeileisRepository: CRANDate/Publication: 2009-11-23 10:22:33
138
Built: R 2.9.2; ; 2009-12-01 17:28:50 UTC; windows
Contributed R Package: xts
The description file of the xts package used in this version of the ebook.
> listDescription(xts)
xts Description:
Package: xtsType: PackageTitle: Extensible Time SeriesVersion: 0.7-0Date: 2010-01-04Author: Jeffrey A. Ryan, Josh M. UlrichDepends: zooSuggests: timeSeries,timeDate,tseries,its,chron,fts,tisLazyLoad: yesMaintainer: Jeffrey A. Ryan <[email protected]>Description: Provide for uniform handling of R's different
time-based data classes by extending zoo,maximizing native format information preservationand allowing for user level customization andextension, while simplifying cross-classinteroperability.
License: GPL-3URL: http://r-forge.r-project.org/projects/xts/Packaged: 2010-01-25 16:09:36 UTC; jryanRepository: CRANDate/Publication: 2010-01-26 08:27:09Built: R 2.9.2; i386-pc-mingw32; 2010-01-28 17:32:30 UTC;
windows
Rmetrics Package: timeDate
The description file of the timeDate package used in this version of theebook.
> listDescription(timeDate)
timeDate Description:
Package: timeDateVersion: 2110.88Revision:Date: 2009-12-10Title: Rmetrics - Chronological and Calendarical ObjectsAuthor: Diethelm Wuertz and Yohan Chalabi with contributions
from Martin Maechler, Joe W. Byers, and othersDepends: R (>= 2.6.0), graphics, utils, stats, methodsSuggests: RUnitMaintainer: Rmetrics Core Team <[email protected]>Description: Environment for teaching "Financial Engineering and
Computational Finance"NOTE: SEVERAL PARTS ARE STILL PRELIMINARY AND MAY BE CHANGED
IN THE FUTURE. THIS TYPICALLY INCLUDES FUNCTION ANDARGUMENT NAMES, AS WELL AS DEFAULTS FOR ARGUMENTS ANDRETURN VALUES.
LazyLoad: yesLazyData: yesLicense: GPL (>= 2)URL: http://www.rmetrics.orgBuilt: R 2.9.1; ; 2009-12-28 09:59:44 UTC; windows
Rmetrics Package: timeSeries
The description file of the timeSeries package used in this version of theebook.
> listDescription(timeSeries)
timeSeries Description:
Package: timeSeriesVersion: 2110.88Revision:Date: 2010-01-06Title: Rmetrics - Financial Time Series ObjectsAuthor: Diethelm Wuertz and Yohan ChalabiDepends: R (>= 2.6.0), graphics, grDevices, methods, stats,
utils, timeDate (>= 2100.86)Suggests: robustbase, RUnitMaintainer: Rmetrics Core Team <[email protected]>Description: Environment for teaching "Financial Engineering and
Computational Finance"NOTE: SEVERAL PARTS ARE STILL PRELIMINARY AND MAY BE CHANGED
IN THE FUTURE. THIS TYPICALLY INCLUDES FUNCTION ANDARGUMENT NAMES, AS WELL AS DEFAULTS FOR ARGUMENTS ANDRETURN VALUES.
LazyLoad: yesLazyData: yesLicense: GPL (>= 2)URL: http://www.rmetrics.orgBuilt: R 2.9.1; ; 2010-01-22 23:46:51 UTC; windows
APPENDIX B
FUNCTION LISTINGS
> library(zoo)> library(xts)> library(timeSeries)> library(fBasics)
Contributed R Package: zoo
The functions available from the zoo package used in this version of theebook.
> listFunctions(zoo)
[1] "as.yearmon" "as.yearmon.default" "as.yearqtr"[4] "as.yearqtr.default" "as.yearqtr.yearqtr" "as.zoo"[7] "as.zoo.default" "as.zooreg" "as.zooreg.default"[10] "cbind.zoo" "coredata" "coredata.default"[13] "coredata<-" "format.yearqtr" "frequency<-"[16] "ifelse.zoo" "index" "index<-"[19] "index2char" "is.regular" "is.zoo"[22] "make.par.list" "MATCH" "MATCH.default"[25] "merge.zoo" "na.approx" "na.approx.default"[28] "na.locf" "na.locf.default" "na.spline"[31] "na.spline.default" "na.trim" "na.trim.default"[34] "ORDER" "ORDER.default" "panel.arrows.its"[37] "panel.arrows.tis" "panel.arrows.ts" "panel.arrows.zoo"[40] "panel.lines.its" "panel.lines.tis" "panel.lines.ts"[43] "panel.lines.zoo" "panel.plot.custom" "panel.plot.default"[46] "panel.points.its" "panel.points.tis" "panel.points.ts"[49] "panel.points.zoo" "panel.polygon.its" "panel.polygon.tis"[52] "panel.polygon.ts" "panel.polygon.zoo" "panel.rect.its"[55] "panel.rect.tis" "panel.rect.ts" "panel.rect.zoo"[58] "panel.segments.its" "panel.segments.tis" "panel.segments.ts"[61] "panel.segments.zoo" "panel.text.its" "panel.text.tis"
141
[64] "panel.text.ts" "panel.text.zoo" "plot.zoo"[67] "rbind.zoo" "read.zoo" "rev.zoo"[70] "rollapply" "rollmax" "rollmax.default"[73] "rollmean" "rollmean.default" "rollmedian"[76] "rollmedian.default" "Sys.yearmon" "Sys.yearqtr"[79] "time<-" "write.zoo" "xtfrm.zoo"[82] "yearmon" "yearqtr" "zoo"[85] "zooreg"
Contributed R Package: xts
The functions available from the xts package used in this version of theebook.
> listFunctions(xts)
[1] ".index" ".index<-" ".indexDate"[4] ".indexday" ".indexhour" ".indexisdst"[7] ".indexmday" ".indexmin" ".indexmon"[10] ".indexsec" ".indexwday" ".indexweek"[13] ".indexyday" ".indexyear" ".parseISO8601"[16] ".subset.xts" ".subset_xts" ".xts"[19] "align.time" "apply.daily" "apply.monthly"[22] "apply.quarterly" "apply.weekly" "apply.yearly"[25] "as.fts.xts" "as.timeSeries.xts" "as.xts"[28] "axTicksByTime" "c.xts" "cbind.xts"[31] "CLASS" "CLASS<-" "convertIndex"[34] "diff.xts" "dimnames.xts" "dimnames<-.xts"[37] "endpoints" "first" "firstof"[40] "indexClass" "indexClass<-" "indexFormat"[43] "indexFormat<-" "indexTZ" "is.timeBased"[46] "is.xts" "isOrdered" "lag.xts"[49] "last" "lastof" "merge.xts"[52] "ndays" "nhours" "nminutes"[55] "nmonths" "nquarters" "nseconds"[58] "nweeks" "nyears" "period.apply"[61] "period.max" "period.min" "period.prod"[64] "period.sum" "periodicity" "plot.xts"[67] "rbind.xts" "reclass" "Reclass"[70] "split.xts" "timeBased" "timeBasedRange"[73] "timeBasedSeq" "to.daily" "to.hourly"[76] "to.minutes" "to.minutes10" "to.minutes15"[79] "to.minutes3" "to.minutes30" "to.minutes5"[82] "to.monthly" "to.period" "to.quarterly"[85] "to.weekly" "to.yearly" "try.xts"[88] "use.reclass" "use.xts" "xcoredata"[91] "xcoredata<-" "xts" "xtsAttributes"[94] "xtsAttributes<-" "xtsible"
Rmetrics Package: timeDate
The functions available from the timeDate package used in this versionof the ebook.
> listFunctions(timeDate)
[1] ".by2seconds" ".day.of.week"[3] ".easter" ".easterSunday"[5] ".endpoints" ".fjulian"[7] ".formatFinCenter" ".formatFinCenterNum"[9] ".genDaylightSavingTime" ".isPOSIX"
[11] ".julian" ".JULIAN"[13] ".last.of.nday" ".midnightStandard"[15] ".month.day.year" ".nth.of.nday"[17] ".on.or.after" ".on.or.before"[19] ".sdate" ".sday.of.week"[21] ".sjulian" ".sleap.year"[23] ".subsetByPython" ".subsetBySpan"[25] ".subsetCode" ".whichFormat"[27] "Abidjan" "abline"[29] "Accra" "Adak"[31] "Addis_Ababa" "Adelaide"[33] "Aden" "Advent1st"[35] "Advent2nd" "Advent3rd"[37] "Advent4th" "Algiers"[39] "align" "AllSaints"[41] "AllSouls" "Almaty"[43] "Amman" "Amsterdam"[45] "Anadyr" "Anchorage"[47] "Andorra" "Anguilla"[49] "Annunciation" "Antananarivo"[51] "Antigua" "Apia"[53] "Aqtau" "Aqtobe"[55] "Araguaina" "Aruba"[57] "as.timeDate" "Ascension"[59] "Ashgabat" "AshWednesday"[61] "Asmara" "AssumptionOfMary"[63] "Asuncion" "Athens"[65] "Atikokan" "atoms"[67] "Auckland" "axis.timeDate"[69] "Azores" "Baghdad"[71] "Bahia" "Bahrain"[73] "Baku" "Bamako"[75] "Bangkok" "Bangui"[77] "Banjul" "Barbados"[79] "Beirut" "Belem"[81] "Belgrade" "Belize"[83] "Berlin" "Bermuda"[85] "BirthOfVirginMary" "Bishkek"[87] "Bissau" "Blanc-Sablon"[89] "Blantyre" "blockEnd"[91] "blockStart" "Boa_Vista"[93] "Bogota" "Boise"[95] "BoxingDay" "Bratislava"[97] "Brazzaville" "Brisbane"[99] "Broken_Hill" "Brunei"[101] "Brussels" "Bucharest"[103] "Budapest" "Buenos_Aires"[105] "BuenosAires" "Bujumbura"[107] "CACanadaDay" "CACivicProvincialHoliday"
[109] "CAFamilyDay" "Cairo"[111] "CALabourDay" "Calcutta"[113] "Cambridge_Bay" "Campo_Grande"[115] "Canary" "Cancun"[117] "Cape_Verde" "Caracas"[119] "CaRemembranceDay" "Casablanca"[121] "Casey" "Catamarca"[123] "CAThanksgivingDay" "CAVictoriaDay"[125] "Cayenne" "Cayman"[127] "CelebrationOfHolyCross" "Center"[129] "Ceuta" "Chagos"[131] "CHAscension" "Chatham"[133] "CHBerchtoldsDay" "CHConfederationDay"[135] "Chicago" "Chihuahua"[137] "Chisinau" "CHKnabenschiessen"[139] "Choibalsan" "Chongqing"[141] "Christmas" "ChristmasDay"[143] "ChristmasEve" "ChristTheKing"[145] "CHSechselaeuten" "Cocos"[147] "Colombo" "Comoro"[149] "Conakry" "Copenhagen"[151] "Cordoba" "CorpusChristi"[153] "Costa_Rica" "Cuiaba"[155] "Curacao" "Currie"[157] "Dakar" "Damascus"[159] "Danmarkshavn" "Dar_es_Salaam"[161] "Darwin" "Davis"[163] "Dawson" "Dawson_Creek"[165] "dayOfWeek" "dayOfYear"[167] "DEAscension" "DEChristmasEve"[169] "DECorpusChristi" "DEGermanUnity"[171] "DENewYearsEve" "Denver"[173] "Detroit" "Dhaka"[175] "difftimeDate" "Dili"[177] "Djibouti" "Dominica"[179] "Douala" "Dubai"[181] "Dublin" "DumontDUrville"[183] "Dushanbe" "Easter"[185] "EasterMonday" "Eastern"[187] "EasterSunday" "Edmonton"[189] "Efate" "Eirunepe"[191] "El_Aaiun" "El_Salvador"[193] "Enderbury" "Epiphany"[195] "Eucla" "Fakaofo"[197] "Faroe" "Fiji"[199] "finCenter" "finCenter<-"[201] "Fortaleza" "FRAllSaints"[203] "Frankfurt" "FRArmisticeDay"[205] "FRAscension" "FRAssumptionVirginMary"[207] "FRBastilleDay" "Freetown"[209] "frequency" "FRFetDeLaVictoire1945"[211] "Funafuti" "Gaborone"[213] "Galapagos" "Gambier"[215] "Gaza" "GBBankHoliday"[217] "GBMayDay" "GBMilleniumDay"
[219] "GBSummerBankHoliday" "getRmetricsOption"[221] "getRmetricsOptions" "Gibraltar"[223] "Glace_Bay" "Godthab"[225] "GoodFriday" "Goose_Bay"[227] "Grand_Turk" "Grenada"[229] "Guadalcanal" "Guadeloupe"[231] "Guam" "Guatemala"[233] "Guayaquil" "Guernsey"[235] "Guyana" "Halifax"[237] "Harare" "Harbin"[239] "Havana" "Helsinki"[241] "Hermosillo" "Hobart"[243] "holiday" "holidayNERC"[245] "holidayNYSE" "holidayTSX"[247] "holidayZURICH" "Hong_Kong"[249] "HongKong" "Honolulu"[251] "Hovd" "Indianapolis"[253] "Inuvik" "Iqaluit"[255] "Irkutsk" "isBizday"[257] "isDaily" "isHoliday"[259] "Isle_of_Man" "isMonthly"[261] "isQuarterly" "isRegular"[263] "Istanbul" "isWeekday"[265] "isWeekend" "ITAllSaints"[267] "ITAssumptionOfVirginMary" "ITEpiphany"[269] "ITImmaculateConception" "ITLiberationDay"[271] "ITStAmrose" "Jakarta"[273] "Jamaica" "Jayapura"[275] "Jersey" "Jerusalem"[277] "Johannesburg" "Johnston"[279] "JPAutumnalEquinox" "JPBankHolidayDec31"[281] "JPBankHolidayJan2" "JPBankHolidayJan3"[283] "JPBunkaNoHi" "JPChildrensDay"[285] "JPComingOfAgeDay" "JPConstitutionDay"[287] "JPEmperorsBirthday" "JPGantan"[289] "JPGreeneryDay" "JPHealthandSportsDay"[291] "JPKeirouNOhi" "JPKenkokuKinenNoHi"[293] "JPKenpouKinenBi" "JPKinrouKanshaNoHi"[295] "JPKodomoNoHi" "JPKokuminNoKyujitu"[297] "JPMarineDay" "JPMidoriNoHi"[299] "JPNatFoundationDay" "JPNationalCultureDay"[301] "JPNationHoliday" "JPNewYearsDay"[303] "JPRespectForTheAgedDay" "JPSeijinNoHi"[305] "JPShuubunNoHi" "JPTaiikuNoHi"[307] "JPTennouTanjyouBi" "JPThanksgivingDay"[309] "JPUmiNoHi" "JPVernalEquinox"[311] "Jujuy" "julian"[313] "Juneau" "Kabul"[315] "Kaliningrad" "Kamchatka"[317] "Kampala" "Karachi"[319] "Kashgar" "Katmandu"[321] "Kerguelen" "Khartoum"[323] "Kiev" "Kigali"[325] "Kinshasa" "Kiritimati"[327] "Knox" "Kosrae"
[329] "Krasnoyarsk" "Kuala_Lumpur"[331] "KualaLumpur" "Kuching"[333] "kurtosis" "Kuwait"[335] "Kwajalein" "La_Paz"[337] "La_Rioja" "LaborDay"[339] "Lagos" "Libreville"[341] "Lima" "Lindeman"[343] "lines" "Lisbon"[345] "listFinCenter" "listHolidays"[347] "Ljubljana" "Lome"[349] "London" "Longyearbyen"[351] "Lord_Howe" "Los_Angeles"[353] "LosAngeles" "Louisville"[355] "Luanda" "Lubumbashi"[357] "Lusaka" "Luxembourg"[359] "Macau" "Maceio"[361] "Madeira" "Madrid"[363] "Magadan" "Mahe"[365] "Majuro" "Makassar"[367] "Malabo" "Maldives"[369] "Malta" "Managua"[371] "Manaus" "Manila"[373] "Maputo" "Marengo"[375] "Mariehamn" "Marigot"[377] "Marquesas" "Martinique"[379] "Maseru" "MassOfArchangels"[381] "Mauritius" "Mawson"[383] "Mayotte" "Mazatlan"[385] "Mbabane" "McMurdo"[387] "Melbourne" "Mendoza"[389] "Menominee" "Merida"[391] "Mexico_City" "MexicoCity"[393] "midnightStandard" "midnightStandard2"[395] "Midway" "Minsk"[397] "Miquelon" "Mogadishu"[399] "Monaco" "Moncton"[401] "Monrovia" "Monterrey"[403] "Montevideo" "monthlyRolling"[405] "months" "Monticello"[407] "Montreal" "Montserrat"[409] "Moscow" "Muscat"[411] "Nairobi" "Nassau"[413] "Nauru" "Ndjamena"[415] "New_Salem" "New_York"[417] "NewYearsDay" "NewYork"[419] "Niamey" "Nicosia"[421] "Nipigon" "Niue"[423] "Nome" "Norfolk"[425] "Noronha" "Nouakchott"[427] "Noumea" "Novosibirsk"[429] "Omsk" "Oral"[431] "Oslo" "Ouagadougou"[433] "Pacific" "Pago_Pago"[435] "Palau" "Palmer"[437] "PalmSunday" "Panama"
[439] "Pangnirtung" "Paramaribo"[441] "Paris" "Pentecost"[443] "PentecostMonday" "periodicallyRolling"[445] "periods" "Perth"[447] "Petersburg" "Phnom_Penh"[449] "Phoenix" "Pitcairn"[451] "plot" "Podgorica"[453] "points" "Ponape"[455] "Pontianak" "Port-au-Prince"[457] "Port_Moresby" "Port_of_Spain"[459] "Porto-Novo" "Porto_Velho"[461] "Prague" "PresentationOfLord"[463] "Puerto_Rico" "Pyongyang"[465] "Qatar" "Quinquagesima"[467] "Qyzylorda" "Rainy_River"[469] "Rangoon" "Rankin_Inlet"[471] "Rarotonga" "Recife"[473] "Regina" "Resolute"[475] "Reunion" "Reykjavik"[477] "Riga" "Rio_Branco"[479] "Rio_Gallegos" "Riyadh"[481] "RogationSunday" "Rome"[483] "Rothera" "rulesFinCenter"[485] "Saigon" "Saipan"[487] "Sakhalin" "Samara"[489] "Samarkand" "sample"[491] "San_Juan" "San_Marino"[493] "Santiago" "Santo_Domingo"[495] "Sao_Paulo" "Sao_Tome"[497] "Sarajevo" "Scoresbysund"[499] "Seoul" "Septuagesima"[501] "setRmetricsOptions" "Shanghai"[503] "Shiprock" "Simferopol"[505] "Singapore" "skewness"[507] "Skopje" "Sofia"[509] "SolemnityOfMary" "South_Georgia"[511] "South_Pole" "St_Barthelemy"[513] "St_Helena" "St_Johns"[515] "St_Kitts" "St_Lucia"[517] "St_Thomas" "St_Vincent"[519] "Stanley" "Stockholm"[521] "strptimeDate" "Swift_Current"[523] "Sydney" "Syowa"[525] "Sys.timeDate" "Tahiti"[527] "Taipei" "Tallinn"[529] "Tarawa" "Tashkent"[531] "Tbilisi" "Tegucigalpa"[533] "Tehran" "Tell_City"[535] "Thimphu" "Thule"[537] "Thunder_Bay" "Tijuana"[539] "timeCalendar" "timeDate"[541] "timeFirstDayInMonth" "timeFirstDayInQuarter"[543] "timeLastDayInMonth" "timeLastDayInQuarter"[545] "timeLastNdayInMonth" "timeNdayOnOrAfter"[547] "timeNdayOnOrBefore" "timeNthNdayInMonth"
[549] "timeSequence" "Tirane"[551] "Tokyo" "Tongatapu"[553] "Toronto" "Tortola"[555] "TransfigurationOfLord" "TrinitySunday"[557] "Tripoli" "Truk"[559] "Tucuman" "Tunis"[561] "Ulaanbaatar" "Urumqi"[563] "USChristmasDay" "USColumbusDay"[565] "USCPulaskisBirthday" "USDecorationMemorialDay"[567] "USElectionDay" "USGoodFriday"[569] "Ushuaia" "USInaugurationDay"[571] "USIndependenceDay" "USLaborDay"[573] "USLincolnsBirthday" "USMemorialDay"[575] "USMLKingsBirthday" "USNewYearsDay"[577] "USPresidentsDay" "USThanksgivingDay"[579] "USVeteransDay" "USWashingtonsBirthday"[581] "Uzhgorod" "Vaduz"[583] "Vancouver" "Vatican"[585] "Vevay" "Vienna"[587] "Vientiane" "Vilnius"[589] "Vincennes" "Vladivostok"[591] "Volgograd" "Vostok"[593] "Wake" "Wallis"[595] "Warsaw" "whichFormat"[597] "Whitehorse" "Winamac"[599] "Windhoek" "Winnipeg"[601] "Yakutat" "Yakutsk"[603] "Yekaterinburg" "Yellowknife"[605] "Yerevan" "Zagreb"[607] "Zaporozhye" "Zurich"
Rmetrics Package: timeSeries
The functions available from the timeSeries package used in this versionof the ebook.
> listFunctions(timeSeries)
[1] ".aggregate.timeSeries" ".align.timeSeries"[3] ".applySeries" ".as.data.frame.timeSeries"[5] ".as.list.timeSeries" ".as.matrix.timeSeries"[7] ".as.ts.timeSeries" ".cut.timeSeries"[9] ".description" ".diff.timeSeries"
[11] ".dollar_assign" ".DollarNames.timeSeries"[13] ".end.timeSeries" ".endOfPeriodBenchmarks"[15] ".endOfPeriodSeries" ".endOfPeriodStats"[17] ".extract.turnpointsPastecs" ".fapply"[19] ".findIndex" ".getArgs"[21] ".head.timeSeries" ".isOHLC"[23] ".isOHLCV" ".lines.timeSeries"[25] ".lowessSmoother" ".merge.timeSeries"[27] ".na.omit.timeSeries" ".naOmitMatrix"[29] ".old2newRda" ".old2newTimeSeries"[31] ".plot.timeSeries" ".plot.turnpointsPastecs"
[33] ".plotOHLC" ".plotTimeSeries"[35] ".points.timeSeries" ".print.timeSeries"[37] ".rev.timeSeries" ".rollmax.timeSeries"[39] ".rollmean.timeSeries" ".rollmedian.timeSeries"[41] ".rollmin.timeSeries" ".scale.timeSeries"[43] ".signalCounts" ".signalSeries"[45] ".sort.timeSeries" ".splineSmoother"[47] ".start.timeSeries" ".str.timeSeries"[49] ".subset_timeSeries" ".summary.turnpointsPastecs"[51] ".supsmuSmoother" ".tail.timeSeries"[53] ".time.timeSeries" ".timeSeries"[55] ".turnpoints2" ".turnpointsPastecs"[57] ".turnpointsSeries" ".turnpointsStats"[59] ".validity_timeSeries" ".window.timeSeries"[61] "aggregate" "alignDailySeries"[63] "apply" "applySeries"[65] "as.data.frame" "as.list"[67] "as.matrix" "as.timeSeries"[69] "as.ts" "attach"[71] "colAvgs" "colCummaxs"[73] "colCummins" "colCumprods"[75] "colCumreturns" "colCumsums"[77] "colKurtosis" "colMaxs"[79] "colMeans" "colMins"[81] "colnames" "colnames<-"[83] "colProds" "colQuantiles"[85] "colSds" "colSkewness"[87] "colStats" "colStdevs"[89] "colSums" "colVars"[91] "comment" "comment<-"[93] "countMonthlyRecords" "cumulated"[95] "cut" "description"[97] "diff" "drawdowns"[99] "drawdownsStats" "dummyDailySeries"[101] "dummySeries" "durations"[103] "durationSeries" "end"[105] "fapply" "filter"[107] "getDataPart" "getReturns"[109] "hclustColnames" "head"[111] "interpNA" "is.signalSeries"[113] "is.timeSeries" "is.unsorted"[115] "isMultivariate" "isUnivariate"[117] "lag" "merge"[119] "midquotes" "midquoteSeries"[121] "na.contiguous" "na.omit"[123] "newPositions<-" "ohlcDailyPlot"[125] "orderColnames" "orderStatistics"[127] "outlier" "pcaColnames"[129] "print" "quantile"[131] "rank" "readSeries"[133] "removeNA" "returns"[135] "returnSeries" "rev"[137] "rollDailySeries" "rollMonthlySeries"[139] "rollMonthlyWindows" "rowCumsums"[141] "rownames" "rownames<-"
[143] "runlengths" "sampleColnames"[145] "scale" "series"[147] "series<-" "seriesData"[149] "seriesPositions" "setDataPart"[151] "sort" "sortColnames"[153] "spreads" "spreadSeries"[155] "start" "statsColnames"[157] "str" "substituteNA"[159] "t" "tail"[161] "time" "time<-"[163] "timeSeries" "window"
BIBLIOGRAPHY
zoo Reference ManualAchim Zeileis, Gabor Grothendieckhttp://cran.r-project.org/web/packages/zoo/zoo.pdfzoo FAQ, VignetteGabor Grothendieck, Achim Zeileishttp://cran.r-project.org/web/packages/zoo/vignettes/zoo-faq.pdfzoo Quick Reference, VignetteAjay Shah, Achim Zeileis, Gabor Grothendieckhttp://cran.r-project.org/web/packages/zoo/vignettes/zoo-quickref.pdfzoo: An S3 Class and Methods for Indexed Totally Ordered Observations,VignetteAchim Zeileis, Gabor Grothendieckhttp://cran.r-project.org/web/packages/zoo/vignettes/zoo.pdfxts Reference ManualJeff Ryan, Josh Ulrichhttp://cran.r-project.org/web/packages/xts/xts.pdfxts: Extensible Time Series, VignetteJeff Ryan, Josh Ulrichhttp://cran.r-project.org/web/packages/xts/vignettes/xts.pdftimeDate Reference ManualDiethelm Wuertz, Yohan Chalabihttp://cran.r-project.org/web/packages/timeDate/timeDate.pdftimeDate in RnewsYohan Chalabi, Martin Maechler, Diethelm WuertztimeSeries Reference ManualDiethelm Wuertz, Yohan Chalabihttp://cran.r-project.org/web/packages/timeSeries/timeSeries.pdf
151
INDEX
R classesdata.frame, 43numeric, 2POSIX, 18timeDate, 19, 135timeSeries, 13, 15, 26, 39, 42,
43, 55, 95ts, 130xts, 17, 19, 23, 26, 33, 38, 40,
42, 55zoo, 22, 23, 26, 32, 33, 40, 42
R functionsacf, 117apply, 104approx, 80arima, 116cbind, 40ccf, 117cor, 117cov, 117diff, 105dim, 106dist, 119dnorm, 120end, 112filter, 120finCenter, 20groupGeneric, 61head, 130hist, 122lag, 122lm, 123lowess, 124
mad, 125median, 125na.contiguous, 85pacf, 117print, 89qqline, 125qqnorm, 125rank, 107rbind, 36rev, 108sample, 109scale, 110smooth, 126sort, 28spectrum, 127spline, 81start, 112Sys.timezone, 4timeSeries, 46var, 117xts, 27, 41, 43, 46zoo, 27, 41, 43
152
ABOUT THE AUTHORS
Diethelm Würtz is private lecturer at the Institute for Theoretical Physics,ITP, and for the Curriculum Computational Science and Engineering, CSE,at the Swiss Federal Institute of Technology in Zurich. He teaches Econo-physics at ITP and supervises seminars in Financial Engineering at CSE.Diethelm is senior partner of Finance Online, an ETH spin-off companyin Zurich, and co-founder of the Rmetrics Association.
Andrew Ellis read neuroscience and mathematics at the University inZurich. He is working for Finance Online and is currently doing a 6 monthStudent Internship in the Econophysics group at ETH Zurich at the Insti-tute for Theoretical Physics. Andrew is working on the Rmetrics documen-tation project.
Yohan Chalabi has a master in Physics from the Swiss Federal Institute ofTechnology in Lausanne. He is now a PhD student in the Econophysicsgroup at ETH Zurich at the Institute for Theoretical Physics. Yohan is aco-maintainer of the Rmetrics packages.
153