Search code examples
rforecastingarimasarimax

Sarimax model issue with xreg


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.


Solution

  • 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.