R导入ARIMA模型输出以用于预测

时间:2019-05-09 06:17:14

标签: r arima forecast

我已经使用auto.arima函数对91个模型进行了ARIMA建模。输出位于列表列表中。

一种模型的输出结构如下:

    List of 19
      $ coef     : Named num [1:8] -3.17e-01 -3.78e-01 -8.02e-01 -5.39e+04 -1.33e+05 ...
      ..- attr(*, "names")= chr [1:8] "ar1" "ar2" "ma1" "Price.Diff" ...
      $ sigma2   : num 6.37e+10
      $ var.coef : num [1:8, 1:8] 1.84e-02 8.90e-03 -7.69e-03 -8.80e+02 2.83e+03 ...
      ..- attr(*, "dimnames")=List of 2
      .. ..$ : chr [1:8] "ar1" "ar2" "ma1" "Price.Diff" ...
      .. ..$ : chr [1:8] "ar1" "ar2" "ma1" "Price.Diff" ...
      $ mask     : logi [1:8] TRUE TRUE TRUE TRUE TRUE TRUE ...
      $ loglik   : num -1189
      $ aic      : num 2395
      $ arma     : int [1:7] 2 1 0 0 1 1 0
      $ residuals: Time-Series [1:87] from 1 to 87: 1810 -59503 263294 240970 94842 ...
      $ call     : language auto.arima(y = x[, 2], stepwise = FALSE, approximation = FALSE, xreg = x[, 3:ncol(x)], x = list(x = c(1856264.57,| __truncated__ ...
      $ series   : chr "x[, 2]"
      $ code     : int 0
      $ n.cond   : int 0
      $ nobs     : int 86
      $ model    :List of 10
      ..$ phi  : num [1:2] -0.317 -0.378
      ..$ theta: num -0.802
      ..$ Delta: num 1
      ..$ Z    : num [1:3] 1 0 1
      ..$ a    : num [1:3] -599787 284456 1887763
      ..$ P    : num [1:3, 1:3] 0.00 0.00 -4.47e-23 0.00 3.33e-16 ...
      ..$ T    : num [1:3, 1:3] -0.317 -0.378 1 1 0 ...
      ..$ V    : num [1:3, 1:3] 1 -0.802 0 -0.802 0.643 ...
      ..$ h    : num 0
      ..$ Pn   : num [1:3, 1:3] 1.00 -8.02e-01 -1.83e-23 -8.02e-01 6.43e-01 ...
      $ bic      : num 2417
      $ aicc     : num 2398
      $ xreg     : Time-Series [1:87, 1:5] from 1 to 87: -0.866 -0.466 -1.383 -0.999 -0.383 ...
      ..- attr(*, "dimnames")=List of 2
      .. ..$ : NULL
      .. ..$ : chr [1:5] "Price.Diff" "Easter" "Christmas" "High.Week" ...
      $ x        : Time-Series [1:87] from 1 to 87: 1856265 1393925 2200962 2209996 2161707 ...
      $ fitted   : Time-Series [1:87] from 1 to 87: 1854455 1453429 1937668 1969026 2066864 ...
      - attr(*, "class")= chr [1:3] "ARIMA" "forecast_ARIMA" "Arima"

打印后,输出如下:

Series: x[, 2] 
Regression with ARIMA(2,1,1) errors 

Coefficients:
          ar1      ar2      ma1      Price.Diff      Easter      Christmas      High.Week      Low.Week
         -0.3170  -0.3777  -0.8017  -53931.11       -133187.55  -53541.62      -347146.59     216202.71
s.e.      0.1356   0.1319   0.1069   28195.33        68789.25    23396.62      -74115.78      66881.15

sigma^2 estimated as 6.374e+10:  log likelihood=-1188.69
AIC=2395.38   AICc=2397.75   BIC=2417.47

我编写了以下内容以将模型导出为文本文件格式:

# export model outputs to newly created folder
for(i in 1:length(ts_outputs)){
 sink(paste0(names(ts_outputs[i]), ".txt"))
 print(ts_outputs[i])
 sink()
}

此方法可以查看模型输出本身,但是我需要能够将模型输出重新导入R中,以使用它们预测我的时间序列向前。

我假设一旦重新导入,就需要将它们放回原始结构中。

是否已经编写了某个软件包来执行此操作?

文本文件是否适合原始导出?

我相信以下是来自预测包的源代码,该源代码写入了输出(https://rdrr.io/github/ttnsdcn/forecast-package/src/R/arima.R):

        if (length(x$coef) > 0) {
                cat("\nCoefficients:\n")
                coef <- round(x$coef, digits=digits)
                if (se && nrow(x$var.coef)) {
                    ses <- rep(0, length(coef))
                    ses[x$mask] <- round(sqrt(diag(x$var.coef)), digits=digits)
                    coef <- matrix(coef, 1, dimnames=list(NULL, names(coef)))
                    coef <- rbind(coef, s.e.=ses)
                }
                print.default(coef, print.gap=2)
            }
            cm <- x$call$method
            if (is.null(cm) || cm != "CSS")
            {
                cat("\nsigma^2 estimated as ", format(x$sigma2, digits=digits),
                    ":  log likelihood=", format(round(x$loglik, 2)),"\n",sep="")
                npar <- length(x$coef) + 1
                nstar <- length(x$residuals) - x$arma[6] - x$arma[7]*x$arma[5]
                bic <- x$aic + npar*(log(nstar) - 2)
                aicc <- x$aic + 2*npar*(nstar/(nstar-npar-1) - 1)
                cat("AIC=", format(round(x$aic, 2)), sep="")
                cat("   AICc=", format(round(aicc, 2)), sep="")
                cat("   BIC=", format(round(bic, 2)), "\n",sep="")
            }
            else cat("\nsigma^2 estimated as ", format(x$sigma2, digits=digits),
                ":  part log likelihood=", format(round(x$loglik, 2)),
                "\n", sep="")
            invisible(x)
        }

赞赏任何方向/建议。

0 个答案:

没有答案