business-science / tidyquant

Bringing financial analysis to the tidyverse
https://business-science.github.io/tidyquant/
Other
853 stars 175 forks source link

tq_mutate gives error with tsibble #147

Open spsanderson opened 4 years ago

spsanderson commented 4 years ago

I have the following code:

# Load Libs ####
# Time Series analysis of CCI30 crypto index
install.load::install_load(
  "tidyquant"
  ,"timetk"
  # , "tibbletime"
  , "tsibble"
  , "sweep"
  , "anomalize"
  , "caret"
  , "forecast"
  , "funModeling"
  , "xts"
  , "fpp"
  , "lubridate"
  , "tidyverse"
  , "urca"
  , "prophet"
)

# Daily OHLCV ####
url <- "https://cci30.com/ajax/getIndexHistory.php"
destfile <- "cci30_OHLCV.csv"
download.file(url, destfile = destfile)
df <- read.csv("cci30_OHLCV.csv")
class(df)

# Format Date ####
df$Date <- lubridate::ymd(df$Date)
df.tibble <- as_tsibble(df, index = Date)
class(df.tibble)
head(df.tibble)
tail(df.tibble)

# Add varaibles ####
df.tibble <- df.tibble %>% 
  tq_mutate(
    select = Close
    , mutate_fun = dailyReturn
    , col_rename = "Daily_Return"
  )

At this point it fails with the following error:

Selecting index: "Date"
Selecting index: "Date"
Selecting index: "Date"
Error in dplyr::matches(date_var) : is_string(match) is not TRUE

Session Info

> sessionInfo()
R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

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

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

other attached packages:
 [1] prophet_0.5                rlang_0.4.2                Rcpp_1.0.3                 urca_1.3-0                
 [5] forcats_0.4.0              stringr_1.4.0              dplyr_0.8.3                purrr_0.3.3               
 [9] readr_1.3.1                tidyr_1.0.0                tibble_2.1.3               tidyverse_1.3.0           
[13] fpp_0.5                    tseries_0.10-47            lmtest_0.9-37              expsmooth_2.3             
[17] fma_2.3                    funModeling_1.9.3          Hmisc_4.3-0                Formula_1.2-3             
[21] survival_2.43-3            forecast_8.10              caret_6.0-85               ggplot2_3.2.1             
[25] lattice_0.20-38            anomalize_0.2.0            sweep_0.2.2                tsibble_0.8.5             
[29] timetk_0.1.2               tidyquant_0.5.9            quantmod_0.4-15            TTR_0.23-6                
[33] PerformanceAnalytics_1.5.3 xts_0.11-2                 zoo_1.8-6                  lubridate_1.7.4           

loaded via a namespace (and not attached):
  [1] readxl_1.3.1        backports_1.1.5     plyr_1.8.5          lazyeval_0.2.2      splines_3.5.3      
  [6] entropy_1.2.1       inline_0.3.15       digest_0.6.23       foreach_1.4.7       htmltools_0.4.0    
 [11] gdata_2.18.0        fansi_0.4.0         magrittr_1.5        checkmate_1.9.4     cluster_2.0.7-1    
 [16] ROCR_1.0-7          recipes_0.1.9       modelr_0.1.5        gower_0.2.1         matrixStats_0.55.0 
 [21] anytime_0.3.6       prettyunits_1.0.2   colorspace_1.4-1    install.load_1.2.1  rvest_0.3.5        
 [26] haven_2.2.0         xfun_0.11           callr_3.4.0         crayon_1.3.4        jsonlite_1.6       
 [31] zeallot_0.1.0       iterators_1.0.12    glue_1.3.1          gtable_0.3.0        ipred_0.9-9        
 [36] pkgbuild_1.0.6      Quandl_2.10.0       rstan_2.19.2        scales_1.1.0        DBI_1.1.0          
 [41] htmlTable_1.13.3    foreign_0.8-71      stats4_3.5.3        lava_1.6.6          StanHeaders_2.19.0 
 [46] prodlim_2019.11.13  htmlwidgets_1.5.1   httr_1.4.1          gplots_3.0.1.1      RColorBrewer_1.1-2 
 [51] acepack_1.4.1       pkgconfig_2.0.3     loo_2.2.0           nnet_7.3-12         dbplyr_1.4.2       
 [56] utf8_1.1.4          tidyselect_0.2.5    reshape2_1.4.3      munsell_0.5.0       cellranger_1.1.0   
 [61] tools_3.5.3         cli_2.0.0           generics_0.0.2      moments_0.14        broom_0.5.3        
 [66] ModelMetrics_1.2.2  processx_3.4.1      knitr_1.26          fs_1.3.1            pander_0.6.3       
 [71] caTools_1.17.1.3    packrat_0.5.0       nlme_3.1-137        xml2_1.2.2          compiler_3.5.3     
 [76] rstudioapi_0.10     curl_4.3            reprex_0.3.0        stringi_1.4.3       ps_1.3.0           
 [81] Matrix_1.2-15       vctrs_0.2.1         pillar_1.4.3        lifecycle_0.1.0     data.table_1.12.8  
 [86] bitops_1.0-6        R6_2.4.1            latticeExtra_0.6-28 KernSmooth_2.23-15  gridExtra_2.3      
 [91] codetools_0.2-16    MASS_7.3-51.1       gtools_3.8.1        assertthat_0.2.1    withr_2.1.2        
 [96] fracdiff_1.5-0      parallel_3.5.3      hms_0.5.2           quadprog_1.5-8      grid_3.5.3         
[101] rpart_4.1-13        timeDate_3043.102   class_7.3-15        pROC_1.15.3         base64enc_0.1-3   
mdancho84 commented 4 years ago

This is an issue with tsibble class . A temporary solution is to convert to as_tibble() prior to using tq_mutate() or tq_transmute(). I will revisit tsibble compatibility in the next version of tidyquant.

spsanderson commented 4 years ago

Sounds great Matt. Conversion to tibble is the same work around I have used.

On Wed, Mar 11, 2020 at 8:20 AM Matt Dancho notifications@github.com wrote:

This is an issue with tsibble class . A temporary solution is to convert to as_tibble() prior to using tq_mutate() or tq_transmute(). I will revisit tsibble compatibility in the next version of tidyquant.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/business-science/tidyquant/issues/147#issuecomment-597600816, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAPCNS5HUXGSPNX52WZDC73RG56XLANCNFSM4KFR6XCQ .

-- Steven P Sanderson II, MPH Book on Lulu http://goo.gl/lmrlFI