Search code examples
rcounterarima

Count arimaorder untill order (1, 0, 0) is obtained


I once got answer to a question on how to count how many times 'auto.arima()' function truly confirm the order in 'arima.sim()' function if 'arima.sim()' functions is run 10 times as R Count How Many Time `auto.arima()` Confirm`arima.sim()` to be True

cnt <- 0
for(i in 1:10) { 
   ar1 <- arima.sim(n = 80, model=list(ar=0.8, order = c(1, 0, 0)))
   ar2 <- auto.arima(ar1)
   if(all(arimaorder(ar2) == c(1, 0, 0))) cnt <- cnt + 1}
cnt

How do I count how many times to run arima.sim(n = 80, model=list(ar=0.8, order = c(1, 0, 0))) function in a loop to know how many times the order will not be (1, 0, 0)' until it get order '(1, 0, 0)?


Solution

  • You can use while loop and count the loop if it's not the order, stop if it is

    count <-0
    while(TRUE){
        ar1 <- arima.sim(n = 80, model=list(ar=0.8, order = c(1, 0, 0)))
        ar2 <- auto.arima(ar1)
        if(all(arimaorder(ar2)==c(1,0,0))) break
        count <- count + 1
    }
    count