1 of 13

Lecture 31

Least Squares

DATA 8

Spring 2024

2 of 13

Announcements

  • Project 2 checkpoint due today at 5 pm!
    • Final deadline on April 12th
  • Homework 10 due 04/10
    • Chapter 15 of the textbook is helpful!

​

3 of 13

Regression Roadmap

  • Monday
    • How to measure linear association
  • Wednesday
    • Predicting one numerical variable from a another
    • The regression line
  • Today
    • The “best” linear predictor
    • The method of least squares

4 of 13

Linear Regression

5 of 13

Prediction

Goal: Predict y using x

Examples:

  • Predict # hospital beds available using air pollution
  • Predict house prices using house size
  • Predict # app users using # app downloads

​

So far we have been using numerical variables but it is possible to use categorical variables in the regression as well

​

6 of 13

Regression Estimate

Goal: Predict y using x

​

To find the regression estimate of y:

  • Convert the given x to standard units
  • Multiply by r
  • That’s the regression estimate of y, but:
    • It’s in standard units
    • So convert it back to the original units of y

7 of 13

Slope and Intercept (in original units)

estimate of y = slope * x + intercept

8 of 13

Discussion Question

A course has a midterm (average 70; standard deviation 10)�and a really hard final (average 50; standard deviation 12)

If the scatter diagram comparing midterm & final scores for students has an oval shape with correlation 0.75, then...

What do you expect the average final score would be for students who scored 60 on the midterm?

9 of 13

Discussion Question

Suppose we use linear regression to predict candy prices (in dollars) from sugar content (in grams). What are the units of each of the following?

  • r

​

  • The slope

​

  • The intercept

10 of 13

Least Squares

(Demo)

11 of 13

Error in Estimation

  • error = actual value − estimate

​

  • Typically, some errors are positive and some negative

​

  • To measure the rough size of the errors
    • square the errors to eliminate cancellation
    • take the mean of the squared errors
    • take the square root to fix the units
    • root mean square error (rmse)

(Demo)

12 of 13

Numerical Optimization

  • Numerical minimization is approximate but effective
  • Lots of machine learning uses numerical minimization
  • If the function mse(a, b)returns the mse of estimation using the line “estimate = ax + b”,
    • then minimize(mse)returns array [a₀, b₀]
    • a₀ is the slope and b₀ the intercept of the line that minimizes the mse among lines with arbitrary slope a and arbitrary intercept b (that is, among all lines)

(Demo)

13 of 13

Least Squares Line

  • Minimizes the root mean squared error (rmse) among all lines
  • Equivalently, minimizes the mean squared error (mse) among all lines
    • Minimizing mean squared error gives us the regression line
    • Regression line is the only estimate that minimizes the MSE.
  • Names:
    • “Best fit” line
    • Least squares line
    • Regression line