Search code examples
rdygraphs

Plotting large gts objects in dygraphs


I am new to R programming. I've generated a hierarchical time series using the hts package.I need to plot time series in each hierarchy separately using dygraphs.

    library(hts)
    abc <- ts(5 + matrix(sort(rnorm(1000)), ncol = 10, nrow = 100))
    colnames(abc) <- c("A10A", "A10B", "A10C", "A20A", "A20B",
               "B30A", "B30B", "B30C", "B40A", "B40B")
     y <- hts(abc, characters = c(1, 2, 1))

     fcasts1 <- forecast(y, method = "bu" ,h=4, fmethod = "arima", 
                     parallel = TRUE)

     dygraph(fcasts1,y)

I keep getting this error message ,

  Error in UseMethod("as.xts") : 
  no applicable method for 'as.xts' applied to an object of class "c('gts', 'hts')"

Is there a solution for this issue ?Maybe if someone could tell me how to put the variables right in dygraph.


Solution

  • It is not possible to directly plot hts objects using dygraph. What you need to do is convert the hts$bts object into a matrix and then convert into a normal time series using ts() function.

    Here is an example I've worked out.

    library(hts)
    abc <- ts(5 + matrix(sort(rnorm(1000)), ncol = 10, nrow = 100))
    colnames(abc) <- c("A10A", "A10B", "A10C", "A20A", "A20B",
                       "B30A", "B30B", "B30C", "B40A", "B40B")
    y <- hts(abc, characters = c(1, 2, 1))
    
    fcasts1 <- forecast.gts(y, method = "bu" ,h=4, fmethod = "arima", 
                             parallel = TRUE)
    ts1 <- as.matrix(fcasts1$bts)
    ts1 <- ts(ts1,start = c(2016,3), frequency = 12)
    dygraph(ts1[,"A10A"],main='Sample dygraph ',ylab = 'Demand')