Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Eluvias opened this issue Jun 1, 2019 · 5 comments
Closed

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

Eluvias opened this issue Jun 1, 2019 · 5 comments
Milestone

Comments

@Eluvias
Copy link

Eluvias commented Jun 1, 2019

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 joshuaulrich changed the title endpoints() returns incorrect values when on = years and k > 1 endpoints() returns unexpected values when on = years and k > 1 Jun 1, 2019
@joshuaulrich
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

Eluvias commented Jun 1, 2019

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

@joshuaulrich joshuaulrich added this to the 0.12.3 milestone Oct 13, 2022
@joshuaulrich
Copy link
Owner

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
Copy link
Owner

Related to #153.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants