You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R语言中构建多元线性回归滞后变量模型实现超7个月小时级电力负荷长期预测的预测函数问题

Hey there! Let's work through this problem step by step. The core issue here is that your model depends on 24 lagged LOAD values, which aren't present in your forecast_df—and when you try to use predict() directly, those missing lags lead to invalid results (like the zero estimates you're seeing). Since you need to generate 5736 hourly predictions where each subsequent forecast relies on the previous ones, we'll use recursive forecasting to solve this.

Why Direct Prediction Fails

Your dyn-based linear model expects lagged LOAD values for every row in newdata, but your forecast dataset only has TEMPERATURE, MONTH, WEEKDAY, and HOUR. To generate valid predictions, we need to feed the model the lagged values as we go—using the most recent predictions to fill in the missing lags for the next time step.

Step-by-Step Solution

1. Prepare Initial Lag Values

First, grab the last 24 LOAD values from your training data (my_df)—these will be our starting point for the first prediction, since the model needs 24 hours of prior load data.

2. Recursive Forecasting Loop

Loop through each row in forecast_df, predict the current hour's load, then update your lag sequence with the new prediction (dropping the oldest lag value each time).

Here's the full code implementation:

# Load required packages
library(Hmisc)
library(dplyr)

# Load your datasets
my_df <- read.csv(file = "https://raw.githubusercontent.com/Argiro1983/Load/main/my_df.csv", sep=";")
forecast_df <- read.csv(file = "https://raw.githubusercontent.com/Argiro1983/Load/main/forecast_df.csv", sep=";")

# Re-fit your model (if not already done)
mod_lm <- lm(LOAD ~ TEMPERATURE + MONTH + WEEKDAY + HOUR + Lag(LOAD,1) + Lag(LOAD, 2) + Lag(LOAD, 3) + Lag(LOAD, 4) + Lag(LOAD,5)+ Lag(LOAD, 6) + Lag(LOAD, 7) + Lag(LOAD, 8) + Lag(LOAD,9) + Lag(LOAD, 10) + Lag(LOAD,11)+ Lag(LOAD, 12)+ Lag(LOAD, 13)+ Lag(LOAD, 14) + Lag(LOAD, 15) + Lag(LOAD, 16) + Lag(LOAD, 17) + Lag(LOAD, 18)+ Lag(LOAD, 19) + Lag(LOAD,20) + Lag(LOAD, 21) + Lag(LOAD, 22) +Lag(LOAD, 23)+ Lag(LOAD,24), data=my_df)

# Get initial 24 lag values from the end of the training data
initial_lags <- tail(my_df$LOAD, 24)

# Initialize a vector to store all 5736 predictions
predictions <- numeric(nrow(forecast_df))

# Loop through each hour in the forecast dataset
for (i in 1:nrow(forecast_df)) {
  # Create a temporary row with current forecast data + current lags
  temp_row <- forecast_df[i, ]
  # Add lagged LOAD values to match the model's variable names
  temp_row[paste0("lag(LOAD, ", 1:24, ")")] <- initial_lags
  
  # Predict the current hour's load
  current_pred <- predict(mod_lm, newdata = temp_row)
  predictions[i] <- current_pred
  
  # Update the lag sequence: drop the oldest value, add the new prediction
  initial_lags <- c(initial_lags[-1], current_pred)
}

# Verify the prediction length matches your forecast_df
length(predictions) # Should return 5736

Key Notes

  • Consistent Variable Formats: Make sure MONTH, WEEKDAY, and HOUR in forecast_df match the format (e.g., integer vs. factor) and levels of the same variables in my_df. If they're factors, use forecast_df$MONTH <- factor(forecast_df$MONTH, levels = levels(my_df$MONTH)) to align them.
  • Error Accumulation: Recursive forecasting will accumulate small errors over time (especially for a 7-month horizon). If you need better long-term performance, you might want to explore time-series-specific models like ARIMA or LSTM, but this method works well for your linear regression setup.
  • Model Term Names: The code uses paste0("lag(LOAD, ", 1:24, ")") to match the variable names your model expects—double-check these match the output from summary(mod_lm) to avoid mismatches.

内容的提问来源于stack exchange,提问作者Iro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 21:52:41