1 of 15

Source: xkcd.com/1725

2 of 15

Lecture 31

Least Squares

DATA 8

Fall 2022

3 of 15

Announcements

  • Project 2 checkpoint due today at 11pm!
    • Full project due Friday 11/11
    • Early submission bonus Thursday 11/10
  • Homework 10 due 11/9

4 of 15

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

5 of 15

Linear Regression

6 of 15

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

7 of 15

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

8 of 15

Slope and Intercept (in original units)

estimate of y = slope * x + intercept

9 of 15

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?

10 of 15

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

11 of 15

Least Squares

(Demo)

12 of 15

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)

13 of 15

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)

14 of 15

Least Squares Line

  • Minimizes the root mean squared error (rmse) among all lines

  • Equivalently, minimizes the mean squared error (mse) among all lines

  • Names:
    • “Best fit” line
    • Least squares line
    • Regression line

15 of 15

Non-Linear Regression

(Demo)