R bootstrap boot sampling leads to some predictors having only one value The context is not very important but still, here it is: i am using the boot package to calculate bootstrap confidence interval for different statistics (coefficients of doubly robust method estimates, C-index, etc…) and I happen to face the same problem each time.
If I understood it correctly, the issue is that, no matter which statistic, I need to fit some glm model on each sample and at some point it throws a contrast error because some of the factors predictors in my model have only one level on this sample.
I can think of 3 solutions but either I don’t know how to code it easily or I don’t like it :

*

*I can include some test in my statistic function to exclude a predictor with less than one value but this means that i’m not fitting the same model on each sample, which I don’t like…


*I could just drop the samples where it happens (and I can increase the number of replicates - R parameter of boot function - to compensate the loss of replicates) but I can’t do it easily with boot function since the error breaks the run. I could use some try or tryCatch code inside my statistic function but I feel like there must be an easier way


*I could use the strata parameter of boot function: at the moment, I only stratified the sampling on my dependant variable y="event occured" but I could use a new strata variable that would consist of crossing all categorical predictors so as to have one strata for each combination of factors’ levels. However, this may result in a very large number of stratas...
I hope someone might have some insights on this issue because I can’t find anything online.
Any idea, advice or comment will be much appreciated.
It is not trivial but if needed I'll try to build some reproducible example.

If I understand the question correctly, I've dealt with this issue before using boot.ci to create bootstrapped confidence intervals.
I've used code like the following.  As an example, here, the code returns the r-squared value for a linear model, labeled STAT.
library(boot)

Function = function(input, index){
                    Input = input[index,]

### CAN INSERT TEST OF APPROPRIATENESS HERE

                    Result = lm (Words.per.minute ~ Age + Age2,
                                          data = Input)
                    Stat = summary(Result)$r.squared
                    return(Stat)}

    
Boot = boot(Data,
             Function,
             R=5000)
            
mean(Boot$t[,1])

Reference: here, with the caveat that I wrote it: rcompanion.org/handbook/G_10.html
At the point where it says ### CAN INSERT TEST OF APPROPRIATENESS HERE you can check to be sure that your predictor has at least two levels.  This would be in the data frame called Input.
If it doesn't, there are a couple of options. First is to exclude that iteration, and go on to to calculate the confidence interval.  Second is to return an NA or something for the confidence interval as a whole.
In either case, if you find that your predictor has only one level (or fewer levels than the original data frame), you would set FLAG=1 and then make the next step if(FLAG==0){ so that it doesn't fit the model in the next step if FLAG equals 1.
Hopefully that makes sense, and hopefully I'm actually answering (that part of) the question.
