Accessible version · How to use this site with a screen reader · Course home
Day 2 Afternoon · Accessible lecture notes

Generalized linear models

A screen-reader-friendly transcript of the lecture slides: all slide text, tables, figure descriptions, and speaker notes.

About these notes

Each section is one slide. Figures carry text descriptions where available. For the concepts with worked examples, also read the plain-language summary and lab for this session.

Slide 1: Generalized Linear Models

Some material from Olivier Gimenez 2020

Slide 2: General Linear Model

Often written as:

But even clearer:

From this we can see assumption of constant variance

y ~ N(β0 + β1x1 + β2x2 + β3x3 + …, σ)

Slide 3

Slide from Federico Vegetti

Slide 4

Slide from Federico Vegetti

Slide 5: Regression is ONE KIND of General Linear Model

Figure 5.1. Scatterplot of Y versus X with a magenta regression line sloping up; small normal density curves at three X values show constant-variance errors around the line.

Slide 6: What does it mean to be linear?

Slide 7: All models have stochastic part for errors and deterministic “scientific” model defined by both algebra and geometry

Figure 7.1. 3D scatterplot titled Regression Plane: black points above a tilted gray plane over axes x and z, with red and gray vertical lines showing residuals from points to the fitted plane.

What is the associated R code for this?

Slide 8: General Linear Model

y ~ N(β0 + β1x1 + β2x2 + β3x3 + …, σ)

Slide 9: What influences partying in college?

Figure 9.1. R code and output: fit <- lm(partyHr ~ drinks). Intercept 2.315, drinks slope 0.546, both p<2e-16; R-squared 0.574.

From Yuk Tung Liu’s STAT 390EF

Speaker notes for this slide

What kind of model is this? (regression) How would bad scientists interpret? Let’s interpret correctly

Slide 10: What influences partying in college?

Figure 10.1. R code and output: fit1 <- lm(partyHr ~ drinks + gender). Intercept 1.740, drinks 0.555, genderfemale 0.774 (p=0.008); R-squared 0.576.

Group Work

Speaker notes for this slide

What is this model called in stats classes? (ANCOVA)

Slide 11

Figure 11.1. R code and output: fit2 <- lm(partyHr ~ drinks + gender2) with female reference. Intercept 2.514, drinks 0.555, gender2male -0.774 (p=0.008); R-squared 0.576.

Slide 12: Interaction term

Group Work

Figure 12.1. R output: fit3 <- lm(partyHr ~ drinks + gender + drinks:gender). drinks 0.492, genderfemale -0.322 (ns), interaction drinks:genderfemale 0.152 (p=1.6e-07); R-squared 0.587.

Slide 13: More than two levels – Drinking by religion

Figure 13.1. R output: lm(drinks ~ religion), reference Christian. Intercept 7.54; Muslim -5.35, Hindu -3.57, Buddhist -4.61 significant; R-squared 0.026.

Start with reference level “Christian”

Slide 14: More than two levels – Drinking by religion

Now with reference level “Atheist”

Figure 14.1. R output: lm(drinks ~ religion) with Atheist reference. Intercept 5.80; relChristian 1.74, relJewish 3.92 (p=0.006), relMuslim -3.62; R-squared 0.026.
Speaker notes for this slide

Remember to think about sample size

Slide 15: Sometimes we can’t be Normal

(No text on this slide.)

Slide 16: Titanic Data – Try a General Linear Model

Figure 16.1. Histogram of residuals from a linear model of binary survival. Bars cluster near -0.25 and above 0.5, clearly bimodal and non-normal, showing the model is misspecified.

The actual probability model under the hood

Slide 17: Titanic Data – General Linear Model

Let’s interpret this output

Figure 17.1. R output: summary(titanic.lm), lm(survived ~ class). Intercept 0.240; classfirst 0.385, classsecond 0.174 significant, classthird 0.013 (ns); R-squared 0.087.

Slide 18: Titanic Data – Generalized Linear Model

Figure 18.1. Line plot titled Sigmoid relationship: a smooth S-shaped logistic curve of Y versus X, rising from near 0 at low X to near 1 at high X, passing 0.5 at X=0.

Slide 19: From probability to odds to log of odds

Slide 20: Titanic Data – Generalized Linear Model

Group Work

Slide 21: Titanic Data – Generalized Linear Model

Slide 22: Titanic Data – Let’s look at the data

Figure 22.1. Stacked bar chart: passenger counts by class (crew, first, second, third), colored by survived (0 red, 1 teal). Crew and third are largest; first class has the highest survived proportion.

Slide 23: Binomial GLM (i.e. Logistic Regression)

Figure 23.1. R output, logistic regression coefficients (log-odds): Intercept -1.155; classfirst 1.664, classsecond 0.808 significant; classthird 0.068 (ns).

How do we interpret this?

Slide 24: Interpreting the effect size with odds ratio

Group Work

Show that this is also true for any continuous variables, x, by using the odds ratio of x + 1 to x

Slide 25: Questions

Group Work

Figure 25.1. R output, logistic regression coefficients on log-odds scale: Intercept -1.155, classfirst 1.664, classsecond 0.808, classthird 0.068; used to compute survival probabilities by class.

Slide 26

Figure 26.1. R code slide 'Interpreting logistic regression outputs': logit-scale coefficients (Intercept -1.155, first 1.664, second 0.808, third 0.068); plogis of intercept gives crew survival 0.2395, matching the data.

Slide 27

Figure 27.1. R code slide 'Probability of survival for 1st class passengers': plogis(intercept + classfirst coefficient) gives 0.6246, which matches the observed first-class survival proportion.

So this model was trivial for estimation but was still needed for inference!

Slide 28

Figure 28.1. Effects plot: predicted survival probability by class (crew, first, second, third) with magenta confidence bars. First class highest near 0.62; crew and third lowest near 0.24.

Slide 29: Did males have higher survival than females?

(No text on this slide.)

Slide 30

Figure 30.1. Stacked bar chart of passenger counts by sex, colored by survived (0 red, 1 teal). Males far outnumber females; a much smaller fraction of males survived than females.

Slide 31

Figure 31.1. R output slide 'Fit model': glm(survived ~ sex, binomial). Intercept 1.00, sexmale -2.32 (p=1.2e-83), showing males had much lower survival odds than females.

Slide 32: Did survival vary by class and sex?

(No text on this slide.)

Slide 33

Figure 33.1. R output slide 'Fit model with both factors (interactions)': glm(survived ~ class * sex, binomial). sexmale -3.15 (p=4.7e-07); classthird:sexmale 1.74 (p=0.007) among the terms.

Slide 34: How do we interpret this?

Group Work

Figure 34.1. R output slide 'Fit model with both factors (interactions)': glm(survived ~ class * sex, binomial), same table with class, sex, and their interaction terms and p-values.

Slide 35

Figure 35.1. R output slide 'Effects': allEffects of class*sex gives predicted survival probabilities. Females: crew 0.87, first 0.97, second 0.88, third 0.46. Males: first 0.34, others lower.

Slide 36

Figure 36.1. class*sex effect plot: predicted survival by class in two panels (female, male). Females survive at high rates (crew-second ~0.85-0.97, third ~0.5); males much lower (~0.15-0.35).

Slide 37: Instead of looking at individual passengers, can’t we look at proportion by group?

(No text on this slide.)

Slide 38

Not 1 anymore

Bernoulli becomes a Binomial

Slide 39

Figure 39.1. R output slide 'Use cbind(n.success, n.failures) as response': glm(cbind(Yes,No) ~ Class, binomial). Class2nd -0.856, Class3rd -1.60, ClassCrew -1.66, all highly significant.

Check to make sure results are identical. I’ve made it not 100% easy to check

Slide 40: Models for count data

Unbounded

Bounded

Slide 41

Figure 41.1. Photo of a bright red sockeye salmon (Oncorhynchus nerka) with a green head and hooked jaw, in spawning coloration.

Oncorhynchus nerka (sockeye salmon)

Does environmental DNA (eDNA) contain enough information to estimate salmon escapement and smolt abundance?

Figure 41.2. Illustration of a colorful DNA double helix, representing environmental DNA (eDNA).
Figure 41.3. qPCR amplification plot: fluorescence versus PCR cycles for four primers across two samples, with S-shaped curves and listed Ct values around 20-26.
Figure 41.4. Photo of a benchtop real-time qPCR instrument used to quantify DNA.
Figure 41.5. Photo of a person in waders crouching at the edge of a rushing forest stream, collecting a water sample near a weir.
Figure 41.6. Photo of several people in waders working inside a stream weir, using dip nets to capture and count migrating fish.

=

One person, one sample /day

Several people, all day

Speaker notes for this slide

A minor revolution in ecology is using eDNA from water to sample aquatic species. I have started a project with He Daming of the Asian International Rivers Center at Yunnan University to use this technology to map fish populations.

Slide 42

Figure 42.1. Photo of underwater salmon: bright red spawning sockeye behind a gray Dolly Varden char, over a gravel streambed.

Levi. T., Allen, J. M., Bell, D., Joyce, J. Russel, J. R., Tallmon, D. A., Vulstek, S. C., Yahan, Y., Yu, D. W. 2019. Environmental DNA for the enumeration and management of Pacific salmon. Molecular Ecology Resources

Slide 43

Figure 43.1. Photo of a field crew in waders using dip nets inside the Auke Creek picket weir to capture and count migrating salmon.
Figure 43.2. Photo of the Auke Creek weir field station in winter: a blue-roofed building among snow-covered evergreens, with a metal picket weir spanning the creek in the foreground.
Figure 43.3. Map of southeast Alaska: inset locates Auke Lake near Juneau; detail shows Auke Lake draining via Auke Creek past a weir to Auke Bay.
Figure 43.4. Photo of a field crew in waders working the Auke Creek weir with dip nets to capture and count migrating fish.

Slide 44

Figure 44.1. Multi-panel time series for 2015 and 2016 (mid-summer), sockeye adults. Rows: Counts, Flow x DNA, DNA, Flow (cfs), Temp (C) versus date; count and eDNA peaks roughly coincide.

Sockeye returning adults

Slide 45

Figure 45.1. Same 2015 and 2016 multi-panel time series (Counts, Flow x DNA, DNA, Flow, Temp) for sockeye returning adults; eDNA tracks the count peaks.

Sockeye returning adults

Slide 46

Figure 46.1. Same 2015 and 2016 multi-panel time series (Counts, Flow x DNA, DNA, Flow, Temp) for sockeye returning adults.

Sockeye returning adults

Slide 47

Figure 47.1. Same 2015 and 2016 multi-panel time series (Counts, Flow x DNA, DNA, Flow, Temp) for sockeye returning adults; illustrates flux times concentration giving eDNA rate.

Sockeye returning adults

Figure 47.2. Orange rectangular outline highlighting a peak in the eDNA and streamflow time-series panels.
Figure 47.3. Orange rectangular outline highlighting a peak in the eDNA and streamflow time-series panels.
Figure 47.4. Orange rectangular outline highlighting a peak in the eDNA and streamflow time-series panels.
Figure 47.5. Orange rectangular outline highlighting a peak in the eDNA and streamflow time-series panels.

Water flux (cubic_ft/sec) X DNA concentration (ng/ul) = ng/sec X cubic_ft/ul

Slide 48

Figure 48.1. Same 2015 and 2016 multi-panel time series (Counts, Flow x DNA, DNA, Flow, Temp) for sockeye adults; Flow x DNA row is the eDNA rate in ng/sec.

Sockeye returning adults

eDNA rate: ng/sec

Figure 48.2. Orange rectangular outline highlighting a peak in the eDNA and streamflow time-series panels.
Figure 48.3. Orange rectangular outline highlighting a peak in the eDNA and streamflow time-series panels.
Figure 48.4. Orange rectangular outline highlighting a peak in the eDNA and streamflow time-series panels.
Figure 48.5. Orange rectangular outline highlighting a peak in the eDNA and streamflow time-series panels.

Slide 49

Figure 49.1. Multi-panel time series for 2015 and 2016 spring (Apr-Jun): Counts, Flow x DNA, DNA, Flow (cfs), Temp (C) for out-migrating sockeye smolts; smolt counts peak in mid-May.

Sockeye out-migrating smolts

Figure 49.2. Photo of a small silvery sockeye smolt held in a person's palm above blue water.

Slide 50: Can we predict salmon counts using eDNA

Let’s start with Poisson model using flow-corrected eDNA concentration as a predictor.

Slide 51: What is happening here?

Slide 52: Model output

Slide 53: Model output

Slide 54: How does this look? Concerns? What now? Are we done?

(No text on this slide.)

Slide 55: How does this look? Concerns? What now? Are we done?

Slide 56: 1. X vs. Y

Figure 56.1. Two scatterplots (2015, 2016) of sockeye counts versus eDNA rate with a dashed blue Poisson fit and a very narrow gray confidence band; the curve bends upward, not a straight line.

Slide 57: 2. Good models make good predictions

Figure 57.1. Two panels (2015, 2016) of observed sockeye counts (black dots) versus fitted values (dashed blue) over date; the fit consistently overpredicts the low and zero counts.

Slide 58: What is being fit here exactly?

Figure 58.1. Two panels (2015, 2016) of sockeye count versus date, observed dots and dashed blue Poisson fit; the fitted line stays above zero even when observed counts are zero.

Slide 59: eDNA rate should be transformed from positive to the whole real line! How?

Slide 60: log(Y) = a + b*log(X)

(No text on this slide.)

Slide 61: New model output

Slide 62: New model output

Slide 63: Good models make good predictions

Figure 63.1. Two panels (2015, 2016) of sockeye count versus date, observed dots and dashed blue log-link Poisson fit; the fit now drops to near zero where observed counts are zero.

Slide 64: Quasipoisson GLM

Slide 65: Quasipoisson GLM

Slide 66: Quasipoisson figures

Figure 66.1. Two scatterplots (2015, 2016) of sockeye counts versus eDNA rate, dashed blue quasipoisson fit with a wide gray confidence band that now spans most of the data.

Slide 67: Quasipoisson figures

Figure 67.1. Two panels (2015, 2016) of sockeye count versus date, dashed blue quasipoisson fit with a wide gray confidence band around the observed points.

Slide 68: This is great, but….

Slide 69: Negative binomial

Can’t use glm(). Need to use glm.nb() in the MASS package

Slide 70: Negative binomial

We now have AIC

Slide 71: Negative binomial

Figure 71.1. Two scatterplots (2015, 2016) of sockeye counts versus eDNA rate with a dashed blue negative-binomial fit and a wide gray confidence band.

Slide 72: Negative binomial

Figure 72.1. Two panels (2015, 2016) of sockeye count versus date, dashed blue negative-binomial fit with a gray confidence band around observed points.

Slide 73: Gamma

Slide 74: Gamma

Slide 75: Interaction Terms

“Context dependence” – The effect of one predictor depends on the value of another predictor

Figure 75.1. Journal header image: Biological Reviews (2023) article 'Understanding "it depends" in ecology: a guide to hypothesising, visualising and interpreting statistical interactions.'

Slide 76: Interpretation and Symmetry of Interactions

Let’s focus on deterministic part

Factor out x1

Factor out x2

Slide 77: Interpretation and Symmetry of Interactions

Vs.

Figure 77.1. 3D surface plot titled 'y ~ x1 + x2': a flat tilted plane over x1 and x2, showing an additive model with no interaction (color from dark low to yellow high).
Figure 77.2. 3D surface plot titled 'y ~ x1 * x2': a twisted saddle-shaped surface over x1 and x2, showing how an interaction term warps the response.

Slide 78: Interpretation and Symmetry of Interactions

Vs.

Figure 78.1. Heatmap of predicted y with no true interaction: a smooth gradient increasing with x1 (dark at low x1 to yellow at high x1), roughly parallel bands.
Figure 78.2. Heatmap of predicted y with an interaction: the gradient bends diagonally, brightest at high x1/low x2 and at high x2/high x1, showing curved, non-parallel contours.

Slide 79: Dependence on scaling and transformation I

Figure 79.1. R output for lm(y ~ x1*x2) on data simulated with no interaction: x1 3.0, x2 -4.0 highly significant, interaction x1:x2 -0.00055 (p=0.586, not significant).

lm(y~x1*x2)

Figure 79.2. Two scatterplots: y versus x1 (positive trend) and y versus x2 (negative trend), each with scattered points, from data simulated with no true interaction.

Slide 80: Dependence on scaling and transformation II

Log transform dependent variable

lm(log(y)~x1*x2)

Figure 80.1. Same two scatterplots of y versus x1 (positive) and y versus x2 (negative) from the no-interaction simulation, before log-transforming y.
Figure 80.2. R output for lm(log(y) ~ x1*x2): now the interaction x1:x2 0.00031 is highly significant (p=4.85e-08), a spurious interaction created by the log transform.

What is happening and why?!

Slide 81: Additivity not preserved across nonlinear transformations!

If log(y) is a function of additive x1 and x2, will y generate an interaction?

Equations for reference, work through on paper

Slide 82: Confusion in action with major policy implications!

Figure 82.1. Journal header: Global Change Biology research article 'Habitat alteration or climate: What drives the densities of an invading ungulate?' by Dickie et al., 2024.

Q: Deer are expanding, driving decline of caribou by supporting wolves. Is this due to logging or climate or both?

Figure 82.2. Journal header: Global Change Biology Letter to the Editor 'The influence of habitat alteration on density of invading white-tailed deer should not be discounted.'
Figure 82.3. Journal header: Global Change Biology Response 'The influence of habitat alteration is widespread, but the impact of climate cannot continue to be discounted,' by Dickie et al.

(1)

(2)

(3)

Figure 82.4. List of author names with ORCID icons: Barnas, Anholt, Burton, Carroll, Cote, Festa-Bianchet, Fryxell, St-Laurent, and Fisher.

Slide 83: What was the point made by Barnas et al.?

Figure 83.1. Journal text explaining min-max versus z-standardization of predictors, with Equation (1): x_i(min-max scaled) = (x_i - minimum(x)) / (maximum(x) - minimum(x)).
Figure 83.2. Journal text with Equation (2): z_i = (x_i - mu)/sigma. Explains that switching from min-max to z-scaling reversed the sign and significance of climate and habitat-alteration effects.

Slide 84: What was the point made by Barnas et al.?

Figure 84.1. Dot-and-error-bar plot of fixed-effect coefficients for deer density under two scalings (red min-max, blue z-standardize). Estimates differ in sign and magnitude across terms, especially Climate Dimension 1.

Weird. Why? Who is right?

Slide 85: Models, especially with interactions, require algebra and especially geometry. Interpret the predictions!

Figure 85.1. Six-panel figure of predicted deer density versus habitat alteration and climate, comparing z-standardized (top a-c) and min-max (bottom d-f) scalings; the two approaches give the same predicted-density curves.