How can I generalize it? Suppose I have a string vector like:
list_of_regressor <- colnames(us_change)[3:6]
The function pmap uses as an argument a list with specific strings. How can I make it more general, as if the list of regressor can get bigger, if necessary.
I tried this way, but it didn't worked:
library(purrr)
library(fable)
library(fabletools)
library(rlang)
list_of_regressor <- c("1", colnames(us_change)[3:6])
mlist <- map(1:length(list_of_regressor), ~ combn(list_of_regressor, .x) %>%
apply(., 2, function(v) paste0("Consumption", " ~ ", paste(v, collapse = " + ")))) %>%
unlist()
df_mlist <- data.frame(formula_str = mlist) %>%
mutate(formula_sym = lapply(formula_str, as.symbol)) %>%
mutate(models = set_names(map(formula_str, TSLM), map_chr(formula_sym, deparse)))
us_change %>%
model(
!!!df_mlist$models
)
Update #1: I was able to run the following code, but I don't know if all the lines is actually needed...
library(purrr)
library(fable)
library(fabletools)
library(rlang)
list_of_regressor <- c("1", colnames(us_change)[3:6])
mlist <- map(1:length(list_of_regressor), ~ combn(list_of_regressor, .x) %>%
apply(., 2, function(v) paste0("Consumption", " ~ ", paste(v, collapse = " + ")))) %>%
unlist()
df_mlist <- data.frame(formula_str = mlist) %>%
mutate(formula_sym = lapply(formula_str, as.symbol)) %>%
mutate(formula_list = set_names(map(formula_str, as.formula))) %>%
mutate(models = set_names(map(formula_list, TSLM), map_chr(formula_sym, deparse)))