在有序模拟的不同组合中,如何计算在r中获得第一个真阶之前有阶数不为真的次数 [英] 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

查看:96
本文介绍了在有序模拟的不同组合中,如何计算在r中获得第一个真阶之前有阶数不为真的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数情况下,运行arima.sim()函数来模拟arima mosel的特定顺序,但是当通过auto.arima()函数检查这种模拟的时间序列数据时,时间通常不会与一个ARIMA顺序相同,而是需要指定在arima.sim().

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)

这篇关于在有序模拟的不同组合中,如何计算在r中获得第一个真阶之前有阶数不为真的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆