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.