在R中与arima循环

在R中与arima循环,r,forecasting,arima,R,Forecasting,Arima,我试图用for函数做多个arima 到目前为止,我的尝试是这样的 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) } } } for(0:20中的p){

我试图用for函数做多个arima

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

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)
    }
    }
      
}
for(0:20中的p){
对于(0:20中的q){
对于(0:3中的d){

fit就快到了。最简单的方法是迭代地将每个Arima对象的摘要添加到数据帧中

下面的代码满足了您的需要(我已经减少了迭代次数,否则会花费太长时间)


与@ralph的原始问题和答案中的嵌套循环不同,还可以使用函数,然后使用
apply
将其矢量化。这种样式更易于模块化和调试:

library(forecast)

# create some data
set.seed(456)
y <- rnorm(100)

# create a matrix of all desired orders
pqd <- expand.grid(p=1:2, q=1:2, d=1:2)

# a function that does the analysis for a single case
fit_arima <- function(ord) {
  fit <- arima(y, order = ord, method = "ML")
  acc <- accuracy(fit)
  c(acc = acc, loglikeli = logLik(fit),  AIC = AIC(fit))
}

# test a single case
fit_arima(c(p=1, q=1, d=1))

# run all
ret <- apply(pqd, 1, fit_arima)

# bind input and results together
cbind(pqd, t(ret))
库(预测)
#创建一些数据
种子集(456)

谢谢!这很有效。但是我用的是p&q=20和d=3,所以很多。
# 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
library(forecast)

# create some data
set.seed(456)
y <- rnorm(100)

# create a matrix of all desired orders
pqd <- expand.grid(p=1:2, q=1:2, d=1:2)

# a function that does the analysis for a single case
fit_arima <- function(ord) {
  fit <- arima(y, order = ord, method = "ML")
  acc <- accuracy(fit)
  c(acc = acc, loglikeli = logLik(fit),  AIC = AIC(fit))
}

# test a single case
fit_arima(c(p=1, q=1, d=1))

# run all
ret <- apply(pqd, 1, fit_arima)

# bind input and results together
cbind(pqd, t(ret))