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

period.apply() returns unusual result when function returns more than one row #382

Open
joshuaulrich opened this issue Nov 3, 2022 · 0 comments
Assignees
Labels

Comments

@joshuaulrich
Copy link
Owner

joshuaulrich commented Nov 3, 2022

Taken from this stackoverflow question. period.apply() returns a matrix with one column per row when the function returns multiple rows per period. But it should return the same result as do.call(rbind, split(x, "[period"])). For example:

dates <- seq.Date(from = as.Date("2010-01-01", format = "%Y-%m-%d"),
                  to = as.Date("2013-12-01", format = "%Y-%m-%d"), by = "month")
 
x <- xts(cbind(seq_along(dates), rev(seq_along(dates))), dates)
 
# this should be the same as do.call(rbind, split(x, "years"))
apply.yearly(x, cumsum)
##              [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]  [,10]  [,11]  [,12]
## 2010-12-01      1      3      6     10     15     21     28     36     45     55     66     78
## 2011-12-01     13     27     42     58     75     93    112    132    153    175    198    222
## 2012-12-01     25     51     78    106    135    165    196    228    261    295    330    366
## 2013-12-01     37     75    114    154    195    237    280    324    369    415    462    510
##             [,13]  [,14]  [,15]  [,16]  [,17]  [,18]  [,19]  [,20]  [,21]  [,22]  [,23]  [,24]
## 2010-12-01     48     95    141    186    230    273    315    356    396    435    473    510
## 2011-12-01     36     71    105    138    170    201    231    260    288    315    341    366
## 2012-12-01     24     47     69     90    110    129    147    164    180    195    209    222
## 2013-12-01     12     23     33     42     50     57     63     68     72     75     77     78

The result should have the same number of observations as the input, x.

##               [,1]   [,2]
## 2010-01-01       1     48
## 2010-02-01       3     95
## 2010-03-01       6    141
## 2010-04-01      10    186
## 2010-05-01      15    230
## 2010-06-01      21    273
## 2010-07-01      28    315
## 2010-08-01      36    356
## 2010-09-01      45    396
## 2010-10-01      55    435
##        ...
## 2013-03-01     114     33
## 2013-04-01     154     42
## 2013-05-01     195     50
## 2013-06-01     237     57
## 2013-07-01     280     63
## 2013-08-01     324     68
## 2013-09-01     369     72
## 2013-10-01     415     75
## 2013-11-01     462     77
## 2013-12-01     510     78

Scenarios to test:

  • period.apply(x, "years", range) should return one row per endpoint, where index(output[i]) <- end(input[i]) with a column for each function output.

  • period.apply(x, years, cummax) should return an xts object with the same number of rows and columns as the input (because of cummax.xts() works by column).

  • period.apply(x, "years", rollapply, by.column = T/F) is a good test case. We'll always rbind() the rollapply() output if it's an xts object. How do we handle if it isn't an xts object?

  • period.apply() should always rbind() the result when FUN returns a 1-row matrix. The index should be set to the last index value in the period.

@joshuaulrich joshuaulrich added this to the 0.12.3 milestone Nov 3, 2022
@joshuaulrich joshuaulrich self-assigned this Nov 3, 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
Projects
None yet
Development

No branches or pull requests

1 participant