Method · Statistics Library

Logistic regression

Logistic regression models the probability of a binary (yes/no) outcome as a function of predictors by modeling the log-odds, reporting each predictor's effect as an odds ratio alongside classification accuracy and ROC-AUC.

Topic indexA reference reading outside the guided chapter sequence

Understand the idea

When your outcome is a straight yes/no rather than a number, ordinary regression's straight-line predictions can wander below 0% or above 100% probability, which makes no sense. Logistic regression fixes this by predicting the LOG-ODDS of the outcome (the logarithm of the odds, where the odds are the chance of 'yes' divided by the chance of 'no') instead of the raw probability, a mathematical trick that keeps the final predicted probability always between 0 and 1, however extreme the predictor values get.

The natural way to read a coefficient here is as an ODDS RATIO (raise e to the power of the coefficient, the e^x key on a calculator). An odds ratio of 1.25 means a one-unit increase in that predictor multiplies the odds of the outcome by 1.25, a 25% increase in odds, holding everything else fixed. An odds ratio below 1 means the predictor makes the outcome less likely; exactly 1 means no effect at all.

There is no single, universally agreed 'R-squared' for a model like this the way there is for ordinary regression: there are several competing 'pseudo-R-squared' measures that each answer a slightly different question and will not agree with each other, which is expected rather than a sign something is wrong. Read them together for a rounded picture, not one in isolation as though it were THE fit statistic.

Logistic regression is also called a logit model, because it models the log-odds, or logit, of the outcome. ROC-AUC measures how well the predicted probabilities rank the 'yes' rows above the 'no' rows: 0.5 is no better than guessing and 1 is perfect ranking.

Practitioner explanation

KernelStats fits via maximum likelihood, reports odds ratios OR_j = exp(beta_j) with Wald confidence intervals (beta_hat +/- z*SE, then exponentiated, matching R's confint.default(), NOT R's profile-likelihood confint(), which can differ noticeably in small samples or near separation; the product states this distinction explicitly rather than leaving a silent cross-check discrepancy). FOUR pseudo-R-squared measures are reported side by side because none of them is 'the' answer: McFadden's (1 - lnL_full/lnL_null, typically the lowest of the four, with 0.2-0.4 already considered an excellent fit, a much lower bar than OLS R-squared), Tjur's (the gap between mean predicted probability in the actual-positive group versus the actual-negative group, needing no likelihoods and the easiest to explain), Cox & Snell's (which cannot reach 1 even for a perfect model; its ceiling for a balanced outcome is around 0.75), and Nagelkerke's (Cox & Snell rescaled by exactly that ceiling, so it spans 0-1 but adds no new information, only a familiar scale).

Linearity of the logit is ACTIVELY TESTED, not assumed: a squared term is added for each continuous predictor and likelihood-ratio-tested against the fitted model (a Box-Tidwell-style check), reported as linearity_lrt_stat/p. Perfect (or near-perfect) separation is detected automatically (the separation flag on the card) and the affected coefficient's odds ratio is reported as NOT AVAILABLE rather than an arbitrary diverged huge number, because the MLE has no finite solution in that case. An EXACTLY redundant predictor (a duplicated column, or the same measurement in two units) is rejected outright rather than fitted, since the effect would otherwise be split arbitrarily between the aliased columns.

Categorical predictors are dummy-coded against an explicitly named reference level, and the product states directly that a coefficient is a contrast AGAINST THAT REFERENCE LEVEL specifically, not against 'the average' and not against the other levels pooled together, so comparing two non-reference levels directly requires refitting with a different reference. Classification metrics (confusion matrix, accuracy) use a 0.5 probability threshold by default, while ROC-AUC summarizes ranking performance across ALL thresholds simultaneously (0.5 = chance, 1.0 = perfect), and the Learn More content states plainly that a statistically SIGNIFICANT model (via the likelihood-ratio test) is not automatically an ACCURATE one: with large n, the LR test rejects for tiny effects, so odds ratios, ROC-AUC, and the confusion matrix are what determine practical usefulness.

When to use it

Consider it when

The outcome is binary (yes/no, success/failure, two-level categorical or 0/1), and you want each predictor's effect quantified as an odds ratio along with classification performance (accuracy, ROC-AUC).

Use caution when

The outcome has more than two categories (use multinomial or ordinal logistic regression), the outcome is continuous (use linear regression), or a predictor perfectly separates the classes and its coefficient has diverged; the card will flag this and report no odds ratio for that term rather than a meaningless huge number.

Formulas and symbols

Logistic (logit) model

log⁡ ⁣(p1−p)=β0+β1X1+β2X2+…\log\!\left(\dfrac{p}{1-p}\right) = \beta_0 + \beta_1 X_1 + \beta_2 X_2 + \dots

p the probability the outcome equals 1 (the positive class); beta_j the change in log-odds per one-unit increase in X_j.

Odds ratio

ORj=eβjOR_j = e^{\beta_j}

OR > 1 increases the odds of the outcome; OR < 1 decreases them; OR = 1 no effect.

McFadden's pseudo-R-squared

RMcF2=1−ln⁡Lfullln⁡LnullR^2_{\text{McF}} = 1 - \dfrac{\ln L_{\text{full}}}{\ln L_{\text{null}}}

Compares the fitted model's log-likelihood to an intercept-only null model; typically much lower than OLS R-squared for a comparably 'good' fit.

Assumptions and checks

  • Linearity. Logistic regression assumes the predictors' effect is linear on the LOG-ODDS scale; if a continuous predictor's true relationship with the log-odds curves, the fitted coefficient is a compromise that misrepresents the effect at both ends of its range.

    In KernelStats: A Box-Tidwell-style check: a squared term for each continuous predictor is added and the augmented model is compared with the fitted one by a likelihood-ratio test (squared terms, not the classical x*ln(x) terms), reported directly on the card; reported as not testable when the augmented model fails to converge, and not applicable when every predictor is categorical.

  • Perfect or quasi-perfect separation (a predictor, or combination of predictors, perfectly predicts the outcome for some rows) sends the maximum-likelihood coefficient estimates and their standard errors toward infinity, so the reported coefficients and p-values are not trustworthy even though the optimizer may still return a number.

    In KernelStats: Checked directly: the card detects possible perfect/quasi separation and flags it as a failed 'No perfect separation' check rather than silently reporting an enormous, meaningless coefficient.

  • Independence of observations. Logistic regression assumes each row's outcome is independent given the predictors; clustered or repeated-measures data (multiple rows per subject) understate the true standard errors and overstate significance.

    In KernelStats: Not checked by KernelStats: no clustering or repeated-measures diagnostic is run; a clustered design needs a mixed-effects or GEE approach instead.

  • With a severely imbalanced outcome (the positive class a small or large minority of rows), overall accuracy is a misleading summary of model quality: a model that always predicts the majority class can score high accuracy while being useless for the minority class.

    In KernelStats: Checked directly: the card flags a positive-class rate below 10% or above 90% and recommends relying on ROC-AUC rather than accuracy.

Alternatives

  • Multinomial Logistic Regression. The outcome has more than two unordered categories.
  • Ordinal Logistic Regression. The outcome has more than two categories with a natural order (e.g. low/medium/high).
  • Linear Regression. The outcome is actually continuous rather than binary.

Worked example

Scenario

Predicting penguin sex from body mass and bill depth, with species as a categorical covariate (reference level: Adelie).

Result

Positive class = male. Body mass OR = 1.0063 per gram (95% CI [1.0045, 1.0082], p < .001), i.e. about 1.88 per extra 100 g; bill depth OR = 5.44 per mm (95% CI [3.07, 9.65], p < .001); McFadden's R-squared = 0.65 (Tjur 0.71, Nagelkerke 0.79); ROC-AUC = 0.96; accuracy = 90% at the 0.5 threshold. The linearity-of-the-logit check flags curvature (LR test p = .038).

Interpretation

Both predictors are strongly associated with sex after adjusting for species, and the high ROC-AUC says the model ranks males above females well across all thresholds, not just at 0.5; McFadden's 0.65 is far above the 0.2-0.4 range usually called excellent for that measure. Two cautions travel with it: the AUC and accuracy are computed on the same rows the model was fit to, and the flagged linearity check suggests trying a curved term for body mass or bill depth before quoting a single per-unit odds ratio.

Dataset: penguins.csv

Common misconceptions

Misconception

The four pseudo-R-squared measures should agree with each other, and disagreement means something is wrong with the fit.

Correction

McFadden's, Tjur's, Cox & Snell's, and Nagelkerke's pseudo-R-squared measures answer genuinely DIFFERENT questions and are expected to give different numbers on the exact same fitted model: Nagelkerke reads highest of the four and McFadden usually lowest, by construction, not by coincidence. None of them is literally 'the proportion of variance explained' the way OLS R-squared is; that phrase belongs to linear regression, and these are analogies useful mainly for comparing models fit to the same outcome and rows, not as an absolute grade.

Misconception

A statistically significant overall model (a significant likelihood-ratio test) means the model is accurate and useful for prediction.

Correction

With a large enough sample, the overall likelihood-ratio test rejects the null for even tiny, practically unimportant effects. Statistical significance of the OVERALL model says nothing about whether it usefully discriminates or predicts; check the odds ratios, the ROC-AUC, and the confusion matrix to judge practical usefulness.

Read more: Misconception: statistical significance means practical importance

References

  1. Applied Logistic Regression, Hosmer, D. W., Lemeshow, S. and Sturdivant, R. X. (2013).
  2. Coefficients of determination in logistic regression models, Tjur, T. (2009), The American Statistician, 63(4), 366-372.