[R] debugging a try() statement

2008-02-19 Thread Juliet Hannah
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

__
R-help@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.


Re: [R] debugging a try() statement

2008-02-19 Thread Berwin A Turlach
G'day Juliet,

On Tue, 19 Feb 2008 21:35:11 -0500
Juliet Hannah [EMAIL PROTECTED] wrote:

 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.

[...]

 Is there anything obviously incorrect with my function above. 

Yes, namely:

1) you do not assign it to any object, so how do you call it?
2) it has a formal argument which is not used anywhere.  I guess that
   would be considered bad programming style in any programming
   language. 
3) The way your code is formatted, the return(NA) is on the same line
   has the if-clause.  Hence, at the end of that statement R's parser
   has read a complete statement which is then executed.  Whence, when
   the parser comes across the else, a syntax error should be
   produced. 

Non of these would explain the problem that you mention, but point 3)
raises the question why this code actually works.  From what is shown,
there is (at least to me) no obvious explanation for the behaviour that
you see.

 If not, I will post a full example to illustrate my question.

Well, please not. :) It is pretty obvious, that the code above is
derived by cut  paste from a larger body of code and does not run on
its own. So there is no guarantee that this piece of the code is the
one where the problem lies and you cannot seriously expect people to
debug such code for you (and for that reason your previous posting
probably did not get an answer).  

Likewise, you should not expect people to reverse-engineer and debug a
large body of code for you.  You should do what the footer of e-mails
to r-help requests, namely:

 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html and provide commented,
 minimal, self-contained, reproducible code.

Typically, while trying to produce such a commented, minimal,
self-contained, reproducible example of the problematic code one finds
the bug.

Cheers,

Berwin

=== Full address =
Berwin A TurlachTel.: +65 6515 4416 (secr)
Dept of Statistics and Applied Probability+65 6515 6650 (self)
Faculty of Science  FAX : +65 6872 3919   
National University of Singapore 
6 Science Drive 2, Blk S16, Level 7  e-mail: [EMAIL PROTECTED]
Singapore 117546http://www.stat.nus.edu.sg/~statba

__
R-help@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.