########################################################### # # Materials for Course # Advanced Time Series Modeling # With S-PLUS and S+FinMetrics # Eric Zivot # ########################################################### # Introduction to Time Series Objects ########################################################### # Examples ########################################################### module( finmetrics ) ########################################################### # # timeDate objects (I) # td = timeDate("1/1/2002",in.format="%m/%d/%Y",zone="Pacific") td options("time.in.format") class(td) slotNames(td) ?class.timeDate td@.Data td@format td@time.zone td@format = paste(td@format,"%z") td options("time.zone") td2 = timeDate("Mar 02, 1963 08:00 PM", in.format="%m %d, %Y %H:%M %p", format="%b %02d, %Y %02I:%02M %p %z") td2 td@time.zone = "Eastern" td td@.Data # # timeDate objects (II): MISC date functions # as.character(weekdays(td)) as.character(months(td)) mdy(td) hms(td) wdydy(td) as.numeric(days(td)) hours(td) minutes(td) seconds(td) yeardays(td) # day in the year (of of 365) # # timeDate objects (III) # tdGMT = timeDate("1/1/2002",zone="GMT", format="%m/%d/%02y %H:%02M:%02S %p %z") tdGMT tdGMT@.Data tdPST = timeZoneConvert(tdGMT,"PST") tdPST tdPST@.Data # # manipulating timeDate objects # td1 = timeDate("1/1/2002",in.format="%m/%d/%Y", zone="GMT",format="%m/%d/%04Y %H:%02M:%02S %p %z") td2 = timeDate("2/1/2002",in.format="%m/%d/%Y", zone="GMT",format="%m/%d/%04Y %H:%02M:%02S %p %z") td1 td2 # concatening dates - use concat function td4 = concat(td1,td2) td4 td4@.Data # arithematic calculations as.numeric(td1) td1 + 1 td1 + 0.5 td1 - 1 2*td1 td1+td2 # # timeSpan objects # td.diff = td2 - td1 td.diff class(td.diff) slotNames(td.diff) # # timeRelative objects # args(timeRelative) ?class.timeRelative tr = timeRelative("+1mth +1day +9hr") tr # add relative time to absolute date td1 td1 + tr # forecast horizon computed using absolute time - note increase in hours # due to DST change timeDate('4/1/2004 18:00', zone = 'Eastern') + timeSpan('7d') # timeRelative may be used to compute relative time operations correctly timeDate('4/1/2004 18:00', zone = 'Eastern') + timeRelative('+7day') # # creating sequences of dates # # annual sequences timeCalendar(y=2000:2005, format="%Y") timeSeq("1/1/2000","1/1/2005", by="years", format="%Y") # quarterly timeSeq("1/1/2000","4/1/2001", by="quarters", format="%Y:%Q") # monthly timeSeq("1/1/2000","5/1/2000", by="months", format="%b %Y") # weekly timeSeq("1/1/2000","2/1/2000", by="weeks", format="%a %b %d, %Y") # daily timeSeq("1/1/2000","1/31/2000", by="bizdays", holidays=holiday.NYSE(2000), format="%a %b %d, %Y") # # creating a timeSeries object (convert data frame to time series) # yhoo.df[1:2,] td = timeDate(yhoo.df[,1],in.format="%d-%m-%y", format="%a %b %d, %Y") td[1:2] yhoo.ts = timeSeries(pos=td,data=yhoo.df[,-1]) yhoo.ts[1:2,] # properties of timeSeries objects class(yhoo.ts) slotNames(yhoo.ts) yhoo.ts@data[1:2,] yhoo.ts@positions[1:2] yhoo.ts@title = "Yahoo!" yhoo.ts@documentation = c("Daily open, high, low, close and volume data on Yahoo stock", "sample: 1/1/2002 - 1/28/2002","source: www.yahoo.com") # extractor functions positions(yhoo.ts)[1:2] start(yhoo.ts) end(yhoo.ts) seriesData(yhoo.ts)[1:2,] # # subscripting timeSeries # # subscripting rows and columns yhoo.ts[1:2,1:2] yhoo.ts[1:2,c("Open","Close")] # subscripting rows by date using timeEvent yhoo.ts[timeEvent("2/6/2002","2/8/2002"), 1:2] smpl = positions(yhoo.ts) <= timeDate("2/4/2002") yhoo.ts[smpl,1:2] # concatenating timeSeries ts1 = yhoo.ts[1:2,] ts1 ts2 = yhoo.ts[6:7,] ts2 concat(ts1,ts2) # # creating lags and differences # args(tslag) tslag(yhoo.ts[1:3,"Open"]) tslag(yhoo.ts[1:3,"Open"],k=-1:1) tslag(yhoo.ts[1:3,"Open"],k=-1:1, trim=T) args(diff.timeSeries) diff(yhoo.ts[1:3,"Open"],lag=1,trim=F) diff(yhoo.ts[1:3,"Open"],lag=2,trim=F,pad=0) diff(yhoo.ts[1:3,"Open"],lag=1,diff=2,trim=F) # # plotting timeSeries # wmf.graph(file="C:\\Insightful\\training\\EZtimeSeriesCourse\\timeSeriesObjects\\figYhoo1.wmf") plot(yhoo.ts[,"Open"]) dev.off() wmf.graph(file="C:\\Insightful\\training\\EZtimeSeriesCourse\\timeSeriesObjects\\figYhoo2.wmf") plot(yhoo.ts[,c("Open","Close")], reference.grid=F, plot.args=list(lty=c(1,2), col=c(2,5), lwd=2)) legend(0,15, legend=c("Open","Close"), lty=c(1,2), col=c(2,5), lwd=2) dev.off() wmf.graph(file="C:\\Insightful\\training\\EZtimeSeriesCourse\\timeSeriesObjects\\figYhoo3.wmf") par(mfrow=c(2,1)) plot(yhoo.ts[,1:4], plot.type="hloc", main="High, low, open and close") plot(yhoo.ts[,5], plot.type="stackbar", main="Volume") par(mfrow=c(1,1)) dev.off() wmf.graph(file="C:\\Insightful\\training\\EZtimeSeriesCourse\\timeSeriesObjects\\figYhoo4.wmf") trellisPlot(yhoo.ts[,"Open"],yhoo.ts[,"Close"]) dev.off() trellisPlot(yhoo.ts[,1:4], plot.type="hloc", main="High, low, open and close")