我正在玩hts包以预测一些通过不同类别链接的产品,这是典型的情况,其中的数据类似于:
Business Units (BU) => Category => Sub-Category => SKU
到目前为止,我已经能够根据所需的层次结构创建hts对象,并对所有聚合数据运行一些预测。 由于存在不同的BU,我想为每个BU运行不同的模型,并且还分别分析每个BU的数据。我知道我可以为每个BU创建一个hts对象,然后使用combinef聚合所有预测,但我想知道是否可以只使用一个hts对象和一些修改的 GmatrixH函数来完成它。
为了更加图形化,让我们假设我的初始hts数据中有这样的东西:
=> A
=> A20
=> A201
=> A202
=> A30
=> A301
=> A302
=> B
=> B10
=> B101
=> B102
=> B20
=> B201
=> B202
我的目标是能够将数据分成这样的东西:
=> A
=> A20
=> A201
=> A202
=> A30
=> A301
=> A302
示例代码:假设htseg2是数据,而不是使用不同的数据集,因此,查看htseg2的聚合,我们看到:
all_y <- aggts(htseg2)
Time Series:
Start = 1992
End = 2007
Frequency = 1
Total A B A10 A20 B30 B40 A10A A10B A10C A20A A20B B30A B30B B40A
1992 -2.2112904 -5.103778 2.892488 -4.344453 -0.7593252 0.8077966 2.084691 -2.423474 -1.1620698 -0.7589091 -0.4983286 -0.26099657 0.009695794 0.3270945 0.4710063
1993 -1.7426751 -4.850060 3.107385 -4.103407 -0.7466530 0.8326560 2.274729 -2.196065 -1.1576038 -0.7497389 -0.4979251 -0.24872791 0.022737218 0.3358589 0.4740598
1994 -1.4533386 -4.688907 3.235569 -3.960646 -0.7282608 0.9080350 2.327534 -2.187683 -1.0524048 -0.7205582 -0.4954755 -0.23278530 0.064194769 0.3367939 0.5070464
1995 -1.0151533 -4.386465 3.371312 -3.695228 -0.6912372 0.9728715 2.398440 -1.932308 -1.0519018 -0.7110175 -0.4818329 -0.20940428 0.098071735 0.3611585 0.5136413
1996 -0.2687558 -3.947446 3.678690 -3.331862 -0.6155842 1.0763970 2.602293 -1.637783 -1.0139068 -0.6801723 -0.4253173 -0.19026691 0.146079487 0.3738506 0.5564668
1997 -0.1039130 -3.891031 3.787118 -3.284696 -0.6063356 1.1220049 2.665113 -1.627386 -0.9823162 -0.6749934 -0.4247379 -0.18159770 0.170240635 0.3890064 0.5627578
1998 0.0319309 -3.815398 3.847329 -3.219069 -0.5963289 1.1310482 2.716281 -1.614443 -0.9486647 -0.6559620 -0.4150329 -0.18129604 0.173920725 0.3916081 0.5655194
1999 0.2386283 -3.689795 3.928423 -3.125673 -0.5641226 1.1638044 2.764619 -1.590909 -0.8935917 -0.6411720 -0.3924141 -0.17170855 0.205094593 0.3931835 0.5655263
2000 0.4762250 -3.580883 4.057108 -3.036069 -0.5448139 1.2111129 2.845995 -1.532260 -0.8687816 -0.6350273 -0.3785346 -0.16627934 0.235250213 0.4085820 0.5672806
2001 0.7640473 -3.373380 4.137427 -2.877374 -0.4960051 1.2474490 2.889978 -1.390682 -0.8537141 -0.6329781 -0.3748754 -0.12112965 0.241523003 0.4140894 0.5918366
2002 0.9577465 -3.321418 4.279164 -2.836797 -0.4846210 1.2635735 3.015591 -1.367034 -0.8388968 -0.6308661 -0.3713899 -0.11323106 0.253885861 0.4170116 0.5926760
2003 1.1278373 -3.244452 4.372290 -2.786883 -0.4575690 1.3112792 3.061011 -1.334805 -0.8283677 -0.6237109 -0.3536588 -0.10391014 0.279924841 0.4247898 0.6065645
2004 1.3618556 -3.130861 4.492716 -2.715788 -0.4150729 1.3568514 3.135865 -1.318941 -0.8255442 -0.5713028 -0.3182323 -0.09684063 0.283730794 0.4450246 0.6280960
2005 1.7552645 -2.991873 4.747138 -2.615245 -0.3766279 1.3736268 3.373511 -1.242796 -0.8054984 -0.5669504 -0.3044265 -0.07220149 0.288582178 0.4568084 0.6282362
2006 2.0580599 -2.936933 4.994993 -2.582444 -0.3544895 1.4013893 3.593604 -1.240751 -0.7970741 -0.5446189 -0.3015478 -0.05294174 0.296206517 0.4598029 0.6453799
2007 2.3738339 -2.836078 5.209912 -2.511904 -0.3241741 1.4523193 3.757593 -1.220774 -0.7866848 -0.5044450 -0.2963444 -0.02782969 0.317670953 0.4671018 0.6675465
B40B B40C
1992 0.6941430 1.390548
1993 0.7440005 1.530729
1994 0.7800831 1.547451
1995 0.8195736 1.578866
1996 0.8798774 1.722416
1997 0.9265925 1.738521
1998 0.9710759 1.745205
1999 1.0151125 1.749507
2000 1.0558906 1.790104
2001 1.0990149 1.790963
2002 1.2131865 1.802404
2003 1.2471263 1.813884
2004 1.3140384 1.821827
2005 1.3778140 1.995697
2006 1.3785545 2.215049
2007 1.3875096 2.370083
如果我这样做:
all_y_lA <- aggts(htseg2, levels=1)
我明白了:
Time Series:
Start = 1992
End = 2007
Frequency = 1
A B
1992 -5.103778 2.892488
1993 -4.850060 3.107385
1994 -4.688907 3.235569
1995 -4.386465 3.371312
1996 -3.947446 3.678690
1997 -3.891031 3.787118
1998 -3.815398 3.847329
1999 -3.689795 3.928423
2000 -3.580883 4.057108
2001 -3.373380 4.137427
2002 -3.321418 4.279164
2003 -3.244452 4.372290
2004 -3.130861 4.492716
2005 -2.991873 4.747138
2006 -2.936933 4.994993
2007 -2.836078 5.209912
我想做的是做一些事情:
all_y_lA <- aggts(htseg2, levels="A")
得到:
A A10 A20 A10A A10B A10C A20A A20B
1992 -5.103778 -4.344453 -0.7593252 -2.423474 -1.1620698 -0.7589091 -0.4983286 -0.26099657
1993 -4.85006 -4.103407 -0.746653 -2.196065 -1.1576038 -0.7497389 -0.4979251 -0.24872791
1994 -4.688907 -3.960646 -0.7282608 -2.187683 -1.0524048 -0.7205582 -0.4954755 -0.2327853
1995 -4.386465 -3.695228 -0.6912372 -1.932308 -1.0519018 -0.7110175 -0.4818329 -0.20940428
1996 -3.947446 -3.331862 -0.6155842 -1.637783 -1.0139068 -0.6801723 -0.4253173 -0.19026691
1997 -3.891031 -3.284696 -0.6063356 -1.627386 -0.9823162 -0.6749934 -0.4247379 -0.1815977
1998 -3.815398 -3.219069 -0.5963289 -1.614443 -0.9486647 -0.655962 -0.4150329 -0.18129604
1999 -3.689795 -3.125673 -0.5641226 -1.590909 -0.8935917 -0.641172 -0.3924141 -0.17170855
2000 -3.580883 -3.036069 -0.5448139 -1.53226 -0.8687816 -0.6350273 -0.3785346 -0.16627934
2001 -3.37338 -2.877374 -0.4960051 -1.390682 -0.8537141 -0.6329781 -0.3748754 -0.12112965
2002 -3.321418 -2.836797 -0.484621 -1.367034 -0.8388968 -0.6308661 -0.3713899 -0.11323106
2003 -3.244452 -2.786883 -0.457569 -1.334805 -0.8283677 -0.6237109 -0.3536588 -0.10391014
2004 -3.130861 -2.715788 -0.4150729 -1.318941 -0.8255442 -0.5713028 -0.3182323 -0.09684063
2005 -2.991873 -2.615245 -0.3766279 -1.242796 -0.8054984 -0.5669504 -0.3044265 -0.07220149
2006 -2.936933 -2.582444 -0.3544895 -1.240751 -0.7970741 -0.5446189 -0.3015478 -0.05294174
2007 -2.836078 -2.511904 -0.3241741 -1.220774 -0.7866848 -0.504445 -0.2963444 -0.02782969
然后分析和预测A和所有 A 的孩子,然后分析和预测 B 。
希望我能让自己清楚,先谢谢你的帮助。