tq_mutate() throws error - Loop programming technique

105 Views Asked by At

Objective: Calculate stochastics with three different values for nFastK for all variables using TTR::stoch and tidyquant packages.

Topic 1: Error message

The snippet below works, but throws an error with option: bounded = TRUE. What is the reason for the error?

rm(list = ls())
library(tidyquant)
library(lubridate)

my_data <- tibble(  Symbol = as_factor(c( rep("a", 100), rep("b", 100)))
                    , Date   = rep( ymd("2017-11-14") + 7 * (0:99), 2) # weekly 
                    , major  = c (10000 + sample(-800:300, 100), (8000 + sample(-100:900, 100)))
                    , v1     = sample(-1000:1000, 200 ) / 100
                    , v2     = sample(-100:1200, 200) / 100
)


my_final <- my_data %>% 
  gather( -Date, -Symbol, key = "kkeys", value = "wwords") %>%
  mutate(kkeys = as_factor(kkeys)) %>%
  group_by(Symbol, kkeys) %>%
  tq_mutate(
    # tq_mutate args
    select     = wwords,
    mutate_fun = stoch, 
    # args to mutate_fun
    nFastK = 10
    # , bounded = FALSE  # <- uncomment this line for error!
  ) %>%
  select( -wwords, -fastD, -stoch ) %>% 
  mutate( fastK = round(fastK, digits = 2)) %>%
  spread( kkeys, fastK) 

Topic 2: Functional programming on this issue.

A for loop produces three values of nFastK calling the above and then renaming and right-joining to the final table like so.
This is just a brief illustration of my original code:

my_periods <- c(5, 10, 20)
my_vars    <- my_data %>% select (-Date, -Symbol) %>% colnames()
my_final   <- my_data

for (i in seq_along(my_periods)) {

  # Create unique Colnames
  my_vars_to <- str_c( my_vars, "_pk", my_periods[i])

  my_final <- 

    my_data %>% 
    # Do all of the above from topic 1 plus this 
    rename_at( vars(my_vars), ~ my_vars_to) %>%
    right_join(my_final,  by = c("Symbol", "Date"))

}

This loop works and gets me what I want. Still being in the steep learning curve, there are two questions:

Question 1: Acc. to Wickham with solutions provided by Arnold, preallocation operates faster. How would this code need to be written to pre-allocate the memory compared to right_join()? Or is this an OK solution? I looked at https://jrnold.github.io/r4ds-exercise-solutions/iteration.html

Question 2: After reading a few tutorials, purrr::map() appears to be appropriate instead of the for loop. Even after reading tutorials and questions here I can't get my head around how to write it properly. Would you please provide an example or point in the direction of more reading?

Finally:

Thank you all the help via examples, vignettes and other posts. This is probably one of the most active, helpful and knowledgable communities I have ever come across. As a new user to R I appreciate the many examples on stackoverflow and any other websites. This is my first post. Thanks, A.

0

There are 0 best solutions below