Binary logistic regression explained, with odds ratios

Updated

Binary logistic regression models the probability of a yes/no outcome, such as defective or good, pass or fail, as a function of one or more predictors. It fits a straight line on the log-odds scale, which keeps every predicted probability between 0 and 1. Each coefficient converts to an odds ratio: how many times the odds of the event multiply when the predictor rises by one unit.

When to use it

  • The response is binary: a part passes or fails a test, a joint has a void or not, a customer returns a unit or not.
  • You want to know which process settings change the defect probability, and by how much.
  • You want to predict the defect probability at a setting, or find the setting that brings it below a goal.
  • Your data are either one row per unit (0 or 1) or one row per setting with the number of events and the number of trials.

For an outcome with more than two ordered levels (good, marginal, bad), use ordinal logistic regression; for unordered categories, nominal logistic regression. For counts of defects per unit, use Poisson regression. To compare just two groups, a two-proportions test is enough.

How it works

The model links the probability p of the event to the predictors through the logit, the log of the odds:

ln( p / (1 − p) ) = β₀ + β₁x₁ + … + βₖxₖ p = 1 / (1 + e^−(β₀ + β₁x₁ + … + βₖxₖ))

The coefficients are estimated by maximum likelihood. There is no closed formula, so the fit uses iteratively reweighted least squares (IRLS): starting from b = 0, it repeats a weighted least-squares step until the coefficients stop changing. With mᵢ trials, yᵢ events and the current fitted pᵢ and ηᵢ (the linear predictor) in row i:

wᵢ = mᵢpᵢ(1 − pᵢ) zᵢ = ηᵢ + (yᵢ − mᵢpᵢ) / wᵢ b(new) = (XᵀWX)⁻¹ XᵀWz

The standard errors come from (XᵀWX)⁻¹ at convergence. The output to expect:

  • Coefficients, SE and Z (Coef / SE), with p-values from the normal distribution (Wald tests).
  • Odds ratio = e^(b·Δx) for a change Δx in the predictor, with a confidence interval from e^((b ± 1.96·SE)·Δx).
  • Deviance: twice the gap in log-likelihood between your model and a perfect fit. The drop in deviance from the constant-only model gives a likelihood-ratio test of all predictors, compared with a chi-square distribution.
  • Goodness of fit: with grouped data, the deviance and Pearson statistics compared with chi-square on (groups − coefficients) degrees of freedom. A small p-value means the model does not describe the data well. With one row per unit, use the Hosmer–Lemeshow test instead.

Example

An SMT line sees non-wetting defects on a connector after reflow. The process engineer runs 40 boards at each of six peak temperatures and counts the boards with at least one defect:

Peak temperature (°C)232236240244248252
Boards404040404040
Defective boards181012231
Proportion0.4500.2500.3000.0500.0750.025

IRLS converges in six iterations:

TermCoefSE CoefZPOdds ratio95% CI
Constant36.547.384.95< 0.001
Peak temperature−0.158330.03099−5.11< 0.0010.8540.803 to 0.907
ln( p / (1 − p) ) = 36.54 − 0.15833 · Temperature
TestChi-squareDFP
Likelihood ratio (all terms)33.621< 0.001
Deviance goodness of fit5.4740.243
Pearson goodness of fit5.4940.241

The deviance drops from 39.09 for the constant-only model to 5.47.

Peak temperature (°C)232236240244248252
Fitted probability0.4520.3050.1890.1100.0620.034
Proportion of defective boards by peak temperature, observed and fitted0.00.10.20.30.4Proportion defectiveObservedFitted232236240244248252Peak temperature (°C)
Proportion of defective boards by peak temperature, observed and fitted

How to read the results

  • The sign tells the direction. The negative coefficient means hotter profiles give fewer defects.
  • The odds ratio tells the size. Each extra 1 °C multiplies the odds of a defective board by 0.854, a 15% cut in the odds. Over 5 °C the factor is e^(5 × −0.15833) = 0.453 (95% CI 0.334 to 0.614): the odds roughly halve.
  • Odds are not probabilities. The same 5 °C step takes the fitted probability from 0.452 to 0.272 when you start at 232 °C, but from 0.061 to 0.029 when you start at 248 °C. Read the fitted probabilities for practical decisions.
  • The goodness-of-fit p-values (0.24) are large, so the logistic curve describes the six groups adequately. The 240 °C group (0.300 observed against 0.189 fitted) is the biggest gap, still within chance.
  • Watch the range. The model predicts 4.6% defective at 250 °C. Reaching 1% would take about 260 °C, beyond the tested range and close to what the components may tolerate, so it needs confirmation runs before anyone changes the profile.

Common mistakes

  • Reading the coefficient as a change in probability. It is a change in log-odds. Convert to an odds ratio, then look at fitted probabilities.
  • Using ordinary regression on proportions. It can predict probabilities below 0 or above 1 and gives wrong standard errors.
  • Too few events. With only a handful of defects, estimates are unstable. A common guide is at least 10 events (and 10 non-events) per coefficient.
  • Complete separation. If every board above some temperature is good and every one below is bad, the coefficient runs off to infinity and the fit fails to converge. Report the separation itself.
  • Ignoring overdispersion. If boards come in panels that share a cause, the spread can exceed what the binomial allows. A goodness-of-fit p-value near 0 is the warning sign.

Frequently asked questions

What is the difference between odds and probability?

Probability is events divided by trials; odds are events divided by non-events. A probability of 0.2 is odds of 0.25 (1 to 4). For rare events, odds and probability are nearly equal.

How do I interpret an odds ratio below 1?

The event becomes less likely as the predictor rises. An odds ratio of 0.854 per °C means the odds fall by about 15% for each degree.

Can I use logistic regression with one row per part?

Yes. Code the response as 0 and 1 (or two labels). The coefficients come out the same as with grouped data; only the goodness-of-fit tests change, because the deviance is not meaningful for single 0/1 rows.

What does the likelihood-ratio test tell me?

Whether the predictors, taken together, explain the outcome better than a constant probability. Here, chi-square = 33.62 on 1 degree of freedom, p < 0.001.

Try it with your own data

StatLab SPC is free, open-source statistics for engineers. It runs in your browser, works offline, and never uploads your data.

Open StatLab