[R-SIG-Finance] zoo: bug / feature replacing coredata - subsetting by dates

Gabor Grothendieck ggrothendieck at gmail.com
Fri Apr 24 17:32:07 CEST 2009


This has nothing to do with zoo but is just how R works.  For
example consider this which does not use zoo at all:

      library(chron)
      p <- 1:5
      p[chron("01/01/09")] <- 100
      length(p)

The length of the result is 14245 because the index is unclassed
giving 14245 so 100 is assigned to the 14245th element of p and
since p only has 5 elements its extended to have 14245 elements.

Normally in zoo (as is the case with ts in the core of R) one uses a
window<- method like this:

     v <- vdDivs + vdPrices
     window(vdPrices, index(v)) <- v


On Fri, Apr 24, 2009 at 10:52 AM, Phil Joubert <philjoubert at yahoo.com> wrote:
>
>
> Bloomberg free example below:
>
> vdPrices <- zoo(c(0.01,0.01,0.01,0.01,0.01), order.by=seq(chron("31/12/1998", format="d/m/y"), chron("04/01/1999", format="d/m/y"), by="day"))
> length(vdPrices)
>
> vdDivs <- zoo(c(100,100), as.chron(c("01/01/1999","01/02/2009")))
>
> coredata(vdPrices[index(vdDivs+vdPrices)]) <- coredata(vdDivs+vdPrices)
> length(vdPrices)
>
> Its a little clunky, but thats because I've tried to replicate the returns from the BBG calls as closely as possible.
>
> I also noticed a mistake in the earlier code snippet - the merge() call should not be there.
>
> thanks
> Phil
>
>
> --- On Fri, 4/24/09, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
>
>> From: Gabor Grothendieck <ggrothendieck at gmail.com>
>> Subject: Re: [R-SIG-Finance] zoo: bug / feature replacing coredata -  subsetting by dates
>> To: "Phil Joubert" <philjoubert at yahoo.com>
>> Date: Friday, April 24, 2009, 5:02 PM
>> Can you provide a minimal self
>> contained example, i.e. one that
>> does not require bloomberg.
>>
>> On Fri, Apr 24, 2009 at 9:49 AM, Phil Joubert <philjoubert at yahoo.com>
>> 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
>> >
>> > _______________________________________________
>> > R-SIG-Finance at stat.math.ethz.ch
>> mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> > -- Subscriber-posting only.
>> > -- If you want to post, subscribe first.
>> >
>>
>
>
>
>



More information about the R-SIG-Finance mailing list