tq_mutate() throws error - Loop programming technique
up vote
1
down vote
favorite
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.
r purrr tidyquant
add a comment |
up vote
1
down vote
favorite
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.
r purrr tidyquant
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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.
r purrr tidyquant
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.
r purrr tidyquant
r purrr tidyquant
asked Nov 15 at 13:41
rfinfun
62
62
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320792%2ftq-mutate-throws-error-loop-programming-technique%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320792%2ftq-mutate-throws-error-loop-programming-technique%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown