Logistic Regression

Dr. R. Düsing · University of Osnabrück
N ≈ 250 people · smoking-cessation study X₁ = age (years) X₂ = cigarettes/day (before intervention) Y = relapse after 12 months (0/1)
Seed: —
Why not OLS?
The linear probability model (LPM) predicts values outside [0,1] — the S-curve solves the problem
Age → P(Relapse) — outcome jittered (can only be 0 or 1)
New case: Age 88
LPM: P(Relapse)
Logistic: P(Relapse)
Fitted models (same data):
LPM: P̂ = + ·Age
Logit: logit(P̂) = + ·Age
Tutorial — Why not OLS?

What you see
Every person either relapsed (Y=1, top) or did not (Y=0, bottom) — the points are only "jittered" vertically for readability, but in reality there are only these two values. Both curves are fit to the same simulated data: the orange line is an ordinary OLS model (Linear Probability Model, LPM) — lm(Relapse ~ Age). The blue S-curve is the logistic regression — glm(Relapse ~ Age, family = binomial).

The LPM's problem
A straight line knows no bounds. Drag the slider to a high or low age — the red/orange hatched zone marks where the LPM predicts a probability outside [0,1]. A "P(Relapse) = 1.14" or "= −0.08" is not a valid value for a probability — the model is simply misspecified at that point.

The solution: the S-curve
The logistic function is constructed so that it by definition can never fall below 0 or rise above 1 — it only approaches these bounds asymptotically, never reaching them. That makes every prediction an automatically plausible probability, no matter how extreme the X value.

What to do
Click ↻ New Data for a fresh random sample — the basic finding stays stable: the LPM violates its own range as soon as you move away from the center of the data. Logistic regression never does. Onward to Module : how does the S-shape actually come about?

Core idea: Instead of predicting Y directly and linearly, logistic regression transforms the probability P(Y=1) — via odds and the logit — into a quantity that is unbounded (−∞ to +∞). There, ordinary linear modeling is fine. The back-transformation (Module ) automatically produces the S-shape and thereby valid probabilities.
From Probability to Logit — and back
The transformation chain P → Odds → Logit — and why exponentiating coefficients yields odds ratios
Part A — The transformation chain (freely adjustable P)
P(Y=1) .70
Probability P
Odds = P/(1−P)
Odds
Logit = ln(Odds)
Logit
P: 0Range [0, 1]1
Odds: 0Range [0, ∞) — log-scaled
Logit: −6Range (−∞, +∞)+6
Why three steps? P is bounded to [0,1] — unsuitable as a linear target. Odds = P/(1−P) relaxes the upper bound (0 to ∞), but the lower bound remains. Only the logarithm of the odds is completely unbounded — exactly what a linear model b₀ + b₁·X needs as a target.
Part B — Worked example: Relapse ~ Sex
Fitted model (glm, family=binomial): logit(Relapse) = −0.672 + 0.518 · male (female = 0 coded, reference)
♀ female
33.8%
♂ male
46.2%
OR = e0.518 ≈ 1.68 — men have a 1.68-fold higher chance (odds) of relapse than women. On the probability scale, though, that's "only" +12.4 percentage points (33.8% → 46.2%). 68% higher odds ≠ 68% higher probability — that difference is the core of Module .
logit(Y=1|X=0) = b₀  →  logit(P⁰)
logit(Y=1|X=1) = b₀ + b₁  →  logit(P¹)
b₁ = logit(P¹) − logit(P⁰) = ln[ (P¹/(1−P¹)) / (P⁰/(1−P⁰)) ]
⇒ eb₁ = (P¹/(1−P¹)) / (P⁰/(1−P⁰)) = Odds(X=1) / Odds(X=0) = OR

The logit coefficient is thus a difference of log-odds. A difference of logarithms exponentiates to a ratio — which is why eb always yields a ratio (odds ratio), never an additive difference.
Marginal Effects on the Probability Scale
The same step Δ on the X axis — a constant difference in the logit, a constant ratio in the odds, but a changing difference in the probability
Cigarettes/day → Logit / Odds / Probability — logit(P) = −2.5 + 0.06 · cigarettes — points A and B = A+Δ marked
① Logit — linear
② Odds — exponential
③ Probability — S-curve
Point A 15
Step size Δ 15
① ΔLogit = b₁·Δ
constant — independent of A
② OR = e^(b₁·Δ)
constant — independent of A
③ ΔP = P(B) − P(A)
Tutorial — Marginal Effects

What you see
The same two points A and B = A+Δ, plotted simultaneously on three scales of the same model logit(P) = −2.5 + 0.06·cigarettes. The Point A slider shifts both points together along the X axis, Step size Δ changes the distance between them.

Slide Point A back and forth while Δ stays fixed
Panel ① (Logit) and the "ΔLogit" number never change — the line has the same slope b₁ everywhere. Panel ② (Odds) and "OR" also never change — the same step Δ in the predictor always means the same factor on the odds scale. But Panel ③ (Probability) and "ΔP" change substantially, depending on where A currently sits.

Why?
Logit differences are additive (b₁·Δ is a constant), odds ratios are multiplicative (e^(b₁·Δ) is a constant) — but probability is a curved function of the logit. The same additive/multiplicative step sometimes lands on the steep middle of the S-curve (large ΔP), sometimes on a flat edge (small ΔP).

Why isn't one number enough?
A single OR or logit coefficient describes the relationship completely on the logit scale — but nobody thinks in logits. Once you convert back to the probability scale (which people have intuition for), the effect is no longer constant. Only a curve — not a single coefficient — shows where the predictor matters a lot and where it barely matters. That's why Module always plots instead of just tabulating.

Instantaneous (marginal) effect at point A
Let Δ shrink toward 0, and the ΔP secant from above becomes the tangent to the S-curve at A — the instantaneous rate of change dP/dX = b₁ · P·(1−P). This curve is a downward-opening parabola in P: maximal at P=0.5 (value 0.25), zero at P=0 or P=1. It summarizes what the three panels above already show: the same logit coefficient b₁ has the largest effect on the probability in the middle of the S-curve and almost none at its edges. Software (e.g. the R package marginaleffects, Stata's margins) usually reports either the Average Marginal Effect (the mean of dP/dX across all observed X) or the Marginal Effect at the Mean (dP/dX evaluated at X̄) as a compromise number for this curve.
Multiple Predictors — why plot?
Categorical and continuous predictors together in the model, converted back to probabilities with 95% CI
Sex × Status — predicted P(Relapse), 95% CI from refitted model
b₀ (baseline)−0.91
Effect: male+0.54
Effect: status middle−0.04
Effect: status low+0.69
Interaction m×low0.00
Tutorial — Visualizing Multiple Predictors

"Sex × Status" view
Two categorical predictors + interaction are barely intuitive to read as a coefficient table: six numbers (intercept, 2 main effects, plus interaction) on the logit scale. Converted back to probabilities, they become six bars with a 95% confidence interval — at a glance you can see which group carries the highest relapse risk and where the confidence intervals overlap (no reliable difference).

"Status × Cigarettes" view
One categorical and one continuous predictor together: three S-curves (one per status level) over cigarette consumption, with a confidence band. This shows simultaneously the effect of the continuous predictor (curve shape, Module ) and the offset from the categorical variable (distance between curves) — something a coefficient table alone could never show together.

Sliders: change the effects yourself
On the left you can freely set b₀, the main effects, and an interaction on the logit scale. Raise "Interaction m×low", for instance: the sex difference becomes larger or smaller within the "low" status group than in the other groups — the bars visibly diverge instead of staying parallel. In the second view, "Interaction cig.×low" rotates one S-curve's slope relative to the other two. ↺ Lecture values resets the original values from the lecture.

Where do the confidence intervals come from?
Not made up: for whatever effects are currently set, a synthetic but realistic dataset is simulated and refit live via Newton-Raphson (IRLS) — the same algorithm R's glm() uses. The CIs are genuine Wald confidence intervals from that fit's covariance matrix, not an illustration. The info box on the left shows both: the effects you set, and what a fit on the simulated data (with real sampling noise) recovers from them.

Relation to the GLM framework
Logistic regression is a special case of the Generalized Linear Model: the same predictor structure (main effects, interactions, dummy coding) as in OLS & Multiple Regression or ANOVA — just with a logit link instead of an identity link, and a binomial instead of a normal distribution. For classification quality (cutoff, sensitivity/specificity, ROC/AUC), see the dedicated Sensitivity & Specificity tool. For logistic regression as a tool for confounding control, see Propensity Score Matching.

Flashcards — Logistic Regression
Why not OLS?
Modeling a binary Y (0/1) with ordinary OLS regression (a Linear Probability Model) produces predictions outside [0,1] — not valid probabilities. Logistic regression solves this with an S-shaped function that, by construction, can never fall below 0 or rise above 1.
P → Odds → Logit
Odds = P/(1−P) relaxes the upper bound, Logit = ln(Odds) removes the lower bound too — only the logit is unbounded (−∞, +∞) and thus suitable as a linear target: logit(P) = b₀ + b₁·X.
OR = eb₁ — differences of log-odds exponentiate to ratios, not differences. OR = 1.68 means 68% higher odds, not 68 percentage points higher probability. This mix-up is the most common interpretation error in logistic regression.
The marginal effect isn't constant
dP/dX = b₁ · P·(1−P) — the same logit coefficient b₁ produces a differently sized effect on the probability depending on the current P: maximal at P = 0.5, minimal at the extremes (P near 0 or 1).
That's why you always plot
Because the effect on the probability scale depends on where you sit on the S-curve, a coefficient table alone is not enough. Predicted probabilities with confidence intervals — as a curve or a bar chart — are the standard way to report logistic models understandably.
A special case of the GLM
Logistic regression = GLM with a logit link and binomial distribution. The same predictor logic (main effects, interactions, dummy coding) as in OLS still applies — only the back-transformation onto the outcome's scale differs.
? Help — Logistic Regression

Why logistic regression?

When the dependent variable Y is binary (0/1, e.g. relapse yes/no), ordinary OLS regression (a Linear Probability Model) systematically violates its own range: it predicts probabilities below 0 or above 1. Logistic regression instead models the logit of P(Y=1) linearly:

logit(P) = ln( P / (1−P) ) = b₀ + b₁·X₁ + … + bₖ·Xₖ

Back-transformed, this yields an S-shaped function that gives a valid probability for every X value:

P(Y=1) = 1 / (1 + e−(b₀+b₁·X))

▸ Estimation: maximum likelihood / IRLS (for the curious)
Unlike OLS, logistic regression has no closed-form solution. It is estimated via maximum likelihood, iteratively, using Iteratively Reweighted Least Squares (IRLS, equivalent to Newton-Raphson): at each step a weighted linear model is solved, with weights w = P̂·(1−P̂) derived from the current estimate. This procedure actually runs in JavaScript in Module of this tool to compute the confidence intervals shown there — the same algorithm used inside R's glm().

Interpreting coefficients

b₁ (logit scale): "For each +1 unit of X, the logit of P(Y=1) increases by b₁" — correct, but practically unintuitive, since nobody thinks in logits.

OR = eb₁ (odds ratio): "For each +1 unit of X, the odds of Y=1 are multiplied by the factor OR." Multiplicative, not additive — an OR of 2 means "twice the odds," not "50 percentage points more."

Predicted probability: the only quantity people can intuitively picture. But it is nonlinear in X — hence Module of this tool.

Why aren't marginal effects constant?

The derivative of P with respect to X is dP/dX = b₁ · P·(1−P). The term P·(1−P) is a downward-opening parabola with a maximum at P=0.5 (value 0.25) and zeros at P=0 and P=1. The same logit coefficient thus has almost no effect on the probability near the extremes, but the largest effect in the middle. Software usually reports the Average Marginal Effect (AME) as a compromise number.

What logistic regression doesn't replace

  • Classification quality (cutoff, sensitivity/specificity, ROC/AUC) — its own topic, see Sensitivity & Specificity
  • More than two categories — for that, multinomial or ordinal logistic regression (generalizations not covered here)
  • Causality — even a perfectly specified model, logistic regression remains an association method, see Causal Inference

Related tools

References

Hosmer, D. W., Lemeshow, S. & Sturdivant, R. X. (2013). Applied Logistic Regression (3rd ed.). Wiley.