首页 > 解决方案 > 我如何避免使用系列图 R 预测地块中的漏洞

问题描述

我写这个问题是因为我无法在情节中将系列与预测联系起来(我尝试了很多次)。

这是我使用的代码。

AA1<-AA_1
str(AA1)#OUTPUT: Time-Series [1:60] from 2013 to 2018: 309 368 1602 6742 19396


Serie1<-Serie_1
str(Serie1) ##OUTPUT:Classes ‘tbl_df’, ‘tbl’ and 'data.frame':  60 obs. of  7 variables:


X_Reg_Mod_Completo <- cbind(A=ts(Serie1$A),B=ts(Serie1$B), 
                     C=ts(Serie1$C), D=ts(Serie1$D),
                     E=ts(Google1$E), F=ts(Serie1$F))

Mod_Completo<-auto.arima(AA1, xreg=X_Reg_Mod_Completo, trace = TRUE, test = "kpss", ic="aic", seasonal = TRUE)
AIC(Mod_Completo)
FOR_Mod_Completo<-forecast(Mod_Completo,xreg=X_Reg_Mod_Completo)
plot(FOR_Mod_Completo,xlim=c(2016, 2019))

我的目标是避免 2018 年底和 2018 年之间的空白。

如果有人需要数据,请写评论,我会更新。

预先感谢您的帮助。

弗朗切斯科

标签: rplotforecast

解决方案


我已经尝试了一些东西,ggplot2但没有过多地弄乱预测,也许它可以作为一个开始有所帮助:

library(forecast)
library(tidyverse)
fit <- auto.arima(WWWusage)
forec <- forecast(fit,h = 10)

现在,我们必须将 ts 和预测放入data.frames 中,绑定它们,并用 绘制结果ggplot2

# time series
ts_ <- data.frame(Point.Forecast = WWWusage,
                  Lo.80=NA,
                  Hi.80=NA,
                  Lo.95=NA,
                  Hi.95=NA,
                  type = 'ts')

# forecasting
forec <- data.frame(forec, type ='fc')

# together
tot <- union_all(ts_,forec) 

# now add the date, in this case I put a sequence: len
tot$time <- seq( as.Date("2011-07-01"), by=1, len=nrow(ts_)+nrow(forec))

现在你可以绘制它:

  ggplot(tot) + geom_line(aes(time,Point.Forecast))+
                geom_line(aes(time, Lo.95))+
                geom_line(aes(time, Hi.95))+
                geom_line(aes(time, Lo.80))+
                geom_line(aes(time, Hi.80))+
                geom_vline(xintercept=tot$time[nrow(ts_)], color = 'red') + theme_light()

在此处输入图像描述


推荐阅读