Logistic regression

Analytika TeamLast reviewed 10 min read

When the outcome is binary (passed/failed, recovered/didn’t, bought/didn’t), linear regression doesn’t work: it can predict probabilities below 0 or above 1, and its assumptions fail. Logistic regression is the standard tool. It’s one of the most widely used models in medicine, social science and machine learning.

The model

log(p / (1 − p)) = b₀ + b₁x₁ + b₂x₂ + …

p is the probability of the outcome, and p / (1 − p) is the odds. A probability of .75 means odds of 3 (3 to 1). The left side, the log-odds or logit, can take any value, so it can be modelled as a straight line. The S-shaped curve this produces keeps predicted probabilities between 0 and 1.

Odds ratios

Raw coefficients are changes in log-odds, which are hard to interpret. Exponentiating a coefficient gives an odds ratio (OR): the factor by which the odds multiply for a one-unit increase in the predictor, holding the others constant.

  • OR = 1.5: the odds are 50% higher per unit.
  • OR = 0.8: the odds are 20% lower per unit.
  • OR = 1: no association. A confidence interval that includes 1 means no evidence of one.

An odds ratio isn’t a risk ratio. “Twice the odds” only means roughly “twice the probability” when the outcome is rare.

Assumptions

  • A binary outcome, with independent observations.
  • Linearity between each continuous predictor and the log-odds.
  • No severe multicollinearity (check VIF).
  • Enough events: a common rule of thumb is at least 10 cases of the rarer outcome per predictor.
  • No perfect separation, where a predictor perfectly splits the outcomes. Coefficients then run off towards infinity.

Worked example: what predicts a manual transmission?

In R’s mtcars data, 13 of 32 cars have manual transmissions (am = 1). We predict this from weight (1,000 lb) and horsepower.

Analytika engine output (abridged; fit line rounded to 4 dp and wrapped)
Model: Logistic regression
Outcome: am (modelling P(am = 1))
N: 32
Pseudo R-squared: 0.7673
LLR chi-squared: 33.1706 (df=2)
LLR P-value: 6.267e-08
=== COEFFICIENTS ===
const: coef=18.8663, se=7.4436, z=2.5346, p=0.0113, or=156145483.5063, ci=[4.2772, 33.4554]
wt: coef=-8.0835, se=3.0687, z=-2.6342, p=0.0084, or=0.0003, ci=[-14.0980, -2.0690]
hp: coef=0.0363, se=0.0177, z=2.0444, p=0.0409, or=1.0369, ci=[0.0015, 0.0710]
LOGIT_FIT: {"cox_snell_r2": 0.6453, "nagelkerke_r2": 0.8709, "aic": 16.0591, "bic": 20.4563,
  "classification": {"cutoff": 0.5, "tp": 12, "tn": 18, "fp": 1, "fn": 1, "accuracy": 0.9375,
  "sensitivity": 0.9231, "specificity": 0.9474}, "hl_chi2": 4.9517, "hl_df": 8, "hl_p": 0.7627}
R 4.6 output
> summary(glm(am ~ wt + hp, family = binomial, data = mtcars))$coefficients
              Estimate Std. Error   z value    Pr(>|z|)
(Intercept) 18.8662987 7.44355806  2.534581 0.011258199
wt          -8.0834752 3.06867511 -2.634191 0.008433813
hp           0.0362556 0.01773415  2.044394 0.040914646

The same coefficients, standard errors and p-values. Analytika’s test suite also checks the fit statistics (pseudo R², AIC, BIC, Hosmer-Lemeshow) against R.

Overall fit

  • Likelihood-ratio test: χ²(2) = 33.17, p < .001. The model predicts much better than one with no predictors.
  • Pseudo R²: McFadden = .77, Nagelkerke = .87, both very high (see R-squared).
  • Hosmer-Lemeshow: χ²(8) = 4.95, p = .763. No evidence that predicted probabilities are miscalibrated.
  • Classification at a 0.5 cut-off: 30 of 32 cars correct (93.8%), with sensitivity 92.3% and specificity 94.7%.

Coefficients as odds ratios

  • Weight: b = −8.08, p = .008. Per 1,000 lb the OR is 0.0003, an unhelpfully extreme number because 1,000 lb is a big step. Per 100 lb, OR = 0.45, 95% CI [0.24, 0.81]. Each extra 100 lb is associated with roughly halving the odds of a manual transmission, holding horsepower constant.
  • Horsepower: b = 0.036, p = .041. Per 10 hp, OR = 1.44, 95% CI [1.02, 2.03]. At the same weight, more powerful cars had higher odds of being manual.

Choosing a sensible unit for each predictor (100 lb, 10 hp) makes odds ratios far easier to interpret. The p-value doesn’t change.

A caution about this example

With only 32 cars and 13 manuals, there are about 6 events per predictor, below the usual guideline, and the confidence intervals are wide. A study designed to answer this question would need many more cases.

APA write-up

A logistic regression predicted transmission type (manual vs automatic) from weight and horsepower, χ²(2) = 33.17, p < .001, Nagelkerke R² = .87. Holding horsepower constant, each additional 100 lb was associated with lower odds of a manual transmission, OR = 0.45, 95% CI [0.24, 0.81], p = .008. Holding weight constant, each additional 10 hp was associated with higher odds, OR = 1.44, 95% CI [1.02, 2.03], p = .041.

Beyond yes/no

  • Ordinal logistic regression: ordered outcomes (low / medium / high).
  • Multinomial logistic regression: unordered categories with more than two levels.

Analytika fits all three, choosing the family from the outcome’s type. See regression analysis.

How to run it

  • SPSS: Analyze → Regression → Binary Logistic. Request CI for exp(B) and the Hosmer-Lemeshow test under Options.
  • R: glm(y ~ x1 + x2, family = binomial, data = df), then exp(coef(fit)) and exp(confint.default(fit)).
  • Python: statsmodels.formula.api.logit("y ~ x1 + x2", data=df).fit()
  • Analytika: ask what predicts a yes/no outcome. It fits the model and reports odds ratios, fit statistics, classification and diagnostics.

More in Regression

Try it with the free tools

Run this on your own data

Upload a dataset and ask your question in plain English. Analytika checks the assumptions, picks the test, runs it, and writes the results up in APA format.

Try it free