I am trying to use a fable
model fit on one group's time series to predict onto another group's time series:
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.
> 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:
> 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. What is going on here?
You're using
id
as a key, which means you fit a separate model for each key. Yet your training data does not containid==B
, so there is noB
model.It is hard to know what you expect here. What model do you want to use for the
B
rows?If you want to use the
A
model, then set up the test set withB
replaced byA
: