我正在执行一个任务,我已经实现了dynlm模型。代码运行良好,但当我试图绘制预测区间的模型时,它给了我这个错误:Error in forecast$lower[, "95%"]: subscript out of bounds
我只是想在预测值的图上添加80%和95%水平的预测区间。请帮助我!
数据样本:
structure(list(Date = structure(c(19083, 19084, 19085, 19086,
19087, 19088, 19089, 19090, 19091, 19092, 19093, 19094, 19095,
19096, 19097), class = "Date"), US_Inflation_diff = c(-0.000219999999999998,
-0.000189999999999996, 0.000099999999999989, -0.00000999999999998225,
0.00000999999999998225, -0.00000999999999999612, 0.00001000000000001,
-0.0000000000000000138777878078145, -0.00000999999999998225,
0.00000999999999998225, -0.00000999999999999612, 0.00001000000000001,
-0.00001000000000001, 0.00000999999999999612, -0.00000999999999998225
), ATOM_12 = c(0, 2.002691, 0.570558999999999, -1.068854, -1.565735,
-2.428472, 0.824852, -0.849135999999998, 0.406713, -0.688822000000002,
-2.805074, 0.860166000000003, 0.0575009999999985, -0.731065999999998,
0.136664999999997)), row.names = c("2022-04-01", "2022-04-02",
"2022-04-03", "2022-04-04", "2022-04-05", "2022-04-06", "2022-04-07",
"2022-04-08", "2022-04-09", "2022-04-10", "2022-04-11", "2022-04-12",
"2022-04-13", "2022-04-14", "2022-04-15"), class = "data.frame")
验证码:
# Fit the model using the train data set
dynlm_model_train_1 <- dynlm(ATOM_12 ~ US_Inflation_diff, data = train)
# Forecast the next 3, 5, and 7 days using the test data set
forecast_3_1 <- forecast(dynlm_model_train_1, newdata = test[1:3, ])
forecast_5_1 <- forecast(dynlm_model_train_1, newdata = test[1:5, ])
forecast_7_1 <- forecast(dynlm_model_train_1, newdata = test[1:7, ])
# create a vector of forecast data colors
colors_vec <- c("steelblue", "orange", "purple")
# create a list of forecast objects for 3, 5, and 7 days
forecast_list <- list(forecast_3_1, forecast_5_1, forecast_7_1)
for (i in c(3, 5, 7)) {
# create forecast data
forecast_data <- data.frame(Date = seq(as.Date("2023-04-01"), by = "day", length.out = i))
forecast_data$US_Inflation_diff <- tail(data_2$US_Inflation_diff, 1)
# get the forecast from the pre-defined objects
forecast <- forecast_list[[i/2]]
forecast_data$ATOM_12 <- forecast$mean
# create prediction intervals for the forecast data
pi_95 <- cbind(forecast$x, forecast$lower[, "95%"], forecast$upper[, "95%"])
pi_80 <- cbind(forecast$x, forecast$lower[, "80%"], forecast$upper[, "80%"])
# create a new plot with the forecast data and prediction intervals
p <- ggplot() +
geom_line(data = data_2, aes(x = Date, y = ATOM_12), color = "black", size = 1) +
geom_line(data = forecast_data, aes(x = Date, y = ATOM_12, color = "Forecast"), size = 1) +
geom_ribbon(data = pi_95, aes(x = X1, ymin = X2, ymax = X3), fill = "grey70", alpha = 0.5) +
geom_ribbon(data = pi_80, aes(x = X1, ymin = X2, ymax = X3), fill = "grey80", alpha = 0.5) +
scale_color_manual(name = "", values = colors_vec[i/2]) +
theme_bw() +
labs(x = "Date", y = "ATOM_12", title = paste("Forecast with", i, "days")) +
geom_smooth(data = data_2, aes(x = Date, y = ATOM_12), method = "lm", se = FALSE, color = "red")
print(p)
}
我试着建立这样的东西:
1条答案
按热度按时间jhdbpxl91#
下一次,请提供运行代码所需的所有数据,错误为:您提供了一个数据结构,但您的代码需要一个
train
、一个test
和一个data_2
数据集。此外,如果在代码开始时加载调用函数的包,则会很有帮助(我现在使用{dynlm}
表示dynlm()
,{forecast}
表示forecast()
,但这可能并不总是那么明显或明确)。您可以通过在共享代码的顶部添加以下内容来实现:尽管如此,您的代码似乎给出了一个错误,因为您试图子集化一个不存在的列。预测数据如下所示:
您可以通过指定
forecast$lower[, 1]
或forecast$lower[, 2]
而不是80%
或95%
来子集化您感兴趣的列。此外,ggplot返回一些错误:
geom_ribbon()
中的data
参数时,它们需要是 Dataframe 。您可以使用as.data.frame(pi_80)
和as.data.frame(pi_95)
来完成此操作。V1
、V2
和V3
,而不是X1
、X2
和X3
,因此必须在geom_ribbon()
的aes()
中对其进行更改geom_ribbon()
的aes()
中为x
提供的值必须是日期,因为您使用的是日期x轴。似乎V1
是错误的值,但是在没有所有数据来运行代码的情况下很难确定正确的值是什么。