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()` should return the ending value when k>1 for `on = months` or `quarters` #300

Closed Eluvias closed 3 years ago

Eluvias commented 5 years ago

This issue is found when on = months or on = quarters and k>1. The case when on = years is not covered here as it seems there is a different problem, see #299 .

library(xts)
set.seed(1234)

xx <- as.xts(as.matrix(rnorm(341*12)),
              seq.Date(as.Date(Sys.Date()),
                       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

# Expect ending date "2030-08-13"
tail(xx[endpoints(xx, on = "quarters", k = 2),], 3) # OK
#>                  [,1]
#> 2029-09-30  0.5760302
#> 2030-03-31 -0.4003829
#> 2030-08-13 -0.2922716
tail(xx[endpoints(xx, on = "quarters", k = 3),], 3) # NOPE
#>                  [,1]
#> 2028-12-31 -0.8780204
#> 2029-09-30  0.5760302
#> 2030-06-30 -1.0143564
tail(xx[endpoints(xx, on = "quarters", k = 4),], 3) # NOPE
#>                  [,1]
#> 2028-03-31  0.4482753
#> 2029-03-31 -0.6757905
#> 2030-03-31 -0.4003829
tail(xx[endpoints(xx, on = "quarters", k = 5),], 3) # NOPE
#>                  [,1]
#> 2027-12-31 -0.0193127
#> 2029-03-31 -0.6757905
#> 2030-06-30 -1.0143564

tail(xx[endpoints(xx, on = "months", k = 2),], 3) # NOPE
#>                  [,1]
#> 2030-03-31 -0.4003829
#> 2030-05-31 -0.9918847
#> 2030-07-31 -0.3445485
tail(xx[endpoints(xx, on = "months", k = 3),], 3) # OK
#>                  [,1]
#> 2030-02-28 -0.3203597
#> 2030-05-31 -0.9918847
#> 2030-08-13 -0.2922716
tail(xx[endpoints(xx, on = "months", k = 4),], 3) # NOPE
#>                  [,1]
#> 2029-09-30  0.5760302
#> 2030-01-31 -1.8123470
#> 2030-05-31 -0.9918847

# For the "weeks" case works fine

tail(xx[endpoints(xx, on = "weeks", k = 2),], 3) # OK
#>                  [,1]
#> 2030-07-21 -0.6757286
#> 2030-08-04  0.6552194
#> 2030-08-13 -0.2922716
tail(xx[endpoints(xx, on = "weeks", k = 3),], 3) # OK
#>                    [,1]
#> 2030-07-14  0.006043235
#> 2030-08-04  0.655219387
#> 2030-08-13 -0.292271556
tail(xx[endpoints(xx, on = "weeks", k = 4),], 3) # OK
#>                  [,1]
#> 2030-06-23  0.9919737
#> 2030-07-21 -0.6757286
#> 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)u

#>  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

Thanks for the report. This looks like a bug, since endpoints() should always return the last observation. I'd really appreciate it if you could write some unit tests for all the cases, including those that currently work okay.

joshuaulrich commented 3 years ago

Thanks again. Sorry it took so long to fix!