title: Combine nesting and rolling_origin from Tidymodels in R
body: <p>I am trying to train a random forest using rolling_origin from the Tidymodels suite. I would like the folds to be exactly the months of the year. Nesting looks like it could do the trick, but tune_grid is not able to find the variables when the data is nested. How can I make this work? I put a reproducible example below.</p>
  <pre><code>
  suppressPackageStartupMessages(library(tidyverse))
  suppressPackageStartupMessages(library(tidymodels))
  suppressPackageStartupMessages(library(yardstick))
  
  # Create dummy data ====================================================================================================
  
  dates &lt;- seq(from = as.Date(&quot;2019-01-01&quot;), to = as.Date(&quot;2019-12-31&quot;), by = 'day' )
  l &lt;- length(dates)
  
  set.seed(1)
  data_set &lt;- data.frame(
    date = dates,
    v1 = rnorm(l),
    v2 = rnorm(l),
    v3 = rnorm(l),
    y = rnorm(l)
  )
  
  # Random Forest Model  =================================================================================================
  
  model &lt;-
    parsnip::rand_forest(
      mode = &quot;regression&quot;,
      trees = tune()) %&gt;%
    set_engine(&quot;ranger&quot;)
  
  # grid specification
  params &lt;-
    dials::parameters(
      trees()
    )
  
  # Set up grid and model workflow =======================================================================================
  
  grid &lt;-
    dials::grid_max_entropy(
      params,
      size = 2
    )
  
  form &lt;- as.formula(paste(&quot;y ~ v1 + v2 + v3&quot;))
  
  model_workflow &lt;-
    workflows::workflow() %&gt;%
    add_model(model) %&gt;%
    add_formula(form)
  
  # Tuning on the normal data set works ====================================================================================================
  
  data_ro_day &lt;- data_set %&gt;%
    rolling_origin(
      initial = 304,
      assess = 30,
      cumulative = TRUE,
      skip = 30
    )
  
  results &lt;- tune_grid(
    model_workflow,
    grid = grid,
    resamples = data_ro_day,
    param_info = params,
    metrics   = metric_set(mae, mape, rmse, rsq),
    control   = control_grid(verbose = TRUE))
  
  results %&gt;% show_best(&quot;mape&quot;, n = 2)
  
  # Tuning on the nested data set doesn't work =========================================================================================
  
  data_ro_month &lt;- data_set %&gt;%
    mutate(year_month = format(date, &quot;%Y-%m&quot;)) %&gt;%
    nest(-year_month) %&gt;%
    rolling_origin(
      initial = 10,
      assess = 1,
      cumulative = TRUE
    )
  
  results &lt;- tune_grid(
      model_workflow,
      grid = grid,
      resamples = data_ro_month,
      param_info = params,
      metrics   = metric_set(mae, mape, rmse, rsq),
      control   = control_grid(verbose = TRUE))
  
  results$.notes ```
  </code></pre>