2017-04-26 149 views
2

我想繪製一個forecast包時間序列模型的預測使用dygraphs。該documentation提出了預測值與實際值以下辦法:使用`dygraphs`預測'預測'預測

hw <- HoltWinters(ldeaths) 
p <- predict(hw, n.ahead = 36, prediction.interval = TRUE) 
all <- cbind(ldeaths, p) 

dygraph(all, "Deaths from Lung Disease (UK)") %>% 
    dySeries("ldeaths", label = "Actual") %>% 
    dySeries(c("p.lwr", "p.fit", "p.upr"), label = "Predicted") 

結果造成: enter image description here

關於繪製對象all有趣的是,它的類:

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

> is.mts(all) 
[1] TRUE 
> is.ts(all) 
[1] TRUE 
> is.matrix(all) 
[1] TRUE 

str提供了一個littl

> str(all) 
Time-Series [1:108, 1:4] from 1974 to 1983: 3035 2552 2704 2554 2014 ... 
- attr(*, "dimnames")=List of 2 
    ..$ : NULL 
    ..$ : chr [1:4] "ldeaths" "p.fit" "p.upr" "p.lwr" 

更多檢查表明all是一個數組::電子關於對象all更多信息

> tail(all) 
     ldeaths  p.fit p.upr  p.lwr 
Jul 1982  NA 1128.3744 1656.127 600.6217 
Aug 1982  NA 948.6089 1478.090 419.1282 
Sep 1982  NA 960.1201 1491.429 428.8112 
Oct 1982  NA 1326.5626 1859.802 793.3235 
Nov 1982  NA 1479.0320 2014.306 943.7583 
Dec 1982  NA 1929.8349 2467.249 1392.4206 
> dim(all) 
[1] 108 4 
> is.array(all) 
[1] TRUE 

我無法創建使用預測從forecast包裝這種類型的對象

用我的forecast模型unemp.mod我創建了預測:

> f <- forecast(unemp.mod) 
> f 
     Point Forecast Lo 80 Hi 80 Lo 95 Hi 95 
Apr 2017  4.528274 4.287324 4.769224 4.159773 4.896775 
May 2017  4.515263 4.174337 4.856189 3.993861 5.036664 
Jun 2017  4.493887 4.055472 4.932303 3.823389 5.164386 
Jul 2017  4.479992 3.936385 5.023599 3.648617 5.311367 
Aug 2017  4.463073 3.807275 5.118871 3.460116 5.466030 

雖然它看起來類似於在實施例的陣列中,這是一個完全不同的對象:

> class(f) 
[1] "forecast" 
> str(f) 
List of 10 <truncated> 

如果嘗試使用以產生所述預測基礎R的predict像在本例中,我還與風列表對象:

> predict(unemp.mod, n.ahead = 5, prediction.interval = TRUE) 
$pred 
      Apr  May  Jun  Jul  Aug 
2017 4.528274 4.515263 4.493887 4.479992 4.463073 

$se 
      Apr  May  Jun  Jul  Aug 
2017 0.1880140 0.2660260 0.3420974 0.4241788 0.5117221 

有沒有人對如何建立正確的對象使用dygraphs基於forecast模型預測畫出什麼建議嗎?

回答

1

在通過forecast(model)生成的列表的進一步調查,我注意到實際值和預測點給定爲ts目的和上限和下限在相同的陣列形式作爲dygraphs HoltWinters例子。我創建了一個函數來創建繪圖所需的數組,假設forecast_obj <- forecast(model)

gen_array <- function(forecast_obj){ 

    actuals <- forecast_obj$x 
    lower <- forecast_obj$lower[,2] 
    upper <- forecast_obj$upper[,2] 
    point_forecast <- forecast_obj$mean 

    cbind(actuals, lower, upper, point_forecast) 
} 

請注意,下限和上限是2維數組。由於dygraphs不支持多於一個預測間隔,我只挑選一對(95%)。

然後我用這樣的情節生成的數組:

dygraph(ts_array, main = graph_title) %>% 
     dyRangeSelector() %>% 
     dyRangeSelector(height = 40, 
         dateWindow = c("2011-04-01", "2019-4-01")) %>% 
     dySeries(name = "actuals", label = "actual") %>% 
     dySeries(c("lower","point_forecast","upper"), label = "Predicted") %>% 
     dyLegend(show = "always", hideOnMouseOut = FALSE) %>% 
     dyHighlight(highlightCircleSize = 5, 
        highlightSeriesOpts = list(strokeWidth = 2)) %>% 
     dyOptions(axisLineColor = "navy", gridLineColor = "grey") 

在該圖中得到的:

enter image description here

1

?dygraphs::dygraph中,data參數必須是

要麼時間序列數據或數字數據。對於時間序列,這必須是一個xts對象或一個可轉換爲xts的對象。對於數字 數據,這必須是指定的列表或數據框,其中第一個元素/列提供x軸值,所有後續的元素/列提供一個或多個y值序列。

因此,您需要使用summary從預測類中提取數據。將其轉換爲xts類,然後使用dygraph

library(forecast) 
f <- forecast(ldeaths) 

#use summary to get the model fit by forecast package 
df <- summary(f) 

#convert into a Time-Series class 
dfTs <- xts(df, as.Date(paste("01", rownames(df)), "%d %b %Y")) 

#plot 
dygraph(dfTs) 
+0

感謝您的回答。我真的想出了一個適合我的解決方案。我打算髮布它。 – Warner