r - 使用寓言进行预测,如何使用特价?

标签 r fable-r fable

在使用 fable 包进行预测时,我尝试使用 specials,但我无法弄清楚如何使用它们的语法,而且我也找不到一个示例可以用作指南。

我在下面举了一个简单的例子,尝试使用 MEAN 的窗口。预测模型,但它给了我一个错误。

非常感谢任何帮助!

# Load libraries
library(fable)
#> Warning: package 'fable' was built under R version 3.6.3
#> Loading required package: fabletools
#> Warning: package 'fabletools' was built under R version 3.6.3
library(tsibble)
#> Warning: package 'tsibble' was built under R version 3.6.3
library(tsibbledata)
#> Warning: package 'tsibbledata' was built under R version 3.6.3
library(lubridate)
#> Warning: package 'lubridate' was built under R version 3.6.3
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:tsibble':
#> 
#>     interval
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(dplyr)
#> Warning: package 'dplyr' was built under R version 3.6.3
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:lubridate':
#> 
#>     intersect, setdiff, union
#> The following object is masked from 'package:tsibble':
#> 
#>     id
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# Run example
aus_retail %>%
  filter(
    State %in% c("New South Wales", "Victoria"),
    Industry == "Department stores"
  ) %>% 
  model(
    snaive = SNAIVE(Turnover),
    mean_f = MEAN(Turnover, window(size = 12))
  )
#> Error in hasTsp(x): argument "x" is missing, with no default

reprex package于2020年4月23日创建(v0.3.0)

sessionInfo()
#> R version 3.6.2 (2019-12-12)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows Server x64 (build 17763)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_Ireland.1252  LC_CTYPE=English_Ireland.1252   
#> [3] LC_MONETARY=English_Ireland.1252 LC_NUMERIC=C                    
#> [5] LC_TIME=English_Ireland.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] dplyr_0.8.5       lubridate_1.7.8   tsibbledata_0.1.0 tsibble_0.8.6    
#> [5] fable_0.1.2       fabletools_0.1.3 
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.3       compiler_3.6.2   pillar_1.4.3     highr_0.8       
#>  [5] tools_3.6.2      digest_0.6.23    evaluate_0.14    lifecycle_0.2.0 
#>  [9] tibble_3.0.1     gtable_0.3.0     anytime_0.3.7    pkgconfig_2.0.3 
#> [13] rlang_0.4.5      yaml_2.2.0       xfun_0.12        stringr_1.4.0   
#> [17] knitr_1.27       generics_0.0.2   vctrs_0.2.4      grid_3.6.2      
#> [21] tidyselect_1.0.0 glue_1.4.0       R6_2.4.1         rmarkdown_2.1   
#> [25] purrr_0.3.4      ggplot2_3.3.0    tidyr_1.0.2      magrittr_1.5    
#> [29] scales_1.1.0     ellipsis_0.3.0   htmltools_0.4.0  assertthat_0.2.1
#> [33] colorspace_1.4-1 stringi_1.4.5    munsell_0.5.0    crayon_1.3.4

最佳答案

fable包使用公式风格的接口(interface)来进行模型规范,它与lm()函数非常相似。特价商品包含在模型公式的右侧,如果支持多个特价商品,则可以添加它们。不应使用 MEAN(Turnover, window(size = 12)),而应为 MEAN(Turnover ~ window(size = 12)。对于 ARIMA() 模型可以有很多特价,您可以使用 ARIMA(Turnover ~ pdq(0,1,4) + PDQ(3,1,0))

# Load libraries
library(fable)
library(tsibble)
library(tsibbledata)
library(lubridate)
library(dplyr)

# Run example
aus_retail %>%
  filter(
    State %in% c("New South Wales", "Victoria"),
    Industry == "Department stores"
  ) %>% 
  model(
    snaive = SNAIVE(Turnover),
    mean_f = MEAN(Turnover ~ window(size = 12))
  )
#> # A mable: 2 x 4
#> # Key:     State, Industry [2]
#>   State           Industry          snaive   mean_f 
#>   <chr>           <chr>             <model>  <model>
#> 1 New South Wales Department stores <SNAIVE> <MEAN> 
#> 2 Victoria        Department stores <SNAIVE> <MEAN>

reprex package于2020年5月8日创建(v0.3.0)

关于r - 使用寓言进行预测,如何使用特价?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61392597/

相关文章:

r - 您如何在 ggplot 中按大小对堆积条形类别进行排序?

r - Aggregate() 具有更多功能 -

r - 来自 ARIMA 模型的寓言 : Extracting the p, d,q 规范

r - 寓言包 ARIMA 算法是否能够并行工作?

r - 使用 R 进行分层预测

f# - 如何在寓言中使用window.chrome API

r - GBM 模型生成 NA 结果

r - 将不同的 grViz 组合成一个图

r - 如何知道最好的 FASSTER 配方

r - tsibble——当没有隐含的差距时,你如何绕过