How do I ignore errors and continue processing list items?

前端 未结 2 2083
执笔经年
执笔经年 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:41

    you can also use the failwith function in plyr. if f is the function you are passing to plyr, you can instead pass the function

    safef = failwith(NA, f)
    

    of course, you can replace NA with whatever return value you need when the function fails. this code is lifted directly from the examples for failwith.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题