# Is R's glm function useless in a big data / machine learning setting? [duplicate]

I am surprised that R’s glm will “break” (not converge with default setting) for the following “toy” example (binary classification with ~50k data, ~10 features), but glmnet returns results in seconds.

Am I using glm incorrectly (for example, should I set max iteration, etc.), or is R’s glm not good for big data setting? Does adding regularization make a problem easy to solve?

d=ggplot2::diamonds
d$price_c=d$price>2500
d=d[,!names(d) %in% c("price")]

lg_glm_fit=glm(price_c~.,data=d,family = binomial())

library(glmnet)
x=model.matrix(price_c~.,d)
y=d$price_c lg_glmnet_fit=glmnet(x = x,y=y,family="binomial", alpha=0) Warning messages: 1: glm.fit: algorithm did not converge 2: glm.fit: fitted probabilities numerically 0 or 1 occurred  EDIT: Thanks for Matthew Drury and Jake Westfall's answer. I understand the perfect separation issue which is which is already addressed. How to deal with perfect separation in logistic regression? And in my original code, I do have the third line which drops the column that derives the label. The reason I mention about "big data" is because in many "big data" / "machine learning" settings, people may not carefully test assumptions or know if data can be perfectly separated. But glm seems to be easily broken with "unfriendly" messages, and there is not a easy way to add the regularization to fix it. • Regarding " Does adding regularization make a problem easy to solve": yes, it can. See statweb.stanford.edu/~tibs/sta305files/Rudyregularization.pdf: "Inclusion of$\lambda$makes problem non-singular even if$Z^T Z$is not invertible" – Adrian Oct 11 '16 at 10:51 • ...and your perfect separation / non-invertibility issue doesn't necessarily have anything to do with the size of your training data (i.e. whether you're dealing with "big data" or not) – Adrian Oct 11 '16 at 10:53 • Personally, I think those messages are very friendly. If you have a 50,000 x 10 data set with perfect separation, and don't realize it, I don't think you've learned enough about your data. – Cliff AB Oct 11 '16 at 14:53 • glm doesn't fit regularized models because it's not designed to do so. Regularization will induce convexity and hence finite parameter estimates; however, it is not uniformly true that regularization is the correct answer in all contexts. Even if convex models are desired, they can be constructed from alternative, non-regularized models, so it's a matter of choosing the correct tool for the task at hand. Please see Scortchi's answer here: stats.stackexchange.com/questions/11109/… – Sycorax says Reinstate Monica Oct 11 '16 at 15:20 • @smci: unfortunately, that's not really a viable option. For a variety of reasons, it's not so easy to pinpoint the "responsible variable" in this problem, nor is the idea of a responsible variable even well defined. They can tell you that your estimated probabilities are numerically equal to 0 or 1, which should indicate that there are potential problems with your model. – Cliff AB Oct 11 '16 at 22:27 ## 2 Answers The unregularized model is suffering from complete separation because you are trying to predict the dichotomized variable price_c from the continuous variable price from which it is derived. The regularized model avoids the problem of complete separation by imposing a penalty that keeps the coefficient for the price predictor from going off to$\infty$or$-\infty$. So it manages to converge fine and work well. You should remove the continuous price predictor from the design matrix in this toy example. Edit: As @Erik points out, the continuous price predictor is already removed from the design matrix, which I somehow missed. So the complete separation arises from some other predictor or combination of predictors. It's also worth adding that, of course, none of these issues have anything to do with the particular implementation of logistic regression in R's glm() function. It is simply about regularized vs. unregularized logistic regression. This has nothing to do with glm, you simply created a problem with an artificial perfect separation: df <- data.frame(x = rnorm(100), y = rnorm(100)) df$y_c = df\$y > 0

glm(y_c~., data=df, family=binomial())


Warning messages: 1: glm.fit: algorithm did not converge 2: glm.fit: fitted probabilities numerically 0 or 1 occurred

y is a perfect predictor of y_c.

• While I think that perfect separation is still the basic issue, @hxd1011 excludes the price variable from d before fitting the model... see third line of code, though I am a bit unsure whether this works with operator precedence. – Erik Oct 11 '16 at 10:45
• @Erik even without that variable, it's possible to have perfect separation. Even lg_glm_fit=glm(price_c ~ carat, data=d, family = binomial()) starts to run into problems – Adrian Oct 11 '16 at 13:12
• @Adrian Sure, that is why I said that the basic issue is correctly identified. However, note that this is not artificial perfect separation anymore. – Erik Oct 11 '16 at 13:33
• @Adrian True, which is why I did not downvote. Still, my comment and yours right now would improve the answer if they got included. – Erik Oct 11 '16 at 14:11
• @MatthewDrury y is taken out of the data (and the model) in the third line of the code. – Erik Oct 11 '16 at 14:52