如何在R中重复调用lmrob时处理错误

时间:2022-06-17 20:30:22

As part of a large amount of data I'm analyzing using R, I make repeated calls to lmrob from package robustbase for a series of datasets.

作为我使用R分析的大量数据的一部分,我从包robustbase中重复调用lmrob来获取一系列数据集。

for (i in uniq.w) {
    y <- x[x$w==i,4]
    t<-seq(length(y))
    result <- try(lmrob(y~t,na.action=na.exclude))
    if(class(result) == "try-error") next;
    output[[i]] <- result
}

However some data sets return errors "Error in eigen(ret, symmetric = TRUE) : infinite or missing values in 'x'". Which aren't suppressed by the above error handling.

然而,一些数据集返回错误“特征误差(ret,symmetric = TRUE):'x'中的无限或缺失值”。上述错误处理不会抑制哪些内容。

So I try something like

所以我尝试了类似的东西

for (i in uniq.w) {
    y <- x[x$w==i,4]
    t<-seq(length(y))
    result <- suppressWarnings(tryCatch(lmrob(y~t,na.action=na.exclude),error=function(e) e))
    if(inherits(result,"error")) next
    output[[i]] <- result
}

This does suppress the errors and warnings. However they still seem to exist somewhere in the system. When I make a subsequent call to lmrob it doesn't work. Instead it returns the same results as from the previous call. (Of course it is quite possible there is some other issue I have overlooked).

这确实可以抑制错误和警告。但是它们似乎仍然存在于系统的某个地方。当我对lmrob进行后续调用时,它不起作用。相反,它返回与上一次调用相同的结果。 (当然很可能还有一些我忽略的问题)。

What is the correct way to approach this situation so I can repeatedly call an R function without errors impacting subsequent batches.

解决这种情况的正确方法是什么,这样我就可以反复调用R函数,而不会影响后续批处理。

Later I check the results to ensure that the solution converged and discard any results where it didn't. Thanks for any assistance.

稍后我检查结果以确保解决方案收敛并丢弃任何结果。谢谢你的帮助。

1 个解决方案

#1


1  

Define a function that you call within a loop

定义在循环中调用的函数

f.lmrob <- function(i){
  y <- x[x$w==i,4]
  t<-seq(length(y))
  lmrob(y~t,na.action=na.exclude)
}

Then using tryCatch, you flag the bad inputs with a custom code for example:

然后使用tryCatch,使用自定义代码标记错误输入,例如:

lapply(uniq.w, function(i)
  tryCatch(f.lmrob(i), error=function(e)-1000-i))

#1


1  

Define a function that you call within a loop

定义在循环中调用的函数

f.lmrob <- function(i){
  y <- x[x$w==i,4]
  t<-seq(length(y))
  lmrob(y~t,na.action=na.exclude)
}

Then using tryCatch, you flag the bad inputs with a custom code for example:

然后使用tryCatch,使用自定义代码标记错误输入,例如:

lapply(uniq.w, function(i)
  tryCatch(f.lmrob(i), error=function(e)-1000-i))