Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

How to implement regressors in a Hierarchical Series in R, with the Fable package?

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...