0

I am trying to perform an STL decomposition over multiple columns in a dataframe using the feasts package by Rob Hyndman (Forecasting Principles and Practice edition 3 author).

The code below works

dcmp1 <- a_tibble %>%
  model(stl = STL(Cost))
components(dcmp1)

But now I want to generate multiple decompositions for different variables within a_tibble using a for loop. The problem I face is that I iterate over the names of the columns as if they were strings, and that yields an error

Specfically, I am running this code:

for (col_name in c("Cost", "sqrt_cost")){
  
  dcmp <- model(a_tibble, stl = STL(.data[[col_name]]))

}

This yields the following warning everytime the statement within the for loop is run:

Warning: 1 error encountered for stl
[1] values must be length 1,
 but FUN(X[[1]]) result is length 0

I have tried multiple approaches to be able make decompositions for multiple variables using iteration but to no avail.

Couls someone please let me know how I should iterate to avoid this problem?

Thanks

jj_coder
  • 33
  • 4
  • you could try: `dcmp <- model(a_tibble, stl = STL(get(col_name)))` – langtang Feb 07 '22 at 12:16
  • Thank you for your input, but that yields the following error `Error: No supported inverse for the 'get' transformation.` – jj_coder Feb 07 '22 at 12:43
  • Does this answer your question? [dplyr - using column names as function arguments](https://stackoverflow.com/questions/48062213/dplyr-using-column-names-as-function-arguments) – mquantin Feb 07 '22 at 15:22

1 Answers1

0

Ok, so I resorted to this post here:

dplyr - using column names as function arguments

It solved the issue. And it made me feel like I would need to study quite in depth this "quosure" thing.

jj_coder
  • 33
  • 4