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 glm.nb
, write the coefficients to a data.frame
and return that back.
Not surprisingly, glm.nb fails to converge for some datasets. Rather than have the function cough up an error and stop, I'd prefer that it continue through the rest of the datasets and return results where possible.
My first attempt was this:
res.model <- function(x)
{
res <- try(invisible(glm.nb(x~y, data=x)))
if(!("try-error" %in% class(res)))
{
return (data.frame(site=unique(x$site_name),species=unique(x$species),coef=res$coefficients[2]))
}
}
Any thoughts on a more generic way to ignore errors so I can make this work?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With