Skip to content

Trap an error from a function

8 messages · Duncan Murdoch, John Sorkin, David Winsemius +3 more

#
Window 7
R 2.15
 
I am writing a simulation which generates sample sized estimates from simulated data. When I run the function shown below,
power.t.test(delta=14.02528,sd=1.945226,power=0.8,sig.level=0.05)
 
I get an error message:
Error in uniroot(function(n) eval(p.body) - power, c(2, 1e+07)) : 
  f() values at end points not of opposite sign
 
The fact that the function can not return a sample size is OK, however I need to trap the error and set the sample size equal to NA. How do I trap the error so that when the error occurs I can set sample size equal to NA? 
 
Thanks,
John
 
 
John David Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)
Confidentiality Statement:
This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information.  Any unauthorized use, disclosure or distribution is prohibited.  If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
#
On 12-09-18 8:10 PM, John Sorkin wrote:
You can wrap the call in try().  Then check whether the result inherits 
from try-error, e.g.

res <- try( ... )
if (inherits(res, "try-error")) { do something to handle the error }
else { proceed as you would with no error }

In the example you gave, the problem is that even a sample size of 2 
gives more than 0.8 power.  The function should probably check for that 
case, but it doesn't.

Duncan Murdoch
#
On Sep 18, 2012, at 5:10 PM, John Sorkin wrote:

            
?conditions   #### has lots of fancy stuff
# But I use just plain old `try`

test=-10:10
sapply(test, function(x)  if( "try-error" %in% 
                     class( try( test[test[x:1]] ) ) ){
                     2}else{0} )
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
 [1] 2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0

You still get the messages but the code runs.
#
David,
Thank you. I will study your code so I can understand your suggestion. 
Thanks,
John

 
John David Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)>>> David Winsemius <dwinsemius at comcast.net> 9/18/2012 8:35 PM >>>
On Sep 18, 2012, at 5:10 PM, John Sorkin wrote:

            
?conditions   #### has lots of fancy stuff
# But I use just plain old `try`

test=-10:10
sapply(test, function(x)  if( "try-error" %in% 
                     class( try( test[test[x:1]] ) ) ){
                     2}else{0} )
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
[1] 2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0

You still get the messages but the code runs.
#
On Tue, Sep 18, 2012 at 5:35 PM, David Winsemius <dwinsemius at comcast.net> wrote:
Just to add - if the messages bother you (they would bother me since
normally an error message indicates the function aborted), you can add
argument silent = TRUE to the try() call.

Peter
#
On Wed, Sep 19, 2012 at 12:56 PM, David Winsemius
<dwinsemius at comcast.net> wrote:
I think it's easier to use tryCatch(), eg


sample.size <- tryCatch(

power.t.test(delta=14.02528,sd=1.945226,power=0.8,sig.level=0.05)$n,
                 error=function(e) NA
                 )

The first line gets run, and if there's an error, the error is passed
to the error= argument.  This is a function that takes the error as an
argument, and could do sophisticated things with it, but actually just
returns NA for all errors.

tryCatch() is also quieter.

  -thomas
#
Instead of "someClass" %in% class(someThing), as in
it is better to use inherits(someThing, "someClass"), as in
   function(x) {
             res <- try( test[test[x:1]], silent=TRUE )
             if (inherits(res, "try-error")) { NA } else { res }}
(where I return the result of the attempted operation instead of 2
and NA for an error instead of 0.)

A related approach is to use tryCatch():
    function(x) tryCatch(test[test[x:1]], error=function(e)NA)
You can also catch warnings and messages with tryCatch.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com