1
votes

Often times I make use of arima.sim() function to simulate ARIMA model and later discover through auto.arima() function that the ARIMA model simulated is not the same with what I specified in arima.sim() function.

I went further to investigate to know how does the arima.sim() fair in simulating ARIMA model by simulating same ARIMA model with the same arima.sim() detail good number of times and then check each out with auto.arima() here.

result <- matrix(NA_integer_, nrow = 10, ncol = 3)
colnames(result) <- c("p","d","q")
num<-60
set.seed(1234)
for(i in 1:10){
   result[i, ] <- arima.sim(n = num, model=list(ar=0.8, order = c(1, 0, 0))) %>%
     auto.arima() %>%
     arimaorder()
}
result
#p  d   q
#1  0   1
#1  0   0
#1  0   0
#1  0   0
#2  0   1
#1  0   0
#1  0   0
#1  0   0
#1  0   0
#4  0   3

How do I put up an R code that will count how many times ARIMA(1, 0, 0) comes up when I run

num<-60
for(i in 1:10){
   ar1 <- arima.sim(n = num, model=list(ar=0.8, order = c(1, 0, 0)))
     auto.arima(ar1)
}

ten(10) times in a loop

1

1 Answers

1
votes

If we need to get the count within the for loop

cnt <- 0
for(i in 1:10) { 
   ar1 <- arima.sim(n = num, 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
#[1] 3

Or if it is based on the 'result', then do a comparison with the vector, get the rowSums, and check if we have 3 TRUE in a single row, and sum

sum(rowSums(result == c(1, 0, 0)[col(result)]) == 3)