0

I am trying to use Auto Arima function for 70 variables. For it I need to create time series 70 times.

dealer1 <- ts(tier2[,2], start=c(2015,8), end = c(2018,12), frequency=12)
dealer2 <- ts(tier2[,3], start=c(2015,8), end = c(2018,12), frequency=12)
dealer3 <- ts(tier2[,4], start=c(2015,8), end = c(2018,12), frequency=12)..and so on.

And then I need to use the Auto Arima function again for 70 variables.

automatic.dealer1 = auto.arima(dealer1, ic = "aicc")
automatic.dealer2 = auto.arima(dealer2, ic = "aicc")
automatic.dealer3 = auto.arima(dealer3, ic = "aicc")... and so on

And then forecast the output:

forecast.dealer1 = forecast(automatic.dealer1, h = 3)$mean
forecast.dealer2 = forecast(automatic.dealer2, h = 3)$mean
forecast.dealer3 = forecast(automatic.dealer3, h = 3)$mean

I am trying to use the for loop in R, but I am getting an error. What am I doing wrong??

k <- 1
l <- 2

   for(i in seq(1,70)){

      dealer[k] <- ts(dealer1[,l], start=c(2015,8), end = c(2018,12), frequency=12)

      dealer[k]

      automatic <- auto.arima(dealer.[k], ic = "aicc")

      foreArima <- forecast(automatic, h=3)

      automatic

      foreArima

      k <- k+1
      l <- l+1
}

I need the ARIMA model selected for all the 70 variables I have in my data plus the forecast for each one of them to be displayed

Data sample looks like:

enter image description here

Saagar
  • 15
  • 8
  • 1
    could you please share the `tier2` dataframe with us. you can use `dput` or something [similar](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). this helps in answering the question – mischva11 Apr 25 '19 at 14:57
  • 3
    There are several typos in your for-loop. It should start like: `for(i in seq(1,70)){` also it looks like you're trying to print a value `dealer.[k]` it should probably be `dealer.k[k]` – dylanjm Apr 25 '19 at 14:58
  • furthering from @dylanjm response. the dealer should be dealer.k[i] or some variable name indexed by your index. The first line inside the loop should also be indexed, or it will overwrite every time – Chris Littler Apr 25 '19 at 15:02
  • Edited the code in the question above, and in R as well. but still the loop function is giving me an error. – Saagar Apr 25 '19 at 15:11
  • I need the loop to run till the last column and display the ARIMA model selected for each and every dealer. And also the forecast for each model – Saagar Apr 25 '19 at 20:31

1 Answers1

0

first of all you don't need a loop to create a ts object. You can create a multivariate ts object like this

myts <- ts(tier2[,2:70], , start=c(2015,8), frequency=12)

Here is how you use a for loop in R:

result <- numeric(ncol(myts)) #initialize a vector where the results will be stored.

for (j in 1:ncol(myts)) {
    automatic.dealer <- auto.arima(myts[,j], ic = "aicc")
    result[j] <- forecast(automatic.dealer, h = 3)$mean
}

Just as additional information: most of time you can avoid loops in R using apply, sapply or lapply. Doing so increases readability of your code and the performance.

EDIT: if you want to save all the results as well you can store the results from auto.arima into a list:

result <- numeric(ncol(myts))
arima.list <- list()
forecast.list <- list()

for (j in 1:ncol(myts)) {
    automatic.dealer <- auto.arima(myts[,j], ic = "aicc")
    arima.list[[j]] <- automatic.dealer
    forecast.list[[j]] <- forecast(automatic.dealer, h = 3)
    result[j] <- forecast.list[[j]]$mean
}
Cettt
  • 11,460
  • 7
  • 35
  • 58
  • I am trying to run the code that gives me the forecast model for each and every dealer, so 70 models output and then also the forecast of each model/ dealer – Saagar Apr 25 '19 at 15:36
  • Still getting an error, error displayed:::::Error in auto.arima(myts[, j], ic = "aicc") : No suitable ARIMA model found In addition: Warning messages: 1: In is.constant(x) : NAs introduced by coercion 2: In is.constant(x) : NAs introduced by coercion 3: In value[[3L]](cond) : The chosen seasonal unit root test encountered an error when testing for the first difference. 0 seasonal differences will be used. Consider using a different unit root test. 4: In is.constant(x) : NAs introduced by coercion – Saagar Apr 25 '19 at 20:25
  • This means that on one of your columns no ARIMA model could be fit. You can include a `print(j)` in your loop to find the index of this column – Cettt Apr 26 '19 at 08:28
  • Nope, still getting the same error. Including Print function in the loop did not give me the index which is causing that error. Am I overwriting Dealer in the above query.. I do not wanna write 70 codes for 70 dealers.. :( – Saagar Apr 26 '19 at 18:32