我都。我需要统计专家的帮助。我对as
中的几个值做了一个简单的Arima预测。但是我在train_as
中采用了值的子集。
现在可以在此处绘制实际值和预测值。像2019年的实际值是4,8,12,16,预测是9,10,11,12。我们可以绘制吗?
as <- data.frame(a=c(1,2,3,4,2,4,6,8,4,8,12,16))
train_as <- as[c(1:8),]
a1 <- ts(train_as,start = c(2017,1),end = c(2017,8),frequency = 4)
fit_arima <-auto.arima(a1, trace= TRUE, ic ="aic")
print(summary(fit_arima))
checkresiduals(fit_arima)
fcst <- forecast(fit_arima,h=4)
autoplot(fcst,include = 8)
您可以尝试类似的操作,首先创建测试数据集:
test_as <- as[c(9:12),]
现在要绘制data.frame
,您可以看到real
数据,time
和应该具有相同时间长度和实际数据的预测值(及其IC),因此我粘贴了NA
s向量,其长度等于实际数据与预测值和预测值(与IC相同)之差。请注意,时间是使用zoo
包按季度生成的:
library(zoo)
df <-
data.frame(real = as$a,
pred = c(rep(NA,length(as$a)-length(data.frame(fcst)[,1])),data.frame(fcst)[,1]),
time = zoo::as.yearqtr(seq(as.Date("2017/1/1"), as.Date("2019/12/1"), by = "quarter"), format = "%Y-%m-%d"),
Lo80 =c(rep(NA,length(as$a)-length(data.frame(fcst)[,2])),data.frame(fcst)[,2]),
Hi80 =c(rep(NA,length(as$a)-length(data.frame(fcst)[,3])),data.frame(fcst)[,3]),
Lo95 =c(rep(NA,length(as$a)-length(data.frame(fcst)[,4])),data.frame(fcst)[,4]),
Hi95 =c(rep(NA,length(as$a)-length(data.frame(fcst)[,5])),data.frame(fcst)[,5]))
现在您可以绘制它:
library(ggplot2)
ggplot(df, aes(time, pred, group = 1)) +
geom_line() +
geom_line(aes(x = time, y = real, group = 1), color = "red")+
geom_ribbon(aes(time, ymin = Lo95, ymax = Hi95), fill = "red", alpha = 0.25) +
geom_ribbon(aes(time, ymin = Lo80, ymax = Hi80), fill = "red", alpha = 0.25) +
theme_light()