Linear Regression

Week 2. One line through the data: least squares, what every number in summary() means, when not to trust it, and what a second predictor can reveal or fake.

Problem Set 2: Linear Regression — due Monday, October 12, 1:10 PM. Submit on Canvas.

Answer key: Problem Set 2 key

Data: wild_wheat.csv (flowering time of 54 wild einkorn wheat genotypes with the WorldClim variables of their home sites; used in Problem Set 2), wheat_plants.csv (40 wheat plants: irrigation, nitrogen, height, biomass; the running example in the lectures), and birds.csv (bird density in forest patches; the multicollinearity example)


This week — Linear regression

One deck across both lectures: fitting a line (residuals and least squares), lm(), predictions, R², the slope’s t-test and confidence interval, the F-test, assumptions and diagnostic plots, and the two classic fixes (a squared term; log the response); then multiple regression (partial slopes, adjusted R², interactions), hidden relationships (suppressors and confounders), multicollinearity and VIF, and how to report a regression. The second lecture picks up wherever the first ends.

  • Slides (PDF) — one deck for the week (the live-poll links are clickable)
  • Week2_in_class.R — live coding in class: the practice script we open in RStudio and run together, line by line. Separate from the lecture code. It reads cover_crop.csv (cover-crop biomass on 50 farms, simulated) straight from the website.
  • 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.
  • Week2_Linear_Regression.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.
  • Interactive explorers — browser apps for this week: least squares, confidence vs prediction intervals, residual diagnostics, third variables, and multicollinearity. Each one shows the R code that reproduces it.

Fri Oct 9 — Live coding: the Week 2 in-class script

We work through Week2_in_class.R together in RStudio. Bring your laptop.


Learning objectives

  • Explain what least squares does and read a slope in real units
  • Fit a model with lm(), interpret summary(), and make predictions with predict()
  • Report an effect size with a confidence interval, R², and n, not just a p-value
  • Check assumptions with plot(model) and apply the standard fixes
  • Interpret partial slopes in multiple regression, recognize confounding, and diagnose multicollinearity with VIF

Also useful

  • Packages this week: ggplot2, car (for vif()), scatterplot3d (the 3D plot; optional)
  • Textbook: ISLR chapter 3 (Linear Regression)