Classical machine learningML 03
Logistic Regression
Many questions have a yes/no answer: does this patient have the disease, will this loan be repaid? Logistic regression predicts the probability of “yes”. It works out a score from the predictors, as linear regression would, and passes it through the S-shaped logistic curve, which squeezes any number into the range 0 to 1. It is the workhorse behind clinical risk scores and much of epidemiology, because each coefficient translates into an odds ratio. Move the sliders to fit the curve by hand, then compare with the best fit. The data are fictional.
1 · One predictor: the S-curve
orange: your curve · dashed: the best (maximum-likelihood) fit · yellow bars: the share of people with the outcome in each fifth of the range, which a good curve passes through
2 · Two predictors: a straight boundary
shading: the predicted probability, stronger where the model is surer · solid line: P = 50%, the decision boundary · dashed lines: P = 10% and 90%
Probabilities, odds and the S-curve
A probability must stay between 0 and 1, so a straight line cannot predict one. Logistic regression instead models the log-odds, log(p / (1 − p)), as a straight line in the predictors; turning the log-odds back into a probability gives the S-curve. The slope β is the change in log-odds per unit of the predictor, so eβ is the odds ratio: in the heart disease example, each 10 years of age multiplies the odds of disease by e10β. With several predictors each coefficient is adjusted for the others, which is why logistic regression is the standard way to report risk factors. An odds ratio is not a risk ratio, though: when the outcome is rare the two are close, but when it is common (say, above 10%) the odds ratio is further from 1 than the risk ratio, and reading one as the other overstates the effect.
Fitting by maximum likelihood
There is no squared error to minimise here. Instead the fit chooses the coefficients that make the observed outcomes most probable (maximum likelihood), which is the same as minimising the log-loss: the average of −log(p) for the people who had the outcome and −log(1 − p) for those who did not. Confident wrong predictions are punished hard. Unlike least squares there is no direct formula; the demo uses Newton’s method, the standard approach, which converges in a handful of steps. Gradient descent would reach the same answer more slowly.
Boundaries and their limits
With two predictors, the 50% line is where the log-odds are zero, which is a straight line: logistic regression is a linear classifier. The electrolytes example has a round normal range, which a straight line cannot capture; adding the squared terms lets the boundary curve into an ellipse, the same trick as adding x² inputs in the neural network demo. When the classes do not overlap at all (try “no overlap”), the likelihood keeps rising as the curve steepens, the coefficients run off to infinity, and the software must stop somewhere; a penalty such as ridge, or Firth’s correction, fixes this.
References
- Berkson, J. (1944). Application of the logistic function to bio-assay. Journal of the American Statistical Association, 39(227), 357–365. doi.org/10.1080/01621459.1944.10500699
- Cox, D. R. (1958). The regression analysis of binary sequences. Journal of the Royal Statistical Society, Series B, 20(2), 215–232 (with discussion, 232–242). doi.org/10.1111/j.2517-6161.1958.tb00292.x
- Firth, D. (1993). Bias reduction of maximum likelihood estimates. Biometrika, 80(1), 27–38. doi.org/10.1093/biomet/80.1.27
- Hosmer, D. W., Lemeshow, S., & Sturdivant, R. X. (2013). Applied Logistic Regression (3rd ed.). Wiley. doi.org/10.1002/9781118548387