具有以下功能的arima.sim()函数:样本大小,phi值和sd值 [英] arima.sim() function with varying: sample sizes, phi values and sd values

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

问题描述

我想用以下方式模拟 ARIMA(1,1,0):

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

我问了类似的问题

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值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆