【问题标题】:R create a multivariate time series matrix from normalised data.frame for hts()R从归一化的data.frame为hts()创建一个多元时间序列矩阵
【发布时间】:2019-12-19 17:15:34
【问题描述】:

我有一个包含 3 列的 data.frame:

 telar <- data.frame(
  class = c("A","B","A","B"),
  date = as.Date(c("2019-01-01", "2019-01-01", "2019-02-01", "2019-02-01")),
  number = c(10, 20, 11, 21)
)

第一个包含类,第二个包含日期,第三个包含日期。我想创建一个多元时间序列矩阵,可由 hts 包中的 hts 函数使用。它应该是一个根节点,其余的是树的叶子。

代码应如下所示:

nodes <- list(length(unique(telar)))
## Here something to create the new time series matrix
my_hts <- hts(new_time_series_matrix, nodes)

谢谢大家!

【问题讨论】:

    标签: r time-series


    【解决方案1】:
    new_time_series_matrix <- ts(
      select(
        dcast(telar, date ~ class), -date
      ),
      start=c(year(telar$date[1]), month(telar$date[1])), 
      frequency = 12
    )
    new_time_series_matrix
    

    输出

              A  B
    Jan 2019 10 20
    Feb 2019 11 21
    

    【讨论】:

      猜你喜欢
      • 2017-03-13
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2014-09-19
      相关资源
      最近更新 更多