PUBH-311 · Lesson 10 of 12 · 20 min
Logistic regression
▶ Video coming soon A video version of this lesson is on the way.
The Kestrel Valley diabetes prevention working group wanted to know which adults to invite to a new lifestyle program. The survey recorded whether each respondent had ever been told by a health professional that they had diabetes, along with age, BMI, physical activity and much else. Jonah’s first instinct was to use multiple linear regression with diabetes coded 0 or 1 as the outcome. The model produced predicted “probabilities” of −0.04 for young, lean, active respondents and coefficients that assumed every risk factor added the same fixed amount of risk at every level of the others. Dr. Osei suggested logistic regression, the standard tool for binary outcomes, and showed him how to turn its coefficients into odds ratios that the working group could understand.
Binary outcomes are central to public health: disease or no disease, vaccinated or not, survived or died. This lesson explains why linear regression is unsuitable for them, introduces the logistic model, shows how to interpret its coefficients as odds ratios, how to obtain predicted probabilities, how to assess model fit and discrimination, and how odds ratios relate to risk ratios. Like multiple linear regression, logistic regression adjusts for several variables at once, so it is the most widely used method for controlling confounding in epidemiological studies with binary outcomes.
Why not linear regression?
Fitting a straight line to a 0/1 outcome, sometimes called a linear probability model, has three problems. Predicted values can fall below 0 or above 1, which is impossible for probabilities. The variance of a binary outcome depends on its mean, \(p(1-p)\), so the equal-variance assumption is violated. And the relationship between a risk factor and a probability is rarely a straight line: as risk approaches 0 or 1, it must flatten. These problems are solved by modeling not the probability itself but a transformation of it.
Odds and the logit
Recall that the odds of an event are \(p/(1-p)\). A probability of 0.20 corresponds to odds of 0.20/0.80 = 0.25, often expressed as "1 to 4." A probability of 0.50 gives odds of 1, and as the probability approaches 1 the odds grow without limit. To convert odds back to a probability, use \(p = \text{odds}/(1 + \text{odds})\).
The logit is the natural logarithm of the odds:
While a probability is confined between 0 and 1, the logit can take any value from minus infinity to plus infinity. A probability of 0.5 has a logit of 0; probabilities below 0.5 have negative logits and those above 0.5 positive logits. Because the logit is unbounded, it can safely be modeled as a linear function of predictors.
The logistic regression model
Logistic regression models the log odds of the outcome as a linear function of the predictors:
Solving for \(p\) gives the logistic function, which produces the characteristic S-shaped curve:
However large or small the right-hand side becomes, \(p\) stays between 0 and 1. The coefficients are estimated not by least squares but by maximum likelihood: software searches for the values of the coefficients that make the observed pattern of 0s and 1s most probable. The method was developed for this purpose in the mid-20th century, with the statistician David Cox making important contributions in 1958, and became standard in epidemiology once computers made the calculations routine. Its widespread adoption owes much to the Framingham Heart Study, which used multivariable logistic models in the 1960s to estimate the risk of coronary heart disease from several risk factors simultaneously.
Interpreting coefficients as odds ratios
A coefficient \(\beta_j\) is the change in the log odds of the outcome for a one-unit increase in \(x_j\), holding the other predictors constant. Log odds are hard to think about, so we exponentiate: \(e^{\beta_j}\) is the odds ratio for a one-unit increase in \(x_j\). If \(\beta = 0\), the odds ratio is \(e^0 = 1\), meaning no association. Positive coefficients give odds ratios above 1 (higher odds) and negative coefficients give odds ratios below 1 (lower odds).
A single binary predictor
With one binary predictor, logistic regression reproduces the odds ratio from a two-by-two table exactly. In the Kestrel Valley survey (invented data), 45 of 300 respondents with obesity (BMI 30 or more) and 54 of 900 without obesity reported diagnosed diabetes.
| Diabetes | No diabetes | Total | Risk | |
|---|---|---|---|---|
| BMI 30 or more | 45 | 255 | 300 | 0.15 |
| BMI under 30 | 54 | 846 | 900 | 0.06 |
The odds ratio from the table is \((45 \times 846)/(255 \times 54) = 2.76\). A logistic regression of diabetes on an obesity indicator gives an intercept of −2.75 and a coefficient of 1.017. The intercept is the log odds in the reference group: \(e^{-2.75} = 0.064\), which is 54/846. The coefficient exponentiates to \(e^{1.017} = 2.76\), the same odds ratio. The 95 percent CI, calculated as \(e^{\beta \pm 1.96 \times SE}\) with SE 0.214, is 1.82 to 4.21. Note that the risk ratio, 0.15/0.06 = 2.5, is smaller than the odds ratio, because diabetes is not rare in this population.
Several predictors
The real value of logistic regression is adjustment. Table 2 shows a model with three predictors.
| Predictor | Coefficient (β) | Std. error | Odds ratio (e^β) | 95 percent CI for OR | p-value |
|---|---|---|---|---|---|
| Intercept | −5.20 | 0.42 | |||
| Age (per 10 years) | 0.47 | 0.06 | 1.60 | 1.42 to 1.80 | < 0.001 |
| BMI 30 or more (vs under 30) | 0.92 | 0.16 | 2.51 | 1.83 to 3.43 | < 0.001 |
| Physically active (vs inactive) | −0.36 | 0.15 | 0.70 | 0.52 to 0.94 | 0.016 |
Each odds ratio is adjusted for the other variables. Among people of the same obesity status and activity level, each additional 10 years of age multiplies the odds of diabetes by 1.60 (a 60 percent increase in odds). Among people of the same age and activity, obesity is associated with 2.51 times the odds of diabetes. This adjusted odds ratio is smaller than the crude 2.76 because people with obesity in the survey were, on average, a little older and less active. Physically active people had 0.70 times the odds of inactive people of the same age and obesity status, a 30 percent reduction in odds.
Because the model is linear on the log odds scale, odds ratios multiply. For a 20-year age difference, the odds ratio is \(1.60^2 = 2.56\), or equivalently \(e^{2 \times 0.47} = 2.56\). The odds ratio for a 70-year-old obese person compared with a 50-year-old non-obese person, with the same activity, is \(2.56 \times 2.51 = 6.4\).
The units of numerical predictors matter. A coefficient per year of age would be one-tenth of the per-decade coefficient, and its odds ratio would be \(e^{0.047} = 1.05\). Authors often rescale predictors (per 10 years, per 5 kg/m², per standard deviation) to make odds ratios meaningful. Always check the unit before interpreting.
Predicted probabilities
To obtain a predicted probability for a particular person, calculate the linear predictor (the log odds) and apply the logistic function.
Worked problem
Predicted probability of diabetes
Using Table 2, find the predicted probability of diabetes for a physically inactive 60-year-old with BMI 30 or more, and for an inactive 60-year-old with BMI under 30.
Show solution
- Age 60 is 6 decades, so the age term is 0.47 × 6 = 2.82.
- With obesity: log odds = −5.20 + 2.82 + 0.92 = −1.46. Odds = \(e^{-1.46} = 0.232\). Probability = 0.232/1.232 = 0.188.
- Without obesity: log odds = −5.20 + 2.82 = −2.38. Odds = \(e^{-2.38} = 0.093\). Probability = 0.093/1.093 = 0.085.
- Ratio of probabilities (risk ratio at this age): 0.188/0.085 = 2.2, smaller than the odds ratio of 2.51.
Answer: Predicted probabilities are about 19 percent with obesity and 8.5 percent without. Expressing results as predicted probabilities for typical people is often clearer for decision makers than odds ratios.
Figure 1 illustrates an important point. The odds ratio for obesity is the same, 2.51, at every age, but the absolute difference in probability grows from about 3 percentage points at age 30 to more than 20 points at age 90. A constant relative effect implies a larger absolute effect where baseline risk is higher, which is why prevention programs targeted at higher-risk groups prevent more cases per person reached.
Odds ratios and risk ratios
Logistic regression gives odds ratios, but people intuitively think in terms of risk. When the outcome is rare (under about 10 percent in all groups), the odds ratio approximates the risk ratio and the distinction matters little. When the outcome is common, the odds ratio is further from 1 than the risk ratio, and describing an odds ratio of 2.5 as "2.5 times the risk" overstates the effect. A simple approximate conversion, proposed by Jun Zhang and Kai Yu in 1998, is
where \(p_0\) is the risk in the reference group. With OR = 2.51 and \(p_0 = 0.085\), RR ≈ 2.51/1.128 = 2.2, matching the ratio of predicted probabilities above. When adjusted risk ratios are needed directly, analysts can use log-binomial regression or Poisson regression with robust standard errors, an approach popularized by Guangyong Zou in 2004 for common binary outcomes in cohort studies and trials.
Assessing a logistic model
There is no exact equivalent of R² for logistic regression, although several pseudo-R² measures exist. Two other properties are more useful.
Discrimination is the model’s ability to separate people who have the outcome from those who do not. It is measured by the c-statistic, which equals the area under the receiver operating characteristic (ROC) curve. The c-statistic is the probability that a randomly chosen person with the outcome has a higher predicted probability than a randomly chosen person without it. A value of 0.5 means no better than chance; 1.0 means perfect separation. Values around 0.7 to 0.8 are typical of useful clinical and public health risk scores.
Calibration is agreement between predicted probabilities and observed proportions. If the model predicts a 20 percent risk for a group of people, about 20 percent of them should have the outcome. Calibration is checked by grouping people by predicted risk (often into tenths) and comparing predicted and observed proportions in a calibration plot; the Hosmer-Lemeshow test is a formal but limited version of this comparison. A model can discriminate well but be poorly calibrated, for example when applied to a population with a different baseline risk from the one in which it was developed.
Likelihood ratio tests compare nested models, as partial F-tests do in linear regression, and are the preferred way to test whether a group of variables, such as several dummies for income, improves the model. Individual coefficients are usually tested with Wald tests, the coefficient divided by its standard error, compared with the standard normal distribution.
Sample size and practical issues
Logistic regression needs enough events, meaning people with the less common outcome. A long-standing rule of thumb, based on simulation studies by Peter Peduzzi and colleagues in 1996, is at least 10 events per predictor variable (counting each dummy). Table 2 has 118 events and three predictors, comfortably within the rule. More recent work suggests that the number needed depends on the context and is often larger for prediction models. With too few events, coefficients are biased away from zero and confidence intervals unreliable.
Other issues mirror linear regression: choose covariates on subject-matter grounds for explanatory models, beware of multicollinearity, check whether numerical predictors relate linearly to the log odds (by adding squared terms or splines, or by examining categories), consider interaction terms where justified, and account for clustering. A particular problem is separation, when a predictor perfectly predicts the outcome in the sample (for example, all 12 people with a rare exposure have the disease); maximum likelihood estimates then become infinite, and penalized methods are needed.
Case-control studies
Logistic regression has a special role in case-control studies, in which people are selected because they do or do not have the outcome. Because the investigator fixes the ratio of cases to controls, risks cannot be estimated, but odds ratios for exposures can be. Logistic regression applied to case-control data gives valid odds ratios for the exposures, although the intercept does not reflect population risk. Matched case-control studies use conditional logistic regression. Epidemiologic Methods discusses these designs in detail.
Back to the working group
Dr. Osei presented the working group with the adjusted odds ratios and, more usefully, a table of predicted probabilities by age group, obesity and activity. The highest predicted probabilities, above 20 percent, were among inactive adults over 60 with obesity, and the group used this to set invitation criteria. She warned that the survey measured diagnosed diabetes only, so undiagnosed cases were missed, and that a model developed in survey data should be checked before being used to screen individuals.
Common mistakes
MistakeUsing linear regression for a binary outcome.
InsteadUse logistic regression (or a model for risk ratios) so predictions stay between 0 and 1.
MistakeReporting logistic coefficients without exponentiating them.
InsteadReport e^β as odds ratios with confidence intervals, and state the unit of each predictor.
MistakeDescribing an odds ratio as a risk ratio when the outcome is common.
InsteadCall it an odds ratio, convert it, or estimate risk ratios directly with suitable models.
MistakeFitting many predictors with few events.
InsteadKeep roughly 10 or more events per predictor and pre-specify variables.
MistakeAssuming a model with a good c-statistic will give accurate risks in any population.
InsteadCheck calibration as well as discrimination, especially in a new population.
Summary and key terms
- Linear regression is unsuitable for binary outcomes because predictions can fall outside 0 to 1 and the variance is not constant.
- Logistic regression models the log odds (logit) of the outcome as a linear function of predictors; coefficients are estimated by maximum likelihood.
- e^β is the odds ratio for a one-unit increase in a predictor, adjusted for the others; odds ratios multiply across units and predictors.
- Predicted probabilities come from applying the logistic function to the linear predictor and are often clearer than odds ratios.
- Odds ratios exceed risk ratios when outcomes are common; convert or use log-binomial or robust Poisson models for risk ratios.
- Discrimination (c-statistic) and calibration both matter; likelihood ratio tests compare nested models.
- Aim for at least about 10 events per predictor; logistic regression gives valid odds ratios in case-control studies.
- Logit
- The natural logarithm of the odds, ln(p/(1 − p)).
- Logistic regression
- A regression model for a binary outcome in which the log odds is a linear function of predictors.
- Maximum likelihood
- An estimation method that chooses parameter values making the observed data most probable.
- Adjusted odds ratio
- An odds ratio from a model that holds other predictors constant.
- Predicted probability
- The model’s estimated probability of the outcome for given predictor values.
- c-statistic
- The area under the ROC curve; the probability that a random case has a higher predicted risk than a random non-case.
- Calibration
- Agreement between predicted probabilities and observed outcome proportions.
- Events per variable
- The number of outcome events divided by the number of predictor parameters in a model.
- Conditional logistic regression
- Logistic regression for matched case-control data.
Practice problems
Work each problem on paper first, then reveal the answer to check.
A logistic coefficient for current smoking is 0.69. What is the odds ratio and how would you interpret it?
Show answer
OR = e^0.69 ≈ 2.0. Smokers have about twice the odds of the outcome as non-smokers with the same values of the other predictors.
Convert a probability of 0.25 to odds, and odds of 1.5 to a probability.
Show answer
Odds = 0.25/0.75 = 0.33. Probability = 1.5/2.5 = 0.60.
Using Table 2, what is the odds ratio for a 30-year difference in age?
Show answer
e^(3 × 0.47) = e^1.41 ≈ 4.1 (or 1.60³ ≈ 4.1).
A study with 60 events fits a logistic model with 12 predictor parameters. What concern arises?
Show answer
Only 5 events per variable, below the usual guideline of about 10, so coefficients may be biased and unstable; the model should be simplified.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.