R Count How Many Time `auto.arima()` Confirm`arima.sim()` to be True

若如初见. 提交于 2021-02-05 08:42:32

问题


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:


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)


来源:https://stackoverflow.com/questions/62604878/r-count-how-many-time-auto-arima-confirmarima-sim-to-be-true

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!