[R] Where to put tryCatch or similar in a very big for loop
Bonnett, Laura
L.J.Bonnett at liverpool.ac.uk
Thu Sep 15 16:53:45 CEST 2011
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.
Many thanks for any suggestions you can offer.
Laura
More information about the R-help
mailing list