【问题标题】:visualise daily time series data using dygraphs in R使用 R 中的 dygraphs 可视化每日时间序列数据
【发布时间】:2017-12-14 23:38:53
【问题描述】:

我有一个类似于“df1”的数据框。将值列转换为每日时间序列后,我使用 Holt Winters 方法拟合并预测未来 120 天。我希望能够使用 dygraphs 可视化实际和预测。

library(dygraphs)
> head(df1)
   timestamp   value
1 2017-03-29   534.4571
2 2017-03-30   536.4350
3 2017-03-31   534.6661
4 2017-04-01   535.9185
5 2017-04-02   532.6998
6 2017-04-03   534.8282

convert_to_daily_ts <- function(x){
  x <- x[order(x$timestamp),]
  x$value_ts <- ts(x$value, frequency = 7)
  return(x)
}

df1 <- convert_to_daily_ts(df1)

hw <- tryCatch(HoltWinters(df1$value_ts), error=NA)
p <- predict(hw, n.ahead = 120, prediction.interval = TRUE, level=0.95)

act <- df1$value_ts
all <- cbind(act, p)

> class(all)
[1] "mts"    "ts"     "matrix"

> head(all)
Time Series:
Start = c(1, 1)
End = c(1, 6)
Frequency = 7
           actual p.fit p.upr p.lwr
1.000000 534.4571    NA    NA    NA
1.142857 536.4350    NA    NA    NA
1.285714 534.6661    NA    NA    NA
1.428571 535.9185    NA    NA    NA
1.571429 532.6998    NA    NA    NA
1.714286 534.8282    NA    NA    NA


> tail(all)
Time Series:
Start = c(115, 2)
End = c(115, 7)
Frequency = 7
         actual    p.fit    p.upr    p.lwr
115.1429     NA 386.2924 581.7568 190.8279
115.2857     NA 384.4614 580.0625 188.8603
115.4286     NA 383.4728 579.2104 187.7352
115.5714     NA 381.3159 577.1900 185.4418
115.7143     NA 383.3130 579.3234 187.3025
115.8571     NA 384.2098 580.3565 188.0631

 > str(all)
 mts [1:805, 1:4] 534 536 535 536 533 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "actual" "p.fit" "p.upr" "p.lwr"
 - attr(*, "tsp")= num [1:3] 1 116 7
 - attr(*, "class")= chr [1:3] "mts" "ts" "matrix"

dygraph(all, main = "Daily Predictions") %>%
        dySeries("act", label = "Actual") %>%
        dySeries(c("p.lwr", "p.fit", "p.upr"), label = "Predicted") %>%
        dyOptions(drawGrid = F) %>%
        dyRangeSelector()

我得到Error:Unsupported type passed to argument 'data'. 但是'all' 类与dygraph 的预期一样。任何可视化上述数据(实际和预测)的帮助都会有所帮助。另外,我需要 x 轴值来显示月-年(例如:2017 年 6 月、2017 年 7 月)而不是 1、2、3 等等。有可能吗?

【问题讨论】:

  • str(all) 长什么样子?
  • @RyanMorton 添加了 str(all)

标签: arrays r time-series dygraphs timeserieschart


【解决方案1】:

看起来 ts 对象需要 dygraph 的开始和结束日期才能弄清楚。您可以在创建 ts 对象时添加适当的开始日期和结束日期吗?您需要根据需要调整开始日期和结束日期。有一篇关于 here 的帖子。

convert_to_daily_ts <- function(x){
  x <- x[order(x$timestamp),]
  x$value_ts <- ts(x$value, start = c(2017,3), end = c(2017,7), frequency = 7)
  return(x)
}

【讨论】:

猜你喜欢
  • 2016-04-10
  • 1970-01-01
  • 1970-01-01
  • 2017-02-08
  • 2017-10-17
  • 1970-01-01
  • 2019-08-10
  • 1970-01-01
  • 2023-02-15
相关资源
最近更新 更多