简体   繁体   English

使用R中的图表来可视化每日时间序列数据

[英]visualise daily time series data using dygraphs in R

I have a dataframe similar to 'df1'. 我有一个类似于“ df1”的数据框。 After Converting the value column to a daily time series, I fit using Holt Winters method and predict 120 days in the future. 将value列转换为每日时间序列后,我使用Holt Winters方法进行拟合并预测未来的120天。 I want to be able to visualise the actual and predicted using 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()

I get Error:Unsupported type passed to argument 'data'. 我收到Error:Unsupported type passed to argument 'data'. But the class of 'all' is as expected for the dygraph. 但是“全部”的类别与书本所预期的一样。 Any help to visualise above data(actual & predicted) will be helpful. 可视化以上数据(实际的和预测的)的任何帮助将是有帮助的。 Also, I need the x-axis values to show month-year(Ex: Jun 2017, Jul 2017) instead of 1,2,3 so on. 另外,我需要x轴值来显示month-year(Ex:Jun 2017,Jul 2017)而不是1,2,3,依此类推。 Is it possible ? 可能吗 ?

It looks like the ts object needs start and end dates for dygraph to figure things out. ts对象似乎需要dygraph的开始和结束日期才能弄清楚情况。 Could you add the appropirate start and end dates when you create the ts object? 创建ts对象时,可以添加适当的开始日期和结束日期吗? You'll need to adjust the start and end dates as appropriate. 您需要适当地调整开始和结束日期。 There's a post about that 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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM