

Linear Models I: Introduction
Megan Ayers
Math 141 | Spring 2026
Monday, Week 4
Descriptive: Want to estimate quantities (summary statistics) related to the population.
→ How many trees are in the Amazon?
Predictive: Want to predict the value of a variable.
→ Can I use remotely sensed data to predict forest types in the Amazon?
Causal: Want to determine if changes in a variable cause changes in another variable.
→ Do financial contracts prevent people from deforesting their land in the Amazon?
We will focus mainly on descriptive modeling in this course, and occasionally on predictive modeling (take Math 243: Statistical Learning to learn more). If you want to learn more about causality, take Math 394: Causal Inference.
\[ y = f(x) + \epsilon \]
where \(\epsilon\) represents an error term.
Goal:
Determine a reasonable form for \(f()\). (Ex: Line, curve, …)
Estimate \(f()\) with \(\widehat{f}()\) using the data.
Generate predicted values: \(\widehat y = \widehat{f}(x)\).
Consider this model when:
Response variable \((y)\): quantitative
Explanatory variable \((x)\): quantitative
AND, \(f()\) can be approximated by a line.
“The social contract of Halloween is simple: Provide adequate treats to costumed masses, or be prepared for late-night tricks from those dissatisfied with your offer. To help you avoid that type of vengeance, and to help you make good decisions at the supermarket this weekend, we wanted to figure out what Halloween candy people most prefer. So we devised an experiment: Pit dozens of fun-sized candy varietals against one another, and let the wisdom of the crowd decide which one was best.” – Walt Hickey
“While we don’t know who exactly voted, we do know this: 8,371 different IP addresses voted on about 269,000 randomly generated matchups. So, not a scientific survey or anything, but a good sample of what candy people like.”

Rows: 85
Columns: 13
$ competitorname <chr> "100 Grand", "3 Musketeers", "One dime", "One quarter…
$ chocolate <dbl> 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,…
$ fruity <dbl> 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1,…
$ caramel <dbl> 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,…
$ peanutyalmondy <dbl> 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
$ nougat <dbl> 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,…
$ crispedricewafer <dbl> 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
$ hard <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1,…
$ bar <dbl> 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,…
$ pluribus <dbl> 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,…
$ sugarpercent <dbl> 73.2, 60.4, 1.1, 1.1, 90.6, 46.5, 60.4, 31.3, 90.6, 6…
$ pricepercent <dbl> 0.860, 0.511, 0.116, 0.511, 0.511, 0.767, 0.767, 0.51…
$ winpercent <dbl> 66.97173, 67.60294, 32.26109, 46.11650, 52.34146, 50.…
Linear trend?
Direction of trend?

\[ y = \beta_0 + \beta_1 x + \epsilon \]

We want to find \(\beta_1\) (slope) and \(\beta_0\) (intercept) so that the line fits our data well
→ Need summary statistics that quantify the strength and relationship of the linear trend
→ These will help us find a value for slope and determine how well a line fits our data

Measures the strength and direction of linear relationship between two quantitative variables
Symbol: \(r\)
Always between -1 and 1
Sign indicates the direction of the relationship
Magnitude indicates the strength of the linear relationship
\(r\) is calculated using the sample means (\(\bar{x}\), \(\bar{y}\)) and standard deviations (\(s_x\), \(s_y\)) of the variables \(x\) and \(y\):
\[r = \frac{1}{s_x s_y} \cdot \frac{1}{n-1} \sum_{i =1}^n (x_i - \bar{x} ) (y_i - \bar{y} ) \]
Sample Correlation Coefficient: \[ r = \frac{1}{s_x s_y} \cdot \frac{1}{n-1} \sum_{i =1}^n (x_i - \bar{x} ) (y_i - \bar{y} ) \]
Sample Covariance:
\[ cov(x, y) = r \times s_x s_y = \frac{1}{n-1} \sum_{i =1}^n (x_i - \bar{x} ) (y_i - \bar{y} ) \]
The sample correlation coefficient is a standardized sample covariance, which is what causes it to only take values from -1 to 1. The sample covariance can take any real value.
Q: Which will have the largest positive correlation?
Q: Which will have the largest negative correlation?
Q: Which will have correlation closest to 0?
A: 0.7568 B: -0.2172 C: -0.5373 D: -0.1133 E: 0.863 F: -0.8343

# A tibble: 12 × 2
dataset cor
<chr> <dbl>
1 away -0.0641
2 bullseye -0.0686
3 circle -0.0683
4 dino -0.0645
5 dots -0.0603
6 h_lines -0.0617
7 high_lines -0.0685
8 slant_down -0.0690
9 star -0.0630
10 v_lines -0.0694
11 wide_lines -0.0666
12 x_shape -0.0656

Let’s return to the Candy Example.
A line is a reasonable model form.
Where should the line be?

\[ \begin{align} y &= f(x) + \epsilon \\ y &= \beta_0 + \beta_1 x + \epsilon \end{align} \]
\[ y = \beta_0 + \beta_1 x + \epsilon \]
\[ \widehat{y} = \widehat{\beta}_0 + \widehat{\beta}_1 x \]
Need two key definitions:
\[ \widehat{y}_i = \widehat{\beta}_0 + \widehat{\beta}_1 x_i \]
\[ e_i = y_i - \widehat{y}_i \]
Goal: Pick values for \(\widehat{\beta}_0\) and \(\widehat{\beta}_1\) so that the residuals are small!
Want residuals to be small.
Minimize a function of the residuals.
Minimize:
\[ \sum_{i = 1}^n e^2_i \]

Sidenote:
Suppose \(n\) observations of \(x\) and \(y\) are collected: \((x_1, y_1), \ldots, (x_n, y_n)\).
It turns out there are specific values of \(\widehat{\beta}_0\) and \(\widehat{\beta}_1\) that minimize the sum of squared residuals, given this data:
\[ \begin{align} \widehat{\beta}_1 &= \frac{ \sum_{i = 1}^n (x_i - \bar{x}) (y_i - \bar{y})}{ \sum_{i = 1}^n (x_i - \bar{x})^2} \\ \widehat{\beta}_o &= \bar{y} - \widehat{\beta}_1 \bar{x} \end{align} \] where
\[ \begin{align} \bar{y} = \frac{1}{n} \sum_{i = 1}^n y_i \quad \mbox{and} \quad \bar{x} = \frac{1}{n} \sum_{i = 1}^n x_i \end{align} \]
Suppose \(n\) observations of \(x\) and \(y\) are collected: \((x_1, y_1), \ldots, (x_n, y_n)\).
It turns out there are specific values of \(\widehat{\beta}_0\) and \(\widehat{\beta}_1\) that minimize the sum of squared residuals, given this data:
\[ \begin{align} \widehat{\beta}_1 &= \frac{ \sum_{i = 1}^n (x_i - \bar{x}) (y_i - \bar{y})}{ \sum_{i = 1}^n (x_i - \bar{x})^2} = \frac{s_y}{s_x}r \\ \widehat{\beta}_o &= \bar{y} - \widehat{\beta}_1 \bar{x} \end{align} \] where
\[ \begin{align} \bar{y} = \frac{1}{n} \sum_{i = 1}^n y_i \quad \mbox{and} \quad \bar{x} = \frac{1}{n} \sum_{i = 1}^n x_i \end{align} \]
Once we know \(\widehat{\beta}_0\) and \(\widehat{\beta}_1\), we can estimate the whole function with:
\[ \widehat{y} = \widehat{\beta}_0 + \widehat{\beta}_1 x \]
Called the least squares line, regression line, or the line of best fit.
We need to be precise and careful when interpreting \(\beta_0\) and \(\beta_1\) (and their estimates)
Unless experimental data is involved, avoid causal language.
ggplot2 will compute the line and add it to your plot using geom_smooth(method = "lm")

We can calculate the exact values of \(\widehat{\beta}_0\) and \(\widehat{\beta}_1\) using our formulas, by hand or in R
\[ \widehat{y} = \widehat{\beta}_0 + \widehat{\beta}_1 x \] In this case, \(\widehat{\beta}_0 = 44.6094\) and \(\widehat{\beta}_1 = 0.1192\).

\[ \widehat{y} = \widehat{\beta}_0 + \widehat{\beta}_1 x \] In this case, \(\widehat{\beta}_0 = 44.6094\) and \(\widehat{\beta}_1 = 0.1192\).

sugarpercent = 73. What does the model predict for winpercent?\[ \widehat{y} = \widehat{\beta}_0 + \widehat{\beta}_1 x \] In this case, \(\widehat{\beta}_0 = 44.6094\) and \(\widehat{\beta}_1 = 0.1192\).

sugarpercent = 73. What does the model predict for winpercent?winpercents we see for candies with sugarpercent = 73.We can visualize the accuracy of a linear model using residual plots:


lab_difficulty?R to compute the least squares line and the values of \(\widehat{\beta}_0\) and \(\widehat{\beta}_1\).