Re: [R] help on using try() to catch an error

From: Juliet Hannah <juliet.hannah_at_gmail.com>
Date: Sat, 16 Feb 2008 10:13:23 -0500

Dear R Users,

I incorporated the suggestions given to me, which solved the problem of the program ending. However,
once an error occurs, and the value NA is assigned, the program does not run correctly
after this point. All values assigned after this point are NA.

Here is my function.

getLRTp <- function(index)
{

   n <- as.matrix(table(myData[,index[1]],myData[,index[2]],permute.response))    n[n[,1]==0] <- 1
   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"))    {

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

   }
}

which is called in this manner:

for (perm in 1:nperm)
{
  permute.response <- sample(response,replace=TRUE)   pmatrix[perm,] <- apply(pairs.subset, 1, getLRTp) }

Again, I have included only the relevant part of the code.

If the glm is succeeds, p is a p-value. On the occassion that it does not, p is NA.

pmatrix (my result) ends up looking like:

       [,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

It seems that after encountering an error and NA is assigned, it never recovers. Is this is programming
error?

Thanks for your time,

Juliet



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat 16 Feb 2008 - 15:30:36 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 16 Feb 2008 - 16:30:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive