0

I got some problems predicting my arima model when I use expanding window function. My code looks like this:

iTraininSet <- trunc(nrow(adj_consumption)*0.7)
iValidationSet <- trunc(nrow(adj_consumption)*0.1)
iEstimation <- iTraininSet + iValidationSet
iOOS <- trunc(nrow(adj_consumption)*0.2)

OOS_Arima <- matrix(data = NA, nrow = iOOS, ncol = 1)
Actual_OLS <- matrix(data = NA, nrow = iOOS, ncol =1)
Bech_OLS <- matrix(data = NA, nrow = iOOS, ncol = 1)

dfDataArima <- ts(adj_consumption, freq=12, start = c(2008,1), end = c(2019,12))

for(i in 1:iOOS) {
  
  modely <-Arima(dfDataArima[1:iEstimation+i,], order=c(0,1,1))
  
  OOS_Arima[i,] <- predict(modely, newdata = dfDataArima[iEstimation+i,])
  
}

The error is:

Error in OOS_Arima[i, ] <- predict(modely, newdata = dfDataArima[iEstimation +  : 
  number of items to replace is not a multiple of replacement length

Does anyone have a suggestion to fix this. I have used this method with OLS, LASSO and Ridge and it worked fine, but I can't make it work with Arima.

  • It's probably related to the frequency that's been set and the amount of data you're using when you predict. However, I'm going to put together some data to give you a better answer. In the meantime, it looks like you're new to SO; welcome to the community! If you want great answers quickly, it's best to make your question reproducible. This includes sample data like the output from `dput(head(dataObject)))` and any libraries you are using. Check it out: [making R reproducible questions](https://stackoverflow.com/q/5963269). – Kat Apr 18 '22 at 17:23
  • I was wrong, the problem is that the output of `predict` will give you two values, but your matrix only has one column. You're getting a predicted value and the SE. If you only want the predicted value, then add `$pred` to the end of your call to predict. If you want to keep both, give the empty matrix two columns. Alternatively, you could use vectorization and skip the loop and empty matrices and you'll get all of it. For that you use `lapply()` or `purrr::map()`. If you want more information on any of these, let me know! – Kat Apr 18 '22 at 18:00
  • Thanks a lot Kat. The $pred worked for me. I've used to many hours to find the problem. You saved my day! – Christian Jensen Apr 19 '22 at 10:22

0 Answers0