Skip to content
Statistics LabMAST20005 · 2020

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 Yi=β0+β1xi+εiY_i=\beta_0+\beta_1x_i+\varepsilon_i, with independent εi∼N(0,σ2)\varepsilon_i\sim N(0,\sigma^2), is fitted by least squares:

b1=∑(xi−xˉ)(yi−yˉ)∑(xi−xˉ)2,b_1=\frac{\sum(x_i-\bar x)(y_i-\bar y)}{\sum(x_i-\bar x)^2},
b0=yˉ−b1xˉ,b_0=\bar y-b_1\bar x,
s2=∑ei2n−2.s^2=\frac{\sum e_i^2}{n-2}.

At a new value x0x_0 the mean response and a single new response share the point estimate y^0=b0+b1x0\hat y_0=b_0+b_1x_0 but not the uncertainty:

mean:  y^0±tn−2 s1n+(x0−xˉ)2Sxx,\text{mean: }\ \hat y_0 \pm t_{n-2}\,s\sqrt{\tfrac1n+\tfrac{(x_0-\bar x)^2}{S_{xx}}},
new day:  y^0±tn−2 s1+1n+(x0−xˉ)2Sxx.\text{new day: }\ \hat y_0 \pm t_{n-2}\,s\sqrt{1+\tfrac1n+\tfrac{(x_0-\bar x)^2}{S_{xx}}}.

20 rows · lines starting with # are ignored

100
95%

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
Figure 5.1.The confidence band describes where the average sales line could be; the prediction band also allows for one day’s scatter around it, so it never shrinks below ±t·s. Both are narrowest at x̄ = 94.1.
Show the numbers as a table
#xyfittedresidualleverage
195582.8583.96-1.160.050
2123658.2761.42-103.220.176
383574.3507.9166.390.069
473423.1444.53-21.430.117
5108731666.3664.640.079
681504.7495.239.470.076
7103648634.6713.330.062
8103611.1634.67-23.570.062
968341.7412.84-71.140.153
10102716628.3387.670.059
1174530.7450.8779.830.111
1289490.7545.94-55.240.054
1380472.5488.90-16.400.080
1477478.3469.888.420.094
1597566.1596.64-30.540.051
16122749.7755.08-5.380.167
17124782.5767.7614.740.185
1887541533.267.740.058
19122766.2755.0811.120.167
2071396.6431.86-35.260.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-09

Formatted 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

21011

Normal Q–Q

21110

Scale–location

21011

Residuals vs leverage

Figure 5.2.The four panels R draws for plot(mod). Look for curvature (top left), a straight Q–Q line (top right), constant spread (bottom left) and no single point with both high leverage and a large residual (bottom right, Cook’s distance contours at 0.5 and 1).

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.