简体   繁体   English

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

[英]R rugarch: $ operator invalid for atomic vectors?

I am trying to make a huge nested for loop (optimizations be left for later) to fit all of the GARCH models available from rugarch . 我试图制作一个巨大的嵌套for循环(以供日后进行优化)以适合rugarch所有GARCH模型。

This is my MWE that reproduces the error: 这是我的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')    

                    }
                }
            }
        }
    }


}

)

with my spec_creator function defined here: (the fGARCH model allows a submodel family, which is the reason for most of the redundant code) 在这里定义了我的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)))
    }

}

When I run the above, I get successful messages for many sGARCH models, but eventually get this error: Error: $ operator is invalid for atomic vectors , with the traceback pointing to ugarchfit() and a hessian() function. 当我运行上述命令时,我获得了许多sGARCH模型的成功消息,但最终得到以下错误: Error: $ operator is invalid for atomic vectors ,其回溯指向ugarchfit()hessian()函数。

I am assuming this is some sort of convergence issue, but have no idea what kind. 我假设这是某种融合问题,但不知道是哪种问题。

EDIT: This is my data (though this same error comes with other datasets as well), 编辑:这是我的数据(尽管其他数据集也有此错误),

    A
    28.57223993
    28.30616607
    28.2447644
    28.29934366
    28.39485735
    28.80420177
    29.29541506
    29.42504079
    29.31588228
    29.51373208
    30.25737443
    28.94747231
    28.85195861
    28.72915529
    29.17943414
    29.12485489
    29.04298601
    28.96111712
    27.95822332
    28.5381279
    28.68822085
    28.12878349
    27.96504572
    29.32952709
    30.31877609
    30.1345711
    29.629713
    30.01859019
    30.71447569
    30.55756033
    29.09756526
    29.72522669
    29.96401093
    29.96401093
    28.98840675
    27.59663575
    28.07420423
    28.89971546
    28.70868807
    27.75355111
    28.28569885
    29.21354618
    31.89475207
    31.29438027
    31.36260434
    31.41718359

Actually the error appears after very few models. 实际上,该错误在很少的模型之后出现。 Afterwards many other models throw the same error as well. 之后,许多其他模型也抛出相同的错误。

It is and isn't a convergence issue. 这是一个也不是一个收敛问题。 With trace = 1 you can see that in that case hybrid method goes from solnp to nlminb to gosolnp and when, apparently, gosolnp is also unable to get a solution, it fails to exit without errors. trace = 1情况下,您可以看到在这种情况下, hybrid方法从solnp变为nlminbgosolnp并且显然在gosolnp也无法获得解决方案的情况下,它无法正确退出。 The next solver would be nloptr , which actually works fine. 下一个求解器将是nloptr ,它实际上可以正常工作。

In terms of gosolnp , we have 关于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

Meaning that all 500 sets of random initial parameters fail to satisfy inequality constraints. 这意味着所有500组随机初始参数都不能满足不平等约束。 As everything else seems to be working fine, I'd suspect that those initial parameter are very unsuitable for GARCH. 由于其他一切似乎都工作正常,我怀疑那些初始参数非常不适用于GARCH。 Trying up to 50000 sets of parameters doesn't help. 尝试最多50000组参数没有帮助。 You could probably experiment with passing distr of gosolnp through solver.control , but that's not great since the same issue arises also with other models (so, likely it's hard to pick a good set of distributions for every case). 你很可能与传递实验distrgosolnp通过solver.control ,但因为同样的问题与其他模型(所以,可能很难挑个好组分布于各种情况下)也出现,这不是很大。

So, what we may do is to still use hybrid but to look for an error and if there is one, then to use nloptr : 因此,我们可能要做的就是仍然使用hybrid但要查找错误,如果有错误,则使用nloptr

spec <- spec_creator(x, 'null', MA, AR, I, p, q)
garch <- tryCatch(ugarchfit(spec = spec, data = apple['A'],
                            solver = 'hybrid', solver.control = list(trace = 0)),
                  error = function(e) e)
if(inherits(garch, "error")) {
  garch <- ugarchfit(spec = spec, data = apple['A'],
                     solver = 'nloptr', solver.control = list(trace = 0))
}

I didn't finish running your code with this, but it was fine for over 10 minutes. 我没有用此完成您的代码运行,但是十分钟以上就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM