[R-SIG-Finance] problem subsetting xts object with yearmon time index

Joshua Ulrich josh.m.ulrich at gmail.com
Fri Mar 20 19:47:08 CET 2015


On Fri, Mar 20, 2015 at 12:18 PM, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
> I can confirm this behavior with a simplified example using zoo 1.7-12
> and the latest development xts.
>
>> data(sample_matrix)
>> x <- as.xts(sample_matrix)
>> y <- to.monthly(x)
>> y['2007']
>            x.Open   x.High    x.Low  x.Close
> Feb 2007 50.22448 51.32342 50.19101 50.77091
> Mar 2007 50.81620 50.81620 48.23648 48.97490
> Apr 2007 48.94407 50.33781 48.80962 49.33974
> May 2007 49.34572 49.69097 47.51796 47.73780
> Jun 2007 47.74432 47.94127 47.09144 47.76719
>
>
> Thanks for the report.  I'll investigate.
>
This is a timezone issue.  The solution is to ensure your
daily/monthly/quarterly indexed objects have an indexTZ of "UTC".

> indexTZ(msftMonthlyPrices) <- "UTC"
> msftMonthlyPrices['2014']
          MSFT
Jan 2014 36.57
Feb 2014 37.30
Mar 2014 39.91
Apr 2014 39.34
May 2014 40.15
Jun 2014 40.89
Jul 2014 42.32
Aug 2014 44.83
Sep 2014 45.74
Oct 2014 46.33
Nov 2014 47.47
Dec 2014 46.12
Warning message:
timezone of object (UTC) is different than current timezone ().

You can safely ignore the warning.  There probably should be a warning
for non-UTC timezones on objects with a date-based index (or any
higher periodicity)...

> Best,
> Josh
>
>
> On Fri, Mar 20, 2015 at 12:08 PM, Eric Zivot <ezivot at u.washington.edu> wrote:
>> I seem to have a problem sub-setting an xts object with a yearmon time index. I have a xts object holding monthly returns where the time index is a yearmon object. When I subset the data for 2014 using “2014” within the xts object I don’t get the data for January. However, if I subset the data using window() I get all of the data for 2014. Below is the example (from Rstudio on windows 8) and output. Why is January missing when I subset using mydata.xts["2014"]????
>>
>>
>>
>>> options(digits=3, width=70)
>>
>>> # IntroCompFinR available from R-forge
>>
>>> # install.packages("IntroCompFinR", repos="http://R-Forge.R-project.org")
>>
>>> library(IntroCompFinR)
>>
>>> library(PerformanceAnalytics)
>>
>>>
>>
>>> #
>>
>>> # load data and compute returns - MSFT
>>
>>> #
>>
>>>
>>
>>> data(msftMonthlyPrices)
>>
>>> msftRetC = na.omit(Return.calculate(msftMonthlyPrices,
>>
>> +                                     method="log"))
>>
>>> head(msftRetC, n=3)
>>
>>             MSFT
>>
>> Feb 1993 -0.0371
>>
>> Mar 1993  0.1075
>>
>> Apr 1993 -0.0809
>>
>>> tail(msftRetC, n=3)
>>
>>             MSFT
>>
>> Oct 2014  0.0128
>>
>> Nov 2014  0.0243
>>
>> Dec 2014 -0.0289
>>
>>> class(index(msftRetC))
>>
>> [1] "yearmon"
>>
>>> # restric returns to last year
>>
>>> msftRetC2014 = msftRetC["2014"]
>>
>>> msftRetC2014
>>
>>             MSFT
>>
>> Feb 2014  0.0198
>>
>> Mar 2014  0.0676
>>
>> Apr 2014 -0.0144
>>
>> May 2014  0.0204
>>
>> Jun 2014  0.0183
>>
>> Jul 2014  0.0344
>>
>> Aug 2014  0.0576
>>
>> Sep 2014  0.0201
>>
>> Oct 2014  0.0128
>>
>> Nov 2014  0.0243
>>
>> Dec 2014 -0.0289
>>
>>> # why is January missing?????
>>
>>> as.Date(index(msftRetC2014))
>>
>> [1] "2014-02-01" "2014-03-01" "2014-04-01" "2014-05-01" "2014-06-01"
>>
>> [6] "2014-07-01" "2014-08-01" "2014-09-01" "2014-10-01" "2014-11-01"
>>
>> [11] "2014-12-01"
>>
>>> # I get the right data using window()
>>
>>> window(msftRetC, start=as.yearmon("Jan 2014"), end=as.yearmon("Dec 2014"))
>>
>>             MSFT
>>
>> Jan 2014  0.0113
>>
>> Feb 2014  0.0198
>>
>> Mar 2014  0.0676
>>
>> Apr 2014 -0.0144
>>
>> May 2014  0.0204
>>
>> Jun 2014  0.0183
>>
>> Jul 2014  0.0344
>>
>> Aug 2014  0.0576
>>
>> Sep 2014  0.0201
>>
>> Oct 2014  0.0128
>>
>> Nov 2014  0.0243
>>
>> Dec 2014 -0.0289
>>
>>> sessionInfo()
>>
>> R version 3.1.2 (2014-10-31)
>>
>> Platform: x86_64-w64-mingw32/x64 (64-bit)
>>
>>
>>
>> locale:
>>
>> [1] LC_COLLATE=English_United States.1252
>>
>> [2] LC_CTYPE=English_United States.1252
>>
>> [3] LC_MONETARY=English_United States.1252
>>
>> [4] LC_NUMERIC=C
>>
>> [5] LC_TIME=English_United States.1252
>>
>>
>>
>> attached base packages:
>>
>> [1] stats     graphics  grDevices utils     datasets  methods
>>
>> [7] base
>>
>>
>>
>> other attached packages:
>>
>> [1] boot_1.3-13                   PerformanceAnalytics_1.4.3541
>>
>> [3] IntroCompFinR_1.0             xts_0.9-7
>>
>> [5] zoo_1.7-11
>>
>>
>>
>> loaded via a namespace (and not attached):
>>
>> [1] digest_0.6.8    grid_3.1.2      htmltools_0.2.6 lattice_0.20-29
>>
>> [5] rmarkdown_0.5.1 tools_3.1.2     yaml_2.1.13
>>
>>
>>
>> Thanks in advance for any insight.
>>
>>
>>
>>
>>
>> _______________________________________________
>> R-SIG-Finance at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> -- Subscriber-posting only. If you want to post, subscribe first.
>> -- Also note that this is not the r-help list where general R questions should go.
>
>
>
> --
> Joshua Ulrich  |  about.me/joshuaulrich
> FOSS Trading  |  www.fosstrading.com



-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com



More information about the R-SIG-Finance mailing list