0

I've being playing with the hts package in order to forecast some products that are linked through different categories, it's a typical case where the data is something like:

Business Units (BU) => Category => Sub-Category => SKU

So far I've been able to create the hts object according to the needed hierarchy and also run some forecasts for all the aggregated data. Since there are different BU's I want to run a different model for each BU, and also analyse the data for each BU separately. I know I can make an hts object for each BU and then use combinef to aggregate all the forecasts but I was wondering if it was possible to do it with just one hts object and some modified GmatrixH function.

To make it more graphically, lets assume I have something like this in my initial hts data:

=> A
  => A20
    => A201
    => A202
  => A30
    => A301
    => A302
=> B
  => B10
    => B101
    => B102
  => B20
    => B201
    => B202

My goal is to able to separate the data to just something like this:

=> A
  => A20
    => A201
    => A202
  => A30
    => A301
    => A302

Example code: Rather than use a different data set lets assume that htseg2 is the data, so, looking at the aggts of htseg2 we see:

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

If I do:

all_y_lA <- aggts(htseg2, levels=1)

I get:

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

And what I want is to do something like:

all_y_lA <- aggts(htseg2, levels="A")

And get:

              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

Then analyse and forecast A and all childs of A, and then the same for B.

Hope I make myself clear, thanks in advance for any help.

JuanD
  • 35
  • 4
  • 1
    Rather than graphical samples, it would be more helpful to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with a small sample input data and the code you are using to create your hts objects. – MrFlick Apr 14 '15 at 05:20
  • Ok, added some code and data to replicate. – JuanD Apr 14 '15 at 15:59
  • That sample output doesn't make it easy to recreate the objects in a new R session. If i can't copy/paste the code and run it, it's not reproducible. – MrFlick Apr 14 '15 at 17:03
  • Ok, thanks anyway. Maybe someone else? – JuanD Apr 14 '15 at 21:15

0 Answers0