【问题标题】:tryCatch crashes while nls fit当 nls 适合时 tryCatch 崩溃
【发布时间】:2020-11-09 23:48:44
【问题描述】:

我正在尝试使用 nls 函数拟合非线性模型。该模型具有条件并按组工作,因此我尝试实现一个覆盖 300 多种起始值组合的循环。我正在使用 tryCatch,但令我惊讶的是循环崩溃了,我猜是因为起始值或下限和上限。

tryCatch(

for(r in 1:nrow(st4)){

halfLE3[[r]] <- nls(

  inty ~ I(time < (position/velocity) + dinitial) * Inty_S0 +
    (time >= (position/velocity) + dinitial) *
    I(Intyf[probe] + (Inty_S0[probe] - Intyf) * 
        (exp(-Decay * (time - (position/velocity) + dinitial)))),
  
  data = Data4,

  algorithm = "port",

  control = list(warnOnly = TRUE),
  
  start = list(Decay=st4[r,3], dinitial=st4[r,2],
               Intyf=rep(st4[r,4], length(levels(Data4$probe))), 
               Inty_S0=rep(st4[r,5], length(levels(Data4$probe))),
               velocity=st4[r,1]),
  
  lower = list(Decay= .1, dinitial=0.1, velocity=10, Intyf=0.1, inty_S0=.5),
  upper = list(Decay= 1, dinitial=10, velocity=8000, Intyf=1, inty_S0=1.5)
)

}

,error = function(e) {e}

)

st4 是一个具有 300 个起始值组合的数据框。 probe 指的是组。

您知道为什么即使使用 tryCatch 循环也会崩溃吗?你知道我可以改进什么吗?我使用 nls.control 更改 maxiter 和其他参数,但与我在上面使用的控件相同。

如果有任何建议,我将不胜感激。

【问题讨论】:

    标签: conditional-statements try-catch nls


    【解决方案1】:

    我解决了这个问题。 tryCatch 应该位于添加括号的 for 循环内。

    for(r in 1:nrow(st4)){

    tryCatch({ nls()……

    },error = function(e) {e}

    ) }

    【讨论】:

      猜你喜欢
      • 2022-07-24
      • 2014-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 2015-11-20
      • 2018-01-02
      相关资源
      最近更新 更多