[R] Where to put tryCatch or similar in a very big for loop

Ken vicvoncastle at gmail.com
Thu Sep 15 22:43:26 CEST 2011


What type of singularity exactly, if you're working with counts is it a special case? If using a Monte Carlo generation scheme, there are various workarounds such as while(sum(vec)!=0) {sample} for example. More info on the error circumstances would help.
   Good luck!
    Ken Hutchison

On Sep 15, 2554 BE, at 11:41 AM, "Bonnett, Laura" <L.J.Bonnett at liverpool.ac.uk> wrote:

> Hi Steve,
> 
> Thanks for your response.  The slight issue is that I need to use a different starting seed for each simulation.  If I use 'lapply' then I end up using the same seed each time.  (By contrast, I need to be able to specify which starting seed I am using).
> 
> Thanks,
> Laura
> 
> -----Original Message-----
> From: Steve Lianoglou [mailto:mailinglist.honeypot at gmail.com] 
> Sent: 15 September 2011 16:17
> To: Bonnett, Laura
> Cc: r-help at r-project.org
> Subject: Re: [R] Where to put tryCatch or similar in a very big for loop
> 
> Hi Laura,
> 
> On Thu, Sep 15, 2011 at 10:53 AM, Bonnett, Laura
> <L.J.Bonnett at liverpool.ac.uk> wrote:
>> Dear all,
>> 
>> I am running a simulation study to test variable imputation methods for Cox models using R 2.9.0 and Windows XP.  The code I have written (which is rather long) works (if I set nsim = 9) with the following starting values.
>> 
>>> bootrs(nsim=9,lendevdat=1500,lenvaldat=855,ac1=-0.19122,bc1=-0.18355,cc1=-0.51982,cc2=-0.49628,eprop1=0.98,eprop2=0.28,lda=0.003)
>> 
>> I need to run the code 1400 times in total (bootstrap resampling) however, occasionally the random numbers generated lead to a singularity and hence the code crashes as one of the Cox model cannot be fitted (the 10th iteration is the first time this happens).
>> 
>> I've been trawling the internet for ideas and it seems that there are several options in the form of try() or tryCatch() or next.  I'm not sure however, how to include them in my code (attached).  Ideally I'd like it to run everything simulation from 1 to 1400 and if there is an error at some point get an error message returned (I need to count how many there are) but move onto the next number in the loop.
>> 
>> I've tried putting try(....,silent=TRUE) around each cox model (cph statement) but that hasn't work and I've also tried putting try around the whole for loop without any success.
> 
> Let's imagine you are using an `lapply` instead of `for`, only because
> I guess you want to store the results of `bootrs` somewhere, you can
> adapt this to your `for` solution. I typically return NULL when an
> error is caught, then filter those out from my results, or whatever
> you like:
> 
> results <- lapply(1:1400, function(i) {
>  tryCatch(bootrs(...whatever...), error=function(e) NULL)
> })
> went.south <- sapply(results, is.null)
> 
> The `went.south` vector will be TRUE where an error occurred in your
> bootrs call.
> 
> HTH,
> -steve
> 
> -- 
> Steve Lianoglou
> Graduate Student: Computational Systems Biology
>  | Memorial Sloan-Kettering Cancer Center
>  | Weill Medical College of Cornell University
> Contact Info: http://cbio.mskcc.org/~lianos/contact
> 
> ______________________________________________
> 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.



More information about the R-help mailing list