我如何使用系列图 R 避免情节预测中的漏洞
How I can avoid the hole in the plot forecast with series plot R
我写这个问题是因为我不能link(我试了很多次),在情节中,系列与预测。
这是我使用的代码。
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 年 gennary 之间的漏洞。
如果有人需要数据,请发表评论,我会更新。
提前感谢您的帮助。
弗朗切斯科
我已经用 ggplot2
尝试了一些东西,但没有对预测造成太多干扰,也许它可以作为一个开始有所帮助:
library(forecast)
library(tidyverse)
fit <- auto.arima(WWWusage)
forec <- forecast(fit,h = 10)
现在,我们必须将 ts 和预测放在 data.frame
s 中,绑定它们,然后用 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()
我写这个问题是因为我不能link(我试了很多次),在情节中,系列与预测。
这是我使用的代码。
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 年 gennary 之间的漏洞。
如果有人需要数据,请发表评论,我会更新。
提前感谢您的帮助。
弗朗切斯科
我已经用 ggplot2
尝试了一些东西,但没有对预测造成太多干扰,也许它可以作为一个开始有所帮助:
library(forecast)
library(tidyverse)
fit <- auto.arima(WWWusage)
forec <- forecast(fit,h = 10)
现在,我们必须将 ts 和预测放在 data.frame
s 中,绑定它们,然后用 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()