0

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

我有一个名为 datatsr 的 df。

我以这种方式提取 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,或者不?

最好的祝福!

4

2 回答 2

4

函数 garchFit() 需要一个公式作为第一个参数(参见garchFit() 此处)。“公式”参数类型无法评估表达式,因此 garchFit() 无法正确解析auto.arima(datatsr[,1])$arma[2]等变量。从此处的表达式中获取公式的一种方法是替换函数(请参见此处的替换())。出于演示的目的,下面是带有 garchFit() 的替代 () 的简化用法:

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])

我希望这会有所帮助,尽管有点晚了。

于 2014-07-17T01:49:08.630 回答
0

1.你指定公式的方式不对

2. 手动输入华宇订单

让我试着用我自己的数据来解释

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 order 的值

尝试通过以下方式检查 garch fit 代码

> 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
}
于 2013-10-16T16:56:03.933 回答