代码之家  ›  专栏  ›  技术社区  ›  Coolio2654

R rugarch:$operator对原子向量无效?

  •  0
  • Coolio2654  · 技术社区  · 6 年前

    我正在尝试创建一个巨大的嵌套for循环(优化将留待以后使用),以适应所有可从 rugarch .

    这是复制错误的我的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 此处定义的函数:(the 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() 和A hessian() 功能。

    我假设这是某种收敛问题,但不知道是哪种。

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

        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
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Julius Vainora    6 年前

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

    这是一个也不是一个融合问题。用 trace = 1 在这种情况下你可以看到 hybrid 方法来自 solnp nlminb gosolnp 当,显然, 哥斯洛 也无法得到解决方案,它无法无错误地退出。下一个解决方案是 nloptr ,实际上工作得很好。

    依据 哥斯洛 我们有

    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 属于 哥斯洛 通过 solver.control 但这并不好,因为同样的问题也出现在其他模型中(因此,很可能很难为每种情况选择一组好的分布)。

    所以,我们可以继续使用 混合的 但要查找错误,如果有错误,则使用 诺洛普 :

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

    我没有用这个完成你的代码运行,但它已经10分钟多了。