我全部。我需要统计专家的帮助。我对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)
答案 0 :(得分:0)
您可以尝试这样的操作,首先创建测试数据集:
test_as <- as[c(9:12),]
现在可以绘制data.frame
了,您可以看到real
数据,time
和应该具有相同时间长度的预测值(及其IC)和实际数据,因此我粘贴了一个NA
向量,其长度等于实际数据与预测数据和预测数据(与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(time, 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()
答案 1 :(得分:0)
使用带有autolayer()
函数的预测程序包很容易做到。
library(forecast)
library(ggplot2)
as <- data.frame(a = c(1, 2, 3, 4, 2, 4, 6, 8, 4, 8, 12, 16))
# Convert to a time series
y <- ts(as$a, start = 2017, frequency = 4)
# Split in two
a1 <- subset(y, end = 8)
a2 <- subset(y, start = 9)
# Fit model
fit_arima <- auto.arima(a1, ic = "aic")
# Compute forecasts
fcst <- forecast(fit_arima, h = 4)
# Plot forecasts and test set
autoplot(fcst) + autolayer(a2)
由reprex package(v0.3.0)于2019-12-02创建