Search code examples
rarimaseeding

Elegant Way to Print Coefficients of Set of Simulated AR(1) Along with there Seeds in R


I got a solution to Automate Seed as a Vector Instead of an Integer in R here and another solution to filter the true arimaorder of the simulated data that follows ARIMA(1, 0, 0) along with the seeds that produce them here also. Out of the seeds that produce the specified ARIMA order, I want to print the ar coefficients of them all along with there seeds.

I tried this:

SEED_vector <- c(14, 152, 165,528, 539, 1091, 1240, 1259, 1314, 1425, 1481, 1552)
arima_order_results = data.frame()
for (my_seed in SEED_vector){
    set.seed(my_seed)
    ar1 <- arima.sim(n = 10, model=list(ar=0.2, order = c(1, 0, 0)), sd = 1)
    ar2 <- auto.arima(ar1, ic ="aicc")
    arr <- ar2$coef
    print(arr)
    arima_order_results = rbind(arima_order_results,arr)
}

I got this as an output:

#ar1  intercept 
#-0.6920070  0.4209332 
       #ar1  intercept 
#-0.7202459 -0.3036454 
       #ar1  intercept 
#-0.8971835 -0.4130711 
      #ar1 
#0.8749406 
       #ar1 
#-0.7520381 
       #ar1  intercept 
#-0.8363416  0.3014670 
       #ar1  intercept 
#-0.7016283  0.4847039 
       #ar1  intercept 
#-0.6667556  0.6719526 
       #ar1  intercept 
#-0.6481393 -0.3125167 
       #ar1  intercept 
#-0.6084819 -0.9350262 
       #ar1  intercept 
#-0.8985071  0.4437974 
       #ar1  intercept 
#-0.7552149  1.2879873 

Instead of ar1 in my R output I will rather prefer the seed number that produces the coefficient


Solution

  • Try this approach:

    library(forecast)
    library(dplyr)
    SEED_vector <- c(14, 152, 165,528, 539, 1091, 1240, 1259, 1314, 1425, 1481, 1552)
    arima_order_results = data.frame()
    for (my_seed in SEED_vector){
      set.seed(my_seed)
      ar1 <- arima.sim(n = 10, model=list(ar=0.2, order = c(1, 0, 0)), sd = 1)
      ar2 <- auto.arima(ar1, ic ="aicc")
      arr <- as.data.frame(t(ar2$coef))
      arr <- cbind(data.frame(seed=my_seed),arr)
      print(arr)
      arima_order_results = bind_rows(arima_order_results,arr)
    }
    

    Output:

    arima_order_results
       seed        ar1  intercept
    1    14 -0.6920070  0.4209332
    2   152 -0.7202459 -0.3036454
    3   165 -0.8971835 -0.4130711
    4   528  0.8749406         NA
    5   539 -0.7520381         NA
    6  1091 -0.8363416  0.3014670
    7  1240 -0.7016283  0.4847039
    8  1259 -0.6667556  0.6719526
    9  1314 -0.6481393 -0.3125167
    10 1425 -0.6084819 -0.9350262
    11 1481 -0.8985071  0.4437974
    12 1552 -0.7552149  1.2879873