Simple linear regression
Twenty days of takings at a coffee shop, and the number of customers on each day: does business scale with foot traffic, and how well can one predict the other? Assignment 2 answered with lm(). This chapter rebuilds everything that call produced.
§5.1Coursework A2 Q3
Fit, intervals and diagnostics
The model , with independent , is fitted by least squares:
At a new value the mean response and a single new response share the point estimate but not the uncertainty:
20 rows · lines starting with # are ignored
Default data are simulated coffee-shop days (sales ≈ −30 + 6.4 × customers + noise) — not the assignment file.
Slope b₁
6.3378
95% CI (5.039, 7.636)
Intercept b₀
-18.13
CI (-142.6, 106.3)
Mean at x₀ = 100
615.7
CI (590.8, 640.5)
New day at x₀ = 100
615.7
PI (507.0, 724.3)
- Observed days
- Least-squares line
- 95% confidence band for the mean
- 95% prediction band for a new day
Show the numbers as a table
| # | x | y | fitted | residual | leverage |
|---|---|---|---|---|---|
| 1 | 95 | 582.8 | 583.96 | -1.16 | 0.050 |
| 2 | 123 | 658.2 | 761.42 | -103.22 | 0.176 |
| 3 | 83 | 574.3 | 507.91 | 66.39 | 0.069 |
| 4 | 73 | 423.1 | 444.53 | -21.43 | 0.117 |
| 5 | 108 | 731 | 666.36 | 64.64 | 0.079 |
| 6 | 81 | 504.7 | 495.23 | 9.47 | 0.076 |
| 7 | 103 | 648 | 634.67 | 13.33 | 0.062 |
| 8 | 103 | 611.1 | 634.67 | -23.57 | 0.062 |
| 9 | 68 | 341.7 | 412.84 | -71.14 | 0.153 |
| 10 | 102 | 716 | 628.33 | 87.67 | 0.059 |
| 11 | 74 | 530.7 | 450.87 | 79.83 | 0.111 |
| 12 | 89 | 490.7 | 545.94 | -55.24 | 0.054 |
| 13 | 80 | 472.5 | 488.90 | -16.40 | 0.080 |
| 14 | 77 | 478.3 | 469.88 | 8.42 | 0.094 |
| 15 | 97 | 566.1 | 596.64 | -30.54 | 0.051 |
| 16 | 122 | 749.7 | 755.08 | -5.38 | 0.167 |
| 17 | 124 | 782.5 | 767.76 | 14.74 | 0.185 |
| 18 | 87 | 541 | 533.26 | 7.74 | 0.058 |
| 19 | 122 | 766.2 | 755.08 | 11.12 | 0.167 |
| 20 | 71 | 396.6 | 431.86 | -35.26 | 0.130 |
R console, recreated
> summary(lm(sales ~ customers))
Residuals:
Min 1Q Median 3Q Max
-103.222 -25.310 3.287 13.685 87.671
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -18.1271 59.2481 -0.306 0.763
customers 6.3378 0.6182 10.253 6.07e-09 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 50.36 on 18 degrees of freedom
Multiple R-squared: 0.8538, Adjusted R-squared: 0.8457
F-statistic: 105.1 on 1 and 18 DF, p-value: 6.074e-09Formatted with R’s own printing rules (formatReal, printCoefmat, format.pval); the test suite checks the text against R 4.6 character for character, for the 2020 data and 15 synthetic datasets.
- Observations (largest three numbered)
- LOWESS smooth, as in plot(lm)
- Reference
Residuals vs fitted
Normal Q–Q
Scale–location
Residuals vs leverage
The four panels are read by eye. Formal tests of the same assumptions (Shapiro–Wilk, Breusch–Pagan, Durbin–Watson and a Bonferroni outlier test) run on this kind of data in Appendix C.
Explain this resultoptional · your own key
Add your own Anthropic or OpenAI key in AI settings to enable. Nothing is sent without one.