[英]Fable forecast results for one group using a model fit on a different group
I am trying to use a fable
model fit on one group's time series to predict onto another group's time series:我正在尝试使用适合一组时间序列的
fable
模型来预测另一组的时间序列:
library(dplyr)
library(fable)
library(feasts)
library(tsibble)
library(fabletools)
df <- data.frame(
id = rep(c('A', 'B'), each = 5),
date = seq(as.Date('2020-01-01'), by = "month", length.out = 10),
y = rnorm(10)
)
train_tsbl <- as_tsibble(filter(df, id == 'A'), key = id, index = date)
test_tsbl <- as_tsibble(filter(df, id == 'B'), key = id, index = date)
model <- train_tsbl %>%
model(lm = TSLM(y ~ trend()))
However, when forecasting onto the "test" set – records corresponding to ID 'B', the forecast
call returns an empty result for 'B'
– the test set.但是,当预测到“测试”集 - 对应于 ID 'B' 的记录时,
forecast
调用返回一个空结果'B'
- 测试集。
> forecast(model, test_tsbl)
# A fable: 0 x 4 [?]
# Key: id, .model [0]
# … with 4 variables: id <fct>, .model <chr>, date <date>, y <dist>
But for train_tsbl
, the following:但是对于
train_tsbl
,以下内容:
> forecast(model, train_tsbl)
# A fable: 5 x 5 [1D]
# Key: id, .model [1]
id .model date y .mean
<fct> <chr> <date> <dist> <dbl>
1 A lm 2020-01-01 N(0.19, 1.8) 0.191
2 A lm 2020-02-01 N(-0.12, 1.5) -0.122
3 A lm 2020-03-01 N(-0.42, 1.3) -0.416
4 A lm 2020-04-01 N(-0.73, 1.5) -0.730
5 A lm 2020-05-01 N(-1, 1.8) -1.03
I can't seem to find any option specifying to predict onto new IDs.我似乎找不到任何指定预测新 ID 的选项。 What is going on here?
这里发生了什么?
You're using id
as a key, which means you fit a separate model for each key.您使用
id
作为键,这意味着您为每个键安装了一个单独的模型。 Yet your training data does not contain id==B
, so there is no B
model.但是您的训练数据不包含
id==B
,因此没有B
模型。
It is hard to know what you expect here.很难知道您在这里期望什么。 What model do you want to use for the
B
rows?您想为
B
行使用什么模型?
If you want to use the A
model, then set up the test set with B
replaced by A
:如果要使用
A
模型,则设置测试集,其中B
替换为A
:
> forecast(model, test_tsbl %>% mutate(id = 'A'))
# A fable: 5 x 5 [1D]
# Key: id, .model [1]
id .model date y .distribution
<chr> <chr> <date> <dbl> <dist>
1 A lm 2020-06-01 -0.100 N(-0.10, 0.32)
2 A lm 2020-07-01 -0.217 N(-0.22, 0.42)
3 A lm 2020-08-01 -0.338 N(-0.34, 0.56)
4 A lm 2020-09-01 -0.459 N(-0.46, 0.73)
5 A lm 2020-10-01 -0.575 N(-0.58, 0.93)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.