How do I ignore errors and continue processing list items?

前端 未结 2 2085
执笔经年
执笔经年 2021-02-14 12:19

I\'m running several hundred datasets through glm.nb using a wrapper function. Nothing fancy, I just pass on each list item via llply, then fit using <

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-14 12:58

    I have an nls() which I run that has the same challenge. I'm applying the regression to every group in a data.frame, but this logic should work for you as well (I think):

     dlply(myData, c("group1", "group2"), function(df){  
           tryCatch(nls(depen ~ exp(a1 + b1 * year) , data=df, start = list(a1 = -1, b1 = 0), na.action=na.omit), error=function(e) NULL)
    

    so if I were to guess how to apply that to your situation, it would be something like:

    res <- trycatch(glm.nb(x~y, data=x), error=function(e) NULL )
    

    The way I use this, I'm throwing NA values any time the regression does not converge. You may want to do something different.

提交回复
热议问题