[R-SIG-Finance] zoo: bug / feature replacing coredata - subsetting by dates
Brian G. Peterson
brian at braverock.com
Sat Apr 25 07:33:59 CEST 2009
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
> library("RBloomberg")
> 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", retval="raw"))
> 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
> length(vdPricesPlusDivs)
>
> #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)
>
> length(vdPricesPlusDivs)
>
> #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!
> Phil
>
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
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock
More information about the R-SIG-Finance
mailing list