Today: We model how a response variable changes, on average, as anexplanatory variable changes, using a straight-line model plus random scatter.
This chapter extends our inference toolbox to relationships betweentwo quantitative variables.
Same general workflow: state \(H_0/H_a\), check conditions, compute a statistic (here, based on theslope), find a p-value, and conclude in context.
A scatterplot shows paired data \((x,y)\) for two quantitative variables.The correlation \(r\) measures the strength and direction of alinear association.
For a linear equation \( y = b_0 + b_1 x \), the number \( b_0 \) is called the y-interceptand the number \( b_1 \) is called the slope.
The slope tells us how fast \(y\) changes as \(x\) increases. Positive slopes tilt upward,negative slopes tilt downward, and a slope of 0 gives a horizontal line.
Drag the sliders to see how the line moves. Try a positive, negative, and zero slope.
A scatterplot is a graph of paired data from two quantitative variables.Each pair \((x_i, y_i)\) is plotted as a point on the plane.
We will study how the age of a car (years) relates to its price (in \$100 units).
| Car | Age (years) \(x\) | Price (\$100) \(y\) |
|---|---|---|
| 1 | 5 | 85 |
| 2 | 4 | 103 |
| 3 | 6 | 70 |
| 4 | 5 | 82 |
| 5 | 5 | 89 |
| 6 | 5 | 98 |
| 7 | 6 | 66 |
| 8 | 6 | 95 |
| 9 | 2 | 169 |
| 10 | 7 | 70 |
| 11 | 7 | 48 |
Question: As car age increases, how does price tend to change? The scatterplot will help us see the pattern and any outliers.
The point at (Age 2, Price 169) is a clear outlier. Most cars are 5–7 years old with lower prices.
Consider just three points:\((1, 3), (2, 1), (3, 5)\).We will compare two candidate lines.
| Point \((x,y)\) | \(\hat{y}\) | Residual \(e = y - \hat{y}\) | \(e^2\) |
|---|---|---|---|
| \(\displaystyle \sum e^2\) | |||
The least-squares line is the one with the smaller \(\sum e^2\).For these points, Line B (\(y = 1 + x\)) has \(\sum e^2 = 6\) versus 8 for Line A.
The least-squares criterion says that the best-fitting line is the one with thesmallest possible sum of squared residuals:
The resulting line is called the regression line, and its equation\(\hat{y} = b_0 + b_1 x\) is the regression equation.
For a set of \(n\) data points \((x_i, y_i)\):
| Quantity | Defining formula | Computing formula |
|---|---|---|
| \(S_{xx}\) | \(\displaystyle \sum (x_i - \overline{x})^2\) | \(\displaystyle \sum x_i^2 - \frac{(\sum x_i)^2}{n}\) |
| \(S_{xy}\) | \(\displaystyle \sum (x_i - \overline{x})(y_i - \overline{y})\) | \(\displaystyle \sum x_i y_i - \frac{(\sum x_i)(\sum y_i)}{n}\) |
| \(S_{yy}\) | \(\displaystyle \sum (y_i - \overline{y})^2\) | \(\displaystyle \sum y_i^2 - \frac{(\sum y_i)^2}{n}\) |
The least-squares regression equation is
$$\hat{y} = b_0 + b_1 x$$where
$$b_1 = \frac{S_{xy}}{S_{xx}}, \qquad b_0 = \overline{y} - b_1 \overline{x}= \frac{1}{n}\left(\sum y_i - b_1 \sum x_i\right).$$In practice, technology uses these (or equivalent) formulas behind the scenes to compute the slope and intercept.
Using the Orion age–price data, we compute the sums needed for \(S_{xx}\) and \(S_{xy}\).
| Car | Age \(x\) | Price \(y\) | \(xy\) | \(x^2\) |
|---|---|---|---|---|
| 1 | 5 | 85 | 425 | 25 |
| 2 | 4 | 103 | 412 | 16 |
| 3 | 6 | 70 | 420 | 36 |
| 4 | 5 | 82 | 410 | 25 |
| 5 | 5 | 89 | 445 | 25 |
| 6 | 5 | 98 | 490 | 25 |
| 7 | 6 | 66 | 396 | 36 |
| 8 | 6 | 95 | 570 | 36 |
| 9 | 2 | 169 | 338 | 4 |
| 10 | 7 | 70 | 490 | 49 |
| 11 | 7 | 48 | 336 | 49 |
| Sum | 58 | 975 | 4732 | 326 |
From these, and the formulas for \(S_{xx}\) and \(S_{xy}\), we obtain the regression line\(\hat{y} = 195.47 - 20.26x\) (price in \$100 vs age in years).
Blue line: all 11 cars, \(\hat{y} = 195.47 - 20.26x\).
Orange line: without the outlier at (2, 169), \(\hat{y} = 160.33 - 14.24x\).
In regression:
In the Orion example, Price is the response variable and Age is the predictor variable.
Extrapolation means using a regression line to predict values of \(y\) for \(x\)-values far outside the range of the original data.
For the Orion data, our ages range from 2 to 7 years. Using the regression line to predict price for a 10-year-old car or a brand-new car (0 years) is extrapolation.
The relationship between age and price may not remain linear beyond the observed range, so such predictions can be very misleading.
These satisfy the regression identity:$$\mathrm{SST} = \mathrm{SSR} + \mathrm{SSE}.$$
For points \((1, 3), (2, 1), (3, 5)\) and \(\overline{y} = 3\):
| \(x\) | \(y\) | \(\hat{y}\) | \(y - \overline{y}\) | \((y - \overline{y})^2\) | \(\hat{y} - \overline{y}\) | \((\hat{y} - \overline{y})^2\) | \(y - \hat{y}\) | \((y - \hat{y})^2\) |
|---|---|---|---|---|---|---|---|---|
| 1 | 3 | 2 | 0 | 0 | -1 | 1 | 1 | 1 |
| 2 | 1 | 3 | -2 | 4 | 0 | 0 | -2 | 4 |
| 3 | 5 | 4 | 2 | 4 | 1 | 1 | 1 | 1 |
| Sum | 8 | 2 | 6 |
Here, \(\mathrm{SST} = 8\), \(\mathrm{SSR} = 2\), \(\mathrm{SSE} = 6\), and indeed \(8 = 2 + 6\).
Technology often uses these computing formulas:
$$\mathrm{SST} = \sum y_i^2 - \frac{(\sum y_i)^2}{n},$$$$\mathrm{SSR} = \frac{\left[\sum x_i y_i - \frac{(\sum x_i)(\sum y_i)}{n}\right]^2}{\sum x_i^2 - \frac{(\sum x_i)^2}{n}},$$$$\mathrm{SSE} = \mathrm{SST} - \mathrm{SSR}.$$| Car | Age \(x\) | Price \(y\) | \(xy\) | \(x^2\) | \(y^2\) |
|---|---|---|---|---|---|
| 1 | 5 | 85 | 425 | 25 | 7225 |
| 2 | 4 | 103 | 412 | 16 | 10609 |
| 3 | 6 | 70 | 420 | 36 | 4900 |
| 4 | 5 | 82 | 410 | 25 | 6724 |
| 5 | 5 | 89 | 445 | 25 | 7921 |
| 6 | 5 | 98 | 490 | 25 | 9604 |
| 7 | 6 | 66 | 396 | 36 | 4356 |
| 8 | 6 | 95 | 570 | 36 | 9025 |
| 9 | 2 | 169 | 338 | 4 | 28561 |
| 10 | 7 | 70 | 490 | 49 | 4900 |
| 11 | 7 | 48 | 336 | 49 | 2304 |
| Sum | 58 | 975 | 4732 | 326 | 96,129 |
These sums feed directly into the computing formulas for \(\mathrm{SST}\), \(\mathrm{SSR}\), and \(\mathrm{SSE}\), and ultimately into measures like \(r\) and \(R^2\).
For a set of paired data \((x_i, y_i)\), the linear correlation coefficient \(r\) is defined by
A convenient computing formula is$$r = \frac{\sum x_i y_i - \frac{(\sum x_i)(\sum y_i)}{n}}{\sqrt{\left[\sum x_i^2 - \frac{(\sum x_i)^2}{n}\right]\left[\sum y_i^2 - \frac{(\sum y_i)^2}{n}\right]}}.$$
The coefficient of determination \(R^2\) is the proportion of variation in the observed values of the response variable explained by the regression:$$R^2 = \frac{\mathrm{SSR}}{\mathrm{SST}}.$$
For simple linear regression with one predictor, we have the relationship$$R^2 = r^2.$$
Move the slider to see stronger/weaker and positive/negative linear associations.
Suppose we regress final exam score (y) on hours of weekly study (x) and obtain\(\hat{y} = 62 + 3.5x\).
A residual is the vertical difference between an observed value and its fitted value:$$e = y - \hat{y}.$$
Hypotheses for testing a linear relationship:
$$H_0 : \beta_1 = 0 \quad\text{vs}\quad H_a : \beta_1 \ne 0$$(or one-sided alternative, depending on context)t with df = \(n - 2\).# Assume dataframe 'dat' with columns x, y
mod <- lm(y ~ x, data = dat)
# Correlation between x and y
cor(dat$x, dat$y)
summary(mod) # slope, intercept, t-test, p-value, R^2
# Fitted values and residuals
fitted_vals <- fitted(mod)
resids <- resid(mod)In summary(mod), the row for x gives the estimate of \(b_1\),its standard error, t statistic, and p-value for testing \(\beta_1 = 0\). The cor() function gives the sample correlation \(r\) between x and y.
# Quick diagnostic plots
par(mfrow = c(2, 2))
plot(mod) # residuals vs fitted, QQ-plot, etc.Use residual plots to check linearity, equal spread, and normality assumptions before trusting inference on the slope.
Assume you exported your Garmin data to a CSV file called Steps.csv with two columns:Week (1, 2, 3, ...) and Steps (total steps that week). This base R code fits a simple linear regression, computes the correlation, and draws the line of best fit.
dat <- read.csv("Steps.csv") # load your Garmin steps data
# Quick sanity check
head(dat)
# Fit simple linear regression
mod <- lm(Steps ~ Week, data = dat)
# Correlation between Week and Steps
cor(dat$Week, dat$Steps)
summary(mod)
# Plot with regression line
plot(dat$Week, dat$Steps,
pch = 19,
xlab = "Week",
ylab = "Steps",
main = "Do my weekly steps change over time?")
abline(mod, col = "red", lwd = 2)To reuse this example: keep the structure the same, but change the file name and the variable names(Week, Steps) to match your own dataset.
Good practice: always pair numerical summaries (slope, \(R^2\), p-value) with graphical checks and a clear contextual conclusion.