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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…