本文介绍了我想在R中模拟并获得最佳ARIMA次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用arima.sim()进行100次仿真,并在每次仿真时使用auto.arima()函数找到最佳模型.我希望程序打印每次获得的ARIMA的顺序.

I want to simulate ARIMA(1,0,0) with arima.sim() 100 times and find the best model with auto.arima() function for each time the simulation is done. I want the program to print the order of ARIMA obtain each time.

reslt = c()
num <- 60
epselon = rnorm(num, mean=0, sd=1^2)
for(i in 1:10){
reslt[i]<-auto.arima(arima.sim(n = num, model=list(ar=0.8, order = c(1, 0, 0)), n.start=1, innov=c(0,epselon[-1])))
}

以上是我尝试过的,但没有结果.

The above is what I tried but no result.

我想要的是将一系列ARIMA(p, d, q)打印10次

What I want is to print a series of ARIMA(p, d, q) into 10 times

推荐答案

这可以做到:

library(forecast)
nsim <- 10
result <- matrix(NA_integer_, nrow = nsim, ncol = 3)
colnames(result) <- c("p","d","q")
num <- 60
for (i in seq(nsim)) {
  result[i, ] <- arima.sim(n=num, model=list(ar=0.8, order=c(1,0,0)), sd=1) %>%
    auto.arima() %>%
    arimaorder()
}
result
#>       p d q
#>  [1,] 0 1 0
#>  [2,] 0 1 0
#>  [3,] 0 1 0
#>  [4,] 1 0 0
#>  [5,] 1 0 0
#>  [6,] 0 1 0
#>  [7,] 0 1 0
#>  [8,] 1 0 0
#>  [9,] 1 0 0
#> [10,] 1 0 0

一些评论:

  • 您的代码每次都会产生相同的系列,因为epselon是在循环外部生成的.由于您只使用随机的常规创新,因此像上面的代码中那样让arima.sim()处理它更简单.
  • 如果您想保留auto.arima()返回的整个模型对象,而不仅仅是我的代码中的订单,则可以这样修改:
  • Your code will produce the same series every time because epselon is generated outside the loop. As you are just using random normal innovations, it is simpler to let arima.sim() handle it as in the code above.
  • If you wanted to keep the whole model object that is returned by auto.arima() rather than just the orders as in my code, you could modify it like this:
library(forecast)
nsim <- 10
result <- list()
num <- 60
for (i in seq(nsim)) {
  result[[i]] <- arima.sim(n=num, model=list(ar=0.8, order=c(1,0,0)), sd=1) %>%
    auto.arima()
}
result
#> [[1]]
#> Series: . 
#> ARIMA(0,1,0) 
#> 
#> sigma^2 estimated as 1.145:  log likelihood=-87.72
#> AIC=177.44   AICc=177.51   BIC=179.52
#> 
#> [[2]]
#> Series: . 
#> ARIMA(1,0,2) with zero mean 
#> 
#> Coefficients:
#>          ar1     ma1     ma2
#>       0.5200  0.4086  0.4574
#> s.e.  0.1695  0.1889  0.1446
#> 
#> sigma^2 estimated as 0.877:  log likelihood=-80.38
#> AIC=168.77   AICc=169.5   BIC=177.15
#> 
#> [[3]]
#> Series: . 
#> ARIMA(0,1,0) 
#> 
#> sigma^2 estimated as 0.9284:  log likelihood=-81.53
#> AIC=165.05   AICc=165.12   BIC=167.13
#> 
#> [[4]]
#> Series: . 
#> ARIMA(1,0,0) with zero mean 
#> 
#> Coefficients:
#>         ar1
#>       0.615
#> s.e.  0.099
#> 
#> sigma^2 estimated as 1.123:  log likelihood=-88.35
#> AIC=180.7   AICc=180.91   BIC=184.89
#> 
#> [[5]]
#> Series: . 
#> ARIMA(0,0,3) with zero mean 
#> 
#> Coefficients:
#>          ma1     ma2      ma3
#>       0.5527  0.2726  -0.3297
#> s.e.  0.1301  0.1425   0.1202
#> 
#> sigma^2 estimated as 0.6194:  log likelihood=-69.83
#> AIC=147.66   AICc=148.39   BIC=156.04
#> 
#> [[6]]
#> Series: . 
#> ARIMA(1,0,0) with non-zero mean 
#> 
#> Coefficients:
#>          ar1    mean
#>       0.7108  0.9147
#> s.e.  0.0892  0.4871
#> 
#> sigma^2 estimated as 1.332:  log likelihood=-93.08
#> AIC=192.15   AICc=192.58   BIC=198.43
#> 
#> [[7]]
#> Series: . 
#> ARIMA(1,0,1) with non-zero mean 
#> 
#> Coefficients:
#>          ar1     ma1     mean
#>       0.6116  0.3781  -1.0024
#> s.e.  0.1264  0.1559   0.4671
#> 
#> sigma^2 estimated as 1.161:  log likelihood=-88.6
#> AIC=185.2   AICc=185.92   BIC=193.57
#> 
#> [[8]]
#> Series: . 
#> ARIMA(1,0,0) with zero mean 
#> 
#> Coefficients:
#>          ar1
#>       0.6412
#> s.e.  0.0969
#> 
#> sigma^2 estimated as 0.8666:  log likelihood=-80.6
#> AIC=165.2   AICc=165.41   BIC=169.39
#> 
#> [[9]]
#> Series: . 
#> ARIMA(0,1,0) 
#> 
#> sigma^2 estimated as 1.314:  log likelihood=-91.78
#> AIC=185.57   AICc=185.64   BIC=187.64
#> 
#> [[10]]
#> Series: . 
#> ARIMA(1,0,0) with non-zero mean 
#> 
#> Coefficients:
#>          ar1    mean
#>       0.6714  1.3449
#> s.e.  0.0985  0.4428
#> 
#> sigma^2 estimated as 1.397:  log likelihood=-94.44
#> AIC=194.89   AICc=195.32   BIC=201.17

这篇关于我想在R中模拟并获得最佳ARIMA次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 07:41