1
$\begingroup$

Context

I'm not sure what to do in the scenario where one of the levels for a variable has so few levels that there's a good chance splitting the data into $70\%$ training $30\%$ testing will result in the level being completely contained in either test or train.

For example with the following data:

> table(testset$x1)

BL BM BR ML MM MR TL TR
90  5 76 19  8 10  2  2
> table(trainset$x1)

BL  BM  BR  ML  MM  MR  TL  TR
217  10 182  37  20  27   1   0

Here BL=bottom-left, MM=middle-middle, TR=top-right.

Given that there are so few observations for TL,TM,TR (actually zero for TM) it's quite likely that they could all be contained in either test / training. If this happens then prediction for that is problematic.

Reordering the variable

The levels could be merged from

BL BM BR ML MM MR TL TR -> B M T
                        -> B NB

Where B,NB represent bottom, not-bottom respectively.

This would greatly reduce the probability of either test / training containing the entirity of the level, but it feels as though it's a pretty extreme distortion. Perhaps to the degree that it's no longer usable.

Model

Currently the model is constructed as

m <- glm(y ~ x1 + x2, data = trainset, family="binomial")

Which throws an error after trying to use it for prediction (as the entirity of one level is contained within one set for the given random seed).

Constructing the model as :

m <- glmer(y ~ (1|x1) + x2, data=df, family="binomial" )

Stops this error from occuring, where x1 is modelled as a random effect. I'm not sure if this makes sense though, because I actually wanted to use this for prediction, rather than just controlling for its variation.

But perhaps I'm a bit confused about what a random effect really means here.

R Code

Here is a link to a pastebin of this code which may / may not be easier to copy into an editor.

df <- structure(list(x1 = structure(c(1L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                      5L, 1L, 1L, 3L, 1L, 1L, 3L, 6L, 1L, 1L, 1L, 3L, 1L, 1L, 4L, 3L, 
                                      3L, 4L, 1L, 1L, 5L, 5L, 4L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 3L, 1L, 
                                      1L, 1L, 4L, 1L, 1L, 3L, 1L, 3L, 1L, 1L, 1L, 6L, 3L, 3L, 1L, 1L, 
                                      1L, 1L, 3L, 1L, 1L, 1L, 8L, 3L, 3L, 4L, 3L, 1L, 1L, 3L, 1L, 3L, 
                                      1L, 1L, 1L, 3L, 1L, 4L, 3L, 4L, 3L, 4L, 1L, 3L, 1L, 1L, 4L, 1L, 
                                      1L, 3L, 2L, 1L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 3L, 
                                      1L, 1L, 3L, 1L, 3L, 3L, 1L, 3L, 1L, 1L, 3L, 1L, 3L, 3L, 4L, 1L, 
                                      3L, 6L, 3L, 3L, 1L, 1L, 5L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 
                                      3L, 3L, 3L, 4L, 3L, 1L, 1L, 1L, 6L, 4L, 3L, 1L, 3L, 3L, 3L, 1L, 
                                      1L, 1L, 3L, 1L, 3L, 1L, 1L, 5L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 1L, 
                                      1L, 6L, 3L, 1L, 1L, 4L, 3L, 4L, 4L, 3L, 3L, 1L, 1L, 6L, 1L, 1L, 
                                      3L, 8L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 5L, 5L, 1L, 5L, 1L, 1L, 1L, 
                                      3L, 1L, 3L, 1L, 1L, 1L, 1L, 3L, 3L, 1L, 1L, 3L, 6L, 3L, 1L, 1L, 
                                      3L, 1L, 4L, 1L, 1L, 1L, 1L, 6L, 1L, 1L, 3L, 3L, 3L, 3L, 1L, 1L, 
                                      3L, 1L, 3L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 4L, 4L, 1L, 3L, 1L, 
                                      3L, 3L, 4L, 1L, 3L, 5L, 3L, 1L, 3L, 3L, 5L, 1L, 3L, 1L, 1L, 3L, 
                                      5L, 4L, 1L, 1L, 1L, 1L, 6L, 3L, 3L, 6L, 1L, 6L, 1L, 1L, 1L, 1L, 
                                      1L, 5L, 1L, 1L, 1L, 5L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 7L, 1L, 4L, 
                                      5L, 3L, 3L, 1L, 3L, 1L, 1L, 3L, 1L, 1L, 4L, 1L, 1L, 4L, 1L, 3L, 
                                      6L, 3L, 3L, 2L, 1L, 3L, 1L, 2L, 6L, 3L, 4L, 3L, 4L, 1L, 6L, 3L, 
                                      3L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 
                                      3L, 3L, 3L, 1L, 6L, 3L, 3L, 1L, 1L, 3L, 4L, 1L, 1L, 1L, 1L, 3L, 
                                      3L, 3L, 1L, 3L, 1L, 1L, 1L, 6L, 3L, 1L, 1L, 3L, 1L, 1L, 6L, 3L, 
                                      3L, 3L, 1L, 3L, 1L, 4L, 6L, 1L, 3L, 1L, 3L, 1L, 3L, 3L, 1L, 1L, 
                                      1L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 6L, 1L, 3L, 1L, 3L, 3L, 4L, 
                                      3L, 3L, 1L, 1L, 3L, 1L, 3L, 3L, 1L, 6L, 6L, 4L, 4L, 1L, 3L, 2L, 
                                      5L, 3L, 2L, 2L, 2L, 3L, 4L, 3L, 6L, 3L, 3L, 3L, 4L, 1L, 3L, 1L, 
                                      6L, 4L, 1L, 3L, 3L, 3L, 1L, 5L, 2L, 6L, 3L, 1L, 1L, 3L, 3L, 5L, 
                                      3L, 1L, 2L, 1L, 6L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 3L, 6L, 4L, 3L, 
                                      3L, 1L, 1L, 1L, 6L, 1L, 3L, 6L, 1L, 1L, 6L, 5L, 1L, 1L, 1L, 1L, 
                                      3L, 3L, 4L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 4L, 1L, 4L, 1L, 3L, 3L, 
                                      1L, 6L, 4L, 4L, 1L, 3L, 4L, 3L, 1L, 3L, 5L, 3L, 1L, 3L, 5L, 3L, 
                                      3L, 3L, 3L, 1L, 3L, 1L, 1L, 1L, 3L, 3L, 4L, 1L, 3L, 3L, 3L, 3L, 
                                      3L, 3L, 4L, 1L, 3L, 3L, 3L, 1L, 1L, 4L, 1L, 1L, 6L, 1L, 2L, 6L, 
                                      NA, 6L, 7L, 1L, 3L, 6L, 1L, 3L, 3L, 7L, 4L, 1L, 4L, 1L, 1L, 2L, 
                                      3L, 4L, 3L, 1L, 4L, 1L, 3L, 1L, 4L, 1L, 1L, 3L, 1L, 3L, 3L, 1L, 
                                      1L, 1L, 5L, 1L, 1L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 
                                      3L, 5L, 3L, 1L, 1L, 5L, 3L, 3L, 3L, 1L, 3L, 1L, 1L, 1L, 3L, 1L, 
                                      3L, 3L, 5L, 3L, 3L, 1L, 3L, 3L, 3L, 1L, 1L, 1L, 5L, 1L, 1L, 1L, 
                                      4L, 1L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 1L, 1L, 3L, 1L, 3L, 
                                      3L, 3L, 1L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 4L, 3L, 3L, 3L, 1L, 1L, 
                                      1L, 1L, 3L, 1L, 1L, 1L, 6L, 1L, 1L, 3L, 3L, 4L, 4L, 1L, 1L, 1L, 
                                      5L, 3L, 3L, 3L, 4L, 1L, 2L, 3L, 2L, 6L, 2L, 3L, 5L, 1L, 3L, 3L, 
                                      3L, 3L, 1L, 4L, 3L, 1L, 4L, 1L, 3L, 1L, 5L), .Label = c("BL", 
                                                                                              "BM", "BR", "ML", "MM", "MR", "TL", "TR"), class = "factor"), 
                     x2 = structure(c(3L, 1L, 2L, 2L, 1L, 1L, 3L, 2L, 1L, 3L, 
                                      1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 3L, 3L, 1L, 2L, 2L, 2L, 
                                      2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 3L, 1L, 2L, 1L, 1L, 
                                      1L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 2L, 3L, 1L, 1L, 1L, 
                                      1L, 1L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 
                                      1L, 1L, 1L, 3L, 2L, 1L, 2L, 3L, 2L, 1L, 1L, 3L, 1L, 3L, 3L, 
                                      2L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 2L, 3L, 2L, 1L, 1L, 1L, 
                                      1L, 3L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 1L, 3L, 1L, 1L, 
                                      1L, 3L, 2L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 
                                      2L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 3L, 2L, 3L, 2L, 1L, 2L, 
                                      1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 3L, 1L, 3L, 
                                      1L, 3L, 1L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                      1L, 1L, 2L, 2L, 1L, 1L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 1L, 
                                      1L, 1L, 3L, 3L, 1L, 1L, 2L, 3L, 1L, 1L, 3L, 1L, 1L, 2L, 2L, 
                                      1L, 1L, 3L, 3L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 
                                      3L, 1L, 3L, 1L, 2L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 
                                      1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 3L, 1L, 2L, 2L, 1L, 2L, 
                                      1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 3L, 1L, 1L, 3L, 1L, 3L, 
                                      2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 3L, 2L, 2L, 1L, 1L, 
                                      1L, 2L, 1L, 1L, 3L, 3L, 2L, 2L, 1L, 1L, 3L, 3L, 1L, 2L, 3L, 
                                      3L, 3L, 1L, 3L, 3L, 3L, 1L, 3L, 1L, 2L, 3L, 3L, 2L, 1L, 2L, 
                                      3L, 1L, 1L, 3L, 3L, 3L, 1L, 1L, 3L, 1L, 1L, 1L, 2L, 3L, 3L, 
                                      2L, 2L, 1L, 2L, 3L, 3L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 
                                      1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 3L, 1L, 1L, 1L, 3L, 
                                      1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 3L, 1L, 1L, 1L, 2L, 2L, 1L, 
                                      3L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 3L, 1L, 1L, 3L, 
                                      2L, 1L, 1L, 2L, 2L, 1L, 3L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 1L, 
                                      1L, 1L, 2L, 1L, 3L, 2L, 2L, 1L, 3L, 1L, 2L, 2L, 2L, 2L, 1L, 
                                      1L, 1L, 3L, 2L, 3L, 2L, 3L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 3L, 
                                      2L, 3L, 3L, 2L, 1L, 3L, 1L, 1L, 1L, 3L, 1L, 2L, 1L, 3L, 2L, 
                                      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 1L, 3L, 3L, 1L, 
                                      3L, 2L, 1L, 1L, 3L, 3L, 2L, 1L, 3L, 2L, 2L, 1L, 2L, 3L, 1L, 
                                      1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 3L, 1L, 3L, 1L, 2L, 2L, 1L, 
                                      3L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 2L, 
                                      3L, 1L, 1L, 2L, 1L, 1L, 3L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 
                                      1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 3L, 2L, 2L, 1L, 2L, 1L, 
                                      2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 
                                      3L, 3L, 2L, 1L, 1L, 3L, 1L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 1L, 
                                      3L, 2L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 3L, 
                                      3L, 1L, 1L, 1L, 3L, 2L, 3L, 1L, 3L, 3L, 1L, 2L, 2L, 1L, 3L, 
                                      3L, 3L, 1L, 2L, 3L, 3L, 3L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 
                                      3L, 2L, 2L, 2L, 1L, 1L, 3L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 
                                      1L, 3L, 2L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 3L, 1L, 
                                      2L, 2L, 1L, 2L, 3L, 3L, 1L, 1L, 1L, 2L, 1L, 3L, 3L, 2L, 1L, 
                                      1L, 2L, 2L, 2L, 1L, 3L, 3L, 1L, 1L, 3L, 2L, 3L, 1L, 2L, 1L, 
                                      2L, 3L, 1L, 1L, 1L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
                                      1L, 1L, 2L, 1L, 1L, 1L, 3L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
                                      2L, 2L, 1L, 3L, 2L, 1L, 2L), .Label = c("MEDIUM", "OLD", 
                                                                              "YOUNG"), class = "factor"), y = c(1L, 0L, 1L, 1L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 
                                                                                                                 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 
                                                                                                                 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 
                                                                                                                 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 
                                                                                                                 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 
                                                                                                                 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 
                                                                                                                 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 
                                                                                                                 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 
                                                                                                                 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 
                                                                                                                 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 
                                                                                                                 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                 1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 
                                                                                                                 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 
                                                                                                                 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 
                                                                                                                 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 
                                                                                                                 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 
                                                                                                                 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 
                                                                                                                 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 
                                                                                                                 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 
                                                                                                                 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 
                                                                                                                 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 
                                                                                                                 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 
                                                                                                                 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 
                                                                                                                 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 
                                                                                                                 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 
                                                                                                                 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 
                                                                                                                 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 
                                                                                                                 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 
                                                                                                                 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                   -707L))


df <- df[complete.cases(df),]
set.seed(1)
t <- round(0.7 * nrow(df))
train <- sample(nrow(df), t, replace = FALSE)
trainset <- df[train,]
testset  <- df[-train,]
m <- glm(y ~ x1 + x2, data = trainset, family="binomial")
predict(m, testset, type="response")
$\endgroup$
1
$\begingroup$

In case of few observations of a certain type, the best thing is always to gather additional data. If this is not possible you can rather discard these observations, or you can merge them into larger groups. So in your case, you could merge 'TL' and 'TR' to 'T' and conduct your analysis with that.

df$x1 = as.character(df$x1) 
df$x1[df$x1=="TL"] = "T"  
df$x1[df$x1=="TR"] = "T"

As you still have only five entries for this new category, this is however also not a satisfactory fix.

For logistic regression the concept of EPV (events per variable) is important.

'Events per variable (EPV) and the relative performance of different strategies for estimating the out-of-sample validity of logistic regression models.' Austin & Steierberg (2017)

This article states that 'Differences between the bootstrap-corrected approach and the use of an independent validation sample were minimal once the number of events per variable was at least 20'. In their experiment they therefore conclude that one should at least have 20 events per variable. You should take a look at the article.

$\endgroup$
  • $\begingroup$ Thanks, gathering more data isn't an option. And merging the two isn't a fix as there's still a very small amount (5 after merging). I then considered merging further into just bottom / not-bottom (as outlined in the OP) but was unsure about the validity of this approach. Perhaps in this instance it's best to just discard those levels (and use only bottom,middle values instead) as though they contained NA values or something instead. I'm not sure $\endgroup$ – baxx Mar 3 at 23:13
  • $\begingroup$ I updated my answer but the problem is still the same. You have to few observations. Discarding might therefore really be an option. $\endgroup$ – peteR Mar 3 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.