xts icon indicating copy to clipboard operation
xts copied to clipboard

Wrong description of the period.apply function returned value in the xts package

Open bobbyx27 opened this issue 7 years ago • 2 comments

Description

Wrong description of the period.apply function returned value in the xts package user's manual

Expected behavior

It is said that one should get "A vector with length of INDEX minus 1",

Observed behavior

I get an output of length = length(INDEX) + as.numeric(tail(INDEX,1) < length(x)) (admitting x is a vector). More precisely, I observed that the returned vector is:

[ FUN(x[1:INDEX[1]]), ..., FUN(x[(INDEX[k]+1):INDEX[k+1]]), ...]

adding FUN(x[(tail(INDEX,1)+1):length(x)]) at the end if tail(INDEX,1) < length(x).

Minimal, reproducible example

Try to modify start and icr in the following:

period.apply(1:10, seq(start, 10, by = icr), mean)

Session Info

R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252    LC_MONETARY=French_France.1252 LC_NUMERIC=C                  
[5] LC_TIME=French_France.1252    

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

other attached packages:
[1] xts_0.10-0 zoo_1.8-0 

loaded via a namespace (and not attached):
[1] compiler_3.4.1  tools_3.4.1     grid_3.4.1      lattice_0.20-35

bobbyx27 avatar Aug 07 '17 12:08 bobbyx27

Your example, period.apply(1:10, seq(start, 10, by = icr), mean), will provide unusual results whenever start != 0. This is because period.apply() expects INDEX to be similar to the output of endpoints(), which always returns a vector that has zero as the first value and nrow(x) as the last value.

Thanks for the report! I will amend the documentation.

joshuaulrich avatar Aug 14 '17 14:08 joshuaulrich

I still don't know if the description or the function itself is wrong:

From the current Description in the documentation:

...where the interval is taken to be the data from INDEX[k] to INDEX[k+1]...

According to the code, the Description should be:

...where the interval is taken to be the data from INDEX[k]**_+1_** to INDEX[k+1]...

PhilippBuehler avatar Dec 20 '18 08:12 PhilippBuehler

The description in the documentation is wrong. I'll fix. Thanks for the report!

joshuaulrich avatar Oct 31 '22 21:10 joshuaulrich