Escolar Documentos
Profissional Documentos
Cultura Documentos
R/Rmetrics Workshop
Meielisalp
June 2009
timeDate Class timeSeries Class Summary
Outline
1 timeDate Class
timeDate Definition
Financial Center and Holiday Management
2 timeSeries Class
timeSeres Definition
Plotting timeSeries Objects
Subsetting timeSeries Objects
Manipulating a timeSeries
@recordIDs Concept
3 Summary
timeDate Class timeSeries Class Summary
Outline
1 timeDate Class
timeDate Definition
Financial Center and Holiday Management
2 timeSeries Class
timeSeres Definition
Plotting timeSeries Objects
Subsetting timeSeries Objects
Manipulating a timeSeries
@recordIDs Concept
3 Summary
timeDate Class timeSeries Class Summary
Time and R
There are different date and time classes in R. The most common are
Date : It supports dates without times.
chron : It is a contributed package which provides dates and times.
But there are not time zones or notion of daylight saving time
(DST).
POSIXt : with its two subclasses POSIXct and POSIXlt. These
classes support time zone and DST. But time zone and DST
calculations are passed to the operating system (OS) and may not
work if the OS has bugs or is not up-to-date!
timeDate Class timeSeries Class Summary
timeDate
timeDate class
Slots:
timeDate class
Operations
FinCenter
The local financial center can be defined with the global variable
myFinCenter and refers, by default, to the Greenwich Mean Time (GMT).
list of FinCenters
There are almost 400 financial centers supported thanks to the Olson
data base. They can be listed with listFinCenter().
Note: You can search for financial centers with regular expressions.
> FCs <- listFinCenter()
> head(FCs) # the first few
[1] "Africa/Abidjan" "Africa/Accra"
[3] "Africa/Addis_Ababa" "Africa/Algiers"
[5] "Africa/Asmara" "Africa/Bamako"
> str(FCs) # the "overview"
chr [1:397] "Africa/Abidjan" "Africa/Accra" ...
> listFinCenter("Europe/[AB].*") # -> nine
[1] "Europe/Amsterdam" "Europe/Andorra"
[3] "Europe/Athens" "Europe/Belgrade"
[5] "Europe/Berlin" "Europe/Bratislava"
[7] "Europe/Brussels" "Europe/Bucharest"
[9] "Europe/Budapest"
timeDate Class timeSeries Class Summary
DST rules
Each financial center has an associated function which returns its daylight
saving time rule (DST). Theses functions are named as their financial
center, e.g. Zurich(), and return a data.frame with 4 columns,
> head(Zurich(), 8)
Zurich offSet isdst TimeZone numeric
1 1901-12-14 20:45:52 3600 0 CET -2147397248
2 1941-05-05 00:00:00 7200 1 CEST -904435200
3 1941-10-06 00:00:00 3600 0 CET -891129600
4 1942-05-04 00:00:00 7200 1 CEST -872985600
5 1942-10-05 00:00:00 3600 0 CET -859680000
6 1981-03-29 01:00:00 7200 1 CEST 354675600
7 1981-09-27 01:00:00 3600 0 CET 370400400
8 1982-03-28 01:00:00 7200 1 CEST 386125200
timeDate Class timeSeries Class Summary
Concatenation
Note: The c() method for timeDate objects takes care of the different
financial centers of the object to be concatenated.
> ZH <- timeDate("2009-01-01 16:00:00", zone = "GMT", FinCenter = "Zurich")
> NY <- timeDate("2009-01-01 18:00:00", zone = "GMT", FinCenter = "NewYork")
> c(ZH, NY)
Zurich
[1] [2009-01-01 17:00:00] [2009-01-01 19:00:00]
> c(NY, ZH)
NewYork
[1] [2009-01-01 13:00:00] [2009-01-01 11:00:00]
timeDate Class timeSeries Class Summary
Holidays
Holidays
This gives
timeFirstDayInMonth()
timeLastDayInMonth()
timeFirstDayInQuarter()
timeLastDayInQuarter()
timeNthNdayInMonth()
timeLastNdayInMonth()
timeNdayOnOrAfter()
timeDate Class timeSeries Class Summary
Holidays
Calendar
The following three functions can be used as model to build new holiday
calendars.
holidayZURICH() : the Zurich holiday calendar,
holidayNYSE() : the NYSE stock exchange holiday calendar
and holidayTSX() : the TSX holiday calendar.
timeDate Class timeSeries Class Summary
Logical Test
Weekdays, weekends, business days and holidays can be tested with the
functions:
isWeekday()
isWeekend()
isBizday()
isHoliday()
timeDate Class timeSeries Class Summary
Logical Test
Outline
1 timeDate Class
timeDate Definition
Financial Center and Holiday Management
2 timeSeries Class
timeSeres Definition
Plotting timeSeries Objects
Subsetting timeSeries Objects
Manipulating a timeSeries
@recordIDs Concept
3 Summary
timeDate Class timeSeries Class Summary
timeSeries class
Slots:
Extends:
Class "structure", from data part
Class "vector", by class "structure", distance 2, with explicit coerce
timeSeries class
where
@.Data : time series data in numeric matrix form
@positions : date/time stamps as a character vector
@format : format of the character vector in @positions
@FinCenter : the financial center of the data
@units : the column names of the data matrix
@recordsIDs : record identification for additional information as a
data.frame
@title and @documentation : descriptive character strings
timeDate Class timeSeries Class Summary
timeSeries class
timeSeries class
> ts@.Data
TS.1 TS.2
[1,] -0.326 0.420
[2,] 0.877 0.416
[3,] 0.948 -0.274
timeDate Class timeSeries Class Summary
timeSeries class
> ts@positions
[1] 1230768000 1233446400 1235865600
> ts@units
[1] "TS.1" "TS.2"
> ts@format
[1] "%Y-%m-%d"
> ts@FinCenter
[1] "GMT"
> ts@title
[1] "Time Series Object"
> ts@documentation
[1] "Tue Jun 30 11:48:08 2009"
timeDate Class timeSeries Class Summary
> showMethods("timeSeries")
Function: timeSeries (package timeSeries)
data="ANY", charvec="ANY"
data="ANY", charvec="missing"
data="matrix", charvec="ANY"
data="matrix", charvec="missing"
data="matrix", charvec="numeric"
data="matrix", charvec="timeDate"
data="missing", charvec="ANY"
data="missing", charvec="missing"
timeDate Class timeSeries Class Summary
Multiple Plots
> library(fEcofin)
> LPP <- as.timeSeries(data(SWXLP))[,4:6]
> plot(LPP, main = "LPP")
LPP
125
LP25
110
95
130
LP40
110
90
110
LP60
90
70
Time
timeDate Class timeSeries Class Summary
Single Plots
Subsetting
One can subset a timeSeires object by
Counts
Date/Time Vectors (timeDate)
Logical tests
Other common subsetting rules.
> showMethods("[", classes = "timeSeries")
Function: [ (package base)
x="timeSeries", i="ANY", j="index_timeSeries"
x="timeSeries", i="character", j="character"
x="timeSeries", i="character", j="index_timeSeries"
x="timeSeries", i="character", j="missing"
x="timeSeries", i="index_timeSeries", j="character"
x="timeSeries", i="index_timeSeries", j="index_timeSeries"
x="timeSeries", i="index_timeSeries", j="missing"
x="timeSeries", i="matrix", j="missing"
x="timeSeries", i="missing", j="character"
x="timeSeries", i="missing", j="index_timeSeries"
x="timeSeries", i="missing", j="integer"
(inherited from: x="timeSeries", i="missing", j="index_timeSeries")
x="timeSeries", i="missing", j="missing"
x="timeSeries", i="timeDate", j="character"
x="timeSeries", i="timeDate", j="index_timeSeries"
timeDate Class timeSeries Class Summary
Column Statistics
This functions return a numeric vector of the same length as the number
of columns of the timeSeries.
Column Statistics
> colMeans(LPP)
LP25 LP40 LP60
108.353 102.887 95.412
> colQuantiles(LPP)
LP25 LP40 LP60
97.708 88.278 76.036
> colStats(LPP, FUN = "median")
LP25 LP40 LP60
103.86 100.35 93.82
timeDate Class timeSeries Class Summary
Manipulating a timeSeries
Aggregation
Rolling Windows
There are four functions to bind time series together. These are, with
increasing complexity, c(), cbind(), rbind() and merge().
> (ts1 <- timeSeries(matrix(rnorm(4), ncol = 2), c("2009-01-01", "2009-03-01")))
GMT
TS.1 TS.2
2009-01-01 0.065859 -0.84444
2009-03-01 -1.434893 0.11602
> (ts2 <- timeSeries(matrix(rnorm(4), ncol = 2), c("2009-02-01", "2009-04-01")))
GMT
TS.1 TS.2
2009-02-01 1.31517 -0.97362
2009-04-01 0.99981 -0.97810
timeDate Class timeSeries Class Summary
c()
cbind()
rbind()
merge()
@recordIDs Concept
The slot @recordIDs is meant for additional information that we
want to keep for each time entries but which is not part of data part.
As starting from timeSeries version 2100.84 we have added a
method for the operator $ to access the @recordIDs as well as
the data part.
by default show() will print the data part with the @recordIDs.
Note the * in the column names of @recordIDs in the output.
@recordIDs can be used to give a data.frame behavior to your
time series.
> ts$id <- "id"
> head(ts)
GMT
> cov(ts)
TS.1 TS.2 id*
2009-01-01 0.634370 0.508988 id TS.1 TS.2
2009-02-01 0.206838 0.569707 id TS.1 0.094909 0.017982
2009-03-01 0.617899 0.138199 id TS.2 0.017982 0.084142
2009-04-01 0.954138 0.532224 id
2009-05-01 0.094575 0.248911 id
2009-06-01 0.970725 0.366148 id
timeDate Class timeSeries Class Summary
@recordIDs Example
> library(pastecs)
> setMethod("turnpoints", "timeSeries", function(x)
{
stopifnot(isUnivariate(x))
tp <- turnpoints(as.ts(x))
x$peaks <- tp$peaks #-> need timeSeries >= 2100.84
x$pits <- tp$pits
x
})
[1] "turnpoints"
@recordIDs Example
@recordIDs Example
We plot the original index series and the smoothed series and add points
for the peaks and pits in green and red respectively.
> plot(LP60)
> lines(LP60low, col = "brown", lwd = 2)
> points(LP60low[LP60low$peaks,], col = "green3", pch = 24)
> points(LP60low[LP60low$pits,], col = "red", pch = 25)
@recordIDs Example
120
110
100
90
80
70
Outline
1 timeDate Class
timeDate Definition
Financial Center and Holiday Management
2 timeSeries Class
timeSeres Definition
Plotting timeSeries Objects
Subsetting timeSeries Objects
Manipulating a timeSeries
@recordIDs Concept
3 Summary
timeDate Class timeSeries Class Summary
Summary
References I
> toLatex(sessionInfo())
R/Rmetrics Workshop
Meielisalp
June 2009