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 list fails on xts #365

Open BibeFiu opened 2 years ago

BibeFiu commented 2 years ago

Description

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

The following example should compute the mean of 3 to 5 elements before, my_xts <- .xts(1:6,1:6) rollapplyr(my_xts, width = list(-3:-5), FUN = mean)

but instead reports am error: Error in rollapply.xts(..., align = align) : 'list' object cannot be coerced to type 'integer'

Expected behavior

But it works if my_xts is converted to zoo: rollapplyr(as.zoo(my_xts), width = list(-3:-5), FUN = mean)

Minimal, reproducible example

my_xts <- .xts(1:6,1:6)
rollapplyr(my_xts, width = list(-3:-5), FUN = mean)
joshuaulrich commented 2 years ago

Thanks for the report! I haven't taken a close look yet, but an (intentional) difference between xts and zoo is that the width argument is positive for xts and negative for zoo when you want to look back in time.

BibeFiu commented 2 years ago

Thanks for the quick feedback. I checked the list(3:5) in both zoo and xts. In xts I got the same error. In zoo it does as expected looks into future.

joshuaulrich commented 2 years ago

@BibeFiu can you please try the patch on the 356-rollapply-width branch? Basically, it calls rollapply.zoo() any time width is a list. I hope that works...

joshuaulrich commented 2 years ago

@BibeFiu It would be great if you could try my proposed patch and let me know if it works as expected. You can install the version on the branch from R using the function: remotes::install_github("joshuaulrich/xts@356-rollapply-width") remotes::install_github("joshuaulrich/xts@365-rollapply-width")

BibeFiu commented 2 years ago

@joshuaulrich thanks a lot. I tried to install the update with the code you provided I got a cannot open URL 'https://api.github.com/repos/joshuaulrich/xts/tarball/356-rollapply-width' error

joshuaulrich commented 2 years ago

@BibeFiu: sorry, I made a typo (356 instead of 365 like it should be). Use this command: remotes::install_github("joshuaulrich/xts@365-rollapply-width")

joshuaulrich commented 1 year ago

@BibeFiu Did this for you? I'm preparing an xts release and noticed this branch wasn't merged. I'd like to include it in this upcoming release.