[R-SIG-Finance] zoo: bug / feature replacing coredata - subsetting by dates
jeff.a.ryan at gmail.com
Sat Apr 25 08:16:53 CEST 2009
This won't work at all in xts.
While the indexing in xts is time-aware (as opposed to simply ordered
like zoo), the internals make use of POSIXct time, which chron does
not map to well.
Gabor is correct in that absent another indexing class, this is simply
a fundamental R issue. I would advise not using chron, as Date would
be a better fit to daily data in this case.
PS. To clarify, I am merely a developer with the zoo project, Achim
and Gabor are the "authors".
On Sat, Apr 25, 2009 at 12:33 AM, Brian G. Peterson <brian at braverock.com> wrote:
> Phil Joubert wrote:
>> Hi all
>> I'm using RBloomberg to download some price and dividend data. I want to
>> construct a time series of total returns, so I need to add the dividends to
>> the price data before taking log returns. My problem is that I obtain very
>> strange behaviour when I try to change a subset of one zoo object (prices)
>> using the indices of the other (dividends).
>> Any ideas, or smarter ways to achieve this?
>> # Connect Bloomberg
>> oBbgConn <- blpConnect(show.days="trading"
>> dtStart <- chron("31/12/1988", format="d/m/y")
>> dtEnd <- chron("31/12/2008", format="d/m/y"))
>> vdPrices <- blpGetData(oBbgConn, sTicker, "PX_LAST", dtStart, dtEnd)
>> # BBG gives dividend data as an awkward table, convert it to a zoo with XD
>> dates and dividend amounts
>> vdDividends <- unlist(blpGetData(oBbgConn, sTicker, "DVD_HIST",
>> cnstszDivColumns <- 7
>> szDividends <- length(vdDividends) / cnstszDivColumns
>> dim(vdDividends) <- c(szDividends, cnstszDivColumns)
>> vdDividends <- merge(zoo(as.numeric(vdDividends[,5]),
>> as.chron(vdDividends[,2])), all=FALSE)
>> vdPricesPlusDivs <- vdPrices
>> #This line returns the expected values
>> coredata(vdPricesPlusDivs[index(vdPrices + vdDividends)])
>> #This should replace the coredata with the sum of the price + div for XD
>> dates only
>> coredata(vdPricesPlusDivs[index(vdPrices + vdDividends)]) <-
>> coredata(vdPrices + vdDividends)
>> #but it does not - it adds in approximately length(vdPrices* vdDividends)
>> NA's to the coredata vector without changing the index vector, and puts the
>> new values at the end. The zoo object vdPricesPlusDivs is now unusable.
>> I suspect this is an issue with date/index handling at some point - if I
>> create dummy timeseries using integers as index variables I cannot replicate
>> the problem. I'm working on a Windows box.
>> Any help or ideas appreciated!
> Use xts instead. It's written by one of the zoo authors (Jeff Ryan) and is
> smart about financial time series, rather than an arbitrary index. Also has
> overall intelligent sub-setting.
> Brian G. Peterson
> Ph: 773-459-4973
> IM: bgpbraverock
> R-SIG-Finance at stat.math.ethz.ch mailing list
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
jeffrey.ryan at insightalgo.com
ia: insight algorithmics
More information about the R-SIG-Finance