如何正确地将auto.arima中的订单输入到garchFit?

时间:2012-09-28 08:52:27

标签: r

我试图将auto.arima函数中的arma模型的顺序输入到garchFit函数中。

我有一个df calles datatsr。

我以这种方式提取AR术语的顺序:

auto.arima(datatsr[,1])$arma[1] :
[1] 4

MA术语的顺序:

auto.arima(datatsr[,1])$arma[2] :
[1] 3

因此,我有arma(4,3)

然后我尝试在garchFit公式中输入这些值,并预测我的嵌入列表中的前一步值(59天用于预测第60天),名为mmk

Garchfore <- function(datatsr, mmk) {
library(fGarch)
windowsL <- split(t(mmk), rep(1:nrow(mmk), each=ncol(mmk)))  
names(windowsL) <- unlist(lapply(windowsL,
                 function(x) paste(rownames(datatsr)[range(x)], sep="",   collapse=" - ")))
one<-lapply(windowsL, function(x) 
predict(garchFit(formula = ~arma(auto.arima(datatsr[,1])$arma[1],auto.arima(datatsr[,1])$arma[2])
                 +garch(1,1), data = datatsr[rev(unlist(x)),1]),
        n.ahead=1))
}

然后我调用函数

predi=Garchfore(datatsr,mmk)

我收到错误:

Error in predict(garchFit(formula = ~arma(auto.arima(datatsr[, 1])$arma[1],  : 
error in evaluating the argument 'object' in selecting a method for function 'predict':
Error in    .garchArgsParser(formula = formula, data = data, trace = FALSE) : 
Formula and data units do not match.

无论如何都知道我做错了什么?或者甚至可以在我尝试时将auto.arima传递给garchFit吗?

祝你好运!

2 个答案:

答案 0 :(得分:4)

函数garchFit()期望公式作为第一个参数(参见garchFit() here)。 &#34;式&#34;参数类型无法计算表达式,因此garchFit()无法正确解析变量,例如 auto.arima(datatsr [,1])$ ​​arma [2] 。从表达式中获取公式的一种方法是替换函数(参见substitute() here)。以下是使用garchFit()的substitute()的简化用法,用于演示:

library(fGarch)
library(doParallel)

N = 200
x.vec = as.vector(garchSim(garchSpec(rseed = 1985), n = N)[,1])
m.list <- 
  foreach(i=1:2) %:%
  foreach(j=1:2) %do% {
    garchFit(substitute(~ garch(p,q),list(p=i, q=j)), 
             data = x.vec, trace = FALSE)  
  }

在garchFit()用法的上下文中,您可以将公式参数替换为garchFit(),如下所示:

garchFit(substitute(formula = ~arma(p,q)+garch(1,1), 
                    list(p=auto.arima(datatsr[,1])$arma[1],
                         q=auto.arima(datatsr[,1])$arma[2])),
         data = datatsr[rev(unlist(x)),1])

我希望这会有所帮助,虽然有点晚。

答案 1 :(得分:-1)

1。指定公式的方式是错误的

2。您应该手动输入arima订单

让我试着解释一下 使用我自己的数据

garchFit(formula= ~arma(3,2) + aparch(1,1), data=ret.fin.chn)

产量结果

Title:
 GARCH Modelling 

Call:
 garchFit(formula = ~arma(3, 2) + aparch(1, 1), data = ret.fin.chn) 

Mean and Variance Equation:
 data ~ arma(3, 2) + aparch(1, 1)
<environment: 0x0df1f498>
 [data = ret.fin.chn]

Conditional Distribution:
 norm 

Coefficient(s):
     mu          ar1          ar2          ar3          ma1          ma2        omega       alpha1  
 1.4860e-04   6.3611e-01  -6.1945e-01   1.9331e-02  -6.6944e-01   6.4677e-01   1.0186e-05   3.1339e-02  
     gamma1        beta1        delta  
 3.6329e-04   9.7088e-01   1.4759e+00  

Std. Errors:
 based on Hessian 

Error Analysis:
         Estimate  Std. Error  t value Pr(>|t|)    
mu      1.486e-04   2.893e-04    0.514 0.607512    
ar1     6.361e-01   3.752e-01    1.695 0.090028 .  
ar2    -6.195e-01   1.872e-01   -3.310 0.000934 ***
ar3     1.933e-02   3.307e-02    0.585 0.558851    
ma1    -6.694e-01   3.754e-01   -1.783 0.074552 .  
ma2     6.468e-01   2.066e-01    3.130 0.001748 ** 
omega   1.019e-05   3.645e-06    2.794 0.005202 ** 
alpha1  3.134e-02   5.490e-03    5.708 1.14e-08 ***
gamma1  3.633e-04   6.787e-02    0.005 0.995729    
beta1   9.709e-01   4.663e-03  208.212  < 2e-16 ***
delta   1.476e+00   3.241e-01    4.554 5.26e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Log Likelihood:
 7311.694    normalized:  2.659765 

Description:
 Wed Oct 16 23:43:41 2013 by user: ASUS 

,而

> garchFit(formula= ~arma(arima.ret.fin.chn$arma[1],arima.ret.fin.chn$arma[2]) + aparch(1,1), data=ret.fin.chn)

产量结果

[1] "arima.ret.fin.chn" "arma"              "data"             
[1] "data"
Error in .garchArgsParser(formula = formula, data = data, trace = FALSE) : 
  Formula and data units do not match.

现在让我们看看

> arima.ret.fin.chn$arma[1]

[1] 3

> arima.ret.fin.chn$arma[2]
[1] 2

> class(arima.ret.fin.chn$arma[1])

[1] "integer"

> class(3)

[1] "numeric"

,而

b<-as.numeric(3)
c<-as.numeric(2)
> garchFit(formula=~garch(b,c),data=ret.fin.chn)
[1] "b"    "c"    "data"
[1] "data"

了解它呢?

我的建议是你 直接写出arima订单的价值

尝试通过

检查garch适合代码
> garchFit

function (formula = ~garch(1, 1), data = dem2gbp, init.rec = c("mci", 
"uev"), delta = 2, skew = 1, shape = 4, cond.dist = c("norm", 
"snorm", "ged", "sged", "std", "sstd", "snig", "QMLE"), include.mean = TRUE, 
include.delta = NULL, include.skew = NULL, include.shape = NULL, 
leverage = NULL, trace = TRUE, algorithm = c("nlminb", "lbfgsb", 
    "nlminb+nm", "lbfgsb+nm"), hessian = c("ropt", "rcd"), 
control = list(), title = NULL, description = NULL, ...) 
{
DEBUG = FALSE
init.rec = match.arg(init.rec)
cond.dist = match.arg(cond.dist)
hessian = match.arg(hessian)
algorithm = match.arg(algorithm)
CALL = match.call()
Name = capture.output(substitute(data))
if (is.character(data)) {
    eval(parse(text = paste("data(", data, ")")))
    data = eval(parse(text = data))
}
data <- as.data.frame(data)
if (isUnivariate(data)) {
    colnames(data) <- "data"
}
else {
    uniqueNames = unique(sort(colnames(data)))
    if (is.null(colnames(data))) {
        stop("Column names of data are missing.")
    }
    if (length(colnames(data)) != length(uniqueNames)) {
        stop("Column names of data are not unique.")
    }
}
if (length(formula) == 3 && isUnivariate(data)) 
    formula[2] <- NULL
if (length(formula) == 2) {
    if (isUnivariate(data)) {
        formula = as.formula(paste("data", paste(formula, 
            collapse = " ")))
    }
    else {
        stop("Multivariate data inputs require lhs for the formula.")
    }
}
robust.cvar <- (cond.dist == "QMLE")
args = .garchArgsParser(formula = formula, data = data, trace = FALSE)
if (DEBUG) 
    print(list(formula.mean = args$formula.mean, formula.var = args$formula.var, 
        series = args$series, init.rec = init.rec, delta = delta, 
        skew = skew, shape = shape, cond.dist = cond.dist, 
        include.mean = include.mean, include.delta = include.delta, 
        include.skew = include.skew, include.shape = include.shape, 
        leverage = leverage, trace = trace, algorithm = algorithm, 
        hessian = hessian, robust.cvar = robust.cvar, control = control, 
        title = title, description = description))
ans = .garchFit(formula.mean = args$formula.mean, formula.var = args$formula.var, 
    series = args$series, init.rec, delta, skew, shape, cond.dist, 
    include.mean, include.delta, include.skew, include.shape, 
    leverage, trace, algorithm, hessian, robust.cvar, control, 
    title, description, ...)
ans@call = CALL
attr(formula, "data") <- paste("data = ", Name, sep = "")
ans@formula = formula
ans
}