rshinyr-highcharterforecast

Objects of class/type decomposed.ts are not supported by hchart (yet)


I would lik to use {highcharter} to plot stl objects. To to so, I would like to obtain seasonal, trend, remainder etc. from stl objects:

str(fit_stl)
> List of 8  $ time.series: Time-Series [1:164, 1:3] from 2010 to 2024:
> 284.6 48.7 156.2 -78.8 -85 ...   ..- attr(*, "dimnames")=List of 2   .. ..$ : NULL   .. ..$ : chr [1:3] "seasonal" "trend" "remainder"  $
> weights    : num [1:164] 1 1 1 1 1 1 1 1 1 1 ...  $ call       :
> language stl(x = TotalGen_ts, s.window = 10)  $ win        : Named num
> [1:3] 10 23 13   ..- attr(*, "names")= chr [1:3] "s" "t" "l"  $ deg   
> : Named int [1:3] 0 1 1   ..- attr(*, "names")= chr [1:3] "s" "t" "l" 
> $ jump       : Named num [1:3] 1 3 2   ..- attr(*, "names")= chr [1:3]
> "s" "t" "l"  $ inner      : int 2  $ outer      : int 0
>  - attr(*, "class")= chr "stl"

plot can handle stl objects:

fit_stl = stl(TotalGen_ts, s.window = 10)
plot(fit_stl)

However, so far, stl seems to be not supported by highcharter::hchart(). I'm wondering is there another way I can go about this rather than digging into creating individual charts and using highchart multiples.

To extract relevant data I have tried

do.call(rbind.data.frame, fit_stl)

as suggested in R Language Collective, which issues

error: invalid list argument: all variables should have the same length

and

df <- data.frame(matrix(unlist(fit_stl), nrow=length(fit_stl), byrow=TRUE))

which issues

Warning message: In matrix(unlist(fit_stl), nrow = length(fit_stl), byrow = TRUE) : data length [668] is not a sub-multiple or multiple of the number of rows [8]


Solution

  • I do not see any problem.

    As you do not provide any data, I use nottem(Average Monthly Temperatures at Nottingham, 1920–1939) from in-built {datasets}

    X = stl(nottem, "per")
    class(X)
    #> [1] "stl"
    str(X)
    #> List of 8
    #>  $ time.series: Time-Series [1:240, 1:3] from 1920 to 1940: -9.35 -9.86 -6.85 -2.76 3.5 ...
    #>   ..- attr(*, "dimnames")=List of 2
    #>   .. ..$ : NULL
    #>   .. ..$ : chr [1:3] "seasonal" "trend" "remainder"
    #>  $ weights    : num [1:240] 1 1 1 1 1 1 1 1 1 1 ...
    #>  $ call       : language stl(x = nottem, s.window = "per")
    #>  $ win        : Named num [1:3] 2401 19 13
    #>   ..- attr(*, "names")= chr [1:3] "s" "t" "l"
    #>  $ deg        : Named int [1:3] 0 1 1
    #>   ..- attr(*, "names")= chr [1:3] "s" "t" "l"
    #>  $ jump       : Named num [1:3] 241 2 2
    #>   ..- attr(*, "names")= chr [1:3] "s" "t" "l"
    #>  $ inner      : int 2
    #>  $ outer      : int 0
    #>  - attr(*, "class")= chr "stl"
    class(X$time.series)
    #> [1] "mts"    "ts"     "matrix" "array"
    

    This is all you need to build plots:

    X$time.series
               seasonal    trend    remainder
    Jan 1920 -9.3471980 49.68067  0.266525379
    Feb 1920 -9.8552496 49.54552  1.109728805
    Mar 1920 -6.8533008 49.41037  1.842931803
    Apr 1920 -2.7634710 49.32862  0.134848770
    May 1920  3.5013569 49.24688  1.351767558
    ...
    

    If you like, transform X$time.series to a data.frame. Also, highcharter::hchart(stl(nottem, "per")) works without any issues.