Search code examples
rtime-serieshierarchical

How can I get Forecast of Time Series with periods the right way?


I am starting with time series forecasting with hts package in R.

I've already got my data and applied forecast() function. Summary of my forecast is like (the numbers are sample):

Hierarchical Time Series                            
2 Levels                            
Number of nodes at each level: 1 8                          
Total number of series: 9                           
Number of observations in each historical series: 53                            
Number of forecasts per series: 12  

Top level series of forecasts:                          

    Jan Feb Mar Apr May Jun Jul
2018                        107 239
2019    127 184 244 123 249     


    Aug Sep Oct Nov Dec     
2018                        107 239
2019    127 184 244 123 249     

So, hts actually knows the periods that I am forecasting for, Jun 2018 and further (12 points). But when I trying to get bts of my time series, I get only values, not periods. Is there a way to build a data frame with periods in hts?

The Decision that I have in my head:

  1. transform bts into a matrix
  2. transform matrix into a simple time series, starting with a known period.

But it does not seem best and can cause errors - we know our desired periods in hts, why don't get them at once?

Thank You for Your kindness.


Solution

  • Here is an example:

    library(hts)
    #> Loading required package: forecast
    
    fc <- forecast(htseg1)
    fc
    #> Hierarchical Time Series 
    #> 3 Levels 
    #> Number of nodes at each level: 1 2 5 
    #> Total number of series: 8 
    #> Number of observations in each historical series: 10 
    #> Number of forecasts per series: 10 
    #> Top level series of forecasts: 
    #> Time Series:
    #> Start = 2002 
    #> End = 2011 
    #> Frequency = 1 
    #>  [1] 53.43685 53.78687 54.13689 54.48690 54.83692 55.18694 55.53696
    #>  [8] 55.88698 56.23699 56.58701
    fc$bts
    #> Time Series:
    #> Start = 2002 
    #> End = 2011 
    #> Frequency = 1 
    #>             AA       AB       AC       BA       BB
    #> 2002  9.286307 10.05677 10.47942 11.04014 12.57421
    #> 2003  9.460299 10.10103 10.49552 11.05347 12.67655
    #> 2004  9.634290 10.14528 10.51162 11.06681 12.77889
    #> 2005  9.808281 10.18954 10.52772 11.08014 12.88122
    #> 2006  9.982273 10.23379 10.54382 11.09348 12.98356
    #> 2007 10.156264 10.27804 10.55993 11.10681 13.08590
    #> 2008 10.330255 10.32230 10.57603 11.12014 13.18823
    #> 2009 10.504247 10.36655 10.59213 11.13348 13.29057
    #> 2010 10.678238 10.41081 10.60823 11.14681 13.39291
    #> 2011 10.852229 10.45506 10.62433 11.16015 13.49525
    

    Created on 2018-09-21 by the reprex package (v0.2.1)

    As you can see, the time series attributes are attached to the bts component of the forecasts. bts is a time series matrix of class mts.