# How to do classification in mixed effect models in python. My data is nested into groups with binary outcome

Lets say I have 10 sellers (S1-S10). Each seller has 7 buyers which are different for each seller (B1-B7 for S1, B11-B17 for S2 and so on). Each Seller buyer combination has a product category (P1, P2...) which can be repetitive for both inter and intra sellers. Now X variables are divided into 3 categories - Seller attributers, buyer attributes and interaction variables. Y is a binary variable saying transaction happened or not.

Seller, Buyer, Prd Cat, ... X variables, transaction
S1, B1, P1 ..........................., 1
S1, B2, P2 ..........................., 0
S1, B3, P3 ..........................., 1
S1, B4, P1 ..........................., 1
S1, B5, P2 ..........................., 0
S1, B6, P4 ..........................., 0
S1, B7, P4 ..........................., 1
S2, B11, P5 .........................., 0
S2, B12, P2 ..........................., 0
S2, B13, P1 ..........................., 1
S2, B14, P6 ..........................., 1
S2, B15, P7 ..........................., 0
S2, B16, P3 ..........................., 0
S2, B17, P3 ..........................., 1


and so on...

I have to find the probability of transaction for every seller buyer combo. I have tried mixed effect random forest and XGBoost with introducing dummy for each seller. Also to reduce the variance, I tried clustering to group similar sellers and seller product combos and then made dummy for those. My accuracy for the model did increase after trying multiple methods. Most of the models in mixed effect have a continuous variable.

Is there a way to model this kind of data through GLMM in python?
Or Is there a better way to attack such structure of data in general?

Statsmodels package has a GLMM functionality: BinomialBayesMixedGLM and PoissonBayesMixedGLM. It is not extremely well documented and takes time to get working - here is a StackOverflow thread with some tips and code: Mixed effects logistic regression.
GPBoost is another option - available both in R and in python. The GitHub examples code contains everything necessary to start using it.
• Thanks for this answer. Note that statsmodels often does not work correctly and produces wrong estimates for coefficients and covariance parameters; see this comparison medium.com/towards-data-science/… Commented May 7 at 9:47