dimanche 23 août 2020

how to use if and else condition in repeat function in r for different combo of arima simulation

Most times one runs arima.sim() function to simulate a particular order of arima mosel but when one check such simulated time series data through auto.arima() function, it will not often time be the same order of ARIMA one desire and specified in the arima.sim().

In my bid to know how many times one may need to run arima.sim() function for a different combination of its parameter (sample size, standard deviation and coefficient of the model) before obtaining the true order of the model sought for, I want this R script to count how many time it will run an arima.sim() before it get the exert ARIMA-order specified in the arima.sim() function.

**Here is my trial**

library(forecast)
N <- c(10, 20, 30)
SD <- c(1, 2, 3, 4, 5) ^ 2
phi <- c(0.2, 0.4, 0.6)

## generate all combos
all_combos <- expand.grid(N = N, SD = SD, phi = phi)

## create function
set.seed(123)
res2 <- by(all_combos, all_combos["N"], function(DF){
  res <- mapply(function(N, SD, phi){
    cnt <- 0
    repeat {
      x <- arima.sim(n=N, model = list(ar=phi, order = c(1, 0, 0)), sd = SD)
      if(all(arimaorder(auto.arima(x), ic = "aicc"))) != c(1, 0, 0) cnt <- cnt + 1){
      }
        {else(all(arimaorder(auto.arima(x), ic = "aicc"))) == c(1, 0, 0) cnt <- cnt + 1)}
        break
    }
    cnt
  }, DF[["N"]], DF[["SD"]], DF[["phi"]])
  names(res) <- paste("SD", DF[["SD"]], "phi", DF[["phi"]], sep = "-")
  res
})
res2

I am interested in knowing how many trials of arima.sim() will one make before obtaining the first ARIMA(1, 0, 0).

I got error with this my code and under why?

Aucun commentaire:

Enregistrer un commentaire