R rugarch:$运算符对原子向量无效吗?

时间:2019-01-18 00:52:22

标签: r mathematical-optimization convergence

我正在尝试制作一个巨大的嵌套for循环(以供日后进行优化),以适应rugarch中提供的所有GARCH模型。

这是我的MWE重现错误:

library(rugarch)

## Small parameter space to search over   
AR_terms = c(0,1,2)
I_terms = c(0,1)
MA_terms = c(0,1,2)

garch_p_terms = c(0,1,2)
garch_q_terms = c(0,1,2)

## Models to search over
var_models = c("sGARCH","eGARCH","gjrGARCH","apARCH","iGARCH","csGARCH")

for (x in var_models) {

    if (x == 'fGARCH') {

        for (y in sub_var_models) {

            for (AR in AR_terms) {
                for (MA in MA_terms) {
                    for (I in I_terms) {
                        for (p in garch_p_terms) {
                            for (q in garch_q_terms) {

                                cat(y)

                                spec = spec_creator('fGARCH', y, MA, AR, I, p, q)
                                garch = ugarchfit(spec = spec, data = apple['A'], solver = 'hybrid', solver.control = list(trace=0))

                                cat('Fit Success')

                            }
                        }
                    }
                }
            }

        }

    next ## To skip evaluating fGARCH as its own model with not submodel below.

    }    

    for (AR in AR_terms) {
        for (MA in MA_terms) {
            for (I in I_terms) {
                 for (p in garch_p_terms) {
                    for (q in garch_q_terms) {

                        cat(x)

                        spec = spec_creator(x, 'null', MA, AR, I, p, q)
                        garch = ugarchfit(spec = spec, data = apple['A'], solver = 'hybrid', solver.control = list(trace=0))

                        cat('Fit Success')    

                    }
                }
            }
        }
    }


}

)

在这里定义了我的spec_creator函数:(fGARCH模型允许子模型族,这是大多数冗余代码的原因)

## Function to create the specs, purely to make the for loop area more readable.
spec_creator = function(model, sub_model, AR_term, I_term, MA_term, garch_p_term, garch_q_term) {

    require(rugarch)

    if (sub_model == 'null') {   
        spec = ugarchspec(variance.model = list(model = model, 
                                        garchOrder = c(garch_p_term, garch_q_term), 
                                        submodel = NULL, 
                                        external.regressors = NULL, 
                                        variance.targeting = FALSE), 

                          mean.model = list(armaOrder = c(AR_term, I_term, MA_term)))
    }

    else {
        spec = ugarchspec(variance.model = list(model = 'fGARCH', 
                                        garchOrder = c(garch_p_term, garch_q_term), 
                                        submodel = sub_model, 
                                        external.regressors = sub_model, 
                                        variance.targeting = FALSE), 

                          mean.model = list(armaOrder = c(AR_term, I_term, MA_term)))
    }

}

运行上述命令时,我收到了许多sGARCH模型的成功消息,但最终收到以下错误:Error: $ operator is invalid for atomic vectors,回溯指向ugarchfit()和{{1} }功能。

我假设这是某种趋同性问题,但不知道是哪种类型。

编辑:这是我的数据(尽管其他数据集也有此错误)

hessian()

1 个答案:

答案 0 :(得分:2)

实际上,在很少的型号之后,该错误就会出现。之后,许多其他模型也会引发相同的错误。

这不是一个融合问题。使用trace = 1,您可以看到在那种情况下,hybrid方法从solnpnlminb再到gosolnp,而且显然gosolnp无法获得解决方案,它无法正确退出。下一个求解器将是nloptr,它实际上可以正常工作。

关于gosolnp,我们有

Trying gosolnp solver...
Calculating Random Initialization Parameters...ok!
Excluding Inequality Violations...
...Excluded 500/500 Random Sequences
Evaluating Objective Function with Random Sampled Parameters...ok!
Sorting and Choosing Best Candidates for starting Solver...ok!
Starting Parameters and Starting Objective Function:
     [,1]
par1   NA
par2   NA
par3   NA
objf   NA

意味着所有500组随机初始参数均未满足不等式约束。由于其他一切似乎都工作正常,我怀疑那些初始参数非常不适用于GARCH。尝试最多50000组参数没有帮助。您可以尝试将distr的{​​{1}}传递到gosolnp,但这并不好,因为其他模型也出现了相同的问题(因此,很难选择一套好的分布对于每种情况)。

因此,我们可能会继续使用solver.control,但要查找错误,如果有错误,则使用hybrid

nloptr

我还没有完成运行您的代码,但是十分钟就可以了。