[R] debugging a try() statement

Juliet Hannah juliet.hannah at gmail.com
Wed Feb 20 03:35:11 CET 2008


Dear R Users,

I implemented a try() statement that looks like:

 <- function(index)
{

   reduced_model <- try(glm.fit(X4,n,family=poisson(link="log")))
   full_model <- try(glm.fit(X5,n,family=poisson(link="log")))

   if (inherits(reduced_model,"try-error") ||
inherits(full_model,"try-error")) return(NA)

   else
   {
     p <- pchisq(reduced_model$deviance - full_model$deviance,
reduced_model$df.residual - full_model$df.residual, lower.tail= FALSE)
     return (p)
   }

}

After an error occurs NA is returned. But after this occurs, all
values returned after this remain as an NA even though this error
should occur only 1/500 to 1/1000 times.

For example, I end up with a matrix like this:

       [,1]       [,2]       [,3]         [,4]       [,5]
[1,] 0.6291316 0.08900112 0.06693455 6.401101e-06 0.06865330
[2,] 0.6259834 0.21140489 0.06722201 6.401101e-06 0.06833421
[3,]        NA         NA         NA           NA         NA
[4,]        NA         NA         NA           NA         NA
[5,]        NA         NA         NA           NA         NA

Is there anything obviously incorrect with my function above. If not,
I will post a full example to illustrate my question.

Thanks for your help,

Juliet



More information about the R-help mailing list