Error in plotting Prediction Intervals in R

I am working on a task where I have implemented dynlm model. The code is running fine but when I try to plot the model with prediction intervals it is giving me this error: Error in forecast$lower[, "95%"]: subscript out of bounds I just want to add prediction intervals at 80% and 95% levels on the plot for forecasted values. Please help me!

Data Sample:

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")

Code:

# 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)
    }

I was trying to build something like this:
image

Please check out the policy on cross-posting.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.