没有通过try和tryCatch避免/跳过错误 [英] not avoiding/skipping errors with try and tryCatch

查看:406
本文介绍了没有通过try和tryCatch避免/跳过错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 nlsLM for循环中,因为我想尝试不同的开始值以适应我的数据。我已经知道一些起始值在初始参数估计中产生这个错误奇异梯度矩阵,但是我想跳过这个错误并继续使用循环,拟合回归与下一个起始值。我试图将所有 for循环放在 try tryCatch 块,设置 silence = TRUE ,但是当初始参数估计 c 单数梯度矩阵时代码仍然停止



有人可以帮助我吗?



这是代码:

  try({
for(scp.loop in scp.matrix){
for(fit.rate in 1:10){
print(scp.loop)
print(fit.rate)

#fit model with nlsLM
#blah,blah,blah
}}
},silent = FALSE)


解决方案

要了解问题,您需要了解 try()的工作原理。具体来说, try 将运行代码,只要它是第一个参数,直到代码完成自己的代码或直到遇到错误为止。 try()特殊的东西是,如果你的代码中有一个错误,它将捕获该错误(不运行剩余的代码在它的第一个参数)和(1)返回该错误和一个普通的R对象和(2)允许代码 try()跑步。例如:

  x<  -  try({
a = 1#这行运行
stop '任意错误')#提出一个明确的错误
b = 2#这行不运行
})
打印('hello world')#这行运行尽管错误
注意,在上面的代码中,x是class'try-error'的对象,而在下面的代码中是相等的到2(块的最后一个值):

  x<  -  try({
a = 1#this线路运行
b = 2#此线路也运行
})

获取返回允许您通过 inherits(x,'try-error')来测试是否有错误。



这样适用于你的是,我很确定你只想包含
在您的 try() statemetn内的for循环中运行的块,如:

  for(scp.loop in scp.matrix)
for(fit.rate in 1:10)
try( {
print(scp.loop)
print(fit.rate)

blah,blah,blah,

else {coeff.poly.only .res< -coef(polyfitted.total)}
},silent = FALSE)


I have a nlsLM inside a for loop because I want to try different start values to fit my data. I already know that some start values generates this error: singular gradient matrix at initial parameter estimates, but I want to skip this error and continue with the loop, fitting the regression with the next start values. I tried to put all the for loop inside a try and a tryCatch block, setting silence=TRUE, but the code still stopping when the singular gradient matrix at initial parameter estimates error occurs.

Someone can help me with that?

Here is the code:

try({
    for (scp.loop in scp.matrix){
    for (fit.rate in 1:10){
         print(scp.loop)
         print(fit.rate)

         #fit model with nlsLM
         #blah, blah, blah
     }}
     },silent=FALSE)

解决方案

To understand the problem you need to understand how try() works. Specifically, try will run the code provided it's first argument until the code completes on it's own or until it encounters an error. The special thing that try() does is that if there is an error in your code it will catch that error (without running the remainder of the code within it's first argument) and (1) return that error and an ordinary R object and (2) allow code after the try() statement to run. For example:

x <- try({
    a = 1  # this line runs
    stop('arbitrary error') # raise an explicit error
    b = 2  # this line does not run
})
print('hello world') # this line runs despite the error

Note that in the above code x is an object of class 'try-error', whereas in the following codex is equal to 2 (the last value of the block):

x <- try({
    a = 1  # this line runs
    b = 2  # this line runs too
})

obtaining the return allows you to test whether there was an error via inherits(x,'try-error').

How this applies to you is that I'm pretty sure you just want to include the block that runs within the for loops in your try() statemetn as in:

for (scp.loop in scp.matrix)
    for (fit.rate in 1:10)
        try({ 
            print(scp.loop)
            print(fit.rate)

            blah, blah, blah, 

            else{coeff.poly.only.res<<-coef(polyfitted.total)}
        },silent=FALSE)

这篇关于没有通过try和tryCatch避免/跳过错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆