Logistic Regression
Context: FIT2086_MOC Β· the binary-target counterpart of Linear Regression (FIT2086) β same linear predictor , squashed into Β· it is the discriminative answer to the curse of dimensionality in Classification and Conditional Class Probabilities Β· fitted by ML on a Bernoulli target Β· pruned by penalised likelihood Β· scored by Classification Evaluation (Confusion Matrix and Metrics), ROC and AUC, Logarithmic Loss Β· run it β Logistic Regression in R (glm, pROC, step)
Quick Revision
- π― Objective: model the log-odds of as a linear function of the predictors β β invert with the logistic function to get a probability.
- π¦ Core Components: linear predictor β odds β probability .
- β‘ Key Constraint: the negative log-likelihood has no closed-form solution β the estimates must be found numerically; it is convex, so there are no spurious local minima.
π How It Works
1. Why the Linear Model Fails on a Binary Target
- The naive attempt β fit by least squares and read .
- The fatal flaw β is unbounded β for extreme feature values it returns or , which are not probabilities.
- The fix β keep the linear predictor, but pass it through a function that bounds it to . Many such functions exist; logistic regression picks one.
2. The Logistic Function
- Definition β .
- Limits β smoothly as and as ; it passes through at .
- Shape β an S-curve (sigmoid) β steepest near , flat in both tails βΉ the same unit change in a predictor moves the probability most for individuals near the decision boundary.
3. Odds and Log-Odds
- Odds β β how many times more likely is than .
- Worked reading β a coin with has odds for a head and for a tail β the two are reciprocals, not symmetric around a centre.
- Log-odds restore symmetry β and β the log scale maps onto , which is exactly the range a linear predictor lives in.
- Odds vs probability β odds ; odds equally likely; odds more likely.
4. The Model and Its Derivation
- The model β the conditional log-odds are the linear predictor:
- Recovering the logistic function β exponentiate, then solve for the probability:
- Why the two forms are the same β divide numerator and denominator by and use βΉ βlinear log-oddsβ and βlogistic functionβ are one assumption stated two ways.
5. Fitting by Maximum Likelihood
- Distributional assumption β targets are independent and with βΉ , .
- Likelihood β the Bernoulli pmf, multiplied over the sample:
- Negative log-likelihood β take , then substitute the logistic form to reach the compact version in :
- The estimates β are the minimisers of β the ML estimates, exactly as in Maximum Likelihood Estimation.
- No closed form β unlike least squares there is no formula βΉ solved numerically (gradient descent / Newton-type optimisers).
- Convexity is the safety net β is convex in the coefficients, so a numerical search cannot get stuck in a spurious local minimum, assuming a finite ML estimate exists.
- Cost β grows with both and , but is per iteration rather than exponential β this is the escape from the curse of dimensionality.
6. Interpreting the Coefficients
- Intercept β the log-odds of when every predictor is zero () β usually an extrapolation with no real-world individual behind it.
- Coefficient β the increase in log-odds per one-unit increase in , holding the others fixed βΉ a multiplicative factor on the odds, not an additive change in probability.
- Sign reading β more likely; more likely.
- Goodness-of-fit β the minimised β smaller is better; compare it against the intercept-only model , whose difference plays the role plays for . The intercept-only model assumes is the same for everybody.
- Deviance (Studio 7) β R reports : null deviance , residual deviance ; the gap measures improvement over the intercept-only model.
7. Predicting, Deciding and Pruning
- Predict β compute for new features, then .
- Decision rule β pick the class maximising that probability βΉ equivalently predict iff , iff the odds exceed , iff β three views of one rule β generalised to any threshold in ROC and AUC.
- Categorical and non-linear predictors β handled exactly as in linear regression β indicator columns for a -category predictor when an intercept is present, plus and polynomial terms β Predictor Transformations (Indicators, Polynomials, Interactions).
- Which predictors β test vs (smaller -value βΉ stronger predictor), or score models by penalised likelihood with (AIC), (KIC), (BIC), then search by forward/backward selection β Model Selection and Information Criteria (AIC, BIC).
- Interactions β add βΉ the change in log-odds per unit of now depends on (and vice versa); without it the change is the same whatever the other predictors are.
- The boundary is linear β the set where is exactly , a line in two features and a -dimensional hyperplane in general βΉ classes that are not linearly separable in the feature space cannot be separated by logistic regression without transformed predictors.
π¬ Model
- Declared spaces β , design matrix (plus an intercept column), , , .
- Link β , , independently.
- Objective (minimise) β
- Contrast with Linear Regression (FIT2086) β same ; the Gaussian mean model becomes a Bernoulli success probability, and the closed-form least-squares solution becomes a numerical convex optimisation β the ML framing of Least Squares as Maximum Likelihood is what makes the swap legal.
βοΈ Core Decision Matrix
| Model for a binary target | What is linear in | Fitted by | Output range | Where it breaks |
|---|---|---|---|---|
| Linear probability model | the probability itself | least squares, closed form | β invalid | returns or at extreme |
| Logistic regression | the log-odds | numerical ML, convex | β always valid | decision boundary forced linear; multi-class awkward |
When It Flips: over a narrow band of where all fitted probabilities sit near , the logistic curve is close to straight and the two models agree β the linear probability model only fails once the data reach into the tails, which real feature ranges almost always do.
π Exam Execution Trace & Applied Exercises
Applied Exercise 1 β from log-odds to odds to probability
Problem: a fitted model gives . At , report the odds and the probability that .
Final Extracted Output: is about three times as likely as ; probability βΉ predict class (odds ).
Applied Exercise 2 β fit interpretation and prediction
Problem: high blood pressure is defined as ; ML fitting on Age gives . Interpret both coefficients and predict for a -year-old.
Final Extracted Output: at the log-odds are (a meaningless extrapolation); each extra year adds to the log-odds, i.e. multiplies the odds by . A -year-old has probability βΉ predict . The model crosses at years.
Applied Exercise 3 β reading a pruned model (Studio 7)
Problem: BIC stepwise on binary SNP predictors () keeps (indicators when mutated). Give the disease probability with neither, only SNP12, and only SNP56 mutated.
Final Extracted Output: a SNP12 mutation lowers the log-odds by (odds ); a SNP56 mutation raises them by (odds ).
β οΈ Common Mistakes
- π‘ Reading as a change in probability β it is a change in log-odds; the same moves the probability a lot near and almost nothing in the tails, so no single βpercentage pointβ answer exists.
- π‘ Exponentiating the wrong thing β is the odds, not the probability; the probability needs the extra step or .
- π‘ Comparing likelihood numbers across packages β some report maximised log-likelihood, some the negative, some twice either β a βbigger is betterβ habit inverts the verdict.
- π‘ Forgetting the boundary is linear β a poor fit on visibly curved class structure is a model-form failure, not an optimisation failure β transform the predictors (, , ) or switch to a tree / forest, which is non-linear natively Β· (Studio 12 exam phrasing) off a scatter: βa straight line would not separate the classes, a simple curve wouldβ βΉ logistic regression is inappropriate.
π§ Active Recall
Why model the log-odds rather than the probability directly?
- Hint: Compare the range of each side of the equation.
Answer
- Short answer: The linear predictor ranges over all of , and so do the log-odds β but a probability only ranges over , so equating to a probability produces impossible values.
- Why: Range matching β probability β the log-odds is the natural scale for an unbounded linear function, and the logistic function is just this chain run backwards.
Least squares has a formula; logistic regression does not. Why is that not a practical disaster?
- Hint: What shape is the objective surface?
Answer
- Short answer: The negative log-likelihood is convex, so any descent method reaches the global minimum β there are no spurious local minima to trap it.
- Why: Convex objective β has a single basin βΉ the estimate is well defined even without a formula, provided a finite ML estimate exists (it fails on perfectly separable data, where coefficients run to infinity).
A model gives for an individual. State the odds, the probability, and the predicted class.
Answer
- Short answer: Odds , probability , and the classes are exactly tied β the individual sits on the decision boundary.
- Why: The three equivalent rules β ; at all three are at their pivot, which is precisely the hyperplane .
How is "which predictors belong in the model" answered here, given there is no ?
Answer
- Short answer: Identically to linear regression, because logistic regression is fitted by ML and therefore has a likelihood β test , or score models by and search by stepwise selection.
- Why: Penalised likelihood transfers β the minimised NLL always falls as predictors are added (exactly as always rises), so a complexity charge for AIC/KIC/BIC is what makes two models comparable β Model Selection and Information Criteria (AIC, BIC).