R语言自定义阶数SARIMAX模型xreg参数报错问题求助
手动指定ARIMA阶数引入外生变量时的列数不匹配错误
问题背景
用R做带外生变量的时间序列预测,已完成训练集和测试集拆分,用auto.arima()自动选择阶数时预测正常,但手动指定ARIMA阶数后运行预测代码出现错误。
数据处理与拆分代码
data$date <- as.Date(data$date, format = "%Y-%m-%d") time <- data$date time <- tail(time, -1) df <- data.frame(date=time, avi96 = as.numeric(avi96_adjseason_diff), avi89 = as.numeric(avi89_adjseason_diff), avi82 = as.numeric(avi82_adjseason_diff), avi75 = as.numeric(avi75_adjseason_diff), msci = as.numeric(msci_adjseason_diff)) train <- df[1:45, ] test <- df[45+1:nrow(df), ] test <- na.omit(test) xreg <- train$msci xreg1 <- test$msci ts_train <- ts(train[,-1], start=c(2019,1),frequency=12) ts_test <- ts(test[,-1],start=c(2022,10),frequency=12)
可正常运行的auto.arima代码
fit1 <- auto.arima(ts_train[,1], xreg = ts_train[,5]) fcast <- forecast::forecast(fit1, xreg = xreg1, h = 6) autoplot(fcast)
出错的手动指定阶数代码及错误信息
fit1 <- arima(ts_train[,1], order = c(5, 0, 5), seasonal = list(order = c(1, 0, 0), period = 12), xreg = ts_train[,5]) fcast <- forecast::forecast(fit1, xreg = xreg1, h = 6) autoplot(fcast)
错误信息:
Warning message in forecast.Arima(fit1, xreg = xreg1, h = 6): “xreg not required by this model, ignoring the provided regressors” Error in predict.Arima(object, n.ahead = h): 'xreg' and 'newxreg' have different numbers of columns Traceback: 1. forecast::forecast(fit1, xreg = xreg1, h = 6) 2. forecast.Arima(fit1, xreg = xreg1, h = 6) 3. predict(object, n.ahead = h) 4. predict.Arima(object, n.ahead = h) 5. stop("'xreg' and 'newxreg' have different numbers of columns")
错误原因
- 外生变量被自动剔除:
arima()函数会自动检验外生变量的系数显著性,如果系数不显著,会直接将其从模型中移除,导致拟合后的模型不再包含xreg,触发第一个警告。 - 格式不匹配:训练时传入的
xreg是向量,测试集的xreg1也是向量,但arima()内部处理时可能将向量视为“无列名的单列结构”,预测时容易出现列数匹配异常;另外原代码中test <- df[45+1:nrow(df), ]的索引写法有问题,应该是(45+1):nrow(df),否则可能导致测试集行数错误。
解决方法
1. 统一外生变量格式为矩阵
将训练和测试的外生变量都转为单列矩阵,确保列数和结构一致:
xreg <- as.matrix(train$msci) xreg1 <- as.matrix(test$msci)
2. 修正测试集索引
原索引写法会生成错误的序列,改成正确的区间索引:
test <- df[(45+1):nrow(df), ]
3. 强制保留外生变量(可选)
如果外生变量被自动剔除,可通过fixed参数强制约束其系数不为0,确保模型保留外生变量:
# 先拟合临时模型查看参数位置 fit_temp <- arima(ts_train[,1], order = c(5,0,5), seasonal = list(order = c(1,0,0), period=12), xreg = xreg) # 构造fixed向量:NA表示允许自由估计,确保外生变量系数位置不为0 fixed_vec <- rep(NA, length(fit_temp$coef)) # 找到外生变量msci对应的系数位置(通常是最后一个) msci_coef_pos <- grep("msci", names(fit_temp$coef)) fixed_vec[msci_coef_pos] <- NA # 设为NA允许估计,若设为固定值则强制该系数为指定值 # 重新拟合模型 fit1 <- arima(ts_train[,1], order = c(5, 0, 5), seasonal = list(order = c(1, 0, 0), period = 12), xreg = xreg, fixed = fixed_vec)
4. 修正预测代码中的变量名
原代码中autoplot(fcast_cons_temp)的变量名错误,应该用fcast:
fcast <- forecast::forecast(fit1, xreg = xreg1, h = 6) autoplot(fcast)
完整修正代码
# 数据处理 data$date <- as.Date(data$date, format = "%Y-%m-%d") time <- tail(data$date, -1) df <- data.frame( date = time, avi96 = as.numeric(avi96_adjseason_diff), avi89 = as.numeric(avi89_adjseason_diff), avi82 = as.numeric(avi82_adjseason_diff), avi75 = as.numeric(avi75_adjseason_diff), msci = as.numeric(msci_adjseason_diff) ) # 修正测试集索引 train <- df[1:45, ] test <- df[(45+1):nrow(df), ] test <- na.omit(test) # 统一外生变量为矩阵格式 xreg <- as.matrix(train$msci) xreg1 <- as.matrix(test$msci) ts_train <- ts(train[, -1], start = c(2019,1), frequency = 12) ts_test <- ts(test[, -1], start = c(2022,10), frequency = 12) # 手动指定阶数拟合ARIMA fit1 <- arima(ts_train[,1], order = c(5, 0, 5), seasonal = list(order = c(1, 0, 0), period = 12), xreg = xreg) # 预测并绘图 fcast <- forecast::forecast(fit1, xreg = xreg1, h = 6) autoplot(fcast)
内容的提问来源于stack exchange,提问作者Laura_777
相关产品推荐
相关产品推荐

