Unit 9, lesson 69

Logistic Regression

How logistic regression models the log odds of a binary outcome, how the logit and logistic function move between scales, what an odds ratio for a predictor says, and what complete separation and calibration mean.

  • Video 69
  • Logistic Regression and Validation

Whether a task is completed is a yes or no outcome, but the probability of completing it can change smoothly with the difficulty of the task and the experience of the person attempting it. A straight line for that probability can give fitted values below 00 or above 11. Logistic regression fits a linear model to the log odds instead. This lesson shows how to move between log odds, odds and probability, what an exponentiated coefficient does and does not say, when the ordinary fit fails to exist, and how to check predicted probabilities against observed outcomes.

Key terms

Logit

Definition For a probability pp with 0<p<10 < p < 1, the odds are p/(1−p)p/(1 - p) and the logit, or log odds, is

logit⁡(p)=log⁡p1−p,\operatorname{logit}(p) = \log \frac{p}{1 - p} ,

where log⁡\log is the natural logarithm. The logit is strictly increasing, takes every real value exactly once as pp runs over 0<p<10 < p < 1, equals 00 at p=1/2p = 1/2 and satisfies logit⁡(1−p)=−logit⁡(p)\operatorname{logit}(1 - p) = -\operatorname{logit}(p). It is not defined at p=0p = 0 or p=1p = 1.

In plain words Odds compare the chance that the event happens with the chance that it does not. Odds of 11 mean the two are equally likely, and odds of 1/31/3 mean the event is a third as likely as its absence. The logarithm then stretches the probability scale over the whole number line, sending probabilities near 00 far to the left and probabilities near 11 far to the right.

Example Three probabilities and their odds and logits are below.

pp0.250.250.50.50.80.8
odds1/31/31144
logit−log⁡3≈−1.0986-\log 3 \approx -1.098600log⁡4≈1.3863\log 4 \approx 1.3863

Logistic function

Definition The logistic function, also called the inverse logit, is

logit⁡−1(η)=eη1+eη=11+e−η\begin{aligned} \operatorname{logit}^{-1}(\eta) &= \frac{e^{\eta}}{1 + e^{\eta}} \\[0.4em] &= \frac{1}{1 + e^{-\eta}} \end{aligned}

for every real η\eta. Its values lie strictly between 00 and 11. It is strictly increasing, equals 1/21/2 at η=0\eta = 0 and satisfies logit⁡−1(−η)=1−logit⁡−1(η)\operatorname{logit}^{-1}(-\eta) = 1 - \operatorname{logit}^{-1}(\eta). It undoes the logit, since logit⁡(logit⁡−1(η))=η\operatorname{logit}(\operatorname{logit}^{-1}(\eta)) = \eta for every real η\eta and logit⁡−1(logit⁡(p))=p\operatorname{logit}^{-1}(\operatorname{logit}(p)) = p for 0<p<10 < p < 1. The formula comes from solving p/(1−p)=eηp/(1 - p) = e^{\eta} for pp.

In plain words The logistic function turns any real number into a probability along an S-shaped curve. It is steepest at η=0\eta = 0, where the probability is 1/21/2, and it flattens toward 00 on the left and toward 11 on the right without reaching either. Equal steps in η\eta therefore move the probability more near the middle than near the ends.

Example The values logit⁡−1(−2)=1/(1+e2)≈0.1192\operatorname{logit}^{-1}(-2) = 1/(1 + e^2) \approx 0.1192, logit⁡−1(0)=0.5\operatorname{logit}^{-1}(0) = 0.5 and logit⁡−1(1)=e/(1+e)≈0.7311\operatorname{logit}^{-1}(1) = e/(1 + e) \approx 0.7311 lie on the curve. By the symmetry, logit⁡−1(2)≈1−0.1192=0.8808\operatorname{logit}^{-1}(2) \approx 1 - 0.1192 = 0.8808.

Logistic regression

Definition Logistic regression models a binary response YY, coded 11 for the event and 00 otherwise, given predictors x1,…,xkx_1, \dots, x_k. With p(x)=P(Y=1∣x)p(x) = P(Y = 1 \mid x), the model states

logit⁡p(x)=β0+β1x1+⋯+βkxk,\begin{aligned} \operatorname{logit} p(x) &= \beta_0 + \beta_1 x_1 \\[0.4em] &\quad + \cdots + \beta_k x_k , \end{aligned}

so p(x)=logit⁡−1(η(x))p(x) = \operatorname{logit}^{-1}(\eta(x)), where η(x)\eta(x) is the linear predictor on the right. The responses are assumed independent given their predictors. Because YY is binary, Var⁡(Y∣x)=p(x)(1−p(x))\operatorname{Var}(Y \mid x) = p(x)\bigl(1 - p(x)\bigr), and no normal errors or constant variance are assumed. The coefficients are usually estimated by maximum likelihood, maximizing

L(β)=∏i=1npiyi(1−pi)1−yiL(\beta) = \prod_{i=1}^{n} p_i^{y_i} (1 - p_i)^{1 - y_i}

over β\beta, where pi=p(xi)p_i = p(x_i). The predicted probability for a profile xx is p^(x)=logit⁡−1(b0+b1x1+⋯+bkxk)\hat{p}(x) = \operatorname{logit}^{-1}(b_0 + b_1 x_1 + \cdots + b_k x_k), with the estimates bjb_j in place of the βj\beta_j. It estimates P(Y=1∣x)P(Y = 1 \mid x) under the model, and it is not a prediction that the event will happen.

In plain words The model puts a linear function of the predictors on the log-odds scale, and the logistic function bends it into an S-shaped curve for the probability that stays between 00 and 11. Each observed 11 contributes its modeled probability to the likelihood and each observed 00 contributes one minus that probability, so maximum likelihood looks for the coefficients under which the observed pattern of outcomes is most probable.

Example For teaching, declare the model logit⁡p(x)=−2+0.5x\operatorname{logit} p(x) = -2 + 0.5x, with coefficients chosen rather than estimated. At x=4x = 4 the log odds are 00, the odds 11 and the probability 0.50.5. At x=0x = 0 the probability is 1/(1+e2)≈0.11921/(1 + e^2) \approx 0.1192, and at x=6x = 6 it is e/(1+e)≈0.7311e/(1 + e) \approx 0.7311. Four records with x=0,2,4,6x = 0, 2, 4, 6 and outcomes 0,0,1,10, 0, 1, 1 have likelihood contributions of about 0.88080.8808, 0.73110.7311, 0.50.5 and 0.73110.7311 under this model, with product about 0.23540.2354. Giving every record probability 0.50.5 yields likelihood 0.54=0.06250.5^4 = 0.0625, so the declared coefficients fit these outcomes better. Comparing two candidates does not find the best one, and for these four records no finite best one exists, as the section on complete separation shows.

Odds ratio for a predictor

Definition Suppose a predictor xjx_j enters a logistic regression only through the term βjxj\beta_j x_j. Raising xjx_j by one unit while every other predictor stays fixed adds βj\beta_j to the log odds and multiplies the odds by

odds(xj+1)odds(xj)=eβj,\frac{\text{odds}(x_j + 1)}{\text{odds}(x_j)} = e^{\beta_j} ,

whatever the starting values. This number eβje^{\beta_j} is the odds ratio for a one-unit increase in xjx_j, adjusted for the other predictors in the model, and an increase of cc units multiplies the odds by ecβje^{c\beta_j}. If xjx_j also appears in an interaction or a squared term, the odds ratio for a one-unit increase depends on the value of the interacting predictor or on the starting value of xjx_j.

In plain words A coefficient is a fixed change on the log-odds scale, and its exponential is a fixed multiplier on the odds scale. It is not a fixed change in probability and not a fixed risk ratio. In percentage points the same odds ratio moves a probability very close to 00 or 11 only slightly, and its largest move is the one centered on 0.50.5. When both probabilities are small the odds ratio and the risk ratio are close, and otherwise they can differ a lot. An adjusted odds ratio describes an association within the model, and reading it as a causal effect needs a design that supports it, such as random assignment.

Example In the declared model e0.5≈1.6487e^{0.5} \approx 1.6487, so each unit of xx multiplies the odds by about 1.651.65. From x=0x = 0 to x=1x = 1 the probability rises from about 0.11920.1192 to 0.18240.1824, by about 6.36.3 percentage points, a risk ratio of about 1.531.53. From x=4x = 4 to x=5x = 5 it rises from 0.50.5 to about 0.62250.6225, by about 12.212.2 percentage points, a risk ratio of about 1.241.24. The odds ratio is the same for both steps, while the probability changes and the risk ratios differ.

Complete separation

Definition The outcomes of a data set are completely separated when some linear predictor b0+b1x1+⋯+bkxkb_0 + b_1 x_1 + \cdots + b_k x_k is positive for every record with y=1y = 1 and negative for every record with y=0y = 0. Multiplying those coefficients by a constant cc and letting cc grow sends every likelihood contribution toward 11. Every finite choice of coefficients gives each record a probability strictly between 00 and 11, so the likelihood approaches 11 without reaching it, and the ordinary maximum likelihood estimate does not exist as a finite vector. A weaker pattern, quasi-complete separation, also rules out a finite estimate. In that pattern some linear predictor is positive or 00 for every record with y=1y = 1, negative or 00 for every record with y=0y = 0, and 00 for some but not all records.

In plain words If a threshold on some weighted sum of the predictors sorts every success from every failure, steeper and steeper S-curves fit better and better without end. Software may stop at a very large coefficient with an enormous standard error, issue a warning, or report fitted probabilities that are numerically 00 and 11. Those numbers show where the algorithm stopped, not a precise estimate. Separation is common with small samples or rare outcomes, and it does not show that the true probabilities are 00 and 11. Penalized or Bayesian methods can give finite estimates, but they change the estimation procedure and should be reported as such.

Example The four records x=0,2,4,6x = 0, 2, 4, 6 with outcomes 0,0,1,10, 0, 1, 1 are separated at x=3x = 3. Take logit⁡p(x)=c(x−3)\operatorname{logit} p(x) = c(x - 3) with c>0c > 0. The likelihood is about 0.4850.485 at c=1c = 1, 0.7720.772 at c=2c = 2, 0.9640.964 at c=4c = 4 and 0.99930.9993 at c=8c = 8. It keeps rising toward 11, so no finite pair of coefficients maximizes it.

Calibration

Definition A model’s predicted probabilities are calibrated in a population when, among the cases that receive predicted probability qq, the proportion of events equals qq, for every value qq that the model produces. In symbols, P(Y=1∣p^=q)=qP(Y = 1 \mid \hat{p} = q) = q. Calibration is checked on observations not used to choose or fit the model, usually by grouping cases with similar predicted probabilities and comparing each group’s observed event rate with its average predicted probability.

In plain words Calibration asks whether the probabilities can be taken at face value, so that events given probability 0.80.8 happen about 8080 percent of the time. It is different from discrimination, the ability to rank cases with events above cases without them. A model can order cases exactly by their true risk and still overstate every probability, and a model that gives every case the overall event rate is calibrated but cannot rank cases at all. Small groups give noisy event rates, so a gap in one group is a reason to look further rather than proof of miscalibration.

Example Twenty held-out cases each receive predicted probability 0.80.8, and 1313 events occur. The predicted count is 20×0.8=1620 \times 0.8 = 16 and the observed rate is 13/20=0.6513/20 = 0.65. If the predictions were correct and the outcomes independent, the count of events would be binomial with n=20n = 20 and p=0.8p = 0.8, and 1313 or fewer events would occur with probability about 0.0870.087. The shortfall deserves a check with more data, but this one group does not establish miscalibration. In a different case, two groups with true event rates 0.40.4 and 0.70.7 that receive predictions 0.60.6 and 0.90.9 are ranked correctly, yet both are overstated by 0.20.2.

Check yourself

  1. A declared model for the probability pp that a task is completed is logit⁡p=2−d+0.5x\operatorname{logit} p = 2 - d + 0.5x, where dd is the difficulty level of the task and xx is the years of experience of the person attempting it. Find the log odds, the odds and the probability of completion for a task of difficulty 44 attempted by someone with 22 years of experience.
Show the answer

The log odds are 2−4+0.5⋅2=−12 - 4 + 0.5 \cdot 2 = -1. The odds are e−1≈0.3679e^{-1} \approx 0.3679, and the probability is

e−11+e−1=11+e≈0.2689.\frac{e^{-1}}{1 + e^{-1}} = \frac{1}{1 + e} \approx 0.2689 .

Under the model, attempts with this profile succeed with probability about 0.270.27, which is about 3737 completions for every 100100 failures in the long run.

  1. In the same model and for the same task, a colleague says that each extra year of experience multiplies the probability of completion by e0.5≈1.65e^{0.5} \approx 1.65, so with 33 years it is 0.2689×1.6487≈0.4430.2689 \times 1.6487 \approx 0.443. Find the correct probability and explain the mistake.
Show the answer

The factor e0.5e^{0.5} multiplies the odds, with difficulty held fixed, not the probability. With 33 years the log odds are −0.5-0.5 and the odds are 0.3679×1.6487≈0.60650.3679 \times 1.6487 \approx 0.6065, so the probability is

0.60651.6065≈0.3775.\frac{0.6065}{1.6065} \approx 0.3775 .

The probability rises by about 0.1090.109, a risk ratio of about 1.401.40, not 1.651.65. The colleague treated the odds ratio as a risk ratio. The coefficient also describes an association within the model. It shows what gaining a year of experience would do only if the design supports that causal reading.

  1. Six tasks have difficulties 11 to 66. The three easiest were completed and the three hardest were not. Fitting logit⁡p=β0+β1d\operatorname{logit} p = \beta_0 + \beta_1 d, the software reports a very large negative slope with an enormous standard error and a convergence warning. What happened, and how should the result be reported?
Show the answer

The outcomes are completely separated. The linear predictor 3.5−d3.5 - d is positive for the three completed tasks and negative for the three others, so for logit⁡p=c(3.5−d)\operatorname{logit} p = c(3.5 - d) the likelihood approaches 11 as cc grows without ever reaching it. No finite maximum likelihood estimate exists, and the reported slope and standard error only show where the algorithm stopped. They are neither a precise estimate nor evidence of an extremely strong association, and a p-value computed from them means little. The report should state the separation and either gather more data or use a penalized or Bayesian method with its penalty or prior stated. Six tasks also cannot show that tasks harder than 3.53.5 are never completed.

How this lesson connects