joshuaulrich / xts

Extensible time series class that provides uniform handling of many R time series classes by extending zoo.
http://joshuaulrich.github.io/xts/
GNU General Public License v2.0
220 stars 71 forks source link

`endpoints()` returns unexpected values when `on = years` and k > 1 #299

Closed Eluvias closed 1 year ago

Eluvias commented 5 years ago
library(xts)
set.seed(1234)

xx <- as.xts(as.matrix(rnorm(341*12)),
              seq.Date(as.Date("2019-06-01"),
                       by = "day",
                       length.out = 341*12))

head(xx, 2)
#>                  [,1]
#> 2019-06-01 -1.2070657
#> 2019-06-02  0.2774292
tail(xx, 2)
#>                  [,1]
#> 2030-08-12  1.7300719
#> 2030-08-13 -0.2922716

# expected starting date 2020-12-31
xx[endpoints(xx, on = "years", k = 2),]
#>                  [,1]
#> 2019-12-31 -1.6070809
#> 2021-12-31 -0.3770091
#> 2023-12-31  0.7714331
#> 2025-12-31  0.4784294
#> 2027-12-31 -0.0193127
#> 2029-12-31  0.2524384
#> 2030-08-13 -0.2922716

# expected starting date 2021-12-31
xx[endpoints(xx, on = "years", k = 3),]
#>                   [,1]
#> 2019-12-31 -1.60708094
#> 2022-12-31  0.04152571
#> 2025-12-31  0.47842942
#> 2028-12-31 -0.87802036
#> 2030-08-13 -0.29227156

# Also, passing negative numbers into `k` should return the k = 1 endpoints, but :

xx[endpoints(xx, on = "years", k = -2),]
#>                   [,1]
#> 2020-12-31 -1.01371766
#> 2022-12-31  0.04152571
#> 2024-12-31  0.35920383
#> 2026-12-31  0.52029913
#> 2028-12-31 -0.87802036
#> 2030-08-13 -0.29227156
xx[endpoints(xx, on = "years", k = -3),]
#>                  [,1]
#> 2020-12-31 -1.0137177
#> 2023-12-31  0.7714331
#> 2026-12-31  0.5202991
#> 2029-12-31  0.2524384
#> 2030-08-13 -0.2922716

devtools::session_info()
#> - Session info ----------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.6.0 (2019-04-26)
#>  os       Windows 7 x64 SP 1          
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United Kingdom.1252 
#>  ctype    English_United Kingdom.1252 
#>  tz       Europe/London               
#>  date     2019-06-01                  
#> 
#> - Packages --------------------------------------------------------------
#>  package     * version date       lib source
#>  xts         * 0.12-1  2019-06-01 [1] Github (joshuaulrich/xts@efe399f)

#>  zoo         * 1.8-6   2019-05-28 [1] CRAN (R 3.6.0)                   
#> 
#> [1] C:/Program Files/R/library
#> [2] C:/Program Files/R/R-3.6.0/library
joshuaulrich commented 5 years ago

I'm not sure this is a bug. endpoints() is based on calculations from the Unix origin (midnight 1970-01-01). That said, I can understand how this is unexpected behavior.

joshuaulrich commented 5 years ago

Oh, I'd also appreciate it if you could omit the advertisement for the reprex package from your reports. Sorry to be picky, but I find it very annoying.

Eluvias commented 5 years ago

You're right it's annoying, I missed it.

joshuaulrich commented 1 year ago

I'm going to resolve this by updating the documentation to clarify how endpoints() calculates the return values based on periods from the origin (midnight 1970-01-01).

Also, note that setting k < 1 is now an error.

joshuaulrich commented 1 year ago

Related to #153.