我是R编程的新手。我使用hts包生成了一个分层时间序列。我需要使用dygraph分别绘制每个层次结构中的时间序列。
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)
我一直收到此错误消息,
Error in UseMethod("as.xts") :
no applicable method for 'as.xts' applied to an object of class "c('gts', 'hts')"
这个问题是否有解决方案?也许有人可以告诉我如何将变量放在dygraph中。
答案 0 :(得分:1)
使用dygraph直接绘制hts对象是不可能的。你需要做的是将hts $ bts对象转换为矩阵,然后使用ts()函数转换为正常的时间序列。
以下是我制定的一个例子。
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')