对于不同的arima模拟组合,在r中获得第一个真阶之前,如何计算arima阶数不为真的次数

对于不同的arima模拟组合,在r中获得第一个真阶之前,如何计算arima阶数不为真的次数,r,if-statement,repeat,arima,R,If Statement,Repeat,Arima,大多数情况下,人们运行arima.sim()函数来模拟arima-mosel的特定顺序,但当人们通过auto.arima()函数检查此类模拟的时间序列数据时,它通常不会与人们期望的、在arima.sim()中指定的arima顺序相同 为了了解在获得所需模型的真实顺序之前,可能需要为其参数的不同组合(样本大小、标准偏差和模型系数)运行多少次arima.sim(),我想让这个R脚本在获得arima.sim()函数中指定的arima顺序之前,计算它运行arima.sim()的次数 **Here is

大多数情况下,人们运行
arima.sim()
函数来模拟
arima-mosel
的特定顺序,但当人们通过
auto.arima()
函数检查此类模拟的时间序列数据时,它通常不会与人们期望的、在
arima.sim()
中指定的arima顺序相同

为了了解在获得所需模型的真实顺序之前,可能需要为其参数的不同组合(样本大小、标准偏差和模型系数)运行多少次
arima.sim()
,我想让这个
R
脚本在获得
arima.sim()
函数中指定的
arima顺序之前,计算它运行
arima.sim()
的次数

**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
**这是我的试用版**
图书馆(预测)

你的花括号放错地方了。当我运行代码时,我会收到有关意外大括号的错误。
if
else
R
符号应如下所示:

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 }
    
然而,我仍然认为在这些方面存在一个问题,我无法理解您试图实现的目标

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

我觉得奇怪的是,您正在运行
by
+
mapply
。我认为只有
mapply
就足够了。此外,
arimaorder
没有
ic
参数,可能您想将其用于
auto.arima
函数

**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
因为您想知道获得
c(1,0,0)
需要多少次试验,所以我添加了一个额外的列(
index
),它是
所有组合中的行号。当您得到输出为
c(1,0,0)
时,循环被中断,它将打印
索引。代码不会在其余的组合中运行

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)
库(预测)

N它运行indefinately@DanielJames-是的。这意味着永远不会满足
中断
条件。
if
语句或
else if
语句始终为真。算法是(1)将每个组合的计数设置为零。(2) 计算在首次成功获取所有组合的
(1,0,0)
之前,模型订单未
(1,0,0)
的次数。(3) 当获得所有组合的顺序
(1,0,0)
时,停止并不包括在计数中。在这种情况下,我的解决方案不会给您期望的结果?如果你运行它足够多次,当你得到
c(1,0,0)
组合时,它会给你一个数字。我不能投票支持下面的任何一个,因为自从我提出悬赏以来,没有增加任何价值。