problem subsetting xts object with yearmon time index

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

problem subsetting xts object with yearmon time index

Eric Zivot
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.

         
 


_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: problem subsetting xts object with yearmon time index

Joshua Ulrich
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.

Best,
Josh


On Fri, Mar 20, 2015 at 12:08 PM, Eric Zivot <[hidden email]> 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.
>
>
>
>
>
> _______________________________________________
> [hidden email] 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

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: problem subsetting xts object with yearmon time index

Daniel Cegiełka
2015-03-20 18:18 GMT+01:00 Joshua Ulrich <[hidden email]>:

> 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.
>
> Best,
> Josh

hi,
I can not reproduce this error with zoo-1.7-12 and xts-0.8-0.

> getSymbols('MSFT')
[1] "MSFT"
> x <- xts::to.monthly(MSFT)
> x['2015']
         MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted
Jan 2015     46.66     47.91    40.35      40.40   918956900         40.11
Feb 2015     40.59     44.30    40.23      43.85   656204800         43.85
Mar 2015     43.67     44.19    40.61      42.29   512133400         42.29
> x['2014']
         MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted
Jan 2014     37.35     37.89    34.63      37.84   930226200         36.57
Feb 2014     37.74     38.46    35.69      38.31   705304500         37.30
Mar 2014     37.92     41.50    37.49      40.99   778425700         39.91
Apr 2014     41.15     41.66    38.90      40.40   746113500         39.34
May 2014     40.24     40.97    38.51      40.94   574362900         40.15
Jun 2014     40.95     42.29    39.86      41.70   555779700         40.89
Jul 2014     41.86     45.71    41.05      43.16   731616500         42.32
Aug 2014     43.21     45.47    42.21      45.43   513919700         44.83
Sep 2014     45.43     47.57    44.53      46.36   860827300         45.74
Oct 2014     46.27     46.97    42.10      46.95   853235700         46.33
Nov 2014     46.89     50.05    46.73      47.81   522988700         47.47
Dec 2014     47.88     49.06    44.90      46.45   626740700         46.12


Daniel

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: problem subsetting xts object with yearmon time index

Joshua Ulrich
On Fri, Mar 20, 2015 at 12:30 PM, Daniel Cegiełka
<[hidden email]> wrote:

> 2015-03-20 18:18 GMT+01:00 Joshua Ulrich <[hidden email]>:
>> 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.
>>
>> Best,
>> Josh
>
> hi,
> I can not reproduce this error with zoo-1.7-12 and xts-0.8-0.
>
>> getSymbols('MSFT')
> [1] "MSFT"
>> x <- xts::to.monthly(MSFT)
>> x['2015']
>          MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted
> Jan 2015     46.66     47.91    40.35      40.40   918956900         40.11
> Feb 2015     40.59     44.30    40.23      43.85   656204800         43.85
> Mar 2015     43.67     44.19    40.61      42.29   512133400         42.29
>> x['2014']
>          MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted
> Jan 2014     37.35     37.89    34.63      37.84   930226200         36.57
> Feb 2014     37.74     38.46    35.69      38.31   705304500         37.30
> Mar 2014     37.92     41.50    37.49      40.99   778425700         39.91
> Apr 2014     41.15     41.66    38.90      40.40   746113500         39.34
> May 2014     40.24     40.97    38.51      40.94   574362900         40.15
> Jun 2014     40.95     42.29    39.86      41.70   555779700         40.89
> Jul 2014     41.86     45.71    41.05      43.16   731616500         42.32
> Aug 2014     43.21     45.47    42.21      45.43   513919700         44.83
> Sep 2014     45.43     47.57    44.53      46.36   860827300         45.74
> Oct 2014     46.27     46.97    42.10      46.95   853235700         46.33
> Nov 2014     46.89     50.05    46.73      47.81   522988700         47.47
> Dec 2014     47.88     49.06    44.90      46.45   626740700         46.12
>
You can't reproduce this with an object returned by getSymbols because
getSymbols sets the index timezone to "UTC".  The xts sample_matrix
data, and IntroCompFinR msftMonthlyPrices data do not have an index
timezone, so the machine's local timezone is used when converting the
ISO 8601 string to a datetime.

>
> Daniel


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

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: problem subsetting xts object with yearmon time index

Joshua Ulrich
In reply to this post by Joshua Ulrich
On Fri, Mar 20, 2015 at 12:18 PM, Joshua Ulrich <[hidden email]> 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 <[hidden email]> 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.
>>
>>
>>
>>
>>
>> _______________________________________________
>> [hidden email] 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

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: problem subsetting xts object with yearmon time index

Eric Zivot
Thanks Josh for the quick reply. However, I am a bit puzzled by the problem. Why should a yearmon (or Date object) time index have a time zone attribute? Shouldn't time zone issues only affect time-date objects? It seems nutty that a date which represents a Year-month can be impacted by a time zone offset from UTC. I know this happens because the internal date for the yearmon object is the first day of the month. I guess this is one of the hidden dangers of using xts objects for calendar time series.

****************************************************************
*  Eric Zivot                                 *
*  Robert Richards Chaired Professor of Economics              *
*  Department of Economics                                     *
*  Adjunct Professor of Finance                                *
*  Adjunct Professor of Statistics
*  Box 353330                  email:  [hidden email] *
*  University of Washington    phone:  206-543-6715            *
*  Seattle, WA 98195-3330                                      *                                                           *
*  www:  http://faculty.washington.edu/ezivot                  *
****************************************************************

On Fri, 20 Mar 2015, Joshua Ulrich wrote:

> On Fri, Mar 20, 2015 at 12:18 PM, Joshua Ulrich <[hidden email]> 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 <[hidden email]> 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.
>>>
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> [hidden email] 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
>
> _______________________________________________
> [hidden email] 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.

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: problem subsetting xts object with yearmon time index

Joshua Ulrich
On Fri, Mar 20, 2015 at 2:11 PM, Eric Zivot <[hidden email]> wrote:
> Thanks Josh for the quick reply. However, I am a bit puzzled by the problem.
> Why should a yearmon (or Date object) time index have a time zone attribute?
> Shouldn't time zone issues only affect time-date objects? It seems nutty
> that a date which represents a Year-month can be impacted by a time zone
> offset from UTC. I know this happens because the internal date for the
> yearmon object is the first day of the month. I guess this is one of the
> hidden dangers of using xts objects for calendar time series.
>
The timezone matters because xts objects always store the index as
POSIXct.  So, internally, there's always a time component.

How did you construct the msftMonthlyPrices data?  The xts constructor
will throw a warning if you try to supply a timezone with a
Date-classed index, but does not if the class is yearmon or yearqtr
(though it should).

>
>
> On Fri, 20 Mar 2015, Joshua Ulrich wrote:
>
>> On Fri, Mar 20, 2015 at 12:18 PM, Joshua Ulrich <[hidden email]>
>> 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 <[hidden email]>
>>> 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.
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> [hidden email] 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
>>
>> _______________________________________________
>> [hidden email] 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

_______________________________________________
[hidden email] 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.