簡體   English   中英

使用模型擬合另一組的寓言預測結果

[英]Fable forecast results for one group using a model fit on a different group

我正在嘗試使用適合一組時間序列的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()))

但是,當預測到“測試”集 - 對應於 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>

但是對於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 

我似乎找不到任何指定預測新 ID 的選項。 這里發生了什么?

您使用id作為鍵,這意味着您為每個鍵安裝了一個單獨的模型。 但是您的訓練數據不包含id==B ,因此沒有B模型。

很難知道您在這里期望什么。 您想為B行使用什么模型?

如果要使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM