Example: Exam Score = 50 + 5 × Study Hours
Example: Exam Score = 40 + 4 × Study Hours + 2 × Sleep Hours - 3 × Anxiety
Open Posit Cloud. Everything in this module runs on one real dataset — 100 students, their weekly study hours, sleep, test anxiety, and exam score.
Run this:
exam <- read.csv("https://jkoxford-a11y.github.io/awesomesaucestats/data/regression_exam_scores.csv")
str(exam)
# one predictor
one <- lm(score ~ hours, data = exam)
summary(one)
# three predictors
three <- lm(score ~ hours + sleep + anxiety, data = exam)
summary(three)
What R gives for R-squared:
hours only R-squared = 0.4298 Adjusted R-squared = 0.4240
hours + sleep + anxiety
R-squared = 0.6839 Adjusted R-squared = 0.6740KEY CONCEPT: Each coefficient represents the effect of that predictor while holding all other predictors constant.
| Coefficient | Interpretation |
|---|---|
| b₁ = 4 | For each additional study hour, score increases by 4 points, holding sleep constant |
| b₂ = 2 | For each additional sleep hour, score increases by 2 points, holding study time constant |
Problem with R²: It ALWAYS increases when you add predictors, even if they're useless!
| Measure | What it does | When to use |
|---|---|---|
| R² | % variance explained | Describing current model fit |
| Adjusted R² | R² penalized for # of predictors | Comparing models with different # of predictors |
The dataset contains a column called shoe_size. It has nothing to do with exam performance. Add it anyway and watch what each measure does.
Run this:
silly <- lm(score ~ hours + sleep + anxiety + shoe_size, data = exam)
summary(silly)
What R gives:
R-squared Adjusted R-squared
hours + sleep + anxiety 0.6839 0.6740
+ shoe_size 0.6850 0.6718
shoe_size coefficient: p = 0.5647This is the whole point of adjusted R². Plain R² went up — it always does, for any predictor, however useless. Adjusted R² went down, because the tiny gain did not pay for the degree of freedom it cost.
What it is: When predictors are highly correlated with each other.
Predicting weight from both height in inches AND height in centimeters
→ These are perfectly correlated! They measure the same thing.
The dataset also has hours_reported — each student's own estimate of their study hours. It measures nearly the same thing as hours (r = .98). Put both in the model.
Run this:
cor(exam$hours, exam$hours_reported)
both <- lm(score ~ hours + hours_reported + sleep + anxiety, data = exam)
summary(both)
library(car) # Chapter 24
vif(both)
vif(three) # for comparison
What R gives:
> cor(exam$hours, exam$hours_reported)
[1] 0.9803
Coefficients for `hours`:
in `three` (hours alone) 2.4900 SE 0.2185 t = 11.397 p < 2e-16
in `both` (with hours_reported) 2.3520 SE 1.1165 t = 2.107 p = 0.0378
> vif(both)
hours hours_reported sleep anxiety
25.851 25.852 1.055 1.046
> vif(three)
hours sleep anxiety
1.000 1.045 1.045Read the standard error, not the estimate. The coefficient for hours barely moved (2.49 → 2.35). Its standard error grew five times, and a predictor that was the strongest thing in the model dropped to p = .04. Nothing about study hours changed — the model simply cannot tell the two versions of it apart.
hours is 25.9 with hours_reported in the model and 1.0 without it. What would you actually do about this, and what would you lose?What it is: When the effect of one predictor depends on the level of another predictor.
Without interaction: Caffeine always improves performance by the same amount
With interaction: Caffeine helps when you're tired but doesn't help when you're already alert
The b₃(X₁ × X₂) term captures the interaction
| Scenario | Model Needed |
|---|---|
| Caffeine and sleep have separate effects | Main effects only (no interaction) |
| Caffeine's effect depends on sleep level | Include interaction term |
Does the benefit of studying depend on how anxious you are? That is an interaction question, and you can test it directly.
Run this:
inter <- lm(score ~ hours * anxiety, data = exam)
summary(inter)
What R gives:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 42.9459 4.8260 8.899 < 2e-16 ***
hours 2.6158 0.4956 5.278 8.05e-07 ***
anxiety -2.2715 0.8325 -2.729 0.0076 **
hours:anxiety -0.0249 0.0841 -0.296 0.7679There is no interaction here (p = .77), and that is worth sitting with. hours * anxiety fits without complaint, produces a coefficient, and means nothing. Being able to fit a term is not evidence the term is real — you have to look at whether it earned its place.
Note also what the main effects mean once an interaction is in the model: hours = 2.62 is now the effect of study hours when anxiety = 0, not the average effect. That is why you do not interpret main effects the usual way alongside an interaction.
| Model | Predictors | R² | Adj R² | Assessment |
|---|---|---|---|---|
| 1 | Study hours | 0.50 | 0.48 | Good baseline |
| 2 | Study + Sleep | 0.62 | 0.59 | Improvement! |
| 3 | Study + Sleep + Breakfast | 0.63 | 0.58 | Adj R² dropped - not helpful |
Comparing R² by eye is not a test. anova() on two nested models is.
Run this:
anova(one, three)
What R gives:
Analysis of Variance Table
Model 1: score ~ hours
Model 2: score ~ hours + sleep + anxiety
Res.Df RSS Df Sum of Sq F Pr(>F)
1 98 12724.8
2 96 7053.8 2 5671 38.59 5.024e-13 ***Adding sleep and anxiety together explains significantly more variance than study hours alone, F(2, 96) = 38.59, p < .001. This only works for nested models — every predictor in the smaller model must also be in the larger one.
anova() to compare score ~ hours + sleep against score ~ anxiety + shoe_size? Why or why not?This is summary(three) in full — the same model you fitted in Demo 1, printed exactly as R prints it.
Call:
lm(formula = score ~ hours + sleep + anxiety, data = exam)
Residuals:
Min 1Q Median 3Q Max
-21.837 -5.014 -0.303 6.423 20.354
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 29.5295 5.9752 4.942 3.27e-06 ***
hours 2.4900 0.2185 11.397 < 2e-16 ***
sleep 2.0282 0.7505 2.703 0.00814 **
anxiety -2.3287 0.3057 -7.616 1.83e-11 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 8.572 on 96 degrees of freedom
Multiple R-squared: 0.6839, Adjusted R-squared: 0.674
F-statistic: 69.24 on 3 and 96 DF, p-value: < 2.2e-16(a) A student studies one hour more per week. How many more points, and what has to be held constant for that to be true?
(b) Where does the 96 come from?
(c) The residual standard error is 8.572 points. Is this model precise enough to predict an individual student's grade to within a letter?
You have completed all four modules on linear regression!
You now understand:
Grading: Credit/no-credit, not scored for correctness — but credit requires a genuine attempt; blank or clearly-unattempted answers earn no credit. Work through it with your partner and say what you actually think; a wrong idea you reasoned your way to is worth more here than a right one you copied.
reg4_lastname1_lastname2.pdf and upload to the course site.