Generalized Linear Models
Week 3. Groups as predictors (t-tests, ANOVA, and ANCOVA are all lm()), interactions and what an “effect” means in summary() vs anova(), then responses that a straight line can’t handle: yes/no (logistic regression) and counts (Poisson regression).
Problem Set 3: General(ized) Linear Models — due Monday, October 19, 1:10 PM. Submit on Canvas.
Answer key: Problem Set 3 key
Data: pistachio.csv (disease, yield, and drone NDVI for 300 pistachio trees of three genotypes; used in Problem Set 3), wheat_trial.csv (90 plots of a wheat field trial: variety, water, nitrogen, yield, rust, aphids; the running example in the lectures, simulated), and tomato_trial.csv (120 greenhouse tomato plants; the in-class practice data, simulated)
This week — General(ized) linear models
One deck across both lectures. First half: dummy variables, a t-test and an ANOVA as lm(), Tukey and multiple testing, eta-squared, two factors and their interaction, why summary() and anova() answer different questions when there is an interaction, ANOVA types I, II, and III, and ANCOVA. Second half: the three parts of a GLM; probability, odds, and log-odds; logistic regression with groups and with a slope, odds ratios, predict(type = "response"), accuracy against a baseline, deviance; counts out of a total; multinomial; Poisson regression, rate ratios, overdispersion, and the negative binomial. The second lecture picks up wherever the first ends.
- Slides (PDF) — one deck for the week (the live-poll links are clickable)
- Live coding in class — the short scripts we open in RStudio and run together, line by line, on the lecture’s wheat trial: Week3_live_1_groups.R (groups as predictors, ANOVA, interactions, ANCOVA) and Week3_live_2_glm.R (logistic and Poisson regression).
- Week3_in_class.R — practice: the same ideas on a new dataset, tomato_trial.csv (120 greenhouse tomato plants, simulated). Work through it after class.
- The week’s code, with output — one page for the whole week, in the same order as the slides, with the code behind every figure and number in the lecture and the output under each block. Every block is labelled Core (the analysis you would run on your own data), Handy (useful, not essential), or Demo (simulations and by-hand sums for the lecture; folded). Start with the Core blocks for the problem set.
- Week3_GLM.R — the same code as a plain script, each block tagged
[CORE],[HANDY], or[DEMO]. It reads the data straight from the course website, so there is no working directory to set. The notebook version (.qmd) is linked from the code page.
Bonus — the GLM family, side by side
Every common GLM on one dataset with one predictor: Normal, binomial (0/1 and \(k\) of \(n\)), Poisson, negative binomial, Poisson with an offset (counts per unit effort), and Gamma. Each is the same three choices: a distribution, a straight line, and a link.
- Slides (PDF) — the bonus deck, with a cheat sheet and a “which family?” chart
- Interactive: the GLM family explorer — pick a family, move β₀ and β₁, and watch the straight line on the link scale and the curve on the data scale; plus spread vs mean, and why the offset matters. Runs in the browser.
- The code, with output and GLM_families.R — every model in the deck, on field_survey.csv (120 wheat fields, seven kinds of response, simulated)
Fri Oct 16 — How to: data wrangling (data.table and tidyverse, side by side)
Learning objectives
- Explain how
lm()turns a group into dummy variables, and read the intercept and each coefficient as a group mean or a difference - Test a factor as a whole with
anova(), compare pairs withTukeyHSD()oremmeans, and say why multiple comparisons need adjusting - Interpret an interaction, and say what a
summary()row and ananova()row each test when one is present - Choose an ANOVA type for unbalanced data
- Fit logistic and Poisson regressions with
glm(), and report odds ratios and rate ratios with confidence intervals - Check a Poisson model for overdispersion and fix it
Also useful
- Packages this week:
ggplot2,emmeans,car(forAnova()),MASS(forglm.nb()),nnet(formultinom()) - Textbook: ISLR (2nd edition) sections 3.3.1–3.3.2 (qualitative predictors; interactions), 4.3 (logistic regression, including multinomial), and 4.6 (generalized linear models; Poisson regression)