本文介绍了具有变化的 arima.sim() 函数:样本大小、phi 值和 sd 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用不同的方式模拟 ARIMA(1,1,0):

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

  1. 样本量
  2. phi 值
  3. 标准偏差值.

我很欣赏下面的 r 代码是如何模拟一个 ARIMA(1,1,0) 我想按照格式来模拟许多 ARIMA(1,1,0) 具有不同的样本量phi值标准偏差值

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)
}

我问了一个类似的问题 here 并根据我的问题给出了一个很好的答案,但现在我看到 arima.sim() 函数在模拟 ARIMA 中是必不可少的 时间序列,因此想将其纳入我模拟 ARIMA 时间序列的风格.我想出了这个试验,它使用 arima.sim() 函数来模拟 N=c(15, 20) ARIMA(1,1,0)具有不同样本大小标准偏差值phi值的时间序列,首先生成N个随机数然后用最初的两个随机数作为前两个ARIMA(1,1,0).第 3 到 **n ** 是遵循ARIMA(1,1,0)`.这是我在下面尝试过的:

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))

最后两行将时间序列数据写入 .csv 并保存在我的工作目录中.

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

推荐答案

这里可能有一个使用 Map 的方法.如果这不符合您的要求,请编辑您的帖子以包含预期的输出.

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)

这篇关于具有变化的 arima.sim() 函数:样本大小、phi 值和 sd 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 11:17