r - 对于不同的 arima 模拟组合,在 r 中获得第一个真实顺序之前,如何计算 arima 顺序不正确的次数

标签 r if-statement repeat arima

大多数时候运行 arima.sim() 函数来模拟 arima mosel 的特定顺序,但是当通过 auto 检查此类模拟时间序列数据时。 arima() 函数,它通常不会与 arima.sim() 中指定的 ARIMA 顺序相同。

我想知道在获得之前可能需要对其参数(样本大小、标准差和模型系数)的不同组合运行 arima.sim() 函数多少次所寻找模型的真实顺序,我希望此 R 脚本能够计算 之前它将运行 arima.sim() 多少次它得到 arima.sim() 函数中指定的 ARIMA-order

**Here is my trial**

library(forecast)
N <- c(10, 20, 30)
SD <- c(1, 2, 3, 4, 5) ^ 2
phi <- c(0.2, 0.4, 0.6)

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

## create function
set.seed(123)
res2 <- by(all_combos, all_combos["N"], function(DF){
  res <- mapply(function(N, SD, phi){
    cnt <- 0
    repeat {
      x <- arima.sim(n=N, model = list(ar=phi, order = c(1, 0, 0)), sd = SD)
      if(all(arimaorder(auto.arima(x), ic = "aicc"))) != c(1, 0, 0) cnt <- cnt + 1){
      }
        {else(all(arimaorder(auto.arima(x), ic = "aicc"))) == c(1, 0, 0) cnt <- cnt + 1)}
        break
    }
    cnt
  }, DF[["N"]], DF[["SD"]], DF[["phi"]])
  names(res) <- paste("SD", DF[["SD"]], "phi", DF[["phi"]], sep = "-")
  res
})
res2

我很想知道在获得第一个 ARIMA(1, 0, 0) 之前要进行多少次 arima.sim() 试验。

最佳答案

你运行 by + mapply 对我来说似乎很奇怪。我认为只有 mapply 就足够了。此外,arimaorder 没有ic 参数,也许您打算将它用于auto.arima 函数。

因为您想知道需要多少次试验才能得到 c(1, 0, 0),所以我添加了一个额外的列 (index),即行all_combos 中的数字。一旦您获得 c(1, 0, 0) 的输出,循环就会中断并打印 index。代码不会针对其余组合运行。

library(forecast)
N <- c(10, 20, 30)
SD <- c(1, 2, 3, 4, 5) ^ 2
phi <- c(0.2, 0.4, 0.6)

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

mapply(function(N, phi, SD, index) {
  x <- with(all_combos, arima.sim(n=N[1], 
             model = list(ar=phi[1], order = c(1, 0, 0)), sd = SD[1]))
  if(all(arimaorder(auto.arima(x, ic = "aicc")) == c(1, 0, 0))) {
    print(index)
    break
  }
}, all_combos$N, all_combos$SD, all_combos$phi, all_combos$index)

关于r - 对于不同的 arima 模拟组合,在 r 中获得第一个真实顺序之前,如何计算 arima 顺序不正确的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63552562/

相关文章:

javascript - 可以获取要在 javascript 上设置的变量

linux - 如何在 shell 中重复破折号(连字符)

r - tidyr::nest 在不同系统和包/R 版本上的不同行为

java - while & for 循环和 JOptionPane 数据输入连接到 for 循环

r - 获取ggplot2图例以在R中显示百分比符号

php - Mysql 触发器 IF 目标字段已更改

regex - 与解析文件并返回 8 到 24 个字符且没有重复模式的字符串的正则表达式 (perl) 苦苦挣扎

java - 为什么在没有显式调用 repaint() 的情况下连续异步调用 paintComponent()?

r - 如何在 RStudio 中为不同版本的 R 设置快捷方式?

r - 日期作为因子的错误转换为日期