本文介绍了我怎样才能让highcharter代表一个预测对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是



但是,如果我遵循相同的配方使用

  require(forecast)
t.arima< - auto.arima(t)
x< - forecast(t.arima,level = c(95,80))

highchart(type ='stock')%>%
hc_add_series(x)%> %
hc_xAxis(type ='datetime')

我得到这个错误:

  as.Date.ts(。)中的错误:无法将ts时间转换为Date类

如何显示预测系列以及历史?我已经在文档中看到了这个,但不明白为什么我会得到这个错误。



JK的JS CONSOLE输出:



正如您所见, fortify 无法检测到实时。所以你需要在你想要的时间内改变索引。

This is a follow-on to this question.

I am trying to get the pipeline given in that question to accept a forecast object as input:

Again, using this data:

> dput(t)
structure(c(2, 2, 267822980, 325286564, 66697091, 239352431,
94380295, 1, 126621669, 158555699, 32951026, 23, 108000151, 132505189,
29587564, 120381505, 25106680, 117506099, 22868767, 115940080,
22878163, 119286731, 22881061), .Dim = c(23L, 1L), index = structure(c(1490990400,
1490994000, 1490997600, 1491001200, 1491004800, 1491008400, 1491012000,
1491026400, 1491033600, 1491037200, 1491040800, 1491058800, 1491062400,
1491066000, 1491069600, 1491073200, 1491076800, 1491109200, 1491112800,
1491120000, 1491123600, 1491156000, 1491159600), tzone = "US/Mountain", tclass = c("POSIXct",
"POSIXt")), class = c("xts", "zoo"), .indexCLASS = c("POSIXct",
"POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "US/Mountain", tzone = "US/Mountain", .CLASS = "double", .Dimnames = list(
    NULL, "count"))

I use

highchart(type = 'stock') %>%
    hc_add_series(t) %>%
    hc_xAxis(type = 'datetime')

To create

But if I follow this same recipe using

require("forecast")
t.arima <- auto.arima(t)
x <- forecast(t.arima, level = c(95, 80))

highchart(type = 'stock') %>%
     hc_add_series(x) %>%
     hc_xAxis(type = 'datetime')

I get this error:

Error in as.Date.ts(.) : unable to convert ts time to Date class

How can I show the forecast series along with the historical? I've seen this in the documentation, but don't understand why I'd be getting this error.

JS CONSOLE OUTPUT FOR JK:DF DATA AFTER RE-INDEXING:

dput(df)
structure(list(Index = structure(c(1490968800, 1490972400, 1490976000,
1490979600, 1490983200, 1490986800, 1490990400, 1491004800, 1491012000,
1491015600, 1491019200, 1491037200, 1491040800, 1491044400, 1491048000,
1491051600, 1491055200, 1491087600, 1491091200, 1491098400, 1491102000,
1491134400, 1491138000, 1491217200, 1491220800, 1491224400, 1491228000,
1491231600, 1491235200, 1491238800, 1491242400, 1491246000, 1491249600,
1491253200, 1491256800, 1491260400, 1491264000, 1491267600), class = c("POSIXct",
"POSIXt")), Data = c(2, 2, 259465771, 315866206, 64582553, 233440220,
91918347, 1, 126563786, 158555699, 32951026, 23, 108000151, 132505189,
29587564, 120381505, 25106680, 117506099, 22868767, 115898351,
22878163, 119285747, 22881061, 157925588, 32447780, 223096830,
281656273, 45406684, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
    Fitted = c(102170573.857143, 102170573.857143, 102170573.857143,
    102170573.857143, 102170573.857143, 102170573.857143, 102170573.857143,
    102170573.857143, 102170573.857143, 102170573.857143, 102170573.857143,
    102170573.857143, 102170573.857143, 102170573.857143, 102170573.857143,
    102170573.857143, 102170573.857143, 102170573.857143, 102170573.857143,
    102170573.857143, 102170573.857143, 102170573.857143, 102170573.857143,
    102170573.857143, 102170573.857143, 102170573.857143, 102170573.857143,
    102170573.857143, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
    `Point Forecast` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, 102170573.857143, 102170573.857143, 102170573.857143,
    102170573.857143, 102170573.857143, 102170573.857143, 102170573.857143,
    102170573.857143, 102170573.857143, 102170573.857143), `Lo 80` = c(NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -16003477.5789723,
    -16003477.5789723, -16003477.5789723, -16003477.5789723,
    -16003477.5789723, -16003477.5789723, -16003477.5789723,
    -16003477.5789723, -16003477.5789723, -16003477.5789723),
    `Hi 80` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, 220344625.293258, 220344625.293258, 220344625.293258,
    220344625.293258, 220344625.293258, 220344625.293258, 220344625.293258,
    220344625.293258, 220344625.293258, 220344625.293258), `Lo 95` = c(NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -78561041.5917782,
    -78561041.5917782, -78561041.5917782, -78561041.5917782,
    -78561041.5917782, -78561041.5917782, -78561041.5917782,
    -78561041.5917782, -78561041.5917782, -78561041.5917782),
    `Hi 95` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
    NA, 282902189.306064, 282902189.306064, 282902189.306064,
    282902189.306064, 282902189.306064, 282902189.306064, 282902189.306064,
    282902189.306064, 282902189.306064, 282902189.306064)), .Names = c("Index",
"Data", "Fitted", "Point Forecast", "Lo 80", "Hi 80", "Lo 95",
"Hi 95"), row.names = c(NA, -38L), class = "data.frame")
解决方案

Not sure this is due to the irregular time series.

Anyway, ggfortify:::fortify.forecast is your friend. Why? Because fortify (try to) transform all the R object in data frames. So:

library(highcharter)
library(forecast)
t.arima <- auto.arima(t)
x <- forecast(t, level = c(95, 80))

library(highcharter)
library(ggplot2)
library(ggfortify)
#>
#> Attaching package: 'ggfortify'
#> The following object is masked from 'package:forecast':
#>
#>     gglagplot
class(x)
#> [1] "forecast"

df <- fortify(x)
head(df)
#>   Index      Data    Fitted Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
#> 1     1         2 140658844             NA    NA    NA    NA    NA
#> 2  3601         2 121734145             NA    NA    NA    NA    NA
#> 3  7201 267822980 105355638             NA    NA    NA    NA    NA
#> 4 10801 325286564 127214522             NA    NA    NA    NA    NA
#> 5 14401  66697091 153863779             NA    NA    NA    NA    NA
#> 6 18001 239352431 142136089             NA    NA    NA    NA    NA

Now you can:

highchart(type = "stock") %>%
  hc_add_series(df, "line", hcaes(Index, Data), name = "Original") %>%
  hc_add_series(df, "line", hcaes(Index, Fitted), name = "Fitted") %>%
  hc_add_series(df, "line", hcaes(Index, `Point Forecast`), name = "Forecast") %>%
  hc_add_series(df, "arearange", hcaes(Index, low = `Lo 80`, high = `Hi 80`), name = "Interval")

As you can see, fortify can't detect the real time too. So you need to transform the Index in the time what you want.

这篇关于我怎样才能让highcharter代表一个预测对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 05:26
查看更多