【问题标题】:how to count how many times an arima order is not true before the first true order is obtained in r for different combo of arima simulation如何计算在 r 中获得第一个真实订单之前有多少次 arima 订单不真实,以用于不同的 arima 模拟组合
【发布时间】:2020-12-12 14:14:34
【问题描述】:

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

我想知道在获得所需模型的真实顺序之前,可能需要对其参数的不同组合(样本大小、标准偏差和模型系数)运行 arima.sim() 函数多少次,我希望这个R 脚本在count 运行arima.sim() 多少时间之后才能获得ARIMA-order 函数中指定的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() 进行多少次试验。

【问题讨论】:

  • 算法是(1)将每个组合的计数设置为零。 (2) 计算在所有combo第一次成功获得(1, 0, 0)之前模型订单不是(1, 0, 0)的次数。 (3)所有combo获得订单(1, 0, 0)时停止,不计入计数。
  • 在这种情况下,我的解决方案不会给您想要的结果?如果您运行它足够多次,它会在您获得c(1, 0, 0) 组合时为您提供数字。
  • 我不能为以下任何一项投票,因为自从我提出赏金以来,已经添加了没有价值的正确答案。

标签: r if-statement repeat arima


【解决方案1】:

你的花括号在错误的位置。运行您的代码时,我收到有关意外大括号的错误。 ifelseR 符号应遵循:

if(condition == TRUE) {
run some code
} else {                    
do some other code       # happens if condition == FALSE
}

如果你想检查不同的条件,你需要else if

if(condition == TRUE) {
run some code
} else if(other_condition == TRUE) {                    
do some other code       
} else {
do some third code      # runs if both conditions are FALSE
}

all() 的括号也放错了位置。基于此,我认为您希望您的条件看起来像:

if(all(arimaorder(auto.arima(x), ic = "aicc")) != c(1, 0, 0)) {
   cnt <- cnt + 1
   } else if (all(arimaorder(auto.arima(x), ic = "aicc")) == c(1, 0, 0)) {
       cnt <- cnt + 1
       } else { break }
    

当我修复这些时,代码几乎可以运行,除了我得到错误

arimaorder(auto.arima(x), ic = "aicc") 中的错误: 未使用的参数(ic = "aicc")

所以,auto.arima() 的括号也放错了位置。以下运行没有错误:

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 if (all(arimaorder(auto.arima(x, ic = "aicc"))) == c(1, 0, 0)) {
   cnt <- cnt + 1
} else { break }
      }
      cnt
   }, DF[["N"]], DF[["SD"]], DF[["phi"]])
names(res) <- paste("SD", DF[["SD"]], "phi", DF[["phi"]], sep = "-")
res
})

但是,我仍然认为这些行存在问题,我无法弄清楚您要完成什么。

      cnt
   }, DF[["N"]], DF[["SD"]], DF[["phi"]]) 

【讨论】:

  • 无限期运行
  • @DanielJames - 是的。这意味着永远不会满足break 条件。 if 语句或else if 语句始终为真。
【解决方案2】:

我觉得你运行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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多