Skip to content
AgileLogics
6 min read

Generalized Linear Models

Prerequisites: Linear regression

Linear regression predicts a continuous number. Many outcomes aren't like that: yes/no (did the customer churn?), counts (how many visits?). Generalized linear models (GLMs) extend regression to these cases.

Logistic regression: yes/no outcomes

Can we predict whether a car has a manual transmission (am = 1) from its weight?

model <- glm(am ~ wt, data = mtcars, family = binomial)
round(coef(model), 3)
(Intercept)          wt
     12.040      -4.024

family = binomial fits a logistic regression. Coefficients are on the log-odds scale, which is hard to read directly. Exponentiating gives odds ratios:

model <- glm(am ~ wt, data = mtcars, family = binomial)
round(exp(coef(model))["wt"], 3)
   wt
0.018

Each extra 1,000 pounds multiplies the odds of a manual transmission by about 0.018 — heavier cars are far less likely to be manual.

Predicted probabilities

Predictions are easier to explain as probabilities:

model <- glm(am ~ wt, data = mtcars, family = binomial)
new_cars <- data.frame(wt = c(2, 3, 4))
round(predict(model, new_cars, type = "response"), 3)
    1     2     3
0.982 0.492 0.017

type = "response" returns probabilities: a 2,000-pound car has about a 98% chance of being manual, a 3,000-pound car about 49%, and a 4,000-pound car about 2%.

Other families

  • family = poisson — for counts (number of complaints, website visits). Check for overdispersion; MASS::glm.nb() (negative binomial) handles counts that vary more than a Poisson model allows.
  • family = Gamma — for positive, right-skewed amounts such as costs.

Evaluating a classifier

For yes/no predictions, look beyond overall accuracy: the confusion matrix (table(predicted, actual)), sensitivity, specificity, and the ROC curve and AUC. When one outcome is rare — fraud, disease — a model can be "95% accurate" by always predicting the common outcome. The next lesson's tidymodels framework makes this evaluation systematic.