在 R 中循环使用 arima [英] Looping with arima in R

查看:49
本文介绍了在 R 中循环使用 arima的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 for 函数执行多个 arimas.

I am trying to do multiple arimas with the for function.

到目前为止,我的尝试是这样的.

So far my attempt is this.

for(p in 0:20){
  for(q in 0:20){
    for (d in 0:3) {
      fit <- arima(y, order=c(p,d,q),method="ML")
      acc <- accuracy(fit)
      print(p);print(d);print(q)
    }
    }
      
}

我想在一个数据集中获得所有准确度向量,对于每个 arima,还有 3 个额外的列,包含 p、d、q.

I want to get all the accuracy vectors in one dataset, for every arima, with 3 extra columns with p,d,q.

那我想拯救模型和AIC的对数似然

Then I'd like to rescue the log-likelihood of the model and AIC

所以最终输出应该是一个看起来像这样的数据帧,其中每一行都是一个模型.

So the final output should be a dataframe that looks like this, where every line is one model.

                   ME     RMSE      MAE         MPE     MAPE     MASE       ACF1 loglikeli  AIC   p   d  q
Training set       x       x         x           x       x        x          x    x          x    x   x  x

Training set       w       w         w           w       w        w          w    w          w    w   w  w 

Training set       y       y         y           y       y        y          y    y          y    y   y  y

```

推荐答案

大功告成.最简单的方法是将每个 Arima 对象的摘要迭代添加到数据框中.

You're almost there. The easiest way to do this is to iteratively add the summary of each Arima object to a data frame.

下面的代码可以满足您的需求(我减少了迭代次数,否则会花费很长时间).

The code below does what you need (I've reduced the number of iterations otherwise it takes too long).

# load package
library(forecast)

# create some data
y <- rnorm(100)

# object to store arima summary in
model_smry <- data.frame()

# loop to store
for(p in 1:2){
  for(q in 1:2){
    for (d in 1:2) {
      fit <- arima(y, order=c(p,d,q),method="ML")
      acc <- accuracy(fit)
      
      # gather everything into a single data frame 
      acc_ext <- data.frame(# information from accuracy function
                            acc,
                            # goodness of fit
                            loglikeli = logLik(fit),
                            AIC = AIC(fit), 
                            # arima order
                            p,
                            q,
                            d)
      
      # add arima summary
      model_smry <- rbind(model_smry, acc_ext)
   
    }
  }
  
}

# show summary
model_smry

输出:


                       ME      RMSE       MAE        MPE     MAPE      MASE         ACF1 loglikeli      AIC p q d
Training set   0.03590650 0.8270888 0.6536260   61.96955 124.9386 0.6845685 -0.006412280 -124.4806 254.9612 1 1 1
Training set1 -0.03384812 0.9791048 0.7565525 -540.90163 825.9039 0.7923675 -0.129324621 -140.8802 287.7604 1 1 2
Training set2  0.03709185 0.8225973 0.6502470   95.99749 134.0075 0.6810295  0.026597486 -123.9961 255.9921 1 2 1
Training set3 -0.04914004 0.8317765 0.6574596  -51.59546 250.4728 0.6885836 -0.013578522 -129.2061 266.4123 1 2 2
Training set4  0.03698832 0.8239479 0.6516438   26.90046 162.6580 0.6824924  0.001452607 -124.0094 256.0188 2 1 1
Training set5 -0.04342442 0.9527430 0.7233051  -39.01621 319.2866 0.7575462 -0.050439230 -138.4554 284.9108 2 1 2
Training set6  0.03606286 0.8227565 0.6522152  -30.19270 220.3092 0.6830908 -0.003839680 -123.8827 257.7654 2 2 1
Training set7 -0.05099161 0.8291406 0.6503652  -91.41328 289.5055 0.6811533 -0.004315754 -128.5307 267.0613 2 2 2

这篇关于在 R 中循环使用 arima的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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