在R中绘制预测区间时出错

yfwxisqw  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(176)

我正在执行一个任务,我已经实现了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)
    }

我试着建立这样的东西:

jhdbpxl9

jhdbpxl91#

下一次,请提供运行代码所需的所有数据,错误为:您提供了一个数据结构,但您的代码需要一个train、一个test和一个data_2数据集。此外,如果在代码开始时加载调用函数的包,则会很有帮助(我现在使用{dynlm}表示dynlm(){forecast}表示forecast(),但这可能并不总是那么明显或明确)。您可以通过在共享代码的顶部添加以下内容来实现:

library(dynlm)
library(forecast)
library(ggplot2)

尽管如此,您的代码似乎给出了一个错误,因为您试图子集化一个不存在的列。预测数据如下所示:

> forecast$lower
                [,1]      [,2]
2022-04-01 -1.606344 -2.885595
2022-04-02 -1.649400 -2.874986
2022-04-03 -2.812910 -3.968169

您可以通过指定forecast$lower[, 1]forecast$lower[, 2]而不是80%95%来子集化您感兴趣的列。
此外,ggplot返回一些错误:

  • 当pi_80和pi_95被提供给geom_ribbon()中的data参数时,它们需要是 Dataframe 。您可以使用as.data.frame(pi_80)as.data.frame(pi_95)来完成此操作。
  • 然后,pi_80和pi_95中的变量被称为V1V2V3,而不是X1X2X3,因此必须在geom_ribbon()aes()中对其进行更改
  • geom_ribbon()aes()中为x提供的值必须是日期,因为您使用的是日期x轴。似乎V1是错误的值,但是在没有所有数据来运行代码的情况下很难确定正确的值是什么。

相关问题