楼主: 18174715760
410 2

[问答] 多步预测的问题 [推广有奖]

  • 0关注
  • 0粉丝

本科生

25%

还不是VIP/贵宾

-

威望
0
论坛币
7 个
通用积分
0.8141
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
3171 点
帖子
51
精华
0
在线时间
89 小时
注册时间
2022-3-8
最后登录
2026-1-8

楼主
18174715760 发表于 2025-3-7 17:37:20 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
我的数据是2004年1月到2019年12月的月度时间序列数据,共192个样本点。想用for循环进行1到7步预测,采用expanding的来滚动数据,模型用的是SARIMAX模型。因为进行多步预测,想先将解释变量预测了之后再预测被解释变量,但是我写的代码在预测x的时候出现了问题,AI了很久,还是没搞懂问题出在哪。球球大家帮帮忙呀[em17]
下面是我的代码:
  1. usadata_y<-usadata[1:192,c(5,9)]##2004.01-2019.12
  2. usadata_x<-usadata[1:192,c(10,11,12,13,14,16,18,31,32,33)]##2004.01-2019.12

  3. #最大最小归一化
  4. min_max_norm_col <- function(mat) {
  5.   apply(mat, 2, function(x) (x - min(x)) / (max(x) - min(x)))
  6. }


  7. ##1.log,预测x
  8. n_cols <- ncol(usadata_x)
  9. arima_predict_x<- array(NA, c(36,7,n_cols))#steps:7,obs:36
  10. test_x <- array(NA, c(36,7,n_cols))
  11. sarimax_mutil_step_forecast_values <- array(NA, c(36,7))
  12. sarimax_predict <- array(NA, c(36,7))
  13. real_values <- array(NA, c(36,7))

  14. dimnames(test_x) <- list(NULL, NULL, colnames(usadata_x))


  15. for (steps in 1:7) {
  16.   for (i in 1:(37-steps)) {
  17.     cat("steps",steps,"观测点",i)
  18.    
  19.     # 训练集和测试集数据
  20.     train_data <- usadata_y[(1+steps):(155 + i),2]    #Y训练集
  21.     #test_data <- data_number[(395+i):(395+i+steps-1)]
  22.     train_data_in<-ts(train_data,frequency=12,start = c(2004,01))#7
  23.    
  24.     datax1<-min_max_norm_col(usadata_x[1:(155+i),])
  25.     datax2 <- as.data.frame(datax1)
  26.    
  27.     # 自变量(非虚拟变量)
  28.     train_x <- as.matrix(datax2[1:(155+i-steps),])   #X训练集,有滞后
  29.     for (j in 1:n_cols) {
  30.       arima_model_x_j <- auto.arima(train_x[, j])
  31.       arima_predict_x[i, steps, j] <- forecast(arima_model_x_j, h = steps)$mean[steps]
  32.     }
  33.     test_x[i,steps,] <- arima_predict_x[i,steps,]
  34.     xreg_for_forecast <- as.matrix(test_x[i, steps, ])  # 未来的外生变量
  35.    
  36.      # 使用auto.arima拟合SARIMA模型
  37.     sarima_model <- auto.arima(train_data_in,xreg = train_x, seasonal = TRUE)
  38.     sarimax_predict[i,steps] <- forecast(sarima_model, xreg = xreg_for_forecast,
  39.                                          h=steps)$mean[steps]
  40.     sarimax_mutil_step_forecast_values[i,steps] <- exp(sarimax_predict[i,steps])
  41.     real_values[i,steps] <- usadata[(156+i+steps-1),5]
  42.   }
  43. }
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Expanding 时间序列数据 sarima expand ARIMAX

沙发
cheetahfly 在职认证  发表于 2025-3-9 11:39:46
没有你的数据,无法复现。你也没有提供系统的出错信息

藤椅
18174715760 发表于 2025-3-10 10:17:06
cheetahfly 发表于 2025-3-9 11:39
没有你的数据,无法复现。你也没有提供系统的出错信息
之前没有提供系统出错信息主要是因为调整了好久,总有各种不一样的报错信息,我人都要麻了。
下面是数据:
USAdata.xlsx (74.54 KB)

非常感谢您百忙之中帮忙!


warnings()
警告信息:
1: In forecast.forecast_ARIMA(sarima_model, xreg = xreg_for_forecast,  ... :
  xreg contains different column names from the xreg used in training. Please check that the regressors are in the same order.
2: In forecast.forecast_ARIMA(sarima_model, xreg = xreg_for_forecast,  ... :
  xreg contains different column names from the xreg used in training. Please check that the regressors are in the same order.
3: In forecast.forecast_ARIMA(sarima_model, xreg = xreg_for_forecast,  ... :
  xreg contains different column names from the xreg used in training. Please check that the regressors are in the same order.
4: In forecast.forecast_ARIMA(sarima_model, xreg = xreg_for_forecast,  ... :
  xreg contains different column names from the xreg used in training. Please check that the regressors are in the same order.
5: In forecast.forecast_ARIMA(sarima_model, xreg = xreg_for_forecast,  ... :
  xreg contains different column names from the xreg used in training. Please check that the regressors are in the same order.
6: In forecast.forecast_ARIMA(sarima_model, xreg = xreg_for_forecast,  ... :
  xreg contains different column names from the xreg used in training. Please check that the regressors are in the same order.
7: In forecast.forecast_ARIMA(sarima_model, xreg = xreg_for_forecast,  ... :
  xreg contains different column names from the xreg used in training. Please check that the regressors are in the same order.

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-2-3 03:57