[R] Continuing on with a loop when there's a failure

David Winsemius dwinsemius at comcast.net
Tue Jul 13 16:34:16 CEST 2010


On Jul 13, 2010, at 10:26 AM, David Winsemius wrote:

>
> On Jul 13, 2010, at 9:24 AM, Josh B wrote:
>
>> In my opinion the try and tryCatch commands are written and  
>> documented rather poorly. Thus I am not sure what to program exactly.
>
> Didn't  you see the "silent" parameter? Its seems to be documented  
> fairly clearly to me.
>
> The testing of try at the console is not going to be very  
> illuminating, since it really only has value within a function that  
> you want to continue despite an error. try() WILL provide that  
> facility but _you_ need to decide what you do with the information  
> it returns, which in the case of its use with the default  
> silent=FALSE is just the error message itself.
>
>
>>
>> For instance, I could query mod.poly3 and use an if/then statement  
>> to proceed,
>
> So why didn't you? A good result would be signaled by:

rather: "lrm" %in% class(mod.poly3)
>
>
> -- 
> David.
>
>> but querying mod.poly3 is weird. For instance, here's the output  
>> when it fails:
>>
>> > mod.poly3 <- try(lrm(x[,2] ~ pol(x1, 3) + pol(x2, 3), data=x))
>> Error in fitter(X, Y, penalty.matrix = penalty.matrix, tol = tol,  
>> weights = weights,  :
>>  NA/NaN/Inf in foreign function call (arg 1)
>> > mod.poly3
>> [1] "Error in fitter(X, Y, penalty.matrix = penalty.matrix, tol =  
>> tol, weights = weights,  : \n  NA/NaN/Inf in foreign function call  
>> (arg 1)\n"
>> attr(,"class")
>> [1] "try-error"
>>
>> ...and here's the output when it succeeds:
>> > mod.poly3 <- try(lrm(x[,1] ~ pol(x1, 3) + pol(x2, 3), data=x))
>> > mod.poly3
>>
>> Logistic Regression Model
>>
>> lrm(formula = x[, 1] ~ pol(x1, 3) + pol(x2, 3), data = x)
>>
>>
>> Frequencies of Responses
>> bagels donuts
>>    10      5
>>
>>       Obs  Max Deriv Model L.R.       d.f.          P          C
>>        15      4e-04       3.37          6     0.7616       0.76
>>       Dxy      Gamma      Tau-a         R2      Brier          g
>>      0.52       0.52      0.248      0.279      0.183      1.411
>>        gr         gp
>>       4.1      0.261
>>
>>          Coef     S.E.    Wald Z P
>> Intercept -5.68583 5.23295 -1.09  0.2772
>> x1         1.87020 2.14635  0.87  0.3836
>> x1^2      -0.42494 0.48286 -0.88  0.3788
>> x1^3       0.02845 0.03120  0.91  0.3618
>> x2         3.49560 3.54796  0.99  0.3245
>> x2^2      -0.94888 0.82067 -1.16  0.2476
>> x2^3       0.06362 0.05098  1.25  0.2121
>>
>> ...so what exactly would I query to design my if/then statement?
>>
>> From: David Winsemius <dwinsemius at comcast.net>
>> To: David Winsemius <dwinsemius at comcast.net>
>> Cc: Josh B <joshb41 at yahoo.com>; R Help <r-help at r-project.org>
>> Sent: Tue, July 13, 2010 9:09:04 AM
>> Subject: Re: [R] Continuing on with a loop when there's a failure
>>
>>
>> On Jul 13, 2010, at 9:04 AM, David Winsemius wrote:
>>
>> >
>> > On Jul 13, 2010, at 8:47 AM, Josh B wrote:
>> >
>> >> Thanks again, David.
>> >>
>> >> ...but, alas, I still can't get it work!
>>
>> (BTW, it did work.)
>>
>> >> Here's what I'm trying now:
>> >>
>> >> for (i in 1:2) {
>> >>    mod.poly3 <- try(lrm(x[,i] ~ pol(x1, 3) + pol(x2, 3), data=x))
>> >>    results[1,i] <- anova(mod.poly3)[1,3]
>> >> }
>> >
>> > You need to do some programming.
>>
>> (Or I suppose you could wrap both the lrm and the anova calls in  
>> try.)
>>
>> > You did not get an error from the lrm but rather from the anova  
>> call because you tried to give the results of the try function to  
>> anova without first checking to see if an error had occurred.
>> >
>> > --David.
>> >>
>> >> Here's what happens (from the console):
>> >>
>> >> Error in fitter(X, Y, penalty.matrix = penalty.matrix, tol =  
>> tol, weights = weights,  :
>> >>  NA/NaN/Inf in foreign function call (arg 1)
>> >> Error in UseMethod("anova") :
>> >>  no applicable method for 'anova' applied to an object of class  
>> "try-error"
>> >>
>> >> ...so I still can't make my results matrix. Could I ask you for  
>> some specific code to make this work? I'm not that familiar with  
>> the syntax for try or tryCatch, and the help files for them are  
>> pretty bad, in my humble opinion.
>> >>
>> >> I should clarify that I actually don't care about the failed  
>> runs per se. I just want R to keep going in spite of them and give  
>> me my results matrix.
>> >>
>> >> From: David Winsemius <dwinsemius at comcast.net>
>> >> To: Josh B <joshb41 at yahoo.com>
>> >> Cc: R Help <r-help at r-project.org>
>> >> Sent: Mon, July 12, 2010 8:09:03 PM
>> >> Subject: Re: [R] Continuing on with a loop when there's a failure
>> >>
>> >>
>> >> On Jul 12, 2010, at 6:18 PM, Josh B wrote:
>> >>
>> >> > Hi R sages,
>> >> >
>> >> > Here is my latest problem. Consider the following toy example:
>> >> >
>> >> > x <- read.table(textConnection("y1 y2 y3 x1 x2
>> >> > indv.1 bagels donuts bagels 4 6
>> >> > indv.2 donuts donuts donuts 5 1
>> >> > indv.3 donuts donuts donuts 1 10
>> >> > indv.4 donuts donuts donuts 10 9
>> >> > indv.5 bagels donuts bagels 0 2
>> >> > indv.6 bagels donuts bagels 2 9
>> >> > indv.7 bagels donuts bagels 8 5
>> >> > indv.8 bagels donuts bagels 4 1
>> >> > indv.9 donuts donuts donuts 3 3
>> >> > indv.10 bagels donuts bagels 5 9
>> >> > indv.11 bagels donuts bagels 9 10
>> >> > indv.12 bagels donuts bagels 3 1
>> >> > indv.13 donuts donuts donuts 7 10
>> >> > indv.14 bagels donuts bagels 2 10
>> >> > indv.15 bagels donuts bagels 9 6"), header = TRUE)
>> >> >
>> >> > I want to fit a logistic regression of y1 on x1 and x2. Then I  
>> want to run a
>> >> > logistic regression of y2 on x1 and x2. Then I want to run a  
>> logistic regression
>> >> > of y3 on x1 and x2. In reality I have many more Y columns than  
>> simply "y1,"
>> >> > "y2," and "y3," so I must design a loop. Notice that y2 is  
>> invariant and thus it
>> >> > will fail. In reality, some y columns will fail for much more  
>> subtle reasons.
>> >> > Simply screening my data to eliminate invariant columns will  
>> not eliminate the
>> >> > problem.
>> >> >
>> >> > What I want to do is output a piece of the results from each  
>> run of the loop to
>> >> > a matrix. I want the to try each of my y columns, and not give  
>> up and stop
>> >> > running simply because a particular y column is bad. I want it  
>> to give me "NA"
>> >> > or something similar in my results matrix for the bad y  
>> columns, but I want it
>> >> > to keep going give me good data for the good y columns.
>> >> >
>> >> > For instance:
>> >> > results <- matrix(nrow = 1, ncol = 3)
>> >> > colnames(results) <- c("y1", "y2", "y3")
>> >> >
>> >> > for (i in 1:2) {
>> >> > mod.poly3 <- lrm(x[,i] ~ pol(x1, 3) + pol(x2, 3), data=x)
>> >> > results[1,i] <- anova(mod.poly3)[1,3]
>> >> > }
>> >> >
>> >> > If I run this code, it gives up when fitting y2 because the y2  
>> is bad. It
>> >> > doesn't even try to fit y3. Here's what my console shows:
>> >> >
>> >> >> results
>> >> >            y1 y2 y3
>> >> > [1,] 0.6976063 NA NA
>> >> >
>> >> > As you can see, it gave up before fitting y3, which would have  
>> worked.
>> >> >
>> >> > How do I force my code to keep going through the loop, despite  
>> the rotten apples
>> >> > it encounters along the way?
>> >>
>> >> ?try
>> >>
>> >> http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-capture-or-ignore-errors-in-a-long-simulation_003f
>> >>
>> >> (Doesn't only apply to simulations.)
>> >>
>> >> > Exact code that gets the job done is what I am
>> >> > interested in. I am a post-doc -- I am not taking any classes.  
>> I promise this is
>> >> > not a homework assignment!
>> >>
>> >> --
>> >> David Winsemius, MD
>> >> West Hartford, CT
>> >>
>> >>
>> >>
>> >
>> > David Winsemius, MD
>> > West Hartford, CT
>> >
>> > ______________________________________________
>> > 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.
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>>
>>
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list