如何使用 Fable 包在 R 中的分层系列中实现回归器? [英] How to implement regressors in a Hierarchical Series in R, with the Fable package?

查看:11
本文介绍了如何使用 Fable 包在 R 中的分层系列中实现回归器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是探索寓言包的新手,我想在分层时间序列模型中实现回归器.数据的维度应该如何?tsibble 对象内是否应该有一个额外的列?例如,在 ARIMA 模型中.预先非常感谢您.

I am new to exploring the fable package, and I was wanting to implement Regressors in a Hierarchical Time Series model. How should the Dimensionality of the data be? Should there be an additional column inside the tsibble object? For example, in an ARIMA model. Thank you very much in advance.

推荐答案

使用外生回归量对分层数据进行建模的方法与对常规数据进行建模的方法相同.对于层次结构中的每个节点,外生回归量应该是用于估计模型的 tsibble 对象的列.

The approach for modelling hierarchical data with exogenous regressors is the same as modelling regular data. The exogenous regressors should be a column of the tsibble object used to estimate the model, for each node in the hierarchy.

下面的代码显示了如何使用动态回归模型(ARIMA with xreg)对简单的层次结构 (T = M + F) 进行建模.请注意,这里的外生回归量只是白噪声,但您会在这里使用一些真实数据.

The code below shows how a simple hierarchy (T = M + F) can be modelled with a dynamic regression model (ARIMA with xreg). Note that the exogenous regressors are just white noise here, but you would use some real data here.

library(fable)
#> Loading required package: fabletools
library(dplyr)
my_data <- as_tsibble(cbind(mdeaths, fdeaths)) %>% 
  aggregate_key(key, value = sum(value)) %>% 
  # Add the regressor (if you have this in your data, could aggregate it above)
  # If the data is pre-aggregated, specify which keys are <aggregated> with agg_vec().
  mutate(my_xreg = rnorm(nrow(.)))
my_data
#> # A tsibble: 216 x 4 [1M]
#> # Key:       key [3]
#>       index key          value my_xreg
#>       <mth> <chr*>       <dbl>   <dbl>
#>  1 1974 Jan <aggregated>  3035  -1.87 
#>  2 1974 Feb <aggregated>  2552   1.93 
#>  3 1974 Mar <aggregated>  2704  -0.420
#>  4 1974 Apr <aggregated>  2554   0.332
#>  5 1974 May <aggregated>  2014  -1.10 
#>  6 1974 Jun <aggregated>  1655   1.22 
#>  7 1974 Jul <aggregated>  1721   1.68 
#>  8 1974 Aug <aggregated>  1524  -1.46 
#>  9 1974 Sep <aggregated>  1596   0.620
#> 10 1974 Oct <aggregated>  2074  -0.505
#> # … with 206 more rows
my_data %>% 
  model(ARIMA(value ~ my_xreg))
#> # A mable: 3 x 2
#> # Key:     key [3]
#>   key                        `ARIMA(value ~ my_xreg)`
#>   <chr*>                                      <model>
#> 1 fdeaths      <LM w/ ARIMA(0,0,0)(1,1,1)[12] errors>
#> 2 mdeaths      <LM w/ ARIMA(0,0,2)(0,1,2)[12] errors>
#> 3 <aggregated> <LM w/ ARIMA(0,0,2)(2,1,0)[12] errors>

reprex 包 (v0.3.0) 于 2021 年 1 月 13 日创建

Created on 2021-01-13 by the reprex package (v0.3.0)

这篇关于如何使用 Fable 包在 R 中的分层系列中实现回归器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆