Book contents

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 [0,1][0,1] 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 YiY_i be a binary variable taking values 0 or 1. Let 𝐱i\mathbf{x}_i be a vector of predictors. The logistic regression model specifies logit(P(Yi=1|𝐱i))=logP(Yi=1|𝐱i)1−P(Yi=1|𝐱i)=β0+𝛃⊤𝐱i.\mathrm{logit}(P(Y_i=1|\mathbf{x}_i)) = \log\frac{P(Y_i=1|\mathbf{x}_i)}{1 - P(Y_i=1|\mathbf{x}_i)} = \beta_0 + \boldsymbol{\beta}^\top \mathbf{x}_i. Equivalently, the probability of success is P(Yi=1|𝐱i)=exp(β0+𝛃⊤𝐱i)1+exp(β0+𝛃⊤𝐱i).P(Y_i=1|\mathbf{x}_i) = \frac{\exp(\beta_0 + \boldsymbol{\beta}^\top \mathbf{x}_i)}{1 + \exp(\beta_0 + \boldsymbol{\beta}^\top \mathbf{x}_i)}. The coefficients βj\beta_j represent log–odds ratios; exponentiating them yields the odds ratio associated with a one–unit change in XjX_j, holding other variables constant.

Maximum Likelihood Estimation

Logistic regression parameters are estimated by maximising the log–likelihood ℓ(𝛃)=∑i=1n[yi(β0+𝛃⊤𝐱i)−log(1+eβ0+𝛃⊤𝐱i)].\ell(\boldsymbol{\beta}) = \sum_{i=1}^n \bigl[y_i (\beta_0 + \boldsymbol{\beta}^\top \mathbf{x}_i) - \log(1 + e^{\beta_0 + \boldsymbol{\beta}^\top \mathbf{x}_i})\bigr]. 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 (Y=1Y=1) 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

Diagnostics and Model Assessment

Assessing logistic regression includes evaluating overall fit, detecting outliers and assessing predictive performance. Key tools are:

Exercises

Practice

  1. 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.

  2. Simulate a binary outcome with two predictors and known coefficients. Fit the logistic model and check whether the estimates are unbiased across repeated simulations.

  3. Derive the likelihood equations for logistic regression and explain why no closed–form solution exists. Outline the Newton–Raphson updating step.

  4. Fit a logistic regression in Julia using the GLM.jl package and compare the results with R and Python for the same data.

References