[R-SIG-Finance] xts assignment via yearmon or string-based indexing

Jeff Ryan jeff.a.ryan at gmail.com
Tue Dec 2 04:47:54 CET 2008


Hi Brian,

The issue is an incompatibility between what zoo keeps in the index
attribute, and what your development version does.

The new xts stores its index as as.numeric(as.POSIXct(yourtimeindex)).
 This is for a multitude of efficiency reasons.

In some places within zoo the code accesses the index via attr(x,
'index') (or in this case index.zoo(), which does the same).  So you
are effectively trying to use a numeric value of 1858.33 and comparing
it to a very small negative integer:

What is actually stored in xts-dev index:
> as.numeric(as.POSIXct(head(index(series))))  # also accessible with the new .index() call
[1] -3534364800 -3531686400 -3529267200 -3526588800 -3523996800 -3521318400

What you are comparing it to:
> as.numeric(head(index(series)))
[1] 1858.000 1858.083 1858.167 1858.250 1858.333 1858.417

This is a unique issue because xts subclasses zoo, specifically the
public interface to zoo compenents (notably relying on index()
dispatch to 'do the right thing', which in this case [index.xts] is to
automatically convert the raw POSIXct to yearmon --- what you are
_expecting_).

In a few spots, zoo directly accesses the index attribute and causes a
breakage between the two.  It has been quite rare, but you have
stumbled across one such place.

Either use the subsetting directly in xts, or convert to zoo and then
reconvert to xts.  For reference, the replacement method for [<-.xts
has been added to the development branch.

> series['185806']
          [,1]
Jun 1858 FALSE
> series['185806'] <- TRUE
> series['185806']
         [,1]
Jun 1858 TRUE

I will add a window method here, but also discuss with Gabor and Achim
about changing the code in zoo that is causing issues to use the
index() dispatch.  This would allow for others to extend zoo without
getting stuck.

Thanks,
Jeff


On Mon, Dec 1, 2008 at 9:09 PM, Brian Lee Yung Rowe <brian at muxspace.com> wrote:
> Surprisingly, that doesn't work. What is particularly strange is that the
> indexing yields (to me) unexpected results. Do you know if this is expected
> behavior? Please bear with the code below; the pay off is at the end (if you
> consider strange behavior a payoff).
>
>> class(index(series))
> [1] "yearmon"
>> head(series) # Initial state
>           [,1]
> Jan 1857 FALSE
> Feb 1857 FALSE
> Mar 1857 FALSE
> Apr 1857 FALSE
> May 1857 FALSE
> Jun 1857 FALSE
>> tail(series)
>           [,1]
> Jul 2001 FALSE
> Aug 2001 FALSE
> Sep 2001 FALSE
> Oct 2001 FALSE
> Nov 2001 FALSE
> Dec 2001 FALSE
>
>> y1 <- as.yearmon("1858-05")
>> y2 <- as.yearmon("1858-07")
>> window(series, start = y1, end = y2) <- TRUE
>>
>> head(series)
>           [,1]
> Jan 1857 FALSE
> Feb 1857 FALSE
> Mar 1857 FALSE
> Apr 1857 FALSE
> May 1857 FALSE # Expected T
> Jun 1857 FALSE # Expected T
>
>> # This is the strange part
>> head(window(series, start=y1))
>           [,1]
> Feb 1970 FALSE # But y1 == as.yearmon("1858-05")
> Mar 1970 FALSE
> Apr 1970 FALSE
> May 1970 FALSE
> Jun 1970 FALSE
> Jul 1970 FALSE
>
>> head(window(series, start=y1, end=c(2001,12))) # No data returned?
> Data:
> numeric(0)
>
> Index:
> integer(0)
>> head(window(series, start=y1, end=as.yearmon('2001-12'))) # Same thing
>> using yearmon?
> Data:
> numeric(0)
>
> Index:
> integer(0)
>
>> # Let's try y2 just for fun
>> y2
> [1] "Jul 1858"
>> head(window(series, start=y2))
>           [,1]
> Feb 1970 FALSE # ???
> Mar 1970 FALSE
> Apr 1970 FALSE
> May 1970 FALSE
> Jun 1970 FALSE
> Jul 1970 FALSE
>
>
> It seems that the values of the yearmons are being interpreted incorrectly.
> Any ideas on why this is happening and what is to be done about it?
>
> Thanks,
> Brian
>
> ----- Original message -----
> Sent: 2008/11/30 21:07:06
> Subject: Re:Re: [R-SIG-Finance] xts assignment via yearmon or string-based
> indexing
>
> Don't know what is wrong but as a workaround note that since xts is a
> subclass of zoo you can also use zoo methods on xts objects.
> See ?window.zoo
>
> y1 <- as.yearmon("1858-05")
> y2 <- as.yearmon("1858-07")
> window(series, start = y1, end = y2) <- TRUE
>
>
>
> On Sun, Nov 30, 2008 at 6:06 PM, Brian Lee Yung Rowe wrote:
>> Hi,
>>
>> I have an xts series and want to update some of the values in the series.
>> This works fine when I use an integer index (or list of integer indices),
>> but it doesn't work if I use other methods like the string-based indexing
>> or
>> a list of yearmons. Is this the expected behavior or am I doing something
>> wrong?
>>
>> Brian
>>
>>
>>> dates <- timeBasedSeq('185801/2002')
>>> series <- xts(rep(F, length(dates)), dates)
>>
>>> series['1858-05::1858-07'] <- T
>>> head(series)
>> [,1]
>> Jan 1858 FALSE
>> Feb 1858 FALSE
>> Mar 1858 FALSE
>> Apr 1858 FALSE
>> May 1858 FALSE # Expected to be T
>> Jun 1858 FALSE # Expected to be T
>>
>>> series[dates.1] <- T
>>> series[dates.1]
>> [,1]
>> May 1858 FALSE # Expected to be T
>> Jun 1858 FALSE # Expected to be T
>> Jul 1858 FALSE # Expected to be T
>>
>>> series[1] <- T
>>> head(series)
>> [,1]
>> Jan 1858 TRUE # T as expected
>> Feb 1858 FALSE
>> Mar 1858 FALSE
>> Apr 1858 FALSE
>> May 1858 FALSE
>> Jun 1858 FALSE
>>
>>> series[c(2,3)] <- T
>>> head(series)
>> [,1]
>> Jan 1858 TRUE
>> Feb 1858 TRUE # This worked as expected
>> Mar 1858 TRUE # This worked as expected
>> Apr 1858 FALSE
>> May 1858 FALSE
>> Jun 1858 FALSE
>>
>>
>>
>> _______________________________________________
>> 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.
>>
> _______________________________________________
> 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.
>



-- 
Jeffrey Ryan
jeffrey.ryan at insightalgo.com

ia: insight algorithmics
www.insightalgo.com



More information about the R-SIG-Finance mailing list