英文:
Sarimax model issue with xreg
问题
以下是您的代码的翻译部分:
请帮助我使用SARIMA模型。我正在使用R来预测我的数据,而对于我的模型,我需要外生变量。首先,我将我的数据拆分为训练集和测试集:
然后,在那之后,我尝试使用auto.arima,它建议了模型的阶数,我使用以下代码进行预测,它起作用了:
问题是,我仍然想尝试其他我自己创建的阶数,但当我创建类似以下的内容时,它不起作用:
从这个错误中我明白了列中的数字有问题,但我找不到一种修复它与我的数据的方法。
提前感谢您。
英文:
Please help me with sarimax model. I am using R to forecast my data and for my model I need exogenous variable.
First I split my data to train and test:
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)
Then after that I try to use auto.arima and it suggest me order of model and I use this code to forecast and it works.
fit1 <- auto.arima(ts_train[,1], xreg = ts_train[,5])
fcast<- forecast::forecast(fit1, xreg = reg1, h = 6)
autoplot(fcast_cons_temp)
Problem is that I still want to try other orders which I would create myself, but when I create smth like this it doesn't work:
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 = reg1, h = 6)
autoplot(fcast_cons_temp)
error I get:
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")
From that error I understand that smth wrong with numbers in columns but I can't find a way to fix it with my data.
Thanks in advance.
答案1
得分: 0
代码不可重现。下次请提供一个最小可重现示例以帮助更容易理解问题。
然而,代码中有一些错误。首先,reg1
未定义(你只定义了 xreg1
)。其次,你在没有 xreg
参数的情况下使用了 stats::arima()
函数,而应该使用 forecast::Arima()
。如果你修复这些语法错误,它应该可以像下面的代码一样工作:
library(forecast)
set.seed(42)
df <- data.frame(date = 1:50,
serie = rnorm(50),
xreg = rnorm(50))
train <- df[1:45, ]
test <- df[(nrow(train) + 1):nrow(df), ]
xreg_train <- train$xreg
xreg_test <- test$xreg
ts_train <- ts(train[, -1], start = c(2019, 1), frequency = 12)
ts_test <- ts(test[, -1], start = c(2022, 10), frequency = 12)
fit_auto <- forecast::auto.arima(ts_train[, 1], xreg = xreg_train)
fcast_auto <- forecast::forecast(fit_auto, xreg = xreg_test, h = 6)
autoplot(fcast_auto)
myfit <- forecast::Arima(ts_train[, 1], order = c(1, 0, 0),
seasonal = list(order = c(1, 0, 0), period = 12),
xreg = xreg_train)
myfcast <- forecast::forecast(myfit, xreg = xreg_test, h = 6)
autoplot(myfcast)
我使用的是 R 4.2.0 和 forecast 8.20。
英文:
The code is not reproducible. Next time, please provide a minimal reproducible example to help understand the problem more easily.
However, there are a couple of errors in the code. Firstly, reg1
is not defined (you only define xreg1
). Secondly, you're using the stats::arima()
function with no xreg
parameter instead of forecast::Arima()
. If you fix these syntax errors, it should work as shown in the code below:
library(forecast)
set.seed(42)
df <- data.frame(date = 1:50,
serie = rnorm(50),
xreg = rnorm(50))
train <- df[1:45, ]
test <- df[(nrow(train) + 1):nrow(df), ]
xreg_train <- train$xreg
xreg_test <- test$xreg
ts_train <- ts(train[, -1], start = c(2019, 1), frequency = 12)
ts_test <- ts(test[, -1], start = c(2022, 10), frequency = 12)
fit_auto <- forecast::auto.arima(ts_train[, 1], xreg = xreg_train)
fcast_auto <- forecast::forecast(fit_auto, xreg = xreg_test, h = 6)
autoplot(fcast_auto)
myfit <- forecast::Arima(ts_train[, 1], order = c(1, 0, 0),
seasonal = list(order = c(1, 0, 0), period = 12),
xreg = xreg_train)
myfcast <- forecast::forecast(myfit, xreg = xreg_test, h = 6)
autoplot(myfcast)
I'm using R 4.2.0 and forecast 8.20.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论