4

I want to simulate ARIMA(1,1,0) with varying:

  1. sample sizes
  2. phi values
  3. standard deviation values.

I admire how the bellow r code is simulating just one ARIMA(1,1,0) which I want to follow the format to simulate many ARIMA(1,1,0) with varying sample sizes, phi values and standard deviation values

wn <- rnorm(10, mean = 0, sd = 1)
ar <- wn[1:2]
for (i in 3:10){
  ar<- arima.sim(n=10,model=list(ar=-0.7048,order=c(1,1,0)),start.innov=4.1,n.start=1,innov=wn)
}

I have asked a similar question here and given a good answer based on my question, but now I see that arima.sim() function is indispensable in simulating ARIMA time series and therefore want to incorporate it into my style of simulating ARIMA time series. I come up with this trial that uses arima.sim() function to simulate N=c(15, 20) ARIMA(1,1,0) time series with varying sample sizes, standard deviation values and phi values by first generating N random number and then using the initial two random number to be the first two ARIMA(1,1,0). The 3rd to **n**th are the made to followARIMA(1,1,0)`. Here is what I have tried bellow:

N <- c(15L, 20L)
SD = c(1, 2) ^ 2
phi = c(0.2, 0.4)
res <- vector('list', length(N))
names(res) <- paste('N', N, sep = '_')
set.seed(123L)
for (i in seq_along(N)){
  res[[i]] <- vector('list', length(SD))
  names(res[[i]]) <- paste('SD', SD, sep = '_')
  ma <- matrix(NA_real_, nrow = N[i], ncol = length(phi)) 
  for (j in seq_along(SD)){
    wn <- rnorm(N[i], mean = 0, sd = SD[j])
    ar[[1:2, ]] <- wn[[1:2]]
    for (k in 3:N[i]){
      ar[k, ] <- arima.sim(n=N[[i]],model=list(ar=phi[[k]],order=c(1,1,0)),start.innov=4.1,n.start=1,innov=wn)
    }
    colnames(ar) <- paste('ar_theta', phi, sep = '_')
    res[[i]][[j]] <- ar
  }
}
res1 <- lapply(res, function(dat) do.call(cbind,  dat))
sapply(names(res1), function(nm) write.csv(res1[[nm]], 
                                           file = paste0(nm, ".csv"), row.names = FALSE, quote = FALSE))

The last two lines write the time series data in .csv and save it in my working directory.

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

1 Answers1

3

Here may be a method using Map. Please edit your post to include expected output if this does not meet your requirements.

N <- c(15L, 20L)
SD <- c(1, 2) ^ 2
phi = c(0.2, 0.4)

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

## create function
fx_arima <- function(n, SD, phi) {
  arima.sim(n = n,
            model=list(ar=phi, order = c(1, 1, 0)),
            start.innov = 4.1,
            n.start = 1,
            rand.gen = function(n) rnorm(n, mean = 0, sd = SD))[-1L]
}

## 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)
Cole
  • 11,130
  • 1
  • 9
  • 24
  • I hail your intelligence on this answer – Daniel James Apr 05 '20 at 16:19
  • I will not need elements of the first row which are 0.0000000, I will like them deleted like `ar[-1]` in my simple case. – Daniel James Apr 05 '20 at 16:40
  • Are you trying to say `n=N` in `rand.gen = function(n) rnorm(n, mean = 0, sd = SD))`? – Daniel James Apr 05 '20 at 16:45
  • I also want to write the output in .csv file like I included in my question above as I will be extending the `N`, `SD` and the `phi` to something like https://stackoverflow.com/questions/59922709/how-to-write-r-output-with-unequal-length-into-excel but the `res1 <- lapply(res, function(dat) do.call(cbind, dat)) sapply(names(res1), function(nm) write.csv(res1[[nm]], file = paste0(nm, ".csv"), row.names = FALSE, quote = FALSE))` does not work – Daniel James Apr 05 '20 at 17:30
  • My attempt to write the output of your answer as I run `sapply(names(res), function(nm) write.csv(res[[nm]], file = paste0(nm, ".csv"), row.names = FALSE, quote = FALSE)) ` gave me 5 different data frame of this nature `$N_50000 NULL $N_70000 NULL $N_100000 NULL $N_150000 NULL $N_200000 NULL` while the first function prints 2 data frame of 4 columns each – Daniel James Apr 05 '20 at 20:50
  • See edits. For ```N = c(15L, 20L)``` the output of the code above is two CSVs. – Cole Apr 06 '20 at 23:41