Szym_Szym
Szym_Szym

Reputation: 17

Forecasting Time Series Groups with tslm() & tidyverse

I want to fit tslm model to each time series group. I am following example from here but instead of fitting ets model, I would like to fit tslm.

I adjusted the code so it looks like this:

library(tidyverse)
library(timetk)
library(sweep)
library(forecast)

monthly_qty_by_cat2 <- 
  bike_sales %>%
  mutate(order.month = as_date(as.yearmon(order.date))) %>%
  group_by(category.secondary, order.month) %>%
  summarise(total.qty = sum(quantity)) %>% 
  mutate(trendx = row_number())

monthly_qty_by_cat2_nest <- 
  monthly_qty_by_cat2 %>%
  group_by(category.secondary) %>%
  nest() %>%
  mutate(data.ts = map(.x       = data, 
                       .f       = tk_ts, 
                       select   = -order.month, 
                       start    = 2011,
                       freq     = 12)) %>%
  mutate(fit.ts = map(data.ts, ~tslm(total.qty ~ season, data=.x))) %>%
  mutate(fcast.ts = map(fit.ts, forecast))

and it works, BUT when I change

mutate(fit.ts = map(data.ts, ~tslm(total.qty ~ season, data=.x)))

to

mutate(fit.ts = map(data.ts, ~tslm(total.qty ~ trendx, data=.x)))

I get an error:

Error: Problem with mutate() input fcast.ts. x object 'trendx' not found and Input fcast.ts is map(fit.ts, forecast).

How do I forecast this data with custom predictors in tslm model?

EDIT

I rewrote the code in order to use fable package:

monthly_qty_by_cat2 <- 
  bike_sales %>%
  mutate(order.month = as_date(as.yearmon(order.date))) %>%
  group_by(category.secondary, order.month) %>%
  summarise(total.qty = sum(quantity)) %>% 
  mutate(trendx = row_number())

monthly_qty_by_cat2_nest <- 
  monthly_qty_by_cat2 %>%
  group_by(category.secondary) %>% 
  as_tsibble(key = category.secondary)
  
monthly_qty_by_cat2_nest %>%
  model(tslm = TSLM(total.qty ~ trendx)) %>% 
  forecast()

and receive the error:

Error: Problem with mutate() input tslm. x object 'trendx' not found Unable to compute required variables from provided new_data. Does your model require extra variables to produce forecasts?

Upvotes: 1

Views: 935

Answers (1)

Rob Hyndman
Rob Hyndman

Reputation: 31800

library(tidyverse)
library(tsibble)
library(fable)
library(lubridate)

monthly_qty_by_cat2 <- 
  sweep::bike_sales %>%
  mutate(order.month = yearmonth(order.date)) %>%
  group_by(category.secondary, order.month) %>%
  summarise(total.qty = sum(quantity)) %>% 
  as_tsibble(index=order.month, key=category.secondary) %>%
  mutate(x = rnorm(length(total.qty)))
#> `summarise()` regrouping output by 'category.secondary' (override with `.groups` argument)

future_x <- new_data(monthly_qty_by_cat2) %>%
  mutate(x = 2)

monthly_qty_by_cat2 %>%
  model(tslm = TSLM(total.qty ~ trend() + x)) %>%
  forecast(new_data=future_x)
#> # A fable: 9 x 6 [1M]
#> # Key:     category.secondary, .model [9]
#>   category.secondary .model order.month      total.qty  .mean     x
#>   <chr>              <chr>        <mth>         <dist>  <dbl> <dbl>
#> 1 Cross Country Race tslm      2016 Jan N(369, 187840) 369.       2
#> 2 Cyclocross         tslm      2016 Jan N(-2.5, 75604)  -2.50     2
#> 3 Elite Road         tslm      2016 Jan N(784, 322470) 784.       2
#> 4 Endurance Road     tslm      2016 Jan N(159, 117760) 159.       2
#> 5 Fat Bike           tslm      2016 Jan   N(95, 66320)  94.6      2
#> 6 Over Mountain      tslm      2016 Jan  N(194, 57732) 194.       2
#> 7 Sport              tslm      2016 Jan  N(120, 81568) 120.       2
#> 8 Trail              tslm      2016 Jan  N(214, 56269) 214.       2
#> 9 Triathalon         tslm      2016 Jan  N(102, 94449) 102.       2

Created on 2020-07-20 by the reprex package (v0.3.0)

Upvotes: 3

Related Questions