0

I want R to count how many times my simulated ARIMA data conform to ARIMA(1,0,0) which I have achieved with:

library(forecast)
library(forecast)
cnt <- 0
num <- 60
phi <- 0.8

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

The above is just for a single case when sd=1, n=60, and ar=0.8.

I want a case when I have varying levels of N <- c(15, 20), SD <- c(1, 2) ^ 2, and phi = c(0.8, 0.9) for sample size, standard diviation and AR parameter respectively.

I have traid this:

library(forecast)
N <- c(15, 20)
SD <- c(1, 2) ^ 2
phi = c(0.8, 0.9)

## generate all combos
all_combos <- expand.grid(N = N, SD = SD, phi = phi)
epselon = function(n) rnorm(n, mean = 0, sd = SD)

## create function
fx_arima <- function(n, SD, phi) {
    cnt <- 0
    num <- 60
    phi <- 0.8

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

## find arima for all combos using Map
set.seed(123L)
res = Map(fx_arima, all_combos[["N"]], all_combos[["SD"]], 
all_combos[["phi"]])

## or a little bit more work:
set.seed(123L)
res2 = by(all_combos, all_combos["N"], 
   function(DF) {
     res = mapply(fx_arima, DF[["N"]], DF[["SD"]], DF[["phi"]])
     colnames(res) = paste("SD", DF[["SD"]], "phi", DF[["phi"]], sep = "_")
     res
   })
res2

## write to csv
Map(function(file, DF) write.csv(DF, paste0("N_", file, ".csv")), 
names(res2), res2)

which I mirror from arima.sim() function with varying: sample sizes, phi values and sd values and R Count How Many Time auto.arima() Confirmarima.sim() to be True

I got this error message

Error in `colnames<-`(`*tmp*`, value = c("SD_1_phi_0.2", "SD_4_phi_0.2", : attempt to set 'colnames' on an object with less than two dimensions
Traceback:

How can I solve this such that will have my result to show in varying form suuch that first row will be the label while the second row will be the count itself. The result will in two sheets; the first will be for 'N=15' and the second will be for 'N=20'.

Daniel James
  • 1,381
  • 1
  • 10
  • 28

1 Answers1

1

If I understood your problem correctly, the error comes from function colnames because your function does not return a "pure" matrix-like object. If, instead, you use the function names in your last chunk of code as follows:

res2 = by(all_combos, all_combos["N"], 
          function(DF) {
            res = mapply(fx_arima, DF[["N"]], DF[["SD"]], DF[["phi"]])
            names(res) = paste("SD", DF[["SD"]], "phi", DF[["phi"]], sep = "_")
            return(res)
          })
res2

You will get:

> res2
N: 15
SD_1_phi_0.8 SD_4_phi_0.8 SD_1_phi_0.9 SD_4_phi_0.9 
           1            3            7            5 
--------------------------------------------------------------------------- 
N: 20
SD_1_phi_0.8 SD_4_phi_0.8 SD_1_phi_0.9 SD_4_phi_0.9 
           3            4            5            2 

With elements accessible by name and index:

> res2$`15`["SD_1_phi_0.8"]
SD_1_phi_0.8 
           1 
> res2$`15`[1]
SD_1_phi_0.8 
           1 
davidnortes
  • 872
  • 6
  • 14