使用 dygraphs 绘制预测预测
Plotting `forecast` prediction using `dygraphs`
我想使用 dygraphs
绘制 forecast
包时间序列模型的预测。 documentation 建议使用以下方法进行实际预测:
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")
导致:
绘制对象 all
的有趣之处在于它的 class:
> class(all)
[1] "mts" "ts" "matrix"
> is.mts(all)
[1] TRUE
> is.ts(all)
[1] TRUE
> is.matrix(all)
[1] TRUE
str
提供了关于对象 all
:
的更多信息
> 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
是一个数组:
> 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
有没有人对如何根据 forecast
模型预测使用 dygraphs
创建正确的绘图对象有任何建议?
在 ?dygraphs::dygraph
中,data
参数必须是
Either time series data or numeric data. For time series, this must be
an xts object or an object which is convertible to xts. For numeric
data, this must be a named list or data frame, where the first
element/column provides x-axis values and all subsequent
elements/columns provide one or more series of y-values.
因此,您需要使用 summary
从预测 class 中提取数据。将其转换为 xts
class 然后使用 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)
在进一步调查由 forecast(model)
生成的列表后,我注意到实际值和点预测作为 ts
对象给出,上限和下限与 [= 的数组格式相同14=] 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)
}
请注意,下限和上限是二维数组。由于 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")
此图表的结果:
我想使用 dygraphs
绘制 forecast
包时间序列模型的预测。 documentation 建议使用以下方法进行实际预测:
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")
导致:
绘制对象 all
的有趣之处在于它的 class:
> class(all)
[1] "mts" "ts" "matrix"
> is.mts(all)
[1] TRUE
> is.ts(all)
[1] TRUE
> is.matrix(all)
[1] TRUE
str
提供了关于对象 all
:
> 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
是一个数组:
> 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
有没有人对如何根据 forecast
模型预测使用 dygraphs
创建正确的绘图对象有任何建议?
在 ?dygraphs::dygraph
中,data
参数必须是
Either time series data or numeric data. For time series, this must be an xts object or an object which is convertible to xts. For numeric data, this must be a named list or data frame, where the first element/column provides x-axis values and all subsequent elements/columns provide one or more series of y-values.
因此,您需要使用 summary
从预测 class 中提取数据。将其转换为 xts
class 然后使用 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)
在进一步调查由 forecast(model)
生成的列表后,我注意到实际值和点预测作为 ts
对象给出,上限和下限与 [= 的数组格式相同14=] 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)
}
请注意,下限和上限是二维数组。由于 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")
此图表的结果: