STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 18
Logistic Regression
When the response variable is binary (two outcomes such as success/failure), linear regression is inappropriate because predicted values can lie outside and error variance is heteroscedastic. Logistic regression models the log–odds of the probability of success as a linear function of predictors. This chapter introduces the model, estimation, interpretation, diagnostics and applications.
The Model
Let be a binary variable taking values 0 or 1. Let be a vector of predictors. The logistic regression model specifies Equivalently, the probability of success is The coefficients represent log–odds ratios; exponentiating them yields the odds ratio associated with a one–unit change in , holding other variables constant.
Maximum Likelihood Estimation
Logistic regression parameters are estimated by maximising the log–likelihood Numerical optimisation is required; software uses iterative algorithms such as Newton–Raphson or iteratively reweighted least squares. Standard errors are obtained from the observed information matrix.
Example: Medical Risk Factors
Suppose we study whether patients develop a disease () based on age and cholesterol level. We fit a logistic regression model.
Worked example
Logistic regression in R, Python and Julia.
import pandas as pd
import statsmodels.api as sm
data = pd.DataFrame({
'disease': [1,0,1,0,1,0,0,1],
'age': [65,50,70,45,80,55,60,75],
'chol': [220,180,250,190,260,200,210,240]
})
X = sm.add_constant(data[['age','chol']])
model = sm.Logit(data['disease'], X).fit()
print(model.summary())
df <- data.frame(disease = c(1,0,1,0,1,0,0,1),
age = c(65,50,70,45,80,55,60,75),
chol= c(220,180,250,190,260,200,210,240))
fit <- glm(disease ~ age + chol, data=df, family=binomial)
summary(fit)
using DataFrames, GLM
df = DataFrame(disease = [1,0,1,0,1,0,0,1],
age = [65,50,70,45,80,55,60,75],
chol = [220,180,250,190,260,200,210,240])
glm_fit = glm(@formula(disease ~ age + chol), df, Binomial(), LogitLink())
coef(glm_fit)
These commands fit the logistic model and print coefficient estimates and standard errors. Exponentiating a coefficient gives the odds ratio for that predictor.
Interpretation
A coefficient corresponds to an odds ratio : the multiplicative change in the odds of for a one–unit increase in , controlling for other covariates.
Confidence intervals for odds ratios are obtained by exponentiating the interval for .
The intercept gives the log–odds of disease when all predictors are zero; interpret carefully.
Diagnostics and Model Assessment
Assessing logistic regression includes evaluating overall fit, detecting outliers and assessing predictive performance. Key tools are:
Deviance: the likelihood ratio statistic comparing the fitted model to the saturated model. The residual deviance should be compared to the degrees of freedom.
ROC Curve: plots sensitivity vs 1-specificity at different thresholds. The area under the curve (AUC) quantifies discrimination.
Hosmer–Lemeshow Test: groups predicted probabilities and compares observed vs expected counts. A significant result indicates lack of fit.
Exercises
Practice
Using a dataset on heart disease, fit a logistic regression predicting disease status from age, cholesterol and blood pressure. Interpret the odds ratios. Evaluate the model using the ROC curve and compute the AUC.
Simulate a binary outcome with two predictors and known coefficients. Fit the logistic model and check whether the estimates are unbiased across repeated simulations.
Derive the likelihood equations for logistic regression and explain why no closed–form solution exists. Outline the Newton–Raphson updating step.
Fit a logistic regression in Julia using the
GLM.jlpackage and compare the results with R and Python for the same data.
References
Agresti A. An Introduction to Categorical Data Analysis. 3rd ed. Wiley; 2019.
Hosmer DW, Lemeshow S, Sturdivant RX. Applied Logistic Regression. 3rd ed. Wiley; 2013.
Menard S. Applied Logistic Regression Analysis. 3rd ed. Sage; 2010.