title: Forecasting Time Series Groups with tslm() & tidyverse
body: <p>I want to fit tslm model to each time series group.
  I am following example from <a href="https://cran.rstudio.com/web/packages/sweep/vignettes/SW01_Forecasting_Time_Series_Groups.html#step-3-forecasting-the-model" rel="nofollow noreferrer">here</a> but instead of fitting ets model, I would like to fit tslm.</p>
  <p>I adjusted the code so it looks like this:</p>
  <pre><code>library(tidyverse)
  library(timetk)
  library(sweep)
  library(forecast)
  
  monthly_qty_by_cat2 &lt;- 
    bike_sales %&gt;%
    mutate(order.month = as_date(as.yearmon(order.date))) %&gt;%
    group_by(category.secondary, order.month) %&gt;%
    summarise(total.qty = sum(quantity)) %&gt;% 
    mutate(trendx = row_number())
  
  monthly_qty_by_cat2_nest &lt;- 
    monthly_qty_by_cat2 %&gt;%
    group_by(category.secondary) %&gt;%
    nest() %&gt;%
    mutate(data.ts = map(.x       = data, 
                         .f       = tk_ts, 
                         select   = -order.month, 
                         start    = 2011,
                         freq     = 12)) %&gt;%
    mutate(fit.ts = map(data.ts, ~tslm(total.qty ~ season, data=.x))) %&gt;%
    mutate(fcast.ts = map(fit.ts, forecast))
  </code></pre>
  <p>and it works, BUT when I change</p>
  <pre><code>mutate(fit.ts = map(data.ts, ~tslm(total.qty ~ season, data=.x)))
  </code></pre>
  <p>to</p>
  <pre><code>mutate(fit.ts = map(data.ts, ~tslm(total.qty ~ trendx, data=.x)))
  </code></pre>
  <p>I get an error:</p>
  <blockquote>
  <p>Error: Problem with <code>mutate()</code> input <code>fcast.ts</code>.
  x object 'trendx' not found
  and Input <code>fcast.ts</code> is <code>map(fit.ts, forecast)</code>.</p>
  </blockquote>
  <p>How do I forecast this data with custom predictors in tslm model?</p>
  EDIT
  <p>I rewrote the code in order to use fable package:</p>
  <pre><code>monthly_qty_by_cat2 &lt;- 
    bike_sales %&gt;%
    mutate(order.month = as_date(as.yearmon(order.date))) %&gt;%
    group_by(category.secondary, order.month) %&gt;%
    summarise(total.qty = sum(quantity)) %&gt;% 
    mutate(trendx = row_number())
  
  monthly_qty_by_cat2_nest &lt;- 
    monthly_qty_by_cat2 %&gt;%
    group_by(category.secondary) %&gt;% 
    as_tsibble(key = category.secondary)
    
  monthly_qty_by_cat2_nest %&gt;%
    model(tslm = TSLM(total.qty ~ trendx)) %&gt;% 
    forecast()
  </code></pre>
  <p>and receive the error:</p>
  <blockquote>
  <p>Error: Problem with <code>mutate()</code> input <code>tslm</code>.
  x object 'trendx' not found
  Unable to compute required variables from provided <code>new_data</code>.
  Does your model require extra variables to produce forecasts?</p>
  </blockquote>