如何计算在R中为不同的Arima模拟组合获得第一个真阶之前,ARIMA阶数不为真的次数



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

为了知道在获得所寻求模型的真实顺序之前,可能需要为函数的不同参数组合(样本大小、标准偏差和模型系数)运行arima.sim()多少次,我希望这个R脚本count它将运行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)

您的大括号位于错误的位置。当我运行您的代码时,我收到有关意外大括号的错误。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"]]) 

最新更新