# Simulating data for logistic regression with unbalanced classes

I want to simulate categorical data for logistic regression, and those data need to have unbalanced class representation. Simulating for logistic regression with (nearly) balanced classes is simple enough in R:

n <- 500
x <- rnorm(n)
lp <- x * log(1.5) # log of the odds ratio - coefficient of the linear predictor
pr <- 1 / (1 + exp(-lp))
y <- rbinom(n,1,pr)


If I run this 10,000 times and look at the mean of the exponentiated coefficient, it always comes out nearly 1.5.

But what if I want the same odds ratio and have a sampling that provides 25% cases and 75% controls? The only approach I've come up with is to simulate too many examples, and then down-sample the appropriate amounts from each class:

  n <- 500
perc.case <- 0.25
x <- rnorm(n*1/perc.case) # simulate more than we need by a factor consistent with the imbalance
lp <- x * log(1.5)
pr <- 1 / (1 + exp(-lp))
y  <- rbinom(n*1/perc.case,1,pr) # again, simulate more than necessary

n.case = n*perc.case
n.control = n*(1-perc.case)
y.case <- sample(which(y==1), n.case, replace = FALSE)
y.control <- sample(which(y==0), n.control, replace = FALSE)

x <- x[c(y.case,y.control)]
y <- y[c(y.case,y.control)]


My interpretation is that I'm simulating a population and then sampling from that population. I tend to get a similar degree of error in the coefficient estimate as with the balanced case, but I want to make sure I'm not misinterpreting something or introducing an additional bias.

• Do you care about the distribution of values in your case, or do you just want any reasonable-looking imbalanced dataset? – Itamar Mushkin Jul 30 at 7:12
• Good point. At this stage, I'm not worried about the distribution within a class as long as I can maintain the odds ratio between classes. That being said, if you have a method to target a specific within class distribution, I'd love to hear it. – KirkD_CO Jul 30 at 12:03
• If you don't care about the distribution at all, you can just modify pr before plugging it into the y computation... – Itamar Mushkin Jul 30 at 14:22
• If you do care about the distribution, I'd just take $x$ from a Gaussian mixture model (or even more simply - I'd plug in a probability $p_1$ to subtract or add some quantity $q_1$ from $x$. – Itamar Mushkin Jul 30 at 14:28
• If I modify pr, won't that affect the odds ratio? I need to guarantee that the odds ratio is maintained. – KirkD_CO Jul 30 at 15:54