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()
inputfcast.ts
. x object 'trendx' not found and Inputfcast.ts
ismap(fit.ts, forecast)
.
How do I forecast this data with custom predictors in tslm model?
EDITI 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()
inputtslm
. x object 'trendx' not found Unable to compute required variables from providednew_data
. Does your model require extra variables to produce forecasts?