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

David Winsemius dwinsemius at comcast.net
Sun Jul 18 13:33:07 CEST 2010


On Jul 18, 2010, at 4:25 AM, Josh B wrote:

> Hello Peter,
>
> I tried your suggestion, but I was still not able to get it to work.  
> Would you
> mind looking at my code again? Here's what I'm trying:
>
> 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)

closeAllConnections()

>
> results <- matrix(nrow = 1, ncol = 3)
> colnames(results) <- c("y1", "y2", "y3")

require(rms)  # or Design
for (i in 1:2) {
    mod.poly3 <- try(lrm(x[,i] ~ pol(x1, 3) + pol(x2, 3), data=x),  
silent=TRUE)
    if(class(mod.poly3)[1] != 'try-error')
        {results[1,i] <- anova(mod.poly3)[1,3]}

}

 > results
             y1 y2 y3
[1,] 0.6976063 NA NA

>
>
> ...and here's the output:
>
>> results
>     y1 y2 y3
> [1,] NA NA NA
>
> The results matrix is empty!
>
>
>
> ________________________________
> From: Peter Konings <peter.l.e.konings at gmail.com>
>
> Sent: Tue, July 13, 2010 5:45:17 PM
> Subject: Re: [R] Continuing on with a loop when there's a failure
>
> Hi Josh,
>
> Test the class of the resulting object. If it is 'try-error' fill  
> your result
> with NA or do some other error handling.
>
> result <- try(somemodel)
> if(class(result) == 'try-error')
> {
> # some error handling
> } else {
> # whatever happens if the result is ok
> }
>
> HTH
> Peter.
>
>
>
>
> In my opinion the try and tryCatch commands are written and  
> documented rather
>> poorly. Thus I am not sure what to program exactly.
>>
>> For instance, I could query mod.poly3 and use an if/then statement  
>> to proceed,
>> 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>
>>
>> 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.
>>>>
>> [[elided Yahoo spam]]
>>
>>
>> (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>
>>
>>
>>>> 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
>>>>
>>>> f
>>>>
>>>> (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
>> [[elided Yahoo spam]]
>>
>>>>
>>>> --
>>>> 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
>>
>>
>>
>>
>       [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> 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.
>>
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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