0

I am running a Monte Carlo simulation with 1000 iterations. Within each iteration, I am fitting a weighted logistic regression model using the lrm function from the Harrell's rms package. The model is fit using this code: lrm(y ~ x, weights=wt,x=T,y=T) From the fitted model, I extract some information such as the regression coefficients and the estimated standard errors.

The simulations crashed with the error message: Unable to fit model using "lrm.fit".

I would like to prevent the simulations from crashing, by only evaluating the function if it is safe to do so. In the very large majority of iterations, there is no problem. Somehow, within each iteration, I would like to tell R to only fit the function if it can be done safely.

Is there a way that this can be done?

1 Answers1

1

Consider using try which will report an error but not exit the whole loop or function.

for (i in 1:10){
    try(lrm(y ~ x, weights=wt,x=T,y=T) )
}

Where something relevant to lrm would be changing on each iteration (such as x for example).

keegan
  • 2,892
  • 1
  • 17
  • 20