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

runXXX returns NA for first value for n = 1, when cumulative = T #108

Closed ethanbsmith closed 3 years ago

ethanbsmith commented 3 years ago

Description

mean of a single value should be the value itself, so the first value should not be NA when n = 1 may be related to #88

Expected behavior

when n = 1, first value should be itself

Minimal, reproducible example

TTR::runMean(1, n = 1, cumulative = F)
[1] 1
> TTR::runMean(1, n = 1, cumulative = T)
[1] NA

Session Info

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

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] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] xgboost_1.3.1.1    matrixStats_0.57.0 rollRegres_0.1.3   rio_0.5.16         rvest_0.3.6        xml2_1.3.2         data.table_1.13.6  curl_4.3           quantmod_0.4.18    TTR_0.24.2         xts_0.12.1        
[12] zoo_1.8-8          doParallel_1.0.16  iterators_1.0.13   foreach_1.5.1      plotrix_3.7-8      checkpoint_0.4.10 

loaded via a namespace (and not attached):
 [1] zip_2.1.1        Rcpp_1.0.5       cellranger_1.1.0 compiler_4.0.3   pillar_1.4.7     forcats_0.5.0    tools_4.0.3      checkmate_2.0.0  jsonlite_1.7.2   tibble_3.0.4     lifecycle_0.2.0  lattice_0.20-41  pkgconfig_2.0.3 
[14] rlang_0.4.10     openxlsx_4.2.3   Matrix_1.2-18    rstudioapi_0.13  haven_2.3.1      stringr_1.4.0    httr_1.4.2       vctrs_0.3.6      hms_0.5.3        grid_4.0.3       R6_2.5.0         readxl_1.3.1     foreign_0.8-80  
[27] selectr_0.4-2    magrittr_2.0.1   backports_1.2.1  codetools_0.2-16 ellipsis_0.3.1   stringi_1.5.3    crayon_1.3.4    
ethanbsmith commented 3 years ago

i think the problem is line 89 in runFun.r : is.na(result) <- c(1:(n-1+NAs)) should be: is.na(result) <- c(0:(n-1+NAs))

ethanbsmith commented 3 years ago

other runXXX functions seem to have the same issue

> identical(runMax(1, n = 1, cumulative = T), runMax(1, n = 1, cumulative = F))
[1] FALSE
> identical(runMin(1, n = 1, cumulative = T), runMin(1, n = 1, cumulative = F))
[1] FALSE
joshuaulrich commented 1 year ago

Note for future me: runCov, runCor, runVar, runSD always return NA for the first element when n = 1, cumulative = TRUE, which is consistent with cov(t(1)). Similarly, runMAD(1:2, n = 1, cumulative = TRUE) is zero because mad(1) is zero.