1 of 15

Lecture 33

Least Squares

DATA 8

Fall 2019

2 of 15

Source: xkcd.com/1725

3 of 15

Regression roadmap

  • Monday:
    • Association and correlation
  • Wednesday:
    • Prediction, scatterplots and lines
  • Today:
    • Least squares: finding the “best” line for a dataset
  • Next Monday:
    • Residuals: analyzing mistakes and errors
  • After Thanksgiving:
    • Regression inference: understanding uncertainty

4 of 15

Announcements

5 of 15

Linear Regression (Review)

6 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

7 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 90 on the midterm?

How about 60 on the midterm?

(Demo)

​

8 of 15

Regression Line

Standard Units

(0, 0)

1

r

Original Units

(Average x,� Average y)

SD x

r * SD y

9 of 15

Slope and Intercept

estimate of y = slope * x + intercept

(Demo)

10 of 15

Regression Line

11 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

12 of 15

Least Squares

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

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

(Demo)

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