Lecture 25 – Regression and Least Squares
DSC 10, Fall 2022
Announcements
Lab 8 is due on Saturday 11/26 at 11:59pm.
The Final Project is due on Tuesday 11/29 at 11:59pm.
No class or office hours on Thursday or Friday. Happy Thanksgiving! 🦃
There are several study sessions/group office hours in Week 10, which should be helpful as you complete the final project and study for the final exam. These are marked in green on the calendar.
Monday 11/28 from 12-2pm in PCNYH 122.
Tuesday 11/29 from 7-9pm in SDSC Auditorium (with no heat 🥶; dress warmly 🧣).
Wednesday 11/30 from 3-7pm in SDSC Auditorium (with no heat 🥶; dress warmly 🧣).
Friday 12/2 from 5-9pm in WLH 2205.
Lecture section C00 is not meeting today or Monday 11/28 – Suraj is in India 🇮🇳.
Agenda
The regression line, in standard units.
The regression line, in original units.
Outliers.
Errors in prediction.
The regression line, in standard units
Example: Predicting heights 👪 📏
Recall, in the last lecture, we aimed to use a mother's height to predict her adult son's height.
Correlation
Recall, the correlation coefficient of two variables and is defined as the
average value of the
product of and
when both are measured in standard units.
The regression line
The regression line is the line through with slope , when both variables are measured in standard units.

We use the regression line to make predictions!
Making predictions in standard units

If , and the given is in standard units, then the prediction for is standard units.
The regression line predicts that a mother whose height is SDs above average has a son whose height is SDs above average.
We always predict that a son will be somewhat closer to average in height than his mother.
This is a consequence of the slope having magnitude less than 1.
This effect is called regression to the mean.
The regression line passes through the origin in standard units. This means that, no matter what is, for an average value, we predict an average value.
Making predictions in original units
Of course, we'd like to be able to predict a son's height in inches, not just in standard units. Given a mother's height in inches, here's how we'll predict her son's height in inches:
Convert the mother's height from inches to standard units.
$$x_{i \: \text{(su)}} = \frac{x_i - \text{mean of $xParseError: KaTeX parse error: Expected 'EOF', got '}' at position 1: }̲}{\text{SD of xParseError: KaTeX parse error: Expected 'EOF', got '}' at position 1: }̲}$
Multiply by the correlation coefficient to predict the son's height in standard units.
Convert the son's predicted height from standard units back to inches.
$$\text{predicted } y_i = \text{predicted } y_{i \: \text{(su)}} \cdot \text{SD of $yParseError: KaTeX parse error: Expected 'EOF', got '}' at position 1: }̲ + \text{mean o…yParseError: KaTeX parse error: Expected 'EOF', got '}' at position 1: }̲$
Concept Check ✅ – Answer at cc.dsc10.com
A course has a midterm (mean 80, standard deviation 15) and a really hard final (mean 50, standard deviation 12).
If the scatter plot comparing midterm & final scores for students looks linearly associated with correlation 0.75, then what is the predicted final exam score for a student who received a 90 on the midterm?
A. 54
B. 56
C. 58
D. 60
E. 62
The regression line, in original units
Reflection
Each time we wanted to predict the height of an adult son given the height of a mother, we had to:
Convert the mother's height from inches to standard units.
Multiply by the correlation coefficient to predict the son's height in standard units.
Convert the son's predicted height from standard units back to inches.
This is inconvenient – wouldn't it be great if we could express the regression line itself in inches?
From standard units to original units
When and are in standard units, the regression line is given by

What is the regression line when and are in their original units?

The regression line in original units
We can work backwards from the relationship to find the line in original units.
Note that , , , and are constants – if you have a DataFrame with two columns, you can determine all 5 values.
Re-arranging the above equation into the form yields the formulas:
is the slope of the regression line and is the intercept.
Let's implement these formulas in code and try them out.
Below, we compute the slope and intercept of the regression line between mothers' heights and sons' heights (in inches).
So, the regression line is
Making predictions
What's the predicted height of a son whose mother is 62 inches tall?
What if the mother is 55 inches tall? 73 inches tall?
Outliers
The effect of outliers on correlation
Consider the dataset below. What is the correlation between and ?
Removing the outlier
Takeaway: Even a single outlier can have a massive impact on the correlation, and hence the regression line. Look for these before performing regression. Always visualize first!
Errors in prediction
Motivation
We've presented the regression line in standard units as the line through the origin with slope , given by . Then, we used this equation to find a formula for the regression line in original units.
In examples we've seen so far, the regression line seems to fit our data pretty well.
But how well?
What makes the regression line good?
Would another line be better?
Example: Without the outlier
We think our regression line is pretty good because most data points are pretty close to the regression line. The red lines are quite short.
Measuring the error in prediction
Typically, some errors are positive and some negative.
What does a positive error mean? What about a negative error?
To measure the rough size of the errors, for a particular set of predictions:
Square the errors so that they don't cancel each other out.
Take the mean of the squared errors.
Take the square root to fix the units.
This is called root mean square error (RMSE).
Notice the similarities to computing the SD!
Root mean squared error (RMSE) of the regression line's predictions
The RMSE of the regression line's predictions is about 2.2. Is this big or small, relative to the predictions of other lines? 🤔
Root mean squared error (RMSE) in an arbirtrary line's predictions
We've been using the regression line to make predictions. But we could use a different line!
To make a prediction for
xusing an arbitrary line defined byslopeandintercept, computex * slope + intercept.
For this dataset, if we choose a different line, we will end up with different predictions, and hence a different RMSE.
Let's compute the RMSEs of several different lines on the same dataset.
Finding the "best" prediction line by minimizing RMSE
RMSE describes how well a line fits the data. The lower the RMSE of a line is, the better it fits the data.
There are infinitely many slopes and intercepts, and thus infinitely many RMSEs. How do we find which combination of slope and intercept have the lowest RMSE?
If you take DSC 40A, you'll learn how to do this using calculus. For now, we'll use a function –
minimize.
Aside: minimize
The function
minimizetakes in a function as an argument, and returns the inputs to the function that produce the smallest output.
For instance, we know that the minimizing input to the function is .
minimizecan find this, too:
The
minimizefunction uses calculus and intelligent trial-and-error to find these inputs; you don't need to know how it works under the hood.
Finding the "best" prediction line by minimizing RMSE
We'll use minimize on rmse, to find the slope and intercept of the line with the smallest RMSE.
Do these numbers look familiar?
Coincidence?
The slopes and intercepts we got using both approaches look awfully similar... 👀
The regression line is the best line!
It turns out that the regression line we defined before before minimizes the root mean squared error (RMSE) among all lines.
It is the best line, regardless of what our data looks like!
All equivalent names:
Line of “best fit”.
Least squares line.
Regression line.
The technique of finding the slope and intercept that have the lowest RMSE is called the method of least squares.
Quality of fit
The regression line describes the "best linear fit" for a given dataset.
The formulas for the slope and intercept work no matter what the shape of the data is.
But the line is only meaningful if the relationship between and is roughly linear.
Example: Non-linear data
What's the regression line for this dataset?
This line doesn't fit the data at all!
Summary, next time
Summary
The regression line in original units is , where
This line is very sensitive to outliers.
This line has the lowest root mean squared error (RMSE) of all possible lines.
It is the "line of best fit".
Next time
As we saw, the regression line is the best line to fit the data, but not all data is linear.
How do we determine whether fitting a line even makes sense for our dataset?
When we use regression, we're making predictions based on data in a sample. What if we had a different sample?