Simple Linear Regression

Modeling the Relationship Between Two Quantitative Variables

Today: We model how a response variable changes, on average, as anexplanatory variable changes, using a straight-line model plus random scatter.

Where Does Regression Fit in Inference?

This chapter extends our inference toolbox to relationships betweentwo quantitative variables.

One Mean (z / t)Two Means (independent / paired)Proportions (one / two)Chi-Square (categorical)Simple Linear RegressionANOVA

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.

Learning Goals

  • Identify response and explanatory (predictor) variables in a regression setting.
  • Describe a linear trend from a scatterplot and correlation.
  • Write and interpret the least-squares regression line \(\hat{y} = b_0 + b_1 x\).
  • Understand residuals and use residual plots to check model conditions.
  • Use software output to perform inference on the slope and interpret results in context.

Recap: Scatterplots & Correlation

A scatterplot shows paired data \((x,y)\) for two quantitative variables.The correlation \(r\) measures the strength and direction of alinear association.

  • \(r\) is between \(-1\) and \(+1\); values near \(\pm 1\) indicate strong linear patterns,values near 0 indicate weak linear association.
  • Correlation is unitless and symmetric in \(x\) and \(y\).
  • Correlation alone does not show form, outliers, or non-linear patterns —always look at the scatterplot.

y-Intercept and Slope

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.

  • \(b_1 > 0\): Line slopes upward as \(x\) increases.
  • \(b_1 < 0\): Line slopes downward as \(x\) increases.
  • \(b_1 = 0\): Horizontal line; \(y\) does not change with \(x\).
1.5
0.0

Drag the sliders to see how the line moves. Try a positive, negative, and zero slope.

Scatterplots & Example Data

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.

Age and Price Data for 11 Used Cars (Orions)

We will study how the age of a car (years) relates to its price (in \$100 units).

CarAge (years) \(x\)Price (\$100) \(y\)
1585
24103
3670
4582
5589
6598
7666
8695
92169
10770
11748

Question: As car age increases, how does price tend to change? The scatterplot will help us see the pattern and any outliers.

Scatterplot: Orion Age vs. Price

The point at (Age 2, Price 169) is a clear outlier. Most cars are 5–7 years old with lower prices.

  • Horizontal axis: Age (years), roughly 1–8.
  • Vertical axis: Price (in \$100), roughly 10–180.
  • Most points cluster between ages 5 and 7 with prices from about 50 to 100 (hundreds of dollars).
  • There is a general negative trend: older cars tend to have lower prices.
  • The point (2, 169) is much higher than the rest and will have a strong influence on the regression line.

Three Data Points & Two Possible Lines

Toy Dataset

Consider just three points:\((1, 3), (2, 1), (3, 5)\).We will compare two candidate lines.

  • Line A: \(y = -1 + 2x\)
  • Line B: \(y = 1 + x\)

Compare Sum of Squared Errors

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.

Least-Squares Criterion & Regression Line

The least-squares criterion says that the best-fitting line is the one with thesmallest possible sum of squared residuals:

$$\sum e^2 = \sum (y_i - \hat{y}_i)^2 \quad \text{is as small as possible.}$$

The resulting line is called the regression line, and its equation\(\hat{y} = b_0 + b_1 x\) is the regression equation.

Notation for Regression Calculations

Sums of Squares & Cross-Products

For a set of \(n\) data points \((x_i, y_i)\):

QuantityDefining formulaComputing 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}\)

Formulas for the Regression Equation

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.

Computation for the Orion Regression Line

Summary Calculations

Using the Orion age–price data, we compute the sums needed for \(S_{xx}\) and \(S_{xy}\).

CarAge \(x\)Price \(y\)\(xy\)\(x^2\)
158542525
2410341216
367042036
458241025
558944525
659849025
766639636
869557036
921693384
1077049049
1174833649
Sum589754732326

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).

Orion Regression Line on the Scatterplot

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\).

  • The negative slope reflects that older cars tend to have lower prices.
  • The outlier at (2, 169) pulls the regression line upward and makes the slope more negative.
  • When we remove that influential observation, the line is flatter and shifted downward.
  • This illustrates how a single extreme point can influence the regression line.

Response and Predictor Variables

In regression:

  • Response variable (dependent variable): the variable we want to model or predict.
  • Predictor variable (explanatory variable): the variable used to explain or predict the response.

In the Orion example, Price is the response variable and Age is the predictor variable.

Extrapolation Beyond the Data

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.

Warning

  • Regression lines are most reliable for predicting within the range of the observed \(x\)-values.
  • Always check where your \(x\)-value lies relative to the data used to fit the model.

Sums of Squares in Regression

Three Sums of Squares

  • Total sum of squares (SST): total variation in \(y\)$$\mathrm{SST} = \sum (y_i - \overline{y})^2.$$
  • Regression sum of squares (SSR): variation in \(y\) explained by the regression$$\mathrm{SSR} = \sum (\hat{y}_i - \overline{y})^2.$$
  • Error sum of squares (SSE): unexplained variation$$\mathrm{SSE} = \sum (y_i - \hat{y}_i)^2.$$

These satisfy the regression identity:$$\mathrm{SST} = \mathrm{SSR} + \mathrm{SSE}.$$

Example: SST, SSR, SSE for 3 Points

Using the Toy Dataset and Line B (\(y = 1 + x\))

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\)
13200-1111
213-2400-24
354241111
Sum826

Here, \(\mathrm{SST} = 8\), \(\mathrm{SSR} = 2\), \(\mathrm{SSE} = 6\), and indeed \(8 = 2 + 6\).

Computing Formulas for SST, SSR, SSE

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}.$$

Computation of SST and SSR for Orion Data

Adding a \(y^2\) Column

CarAge \(x\)Price \(y\)\(xy\)\(x^2\)\(y^2\)
1585425257225
241034121610609
3670420364900
4582410256724
5589445257921
6598490259604
7666396364356
8695570369025
92169338428561
10770490494900
11748336492304
Sum58975473232696,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\).

Linear Correlation Coefficient \(r\)

For a set of paired data \((x_i, y_i)\), the linear correlation coefficient \(r\) is defined by

$$r = \frac{\frac{1}{n-1} \sum (x_i - \overline{x})(y_i - \overline{y})}{s_x s_y}$$where \(s_x\) and \(s_y\) are the sample standard deviations of the x- and y-values.

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]}}.$$

Correlation, \(r\), and Coefficient of Determination, \(R^2\)

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.$$

  • \(r\) can be positive or negative, indicating direction of linear association.
  • \(R^2\) is always between 0 and 1 and describes how much of the variation in \(y\)is explained by the linear model.
  • For example, \(R^2 = 0.64\) means 64% of the variation in \(y\) is explained by the regression line;the remaining 36% is left in the residuals.
0.80

Move the slider to see stronger/weaker and positive/negative linear associations.

The Simple Linear Regression Model

$$y = \beta_0 + \beta_1 x + \varepsilon$$Population model (parameters \(\beta_0, \beta_1\))
  • \(y\): response variable; \(x\): explanatory (predictor) variable.
  • \(\beta_0\): intercept — mean of \(y\) when \(x = 0\) (if that value of \(x\) makes sense).
  • \(\beta_1\): slope — change in mean \(y\) for each 1-unit increase in \(x\).
  • \(\varepsilon\): random error (scatter) around the line.

Least-Squares Regression Line

$$\hat{y} = b_0 + b_1 x$$Sample regression line (statistics \(b_0, b_1\))
  • \(b_1\) and \(b_0\) are chosen to minimize the sum of squared vertical deviations(residuals) between observed \(y\) and fitted values \(\hat{y}\).
  • The line gives the predicted or fitted response \(\hat{y}\) for any given \(x\) in the data range.
  • In R output, the coefficient table lists estimates for the intercept and slope —these are \(b_0\) and \(b_1\).

Interpreting Slope & Intercept

Template Interpretations

  • Slope: “For each additional 1 unit in x, the model predicts that the mean of y changes by b units, on average.”
  • Intercept: “When x is 0, the model predicts that the mean ofy is b units.” (Only interpret if \(x=0\) is meaningful in context.)

Example (Conceptual)

Suppose we regress final exam score (y) on hours of weekly study (x) and obtain\(\hat{y} = 62 + 3.5x\).

  • Slope 3.5: For each additional hour of weekly study, predicted mean exam score increases by about 3.5 points.
  • Intercept 62: Predicted mean exam score for a student who studies 0 hours per week is 62 (may or may not be realistic).

Residuals & Model Conditions

A residual is the vertical difference between an observed value and its fitted value:$$e = y - \hat{y}.$$

  • Residual plots help us judge whether a linear model is reasonable.
  • For inference on the slope, usual conditions include:
    • Linearity: mean relationship is roughly straight in the scatterplot /residual vs fitted plot.
    • Independence: individual observations are independent (check design).
    • Equal spread: residuals show roughly constant variability across fitted values.
    • Normality: residuals are roughly normal (especially important for small \(n\)).

Inference on the Regression Slope

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)
  1. State the parameter: \(\beta_1\), the true slope relating mean response to x.
  2. Check regression conditions using plots and study design.
  3. Use software output: test statistic is usually labeled t with df = \(n - 2\).
  4. Obtain the p-value and decide whether the data provide evidence of a linear relationship.
  5. Interpret the slope and conclusion in the original context.

R Basics for Simple Linear Regression

Fit, Correlation, & Summarize the Model

# 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.

Diagnostic Plots

# 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.

Example: Weekly Steps in R

Using R to Model Weekly Steps

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.

Common Mistakes & Tips

Watch Out For:

  • Interpreting correlation or a significant slope as causation without an experiment.
  • Extrapolating far outside the range of observed x-values.
  • Ignoring outliers or influential points that can distort the slope.
  • Using a straight-line model when the pattern is clearly curved.
  • Forgetting to interpret the slope and intercept in context and with correct units.

Good practice: always pair numerical summaries (slope, \(R^2\), p-value) with graphical checks and a clear contextual conclusion.