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

rollapply with width as a numeric vector fails on xts #290

Open quantitative-technologies opened 5 years ago

quantitative-technologies commented 5 years ago

rollapply does not work for xts when the width parameter is a numeric vector.

The following example should compute the cumulative sum, but instead does nothing:

my_xts <- .xts(1:6,1:6)
cumsum <- rollapplyr(my_xts, width = 1:6, FUN = sum)
cumsum
                    [,1]
1970-01-01 00:00:01    1
1970-01-01 00:00:02    2
1970-01-01 00:00:03    3
1970-01-01 00:00:04    4
1970-01-01 00:00:05    5
1970-01-01 00:00:06    6

But it works if my_xts is converted to zoo:

rollapplyr(as.zoo(my_xts), width = 1:6, FUN = sum)
1970-01-01 00:00:01  1
1970-01-01 00:00:02  3
1970-01-01 00:00:03  6
1970-01-01 00:00:04 10
1970-01-01 00:00:05 15
1970-01-01 00:00:06 21

Session Info


R version 3.5.3 (2019-03-11)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.2 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_CA.UTF-8          LC_NUMERIC=C                  LC_TIME=en_CA.UTF-8           LC_COLLATE=en_CA.UTF-8       
 [5] LC_MONETARY=en_CA.UTF-8       LC_MESSAGES=en_CA.UTF-8       LC_PAPER=en_CA.UTF-8          LC_NAME=en_CA.UTF-8          
 [9] LC_ADDRESS=en_CA.UTF-8        LC_TELEPHONE=en_CA.UTF-8      LC_MEASUREMENT=en_CA.UTF-8    LC_IDENTIFICATION=en_CA.UTF-8

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] tbl2xts_0.1.2              IKTrading_1.0              roxygen2_6.1.1             Rcpp_1.0.1                
 [5] quantstrat_0.15.0          blotter_0.14.2             FinancialInstrument_1.3.1  usethis_1.4.0             
 [9] devtools_2.0.1             pryr_0.1.4                 timeDate_3043.102          magrittr_1.5              
[13] dtplyr_0.0.3               data.table_1.12.0          reticulate_1.11.1          XLConnect_0.2-15          
[17] XLConnectJars_0.2-15       timetk_0.1.1.1             tidyquant_0.5.5            quantmod_0.4-13           
[21] TTR_0.23-4                 PerformanceAnalytics_1.5.2 xts_0.11-2                 zoo_1.8-4                 
[25] lubridate_1.7.4            forcats_0.4.0              stringr_1.4.0              dplyr_0.8.0.1             
[29] purrr_0.3.2                tidyr_0.8.3                tibble_2.1.1               ggplot2_3.1.0             
[33] tidyverse_1.2.1            digest_0.6.18              foreign_0.8-70             nlme_3.1-137              
[37] readr_1.3.1                rlist_0.4.6.1              here_0.1                   tictoc_1.0                
[41] lattice_0.20-38            boot_1.3-20                h2o_3.22.1.1               feather_0.3.2             
[45] jsonlite_1.6               doParallel_1.0.14          iterators_1.0.10           foreach_1.4.4             
[49] scales_1.0.0               assertthat_0.2.0           pacman_0.5.1               knitr_1.22                

loaded via a namespace (and not attached):
 [1] fs_1.2.6          bitops_1.0-6      httr_1.4.0        rprojroot_1.3-2   tools_3.5.3       backports_1.1.3  
 [7] R6_2.4.0          lazyeval_0.2.2    colorspace_1.4-1  withr_2.1.2       tidyselect_0.2.5  prettyunits_1.0.2
[13] processx_3.3.0    curl_3.3          compiler_3.5.3    cli_1.1.0         rvest_0.3.2       xml2_1.2.0       
[19] desc_1.2.0        quadprog_1.5-5    callr_3.2.0       commonmark_1.7    rmarkdown_1.12    htmltools_0.3.6  
[25] pkgconfig_2.0.2   sessioninfo_1.1.1 highr_0.7         rlang_0.3.1       readxl_1.3.1      rstudioapi_0.9.0 
[31] generics_0.0.2    RCurl_1.95-4.12   Matrix_1.2-16     Quandl_2.9.1      munsell_0.5.0     stringi_1.4.3    
[37] yaml_2.2.0        MASS_7.3-51.1     pkgbuild_1.0.2    plyr_1.8.4        grid_3.5.3        crayon_1.3.4     
[43] haven_2.1.0       hms_0.4.2         ps_1.3.0          pillar_1.3.1      codetools_0.2-16  pkgload_1.0.2    
[49] glue_1.3.1        evaluate_0.13     remotes_2.0.2     modelr_0.1.4      testthat_2.0.1    cellranger_1.1.0 
[55] gtable_0.2.0      xfun_0.5          broom_0.5.1       rJava_0.9-10      memoise_1.1.0