01 / 20
S

Logistic Regression
with glm() in R

Learn to fit and interpret binary classification models using R's glm() function with family = binomial.

R Programming

What Is Logistic Regression?

Logistic regression is a classification algorithm that predicts the probability of a binary outcome (yes/no, pass/fail, 0/1). Unlike linear regression, which outputs any real number, logistic regression outputs a probability between 0 and 1.

Outcome
Binary: 0 or 1
Output
Probability \( \in [0,1] \)
Link
Logit (log-odds)

The Sigmoid Function

The sigmoid (logistic) function transforms any real-valued input into a probability:

0 0.5 1 0 -4 4 \( \sigma(z) \) \( z \)

Why Not Linear Regression?

When the outcome is binary, a straight line is a poor model. It can predict values below 0 or above 1, which don't make sense as probabilities.

0 0.5 1 Linear (bad) Logistic (good)

The Logit Link Function

Logistic regression models the log-odds of the outcome as a linear function of predictors. The logit function is the inverse of the sigmoid:

\( p \)
Probability of success: \( P(Y=1) \)
\( \frac{p}{1-p} \)
Odds of success (always > 0)
\( \ln(\text{odds}) \)
Log-odds: can be any real number

R's glm() Function

In R, glm() stands for Generalized Linear Model. To fit a logistic regression, you set family = binomial:

# Basic syntax glm(formula, data, family = binomial) # Example: predict admission from GPA model <- glm(admitted ~ gpa, data = students, family = binomial)

lm() vs glm()

lm() glm(..., binomial)
Outcome Type Continuous Binary (0/1)
Output Range \( (-\infty, +\infty) \) \( [0, 1] \)
Link Function Identity Logit
Loss Function Least Squares Max Likelihood
Coefficients Mean Change in \( Y \) Change in log-odds
Fit Measure \( R^2 \) AIC, Deviance

Step-by-Step Workflow

1
Prepare your data. Ensure your response variable is coded as 0/1 or a two-level factor.
2
Fit the model. Use glm(y ~ x1 + x2, data, family = binomial).
3
Check summary(). Look at coefficients, z-values, p-values, and AIC.
4
Interpret coefficients. Each \( \beta \) represents a change in log-odds. Exponentiate with exp(coef()) for odds ratios.
5
Predict. Use predict(model, type = "response") to get probabilities on the \([0,1]\) scale.

Example: Fitting the Model

Predict whether a student passes an exam (1 = pass, 0 = fail) based on hours studied:

# Load data exam <- data.frame( hours = c(1, 2, 3, 4, 5, 6, 7, 8), passed = c(0, 0, 0, 1, 0, 1, 1, 1) ) # Fit logistic regression fit <- glm(passed ~ hours, data = exam, family = binomial) summary(fit)

Reading summary() Output

# Key output from summary(fit): # Estimate Std.Error z value Pr(>|z|) # (Intercept) -3.174 1.930 -1.64 0.100 # hours 0.732 0.400 1.83 0.067 . # # AIC: 11.23
Intercept: \(-3.17\)
Log-odds of passing when hours = 0
hours: \(0.73\)
Each extra hour increases log-odds by 0.73
p-values > 0.05
Coefficients not significant at the α=0.05 level.

Interpreting Coefficients

Coefficients in logistic regression are on the log-odds scale. To get odds ratios, exponentiate them:

# Get odds ratios exp(coef(fit)) # (Intercept) hours # 0.0418 2.079 # Confidence intervals for odds ratios exp(confint(fit))

Making Predictions

# Predict probability for a student who studied 5 hours new_data <- data.frame(hours = 5) predict(fit, new_data, type = "response") # [1] 0.619 <-- 61.9% chance of passing # Without type="response", you get log-odds: predict(fit, new_data) # [1] 0.486 <-- log-odds scale

Adding Multiple Predictors

Just like lm(), you can include multiple predictors and even interaction terms:

# Multiple predictors fit2 <- glm(admitted ~ gpa + gre + rank, data = grad_school, family = binomial) # With interaction fit3 <- glm(admitted ~ gpa * gre, data = grad_school, family = binomial)
+ Additive
y ~ x1 + x2: independent effects
* Interaction
y ~ x1 * x2: includes x1:x2 term

The Full Pipeline

Data y = 0/1, x glm() family = binomial Coefficients summary() exp(coef()) Predict type="response" Interpret Odds Ratios

Common Mistakes

✕ Forgetting family = binomial
Writing glm(y ~ x, data) without specifying family runs a linear regression (Gaussian family), not logistic.
✕ Omitting type = "response"
predict() without type = "response" returns log-odds, not probabilities. Values outside [0,1] are a giveaway.
✕ Interpreting coefficients directly
A coefficient of 1.5 does NOT mean "Y increases by 1.5." It means log-odds increase by 1.5. Use exp() for odds ratios.

Quick Reference Cheat Sheet

Task R Code
Fit model glm(y ~ x, data, family = binomial)
View results summary(model)
Odds ratios exp(coef(model))
CI for odds ratios exp(confint(model))
Predict probabilities predict(model, newdata, type = "response")
Model fit AIC(model) or model$deviance

Question 1 of 5

Which argument in glm() tells R to perform logistic regression instead of linear regression?

Question 2 of 5

If a logistic regression coefficient for hours is \( \beta = 0.8 \), what is the odds ratio?

Question 3 of 5

What does predict(model, newdata) return by default (without specifying type) for a logistic regression model?

Question 4 of 5

The logistic regression equation is \( \text{logit}(p) = -3 + 2x \). What is the predicted probability when \( x = 1 \)?

Question 5 of 5

Which R function do you use to obtain confidence intervals for odds ratios from a logistic regression model?