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

rollapply with width as a list fails on xts #365

Open
BibeFiu opened this issue Apr 10, 2022 · 7 comments
Open

rollapply with width as a list fails on xts #365

BibeFiu opened this issue Apr 10, 2022 · 7 comments
Assignees
Labels
feature request New features

Comments

@BibeFiu
Copy link

BibeFiu commented Apr 10, 2022

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

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

BibeFiu commented Apr 12, 2022

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 joshuaulrich self-assigned this Apr 24, 2022
@joshuaulrich joshuaulrich added the feature request New features label Apr 24, 2022
joshuaulrich added a commit that referenced this issue Apr 24, 2022
Dispatch to rollapply.zoo() when width is a list. Then convert the
resulting zoo object back to an xts object, making sure the tzone and
tclass attributes are set on the resulting xts object.

See #365.
@joshuaulrich
Copy link
Owner

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

joshuaulrich commented May 9, 2022

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

BibeFiu commented May 12, 2022

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

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

@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.

@joshuaulrich joshuaulrich modified the milestones: 0.12.2, 0.12.3 Oct 6, 2022
@joshuaulrich joshuaulrich modified the milestones: 0.13.0, 0.13.1 Jan 16, 2023
@joshuaulrich joshuaulrich removed this from the 0.13.1 milestone Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New features
Projects
None yet
Development

No branches or pull requests

2 participants