问题描述
大多数时候运行arima.sim()
函数来模拟arima mosel
的特定顺序,但是当通过auto 检查这些模拟的时间序列数据时.arima()
函数,它通常不会与 arima.sim()
中指定的 ARIMA 的顺序相同.
Most times one runs arima.sim()
function to simulate a particular order of arima mosel
but when one check such simulated time series data through auto.arima()
function, it will not often time be the same order of ARIMA one desire and specified in the arima.sim()
.
为了知道在获得参数(样本大小、标准偏差和模型系数)的不同组合之前,可能需要运行 arima.sim()
函数多少次所寻求模型的真正顺序,我希望这个 R
脚本在 count
之前运行 arima.sim()
多少次它得到 arima.sim()
函数中指定的施加 ARIMA-order
.
In my bid to know how many times one may need to run arima.sim()
function for a different combination of its parameter (sample size, standard deviation and coefficient of the model) before obtaining the true order of the model sought for, I want this R
script to count
how many time it will run an arima.sim()
before it get the exert ARIMA-order
specified in the arima.sim()
function.
**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()
试验.
I am interested in knowing how many trials of arima.sim()
will one make before obtaining the first ARIMA(1, 0, 0).
推荐答案
你在运行 by
+ mapply
对我来说似乎很奇怪.我认为只有 mapply
就足够了.此外,arimaorder
没有 ic
参数,也许您打算将它用于 auto.arima
函数.
It seems odd to me that you are running by
+ mapply
. I think only mapply
is enough. Moreover, arimaorder
does not have ic
argument, maybe you meant to use it for auto.arima
function.
由于您想知道获得 c(1, 0, 0)
需要多少次试验,我添加了一个额外的列 (index
),即行all_combos
中的数字.一旦您获得 c(1, 0, 0)
输出,循环就会中断并打印 index
.该代码不会针对其余组合运行.
Since you want to know how many trials are needed to get c(1, 0, 0)
, I add an additional column (index
) which is the row number in all_combos
. As soon as you get output as c(1, 0, 0)
the loop is broken and it prints the index
. The code doesn't run for rest of the combinations.
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)
这篇关于对于不同的 arima 模拟组合,如何在 r 中获得第一个真实顺序之前计算 arima 顺序不正确的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!