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) βž”

βš–οΈ Core Decision Matrix

Model for a binary targetWhat is linear in Fitted byOutput rangeWhere it breaks
Linear probability modelthe probability itselfleast squares, closed form βž” invalidreturns or at extreme
Logistic regressionthe log-oddsnumerical ML, convex βž” always validdecision 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