Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

On the evening of January 27, 1986, engineers at Morton Thiokol and NASA argued on a conference call about whether to launch the space shuttle Challenger the next morning. The forecast for Cape Canaveral was cold, near freezing, colder than any previous launch. The concern was the rubber O-rings that sealed the joints of the solid rocket boosters. In the cold the rubber stiffens, and a stiff O-ring might not seal in time to hold back the burning gas. The engineers had data from 23 earlier flights: for each one, the temperature at launch and the number of O-rings, out of six, that showed heat damage afterward.

Figure 1 shows that data. Damage happened on both warm and cool flights, but the coldest launches carried the worst incidents, and the launch under discussion would be far colder than anything on the record. That is the hard part. Every flight that had ever flown did so at 53 degrees Fahrenheit or warmer. The predicted launch temperature was about 31 degrees, off the left edge of all experience. To say anything about the risk at 31 degrees, you have to extend a pattern past the last data point, which is exactly the move a statistician is trained to distrust.

A scatterplot of the fraction of six O-rings damaged on the vertical axis against launch temperature in degrees Fahrenheit on the horizontal axis, for 23 shuttle flights. Points sit between 53 and 81 degrees; the coolest flights near 53 degrees show the highest damage fractions, and warm flights above 70 degrees mostly show no damage. A red dashed line marks the 31 degree launch temperature far to the left of every observation, inside a shaded band that is colder than any recorded flight.

Figure 1:The 23 flights before Challenger. Damage was worst at the coolest launches, and the 31 degree launch decision (dashed line) sat far below every temperature the program had ever flown, so judging its risk means extrapolating.

The response here is not a number like work hours or savings rate. It is closer to a yes-or-no: did an O-ring fail or not. Straight-line regression, the tool of the last eleven chapters, is built for a continuous response and breaks in specific ways when the outcome is binary. This chapter builds the right tool. You will learn the logistic regression model, the odds and log-odds that make it linear, how to fit it by maximum likelihood when no formula gives the estimates, how to read its coefficients as odds ratios without the usual mistakes, how to test them, and how to judge how well the model separates the two outcomes. By the end you will be able to put a number on the risk the Challenger engineers were arguing about, and to say honestly how much to trust it.

13.1 Why a straight line fails, and what to use instead

Intuition

Start with what goes wrong. Suppose you code the response as Y=1Y = 1 for a positive outcome (an O-ring damaged, a diabetes test positive) and Y=0Y = 0 otherwise, and you fit the ordinary line Y=β0+β1X+εY = \beta_0 + \beta_1 X + \varepsilon. Three things break.

First, the fitted line is unbounded. A line keeps rising as XX grows, so it eventually predicts probabilities above 1 and, going the other way, below 0. Figure 2 shows the least-squares line through the diabetes data sliding straight out of the [0,1][0, 1] range where a probability has to live.

Second, the spread is not constant. A 0/10/1 response has variance π(1π)\pi(1 - \pi), where π\pi is the probability that Y=1Y = 1. That variance is largest near π=0.5\pi = 0.5 and shrinks toward zero as π\pi approaches 0 or 1, the upside-down parabola in Figure 3. A coin near 50-50 is the hardest to call, so its outcome is the most variable; a coin that almost always lands heads is nearly a sure thing, so it barely varies. The spread is fixed by the mean. So the constant-variance assumption behind least squares (2.1 The simple linear regression model) is false by construction, and the weighting that would fix it changes with the mean, an idea we met in weighted least squares (10.4 Weighted least squares).

Third, the errors cannot be normal. If YY is only ever 0 or 1, then for a given XX the “error” takes just two values, so the normal error model that justified our tt and FF inference simply does not apply.

A scatterplot of diabetes test result, coded 0 or 1 and jittered vertically, against plasma glucose. A red dashed straight line rises through the cloud and passes below 0 at low glucose and above 1 at high glucose. A blue S-shaped logistic curve rises from near 0 to near 1 but stays inside the 0-to-1 band the whole way. Two thin gray horizontal lines mark 0 and 1.

Figure 2:The least-squares line (red dashed) leaves the range a probability must stay in, dropping below 0 and climbing above 1. The logistic curve (blue) bends to fit inside the band.

A curve of the variance of a zero-one response on the vertical axis against the probability pi of a positive outcome on the horizontal axis. The curve is an upside-down parabola equal to pi times one minus pi, rising from zero at pi equals 0 to a peak of 0.25 at pi equals 0.5 and falling back to zero at pi equals 1. The peak is marked with a red dot and labeled largest spread, and both ends are labeled almost no spread.

Figure 3:The variance of a yes-or-no outcome is not a free constant: it equals π(1π)\pi(1-\pi), peaking at π=0.5\pi = 0.5 and vanishing at both ends. Because the spread is welded to the mean, the constant-variance assumption of least squares cannot hold for a binary response.

The fix is to model the probability π\pi directly and to bend the line so it can never leave [0,1][0, 1]. The bend is the S-shaped logistic curve in the figure.

From probability to odds to log-odds

The bridge is the odds (Definition 13.1).

A probability of 0.5 is odds of 1 (even money). A probability of 0.75 is odds of 3 (three to one). As π\pi climbs toward 1 the odds shoots off to infinity, and as π\pi falls toward 0 the odds falls toward 0 but never goes negative. Figure 4 draws this mapping. Odds stretch the bounded [0,1][0, 1] probability scale onto the half-line [0,)[0, \infty).

A curve showing odds on the vertical axis as a function of probability on the horizontal axis. The curve starts near the origin, passes through the point where probability is one half and odds is one, and then rises ever more steeply, heading toward infinity as probability approaches one. Dotted guide lines mark probabilities of 0.1, 0.5, 0.75, and 0.9 with their odds of about 0.11, 1, 3, and 9.

Figure 4:Odds as a function of probability. Equal steps in probability are not equal steps in odds: from 0.5 to 0.75 the odds triples, but from 0.75 to 0.9 it triples again, so the odds scale expands as you approach certainty.

Taking one more step, the log-odds or logit (Definition 13.2) is the natural logarithm of the odds:

logit(π)=log ⁣(π1π).\operatorname{logit}(\pi) = \log\!\left(\frac{\pi}{1 - \pi}\right) .

In words: the logit is the log of the odds. The logarithm sends [0,)[0, \infty) onto the whole real line (,)(-\infty, \infty), the same range a linear predictor β0+β1X\beta_0 + \beta_1 X can take. That is the whole trick. We cannot set a probability equal to a line, because one is bounded and the other is not, but we can set the logit of the probability equal to a line.

The logistic regression model

The logistic regression model (Definition 13.3) for a binary response says the log-odds is linear in the predictors:

logit(πi)=log ⁣(πi1πi)=β0+β1Xi1++βp1Xi,p1,\operatorname{logit}(\pi_i) = \log\!\left(\frac{\pi_i}{1 - \pi_i}\right) = \beta_0 + \beta_1 X_{i1} + \dots + \beta_{p-1} X_{i,p-1} ,

where πi=P(Yi=1Xi)\pi_i = P(Y_i = 1 \mid X_i) is the probability that case ii has a positive outcome, the β\beta’s are the regression parameters (with pp of them counting the intercept), and Xi1,,Xi,p1X_{i1}, \dots, X_{i,p-1} are the predictors for case ii. Writing ηi=β0+β1Xi1+\eta_i = \beta_0 + \beta_1 X_{i1} + \dots for the linear predictor, we solve the logit equation for πi\pi_i to get the model on the probability scale:

πi=11+eηi=eηi1+eηi.\pi_i = \frac{1}{1 + e^{-\eta_i}} = \frac{e^{\eta_i}}{1 + e^{\eta_i}} .

In words: the probability is the linear predictor passed through the logistic function 1/(1+eη)1/(1 + e^{-\eta}), the S-curve of Figure 5. The function is squashed to lie strictly between 0 and 1, so no matter what the coefficients are, the predicted probability is always a legal probability.

Two side-by-side panels. The left panel shows probability pi on the vertical axis as an S-shaped logistic function of the linear predictor eta on the horizontal axis, rising from near 0 at eta of minus 6 through 0.5 at eta of 0 to near 1 at eta of 6. The right panel shows the log-odds of pi as a straight diagonal line against eta, confirming that the logit of the probability equals the linear predictor.

Figure 5:Left: the logistic function turns any linear predictor into a probability between 0 and 1. Right: on the log-odds scale the same relationship is a straight line, which is why logistic regression is a linear model in disguise.

The two links in the term generalized linear model are visible here. There is a random part, YiY_i being 0 or 1 with probability πi\pi_i, and a systematic part, the linear predictor ηi\eta_i, joined by the link function logit that maps the mean onto the linear scale. Ordinary regression is the same picture with the identity link and a normal response; Chapter 14 makes the family explicit and adds the Poisson member (14.6 One family: the generalized linear model).

R and Python

For the O-ring data the response is grouped: each flight contributes six O-rings, of which YiY_i were damaged. That is a binomial count, and logistic regression handles it with the same machinery, modeling the probability πi\pi_i that a single O-ring is damaged at temperature XiX_i. In R you pass the two-column response cbind(successes, failures) to glm with family = binomial; in Python you give statsmodels the two counts on the left of the formula.

The O-ring damage fraction plotted against temperature with a fitted logistic curve overlaid in blue. The curve is high, near 1, at cold temperatures on the left, falls through about 0.55 at 53 degrees, and flattens toward 0 at warm temperatures on the right. A red point at 31 degrees sits near a probability of 0.99, inside a shaded band colder than any observed flight, labeled 31 F: pi-hat equals 0.99, and the fitted curve itself is labeled pi-hat of temperature.

Figure 6:The fitted logistic curve extended to the launch temperature. At 31 degrees (red point, shaded extrapolation region) the model predicts about a 0.99 probability of damage to any given O-ring.

That 0.99 is easier to weigh honestly once you have walked the curve down there yourself.

One slider moves the launch temperature along the fitted curve π^(x)=1/(1+e(11.6630.2162x))\hat\pi(x) = 1/(1 + e^{-(11.663 - 0.2162x)}) through the 23 flights. The readouts give the fitted probability, the odds, the expected number of damaged rings out of six, and whether any flight was ever launched that cold.

What to notice. Inside the data the curve is gentle, but the moment you cross 53 degrees into the shaded strip the fitted probability climbs from 0.550 to 0.993 with no observation underneath it. Try this. Park the slider at 31 degrees, read the “none: extrapolating” label, and ask what would have to stay true about the world for that number to hold. Back to Section 13.1.

13.2 Fitting by maximum likelihood

Intuition

In simple regression we had formulas: b1=Sxy/Sxxb_1 = S_{xy}/S_{xx} and b0=Yˉb1Xˉb_0 = \bar Y - b_1 \bar X. Logistic regression has no such closed form. The reason is the S-curve: the probability πi\pi_i is a nonlinear function of the coefficients, so setting derivatives to zero gives equations you cannot solve with algebra. Instead we choose the coefficients that make the observed data most probable, the principle of maximum likelihood we first met for the normal model in Chapter 2, and we find them by climbing the likelihood hill numerically.

The likelihood and log-likelihood

Let case ii have mim_i trials and YiY_i successes (for a plain binary response mi=1m_i = 1 and Yi{0,1}Y_i \in \{0, 1\}; for the O-rings mi=6m_i = 6). Given the probabilities πi\pi_i, the successes are independent binomial counts, so the likelihood, the probability of the observed data as a function of the coefficients, is

L(β)=i=1n(miYi)πiYi(1πi)miYi,L(\beta) = \prod_{i=1}^{n} \binom{m_i}{Y_i}\, \pi_i^{\,Y_i}\,(1 - \pi_i)^{\,m_i - Y_i} ,

where each πi=1/(1+eηi)\pi_i = 1/(1 + e^{-\eta_i}) depends on β\beta through the linear predictor ηi=Xiβ\eta_i = X_i'\beta. In words: the likelihood multiplies together, across all cases, the binomial chance of seeing exactly the successes we observed, so larger values point to coefficients the data favor. Taking logs turns the product into a sum, the log-likelihood:

(β)=i=1n[Yilogπi+(miYi)log(1πi)]+C,\ell(\beta) = \sum_{i=1}^{n}\Big[\, Y_i \log \pi_i + (m_i - Y_i)\log(1 - \pi_i) \,\Big] + C ,

where CC collects the binomial coefficients log(miYi)\log\binom{m_i}{Y_i}, which do not involve β\beta and so do not affect where the maximum sits. In words: the log-likelihood rewards coefficients that put high probability on the successes we saw and high non-probability on the failures we saw.

The score equations

Proof. We want the β\beta that maximizes \ell, so we differentiate and set the result to zero. Two facts about the logistic link make the algebra collapse. First, from πi=eηi/(1+eηi)\pi_i = e^{\eta_i}/(1 + e^{\eta_i}),

logπi=ηilog(1+eηi),log(1πi)=log(1+eηi).\log \pi_i = \eta_i - \log(1 + e^{\eta_i}), \qquad \log(1 - \pi_i) = -\log(1 + e^{\eta_i}).

Substituting these into \ell and collecting terms,

(β)=i=1n[Yiηimilog(1+eηi)]+C.\ell(\beta) = \sum_{i=1}^n \Big[\, Y_i \eta_i - m_i \log(1 + e^{\eta_i}) \,\Big] + C .

Second, differentiate log(1+eηi)\log(1 + e^{\eta_i}) with respect to ηi\eta_i to get exactly πi\pi_i. Using the chain rule with ηi/βj=Xij\partial \eta_i / \partial \beta_j = X_{ij} (the jj-th predictor value for case ii, with Xi0=1X_{i0} = 1 for the intercept),

βj=i=1n(Yimiπi)Xij,j=0,1,,p1.\frac{\partial \ell}{\partial \beta_j} = \sum_{i=1}^n \big(Y_i - m_i \pi_i\big) X_{ij}, \qquad j = 0, 1, \dots, p-1 .

Setting all pp partial derivatives to zero gives the score equations. Stacking them with the design matrix X\mathbf{X} (rows XiX_i'), the fitted mean vector μ\boldsymbol{\mu} with entries μi=miπi\mu_i = m_i \pi_i, and the response vector Y\mathbf{Y},

X(Yμ)=0.\mathbf{X}'(\mathbf{Y} - \boldsymbol{\mu}) = \mathbf{0} .

In words: at the maximum-likelihood estimate, each predictor is orthogonal to the residuals YiμiY_i - \mu_i, exactly the condition the least-squares normal equations X(YXb)=0\mathbf{X}'(\mathbf{Y} - \mathbf{X}\mathbf{b}) = \mathbf{0} imposed in 7.1 The model and least squares in matrix form. The difference is that μ\boldsymbol{\mu} here bends through the logistic link, so the equations are nonlinear in β\beta and cannot be solved in closed form. \blacksquare

Newton-Raphson and IRLS

Proof. To solve X(Yμ)=0\mathbf{X}'(\mathbf{Y} - \boldsymbol{\mu}) = \mathbf{0} we use Newton’s method, which needs the matrix of second derivatives. Differentiating the score once more, and using πi/ηi=πi(1πi)\partial \pi_i / \partial \eta_i = \pi_i(1 - \pi_i),

2βjβk=i=1nmiπi(1πi)XijXik,\frac{\partial^2 \ell}{\partial \beta_j \partial \beta_k} = -\sum_{i=1}^n m_i \pi_i (1 - \pi_i)\, X_{ij} X_{ik} ,

so the Hessian is XWX-\mathbf{X}'\mathbf{W}\mathbf{X} with the diagonal weight matrix W=diag ⁣(miπi(1πi))\mathbf{W} = \operatorname{diag}\!\big(m_i \pi_i (1 - \pi_i)\big). Each weight wi=miπi(1πi)w_i = m_i \pi_i(1 - \pi_i) is the variance of YiY_i, largest for cases near πi=0.5\pi_i = 0.5 and small for cases the model is already sure about. A Newton step from a current guess β(t)\beta^{(t)} is

β(t+1)=β(t)+(XWX)1X(Yμ),\beta^{(t+1)} = \beta^{(t)} + (\mathbf{X}'\mathbf{W}\mathbf{X})^{-1}\mathbf{X}'(\mathbf{Y} - \boldsymbol{\mu}) ,

with W\mathbf{W}, μ\boldsymbol{\mu} evaluated at β(t)\beta^{(t)}. Now the tidy part. Define the working response zi=ηi+(Yiμi)/wiz_i = \eta_i + (Y_i - \mu_i)/w_i. Then

XWz=XWη+X(Yμ)=XWXβ(t)+X(Yμ),\mathbf{X}'\mathbf{W}\mathbf{z} = \mathbf{X}'\mathbf{W}\boldsymbol{\eta} + \mathbf{X}'(\mathbf{Y} - \boldsymbol{\mu}) = \mathbf{X}'\mathbf{W}\mathbf{X}\beta^{(t)} + \mathbf{X}'(\mathbf{Y} - \boldsymbol{\mu}) ,

using η=Xβ(t)\boldsymbol{\eta} = \mathbf{X}\beta^{(t)}. Multiplying the Newton step by XWX\mathbf{X}'\mathbf{W}\mathbf{X} shows it is equivalent to

β(t+1)=(XWX)1XWz.\beta^{(t+1)} = (\mathbf{X}'\mathbf{W}\mathbf{X})^{-1}\mathbf{X}'\mathbf{W}\mathbf{z} .

That is the formula for a weighted least squares fit of the working response z\mathbf{z} on X\mathbf{X} with weights wiw_i (10.4 Weighted least squares). Because W\mathbf{W} and z\mathbf{z} change each time β\beta updates, we recompute and refit, which is why the method is called iteratively reweighted least squares (IRLS). Starting from any reasonable guess, the iterates climb the concave log-likelihood to its single maximum. \blacksquare

R and Python

The centerpiece is worth doing by hand once, so IRLS stops being a black box inside glm.

Two panels tracking IRLS iterations 0 through 6 on the O-ring fit. The left panel shows the binomial deviance dropping steeply from about 153 at iteration 0 to about 17 and flattening by iteration 4. The right panel shows the temperature coefficient rising from 0 and settling at about minus 0.216 by iteration 5, marked as the maximum-likelihood slope.

Figure 8:IRLS converges fast. The deviance (left) collapses to its minimum and the temperature coefficient (right) reaches its maximum-likelihood value within about five steps, because the log-likelihood is concave with a single peak.

IRLS climbs the log-likelihood in six passes; climb it by hand once and the algorithm stops being a black box.

Two sliders set b0b_0 and b1b_1 for ten binary observations at ten doses. The log-likelihood readout scores every setting you try, and it is exactly the quantity IRLS is climbing.

What to notice. The log-likelihood is always negative and has a single peak, so every move away from the best setting costs you something, which is the concavity the proof relied on. Try this. Get the log-likelihood as close to zero as you can by hand, then set b1=0b_1 = 0 and watch the S-curve flatten and the odds ratio drop to 1. Back to Section 13.2.

13.3 Reading coefficients as odds ratios

Intuition

A slope in ordinary regression is easy to say out loud: one more unit of XX adds b1b_1 to the predicted response. The logistic slope is not that, and saying it that way is the single most common mistake in applied logistic regression. The coefficient lives on the log-odds scale, so before interpreting it we have to undo the log.

Here is the wrong reading first, because you will hear it constantly. For the diabetes model below, the glucose coefficient is about 0.04. The tempting sentence is “each extra unit of glucose raises the probability of a positive test by 0.04.” That is wrong twice over. The coefficient is not a change in probability at all, and the effect on probability is not even constant: it depends on where you start on the S-curve, tiny out in the flat tails and largest in the steep middle. What is constant is the effect on the odds. Figure 10 makes the split visible: one fixed step along the curve gives three different probability jumps but always the same odds multiplier.

Formula

Exponentiate the coefficient. Because log(odds)=β0+β1X\log(\text{odds}) = \beta_0 + \beta_1 X, raising XX by one unit changes the log-odds by β1\beta_1, so it multiplies the odds by eβ1e^{\beta_1}:

odds(X+1)odds(X)=eβ0+β1(X+1)eβ0+β1X=eβ1.\frac{\text{odds}(X + 1)}{\text{odds}(X)} = \frac{e^{\beta_0 + \beta_1(X+1)}}{e^{\beta_0 + \beta_1 X}} = e^{\beta_1} .

The quantity eβ1e^{\beta_1} is the odds ratio (Definition 13.6) for a one-unit increase in XX. In words: a one-unit rise in XX multiplies the odds of a positive outcome by eβ1e^{\beta_1}, whatever the starting odds were. An odds ratio of 1 means no effect; above 1 the odds go up, below 1 they go down. This is the same “logs make it multiplicative” logic as the log-transformed response in 10.2 The log transformation and reading its coefficients, moved onto the odds scale. For a step of cc units the odds ratio is ecβ1e^{c\beta_1}, which is how you report an effect per 10 units or per decade of age.

An S-shaped logistic curve of probability against the linear predictor. Three one-unit steps are drawn along it, one in the left flat tail, one through the steep middle, and one in the right flat tail. Each step's vertical probability jump is marked with a red bar and labeled: about 0.07 in the left tail, about 0.24 in the middle, and about 0.03 in the right tail. A text box notes that the same one-unit step always multiplies the odds by e to the first power, about 2.72, even though the probability jump changes.

Figure 10:The same one-unit step multiplies the odds by the same fixed factor everywhere (here about 2.72), yet the jump in probability is large in the steep middle and small in the flat tails. That is why the odds ratio is a single honest number but “the change in probability” is not.

R and Python

Figure 11 shows the single-predictor fit. The probability climbs smoothly with glucose, steeply through the middle of the range and flattening at both ends, the S-curve doing its job.

A scatterplot of diabetes test result, jittered near 0 and 1, against plasma glucose, with a blue logistic curve, labeled pi-hat, rising from about 0.05 at low glucose to about 0.85 at high glucose. Most points near test equals 1 sit at higher glucose, and most points near test equals 0 sit at lower glucose, though the two clouds overlap heavily in the middle.

Figure 11:The single-predictor logistic fit to the Pima data. The probability of a positive test rises with glucose, but the heavy overlap of the two outcome bands warns that glucose alone will not cleanly separate positives from negatives.

The split between a constant odds ratio and a moving probability change is worth watching move under your finger.

The curve is the fitted glucose model logit(π^)=5.715+0.0406X\operatorname{logit}(\hat\pi) = -5.715 + 0.0406X. One slider sets where the step starts, the other sets its size cc, and the readouts give both fitted probabilities, their difference, and the odds ratio ecb1e^{cb_1}.

What to notice. Hold cc at 40 and slide the starting point: the change in probability runs from about 0.12 at glucose 60 up past 0.33 near glucose 100 and back down, while the odds ratio never leaves 5.080. Try this. Hunt for the starting glucose with the largest probability jump, and notice it sits in the steep middle of the S-curve, never in a tail. Back to Section 13.3.

13.4 Testing coefficients: Wald and likelihood-ratio

Intuition

The first section fit the model; the obvious next question is which predictors are pulling their weight. There are two standard tests, and they answer the same question by different routes. The Wald test asks how many standard errors a coefficient sits from zero, using the fitted model alone. The likelihood-ratio test asks how much the fit gets worse when you delete the predictor and refit, comparing two nested models the way the general linear test compared them for ordinary regression (8.4 The general linear test).

Formula

For the Wald test of H0:βj=0H_0: \beta_j = 0, the statistic is

zj=bjs{bj},zjapproxN(0,1) under H0,z_j = \frac{b_j}{s\{b_j\}}, \qquad z_j \overset{\text{approx}}{\sim} N(0, 1) \text{ under } H_0 ,

In words: the Wald statistic counts how many standard errors the estimate bjb_j sits from zero, and under the null that distance follows a standard normal curve. Here s{bj}s\{b_j\} is the standard error printed by the fit. You can compare zj2z_j^2 to a chi-square with one degree of freedom instead, which gives the same test. The Wald confidence interval for βj\beta_j is bj±zs{bj}b_j \pm z^* s\{b_j\}, and exponentiating its endpoints gives a confidence interval for the odds ratio. These standard errors and the normal reference are large-sample approximations, exact only as nn \to \infty. With a small sample or a near-perfect predictor they can mislead.

The likelihood-ratio test uses the deviance (Definition 13.7). For a fitted model with maximized log-likelihood (β^)\ell(\hat\beta), the deviance is

D=2[sat(β^)]=2i=1n[YilogYiμ^i+(miYi)logmiYimiμ^i],D = 2\big[\ell_{\text{sat}} - \ell(\hat\beta)\big] = 2\sum_{i=1}^n\left[ Y_i \log\frac{Y_i}{\hat\mu_i} + (m_i - Y_i)\log\frac{m_i - Y_i}{m_i - \hat\mu_i}\right] ,

where sat\ell_{\text{sat}} is the log-likelihood of the saturated model that fits every observation perfectly (μ^i=Yi\hat\mu_i = Y_i), and μ^i=miπ^i\hat\mu_i = m_i \hat\pi_i are the fitted counts.

In words: the deviance measures how far the fitted model sits from a perfect fit, smaller being better; it is the logistic stand-in for the residual sum of squares. To compare a full model to a nested reduced model that drops qq predictors, the likelihood-ratio statistic is the increase in deviance,

G2=DreducedDfull=2[(β^full)(β^reduced)]approxχq2 under H0,G^2 = D_{\text{reduced}} - D_{\text{full}} = 2\big[\ell(\hat\beta_{\text{full}}) - \ell(\hat\beta_{\text{reduced}})\big] \overset{\text{approx}}{\sim} \chi^2_q \text{ under } H_0 ,

with qq the number of coefficients set to zero. The special case comparing the full model to the intercept-only model uses the null deviance as DreducedD_{\text{reduced}} and tests whether any predictor matters at all.

R and Python

Figure 13 plots the odds ratios and their intervals on a log scale, each rescaled to an interpretable step. An interval that clears the vertical line at 1 flags a predictor distinguishable from no effect; age’s interval straddles it.

A horizontal forest plot of odds ratios with 95 percent confidence intervals on a logarithmic axis, one row per predictor: glucose per 10 units, BMI per 5 units, diabetes pedigree per 1 unit, pregnant per 1, and age per 10 years. A red dashed vertical line marks an odds ratio of 1. All intervals sit clearly to the right of 1 except age, whose interval crosses the line.

Figure 13:Odds ratios per interpretable step for the Pima model, with Wald 95 percent intervals on a log scale. Every predictor’s interval clears the no-effect line at 1 except age, matching its non-significant test.

The Wald and likelihood-ratio tests usually agree, but not always. The likelihood-ratio test is generally the more reliable of the two, because it uses the actual shape of the log-likelihood rather than a single quadratic approximation at the estimate. In the awkward case of a very large coefficient (a predictor that nearly separates the two outcomes), the Wald standard error can inflate and pull its zz toward zero, hiding a strong effect. The likelihood-ratio test does not suffer that failure. When the two disagree, trust the likelihood-ratio test. Figure 14 shows the reason in one picture: the Wald test replaces the true log-likelihood with a symmetric parabola matched at the peak, and when the true curve is lopsided the parabola drifts away from it, so the two tests measure different drops down to the null.

A plot of a log-likelihood as a function of a single coefficient beta. The true log-likelihood is a solid blue curve that is skewed, rising steeply on the left and falling gently on the right, with its peak at the estimate near beta equals 2.2. A dashed orange parabola, the Wald approximation, is matched to the blue curve at the peak but is symmetric, so it sits above the blue curve on the left. A red dotted vertical line marks the null at beta equals zero, where the blue curve and the orange parabola reach clearly different heights, showing the two tests disagree.

Figure 14:The Wald test judges a coefficient by a symmetric parabola fitted at the peak of the log-likelihood, while the likelihood-ratio test uses the true curve. When the curve is skewed, the two part company away from the peak and can reach the null at different heights, which is why they can disagree and why the likelihood-ratio test is the one to trust.

13.5 How good is the fit? Classification, ROC, and data checks

Intuition

A model can have significant coefficients and still classify poorly, so the last step is to ask how well the fitted probabilities actually separate the two outcomes. We keep this light: a classification table, an ROC curve, and, first, a look at whether the data deserve trust at all.

Data first: the disguised zeros

Recall the impossible zeros from 13.3 Reading coefficients as odds ratios. Figure 15 counts them. Serum insulin is zero for 374 of 768 women and skinfold thickness for 227; a living person has neither. These are missing values recorded as zeros, and a model that swallows them whole will estimate, for instance, a nonsense insulin slope driven by a spike of fake zeros. This is why we set glucose and BMI zeros to NA before fitting and why we left insulin and triceps out of the model entirely. The lesson generalizes: look at your predictors before you trust any coefficient, because the software will fit whatever you feed it.

A bar chart counting impossible zero values in five Pima predictors. Insulin has the tallest bar at 374, triceps skinfold next at 227, diastolic blood pressure 35, BMI 11, and glucose 5. The counts are labeled on top of each bar.

Figure 15:Disguised missing data in the Pima predictors. Insulin and triceps are zero for hundreds of women, which is physiologically impossible, so those zeros are missing values in disguise and must be handled before modeling.

Classification table and ROC

To turn probabilities into yes-or-no predictions, pick a threshold (0.5 is the default) and predict positive when π^i\hat\pi_i exceeds it. Cross-tabulating predictions against truth gives a classification table, from which sensitivity (the fraction of true positives caught) and specificity (the fraction of true negatives correctly cleared) follow (Definition 13.8). Because one threshold is an arbitrary choice, the ROC curve sweeps every threshold at once, plotting sensitivity against one-minus-specificity; the area under the curve (AUC) summarizes the whole sweep as the probability that the model scores a random positive above a random negative (Definition 13.9).

An ROC curve for the Pima model, plotting true-positive rate against false-positive rate. The blue curve bows well above the diagonal dotted chance line, reaching toward the top-left corner, with an area under the curve labeled 0.843. A red point marks the operating point at the 0.5 threshold, at a false-positive rate near 0.12 and a true-positive rate near 0.57.

Figure 16:The ROC curve sweeps every classification threshold. The curve bows above the diagonal (chance), with AUC 0.84; the red point is the default 0.5 threshold, showing its high specificity but modest sensitivity.

The 0.5 in that table is a choice you made, not something the model handed you, so the fair thing to do is move it.

The ROC curve of the five-predictor model on all 752 women, with an operating point that follows your threshold. Sensitivity, specificity, accuracy, and the cells of the classification table are recomputed from the 752 fitted probabilities at every step.

What to notice. Dropping the threshold from 0.50 to 0.30 lifts sensitivity from 0.568 to 0.784 and cuts the missed positives from 114 to 57, while false alarms climb from 57 to 140 and accuracy falls. Try this. Find the threshold with the highest accuracy, count how many positive cases it still misses, then decide whether you would ship it as a screening test. Back to Section 13.5.

13.6 Chapter summary

This chapter built a regression model for a binary or binomial response. Ordinary least squares fails for a yes-or-no outcome in three ways (unbounded fits, non-constant variance, non-normal errors), and logistic regression fixes all three by making the log-odds linear. Because the S-curve makes the likelihood equations nonlinear, there is no closed-form estimate: maximum likelihood finds the coefficients, and IRLS computes them by repeated weighted least squares. Each coefficient reads as an odds ratio, tested by the Wald and likelihood-ratio tests, and the fitted probabilities are judged by deviance, a classification table, and an ROC curve, after the data are checked for disguised missing values.

Key results at a glance

ResultStatement or formulaValid when
Odds (Def 13.1)odds=π/(1π)\text{odds} = \pi/(1-\pi)any probability π(0,1)\pi \in (0,1)
Logit (Def 13.2)logit(π)=log[π/(1π)]\operatorname{logit}(\pi) = \log[\pi/(1-\pi)]0<π<10 < \pi < 1
Logistic model (Def 13.3)logit(πi)=ηi\operatorname{logit}(\pi_i) = \eta_i, πi=1/(1+eηi)\pi_i = 1/(1 + e^{-\eta_i})binary or binomial response, independent cases
Score equations (Thm 13.4)X(Yμ)=0\mathbf{X}'(\mathbf{Y} - \boldsymbol{\mu}) = \mathbf{0}, μi=miπi\mu_i = m_i \pi_iat the maximum-likelihood estimate
IRLS (Thm 13.5)β(t+1)=(XWX)1XWz\beta^{(t+1)} = (\mathbf{X}'\mathbf{W}\mathbf{X})^{-1}\mathbf{X}'\mathbf{W}\mathbf{z}each Newton step; log-likelihood is concave
Odds ratio (Def 13.6)eβje^{\beta_j} (or ecβje^{c\beta_j} per cc units)log-odds linear in XjX_j
Deviance (Def 13.7)D=2[sat(β^)]D = 2[\ell_{\text{sat}} - \ell(\hat\beta)]fitted model vs saturated model
Wald statisticzj=bj/s{bj}N(0,1)z_j = b_j / s\{b_j\} \sim N(0,1)large sample
Likelihood-ratioG2=DredDfullχq2G^2 = D_{\text{red}} - D_{\text{full}} \sim \chi^2_qnested models, large sample
Sensitivity, specificity (Def 13.8)TP/(TP+FN)\text{TP}/(\text{TP}+\text{FN}), TN/(TN+FP)\text{TN}/(\text{TN}+\text{FP})a chosen threshold
AUC (Def 13.9)P(π^pos>π^neg)P(\hat\pi_{\text{pos}} > \hat\pi_{\text{neg}})ranking quality, all thresholds

Key terms

logistic regression, odds, log-odds (logit), linear predictor, link function, maximum likelihood, likelihood, log-likelihood, score equations, iteratively reweighted least squares (IRLS), working response, odds ratio, Wald test, likelihood-ratio test, deviance, saturated model, classification table, sensitivity, specificity, ROC curve, area under the curve (AUC).

You should now be able to

Where this fits. In the workflow of The modeling workflow this chapter is mostly FIT and USE for a new kind of response: we ASK a yes-or-no question, EXPLORE with the same plots (now of proportions), FIT by maximum likelihood instead of least squares, CHECK with deviance and the disguised-zero audit, and USE the fitted probabilities to interpret odds ratios and to classify. The machinery carries the earlier chapters with it: the score equations echo the normal equations of 7.1 The model and least squares in matrix form, IRLS is weighted least squares (10.4 Weighted least squares) run in a loop, the likelihood-ratio test is the general linear test (8.4 The general linear test) in deviance form, categorical predictors enter through the dummy coding of 11.1 From categories to numbers: indicator coding, and honest evaluation needs the validation mindset of 12.4 Validation: train, test, and cross-validate. Chapter 14 takes the last step, keeping the maximum-likelihood and deviance machinery but swapping the binomial family for the Poisson to model counts, and names the generalized linear model family that holds linear, logistic, and Poisson regression together (14.6 One family: the generalized linear model).

13.7 Frequently asked questions

Q1. Why maximum likelihood instead of least squares here? Least squares minimizes squared error, which is the right criterion when the response is continuous with constant-variance normal noise. A binary response has neither, so squared error is no longer the natural loss. Maximum likelihood asks which coefficients make the observed 0/1 pattern most probable under the logistic model, which is the principled choice for this response, and for the normal model it happens to reproduce least squares anyway (Chapter 2).

Q2. Is an odds ratio the same as a relative risk? No, and confusing them is a common error. Relative risk is a ratio of probabilities; the odds ratio is a ratio of odds. When the outcome is rare (small π\pi) the two are close, because odds \approx probability there, but for a common outcome they diverge, and the odds ratio is always the more extreme number. Report an odds ratio as an odds ratio.

Q3. What does a negative coefficient mean? It means the predictor lowers the log-odds, so its odds ratio eβje^{\beta_j} is below 1 and the probability of a positive outcome falls as the predictor rises. The O-ring temperature coefficient is negative: warmer launches have lower damage odds.

Q4. Why is the deviance the logistic version of the residual sum of squares? Both measure how far the fitted model sits from the data. In ordinary regression, twice the negative log-likelihood gap between your model and a perfect fit is exactly the residual sum of squares (up to a constant); for logistic regression that same gap is the deviance. Smaller deviance is a better fit, and differences in deviance between nested models follow a chi-square distribution, just as differences in the sum of squares gave FF statistics before.

Q5. Can I use R2R^2 for a logistic model? Not the ordinary one, because there is no residual sum of squares to divide. Several “pseudo-R2R^2” measures exist (McFadden’s, Cox-Snell, Nagelkerke), each built from log-likelihoods, and software reports them, but they do not have the clean “fraction of variance explained” meaning of the linear R2R^2. For judging a logistic model, deviance, the likelihood-ratio test, and the AUC are more informative.

Q6. My predicted probability at an extreme XX is 0.999. Should I believe it? Treat it the way you would any extrapolation. If the extreme XX is inside the range of your data, the probability is as trustworthy as the fit. If it is outside, as with the O-rings at 31 degrees, the S-curve is being asked to keep bending where you have no evidence about its shape, and the tidy number hides real uncertainty. Report it, but say plainly that it is an extrapolation.

Q7. Why did glm drop 16 observations from the diabetes model? Because we set the impossible zeros in glucose (5 of them) and BMI (11) to NA, and glm uses only complete cases by default. Those 16 women are missing a predictor the model needs. Dropping them is defensible here, but for a serious analysis you would consider whether the missingness is related to the outcome, which can bias the fit, and possibly impute rather than delete.

13.8 Practice problems

  1. (A) In one sentence each, name the three ways ordinary least squares fails for a binary response.

  2. (A) Convert these probabilities to odds: 0.2, 0.5, 0.8. Then convert these odds to probabilities: 0.25, 1, 4.

  3. (A) The O-ring temperature coefficient is -0.2162. State its odds ratio for a one-degree increase and say in words what that number means.

  4. (A) Explain the difference between the error YiπiY_i - \pi_i that logistic regression works with and the fitted probability π^i\hat\pi_i. Which is observed?

  5. (A) A student writes “the glucose odds ratio is 1.04, so a positive test is 4 percent more likely per unit.” Identify the misconception and correct it.

  6. (A) Why does logistic regression have no closed-form formula for its coefficients, unlike simple linear regression?

  7. (A) The AUC of a model is 0.5. What does that say about the model’s ability to rank cases?

  8. (A) Give the sensitivity and specificity from the Pima classification table in Example 13.5, and say which kind of error a diabetes screening test should most want to avoid.

  9. (A) The diabetes-pedigree odds ratio is 2.51 with 95 percent interval (1.39,4.53)(1.39, 4.53). Is its effect distinguishable from no effect? How can you tell from the interval?

  10. (A) Explain why the age coefficient can be non-significant in the multiple model even though older women are, marginally, more likely to test positive.

  11. (B) Starting from (β)=i[Yilogπi+(miYi)log(1πi)]\ell(\beta) = \sum_i [Y_i \log \pi_i + (m_i - Y_i)\log(1 - \pi_i)] and πi=1/(1+eηi)\pi_i = 1/(1 + e^{-\eta_i}), show that =i[Yiηimilog(1+eηi)]+C\ell = \sum_i [Y_i \eta_i - m_i \log(1 + e^{\eta_i})] + C.

  12. (B) Differentiate the log-likelihood in Problem 11 to derive the score equations i(Yimiπi)Xij=0\sum_i (Y_i - m_i \pi_i) X_{ij} = 0 (Theorem 13.4), stating where you use πi/ηi=πi(1πi)\partial \pi_i / \partial \eta_i = \pi_i(1 - \pi_i).

  13. (B) Show that the second derivative of the log-likelihood is imiπi(1πi)XijXik-\sum_i m_i \pi_i(1 - \pi_i) X_{ij} X_{ik}, and conclude that the Hessian is XWX-\mathbf{X}'\mathbf{W}\mathbf{X} with W=diag(miπi(1πi))\mathbf{W} = \operatorname{diag}(m_i \pi_i(1 - \pi_i)). Explain why this makes \ell concave.

  14. (B) Derive the IRLS update β(t+1)=(XWX)1XWz\beta^{(t+1)} = (\mathbf{X}'\mathbf{W}\mathbf{X})^{-1}\mathbf{X}'\mathbf{W}\mathbf{z} (Theorem 13.5) from the Newton step, identifying the working response ziz_i.

  15. (B) Using the intercept score equation, prove that a logistic model with an intercept has iYi=imiπ^i\sum_i Y_i = \sum_i m_i \hat\pi_i. Interpret this for a plain binary response.

  16. (B) Derive the odds ratio ecβ1e^{c\beta_1} for a cc-unit increase in a predictor, starting from logit(π)=β0+β1X\operatorname{logit}(\pi) = \beta_0 + \beta_1 X.

  17. (B) Show that for a rare outcome (small π\pi) the odds ratio and the relative risk are approximately equal, by expanding both for small probabilities.

  18. (B) Write the deviance for a binary (mi=1m_i = 1) logistic model and explain why each term is zero exactly when π^i\hat\pi_i equals the observed YiY_i, so the saturated model has deviance 0.

  19. (B) Explain, in terms of the shape of the log-likelihood, why the likelihood-ratio test can disagree with the Wald test when a coefficient is very large, and which test to trust.

  20. (B) The logistic function is π(η)=1/(1+eη)\pi(\eta) = 1/(1 + e^{-\eta}). Show that π(η)=π(η)(1π(η))\pi'(\eta) = \pi(\eta)(1 - \pi(\eta)), and explain why this makes the probability change per unit of η\eta largest at η=0\eta = 0.

  21. (C) Fit the O-ring model in R or Python and reproduce b0=11.663b_0 = 11.663 and b1=0.2162b_1 = -0.2162. Predict the damage probability at 50 and 75 degrees and interpret both.

  22. (C) Refit the O-ring model treating each flight as a single binary “any damage” outcome (damage > 0) instead of the count of six. Compare the temperature coefficient to the grouped fit and comment on what changed.

  23. (C) On the Pima data, fit test ~ glucose and test ~ glucose + bmi. Report the glucose odds ratio in each and explain why it changes when BMI is added.

  24. (C) Reproduce the five-predictor Pima classification table at the 0.5 threshold, then recompute sensitivity and specificity at thresholds 0.3 and 0.7. Describe the trade-off as the threshold moves.

  25. (C) Bin age into a factor with levels “under 30”, “30 to 45”, and “over 45”, add it to test ~ glucose + bmi as a categorical predictor (as in 11.1 From categories to numbers: indicator coding), and interpret the odds ratios of its levels relative to the reference.

  26. (C) Compute the whole-model likelihood-ratio statistic for test ~ glucose + bmi + age + diabetes + pregnant from its null and residual deviances, and confirm your number against drop1 or a manual null-model comparison.

  27. (C) Carry out a rough validation: split the Pima data 70/30 (seed 4210), fit the five-predictor model on the training part, and compute the AUC on the held-out part. Compare it to the in-sample AUC of 0.84 and explain any gap using 12.4 Validation: train, test, and cross-validate.

  28. (C) Draw the fitted probability curve for test ~ glucose and add the observed positive rate within glucose deciles as points. Does the curve track the binned rates? What would a poor fit look like here?

13.9 Exam practice

These five questions are written in the style of the course exams. Every one asks you to explain your reasoning in full sentences, not to report a bare number: on the real exam a correct number with no supporting words earns little credit, and clear reasoning with a small arithmetic slip earns most of it. Where a question shows software output, it was produced on the course machine (R 4.6.0) from the same CSV files in data/ that you used all term; Python with statsmodels gives the same numbers. Work each one before opening its model answer.

EP 13.1 (interpret output in context). A clinic fits a logistic model for a positive diabetes test on the cleaned pima data (impossible zeros in glucose and bmi set to NA and dropped), using plasma glucose, body mass index, and number of pregnancies.

pima <- read.csv("data/pima.csv")
pima$glucose[pima$glucose == 0] <- NA
pima$bmi[pima$bmi == 0] <- NA
fit <- glm(test ~ glucose + bmi + pregnant, family = binomial, data = pima)
summary(fit)
Coefficients:
             Estimate Std. Error z value Pr(>|z|)
(Intercept) -8.780034   0.684606 -12.825  < 2e-16 ***
glucose      0.037079   0.003459  10.720  < 2e-16 ***
bmi          0.089899   0.014598   6.158 7.35e-10 ***
pregnant     0.131273   0.027299   4.809 1.52e-06 ***
---
    Null deviance: 974.75  on 751  degrees of freedom
Residual deviance: 714.58  on 748  degrees of freedom

Interpret the pregnant coefficient as an odds ratio in words a clinician could use. Then, for a woman with glucose = 150, bmi = 30, and pregnant = 4, the reported linear predictor is η^=0.004\hat\eta = 0.004; compute her estimated probability of a positive test, show the arithmetic, and say in one sentence why that probability is not four times the odds-ratio story.

EP 13.2 (a student claims something; evaluate it). Looking at the fit in EP 13.1, a student writes: “The BMI coefficient is 0.0899, and its odds ratio is e0.0899=1.094e^{0.0899} = 1.094, so a woman with a BMI of 45 is about 9 percent more likely to test positive than a woman with a BMI of 44.” Evaluate the claim. Say precisely what is right, what is wrong, and give the corrected sentence.

EP 13.3 (explain why). Simple linear regression has closed-form formulas for its coefficients, b1=Sxy/Sxxb_1 = S_{xy}/S_{xx} and b0=Yˉb1Xˉb_0 = \bar Y - b_1 \bar X, but logistic regression has no such formula and is fit by iteratively reweighted least squares instead. Explain why no closed form exists, what IRLS actually does at each step, and why the procedure is guaranteed to climb to a single best set of coefficients rather than getting stuck.

EP 13.4 (what would change if). The five-predictor Pima model of Example 13.4 classifies at the default 0.5 probability cutoff with the table on the left below. A screening clinic proposes lowering the cutoff to 0.3, which gives the table on the right.

   cutoff 0.5                        cutoff 0.3
          actual 0  actual 1                  actual 0  actual 1
predict 0      431       114        predict 0      348        57
predict 1       57       150        predict 1      140       207

Explain what changes when the clinic moves the cutoff from 0.5 to 0.3. Compute the sensitivity and specificity at each cutoff, describe the trade-off in plain terms, and say whether the move is a good idea for a screening test and why.

EP 13.5 (interpret output in context). To ask whether age, diabetes pedigree, and number of pregnancies add anything beyond glucose and BMI, an analyst runs a likelihood-ratio test on the cleaned pima data and also reports the in-sample AUC of the larger model.

Model 1: test ~ glucose + bmi                     Residual deviance 738.51 on 749 df
Model 2: test ~ glucose + bmi + age + diabetes + pregnant   Residual deviance 703.24 on 746 df
Likelihood-ratio test: deviance drop = 35.27 on 3 df, p-value = 1.1e-07
in-sample AUC (Model 2) = 0.843

State the null hypothesis, show where the statistic 35.27 comes from, give its reference distribution and conclusion, and then explain why the AUC of 0.843 is likely optimistic and what tool from Chapter 12 would give an honest estimate.

Chapter game