joshuaulrich / TTR

Technical analysis and other functions to construct technical trading rules with R
GNU General Public License v2.0
325 stars 102 forks source link

runMedian handling of leading NA with cumulative = T #88

Closed ethanbsmith closed 3 years ago

ethanbsmith commented 4 years ago

Description

I'm not sure if this is a bug or not. It seems an arguable case

runMedian produces NA output for the first n values in a series, if any of the n values is NA This is also inconsistent with other runXXX functions

Expected behavior

since runMedian considers the median of a single input value to be that value itself, runMedian should produce a value based on the available non-NA inputs. I think these should produce identical results

> runMedian(c(NA,2), n = 1, cumulative = F)
[1] NA  2
> runMedian(c(NA,2), n = 1, cumulative = T)
[1] NA NA
> 

Minimal, reproducible example

identical(runMedian(c(NA,2), n = 1, cumulative = T), 
              runMedian(c(NA,2), n = 1, cumulative = F))

Session Info

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                           LC_TIME=English_United States.1252    

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

other attached packages:
[1] TTR_0.23-5       checkpoint_0.4.7

loaded via a namespace (and not attached):
[1] zoo_1.8-6       compiler_3.6.3  tools_3.6.3     xts_0.11-2      curl_4.2        grid_3.6.3      lattice_0.20-38
joshuaulrich commented 3 years ago

This appears fixed in 4b981487d534cef9ff471e203ae80f11e2cd6b8e.

R> TTR::runMedian(c(NA, 2), n = 1, cumulative = FALSE)
[1] NA  2
R> TTR::runMedian(c(NA, 2), n = 1, cumulative = TRUE)
[1] NA  2

Please reopen if it does not fix your actual use case.