为 MEAN 指定 window 时为 NULL 模型(寓言)

NULL model when specifying window for MEAN (fable)

我是 R CodingFable 的新手,目前正在创建一些基本的移动平均预测模型,我的目的是创建多个采用不同周期长度的平均值的模型。
我试图指定 ~window for MEAN(),但它 returns 是 NULL 模型。

这是一些测试数据:

Units <- sample(1:30)
Date  <- ymd(paste0("2019-01-",1:30))
key[1:30] <- "test1"
trial <- bind_cols(Date=Date, Units=Units, key = key) %>%
  as_tsibble(index=Date, key=key)

fit <- trial %>% 
  model(Avg = MEAN(Units~window(size = 12)))

运行 我得到以下结果的代码:


> Units <- sample(1:30)
> Date  <- ymd(paste0("2019-01-",1:30))
> key[1:30] <- "test1"
> trial <- bind_cols(Date=Date, Units=Units, key = key) %>%
+   as_tsibble(index=Date, key=key)
> 
> fit <- trial %>% 
+   model(Avg = MEAN(Units~window(size = 12)))
Advarselsbesked:
1 error encountered for Avg
[1] `slide()` was deprecated in tsibble 0.9.0 and is now defunct.
Please use `slider::slide()` instead.
 
> 
> fit
# A mable: 1 x 2
# Key:     key [1]
  key            Avg
  <chr>      <model>
1 test1 <NULL model>

任何关于如何让 MEAN() 仅取最后 12 个观察值的平均值的指示,将不胜感激。

这适用于使用最新版本的软件包:

library(tsibble)
library(fable)
library(lubridate)
library(dplyr)

Units <- sample(1:30)
Date  <- ymd(paste0("2019-01-",1:30))
key <- "test1"
trial <- bind_cols(Date=Date, Units=Units, key = key) %>%
  as_tsibble(index=Date, key=key)

fit <- trial %>% 
  model(Avg = MEAN(Units ~ window(size = 12)))

# .fitted shows mean of previous 12 observations
augment(fit) %>% tail()
#> # A tsibble: 6 x 7 [1D]
#> # Key:       key, .model [1]
#>   key   .model Date       Units .fitted .resid .innov
#>   <chr> <chr>  <date>     <int>   <dbl>  <dbl>  <dbl>
#> 1 test1 Avg    2019-01-25    24    16.4   7.58   7.58
#> 2 test1 Avg    2019-01-26    27    18.1   8.92   8.92
#> 3 test1 Avg    2019-01-27    26    18.9   7.08   7.08
#> 4 test1 Avg    2019-01-28    18    20.5  -2.5   -2.5 
#> 5 test1 Avg    2019-01-29    11    19.6  -8.58  -8.58
#> 6 test1 Avg    2019-01-30     3    20.1 -17.1  -17.1

# Check
mean(trial$Units[18:29])
#> [1] 20.08333

reprex package (v2.0.0)

于 2021-08-04 创建