[英]Forecasting Time Series Groups with tslm() & tidyverse
I want to fit tslm model to each time series group.我想将 tslm model 适合每个时间序列组。 I am following example from here but instead of fitting ets model, I would like to fit tslm.
我从这里跟随示例,但我不想安装 ets model,而是安装 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
.错误:
mutate()
输入fcast.ts
有问题。 x object 'trendx' not found and Inputfcast.ts
ismap(fit.ts, forecast)
.x object 'trendx' not found 并且输入
fcast.ts
是map(fit.ts, forecast)
。
How do I forecast this data with custom predictors in tslm model?如何使用 tslm model 中的自定义预测器预测此数据?
I rewrote the code in order to use fable package:我重写了代码以使用寓言 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
.错误:
mutate()
输入tslm
有问题。 x object 'trendx' not found Unable to compute required variables from providednew_data
.x object 'trendx' not found 无法从提供的
new_data
计算所需的变量。 Does your model require extra variables to produce forecasts?您的 model 是否需要额外的变量来生成预测?
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)由reprex package (v0.3.0) 于 2020 年 7 月 20 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.