# Logistic regression with high cardinality categorical variable

I have a logistic regression model where I care about predictive power solely over comprehensibility. I'm interested in predicting win rates in a video game.

There are 133 characters. Each team picks 5 of them (no repeats). Each of these characters is assigned to one of five positions (again no repeats).

Currently I have each of these characters as a dummy variable. In addition I have an interaction variable between each of these variables. The position of a character is not included in the model at present.

I know I can trim down the size of the model by excluding low-playrate characters, however my concern is that the required sample size is still far too small for the complexity of the model. Any advice would be appreciated.

• Sample Size: Aprox. Two million

So I believe you're building a model on the binary outcome {lose, win}:= {0, 1}, correct?

I'd recommend just using a one-hot-encoding or a sparse matrix to store these inputs, then the model should run just fine. This is very straightforward in R (sparse.model.matrix) or Python (pd.get_dummies(sparse=True)).

Here's a quick demo of how to build a sparse matrix in R out of sampled categories and select a subset of them with at least 5 observations.

library(MASS)
require(glmnet)
n <- 1000
x1 <- sample(paste(letters,1), n, replace=T)
x2 <- sample(paste(letters,2), n, replace=T)
x3 <- paste(x1,x2,sep='-')
xdf <- data.frame(x1,x2,x3)
xs <- sparse.model.matrix(~.-1, data=xdf)
vars <- colnames(xs)
colsmry <- colSums(xs)
colsubset <- colsmry > 4
xs_ss <- xs[,vars[colsubset]]
dim(xs)
dim(xs_ss)

• Yes that's right. I want to calculate win percentages for different combinations of characters which is why I selected logistic regression. Does using one-hot encoding or a sparse matrix introduce any other considerations I have to account for? Also I know it's beyond the scope of my initial question but why do these fix the problems that exist in the dummy variable approach. I was told I needed 10 training examples for every possible combination that could exist for any statistical power. Nov 8 '16 at 15:07
• It's actually still using binary variables, but just efficient storage of them. The binary representation is the correct way to model this but when you have many categories and start doing interactions of those categories, you end up with an extremely sparse matrix. As far as your last question, I'd recommend just building all of the categories and then just choose some threshold defining whether or not to include them. I've updated my response with a quick example. Nov 8 '16 at 16:32

Look at techniques for dimensionality reduction, such as PCA. You can find several different methods in the scikit-learn documentation.This will shrink the feature space based on tranformations from the original input features.