Main content

## More on regression

Current time:0:00Total duration:6:47

# Squared error of regression line

## Video transcript

In the next few videos I'm going
to embark on something that will just result in
a formula that's pretty straightforward to apply. And in most statistics classes,
you'll just see that end product. But I actually want to show
how to get there. But I just want to warn
you right now. It's going to be a lot
of hairy math, most of it hairy algebra. And then we're actually going
have to do a little bit of calculus near the end. We're going to have to do a
few partial derivatives. So if any of that sounds
daunting, or sounds like something that will discourage
you in some way, you don't have to watch it. You could skip to the end and
just get the formula that we're going to derive. But I, at least, find it
pretty satisfying to actually derive it. So what we're going to think
about here is, let's say we have n points on a
coordinate plane. And they all don't have to
be in the first quadrant. But just for simplicity of
visualization, I'll draw them all in the first quadarant. So let's say I have this
point right over here. Let me do them in different
colors. And that coordinate is x1, y1. And then let's say I have
another point over here. The coordinates there
are x2, y2. And then I can keep
adding points. And I could keep drawing them. We'd just have a
ton of points. There and there and there. And we go all the way
to the nth point. Maybe it's over here. And we're just going to
call that xn, yn. So we have n points here. I haven't drawn all of
the actual points. But what I want to do is find
a line that minimizes the squared distances to these
different points. So let's think about it. Let's visualize that
line for a second. So there's going to
be some line. And I'm going to try to draw a
line that kind of approximates what these points are doing. So let me draw this line here. So maybe the line might look
something like this. I'm going to try my best
to approximate it. Actually, let me draw it
little bit different. Maybe it looks something
like that. I don't even know what it
looks like right now. And what we want to do is
minimize this squared error from each of these points
to the line. So let's think about
what that means. So if the equation of this line
right here is y is equal to mx plus b. And this just comes straight
out of Algebra 1. This is the slope on the line,
and this is the y-intercept. This is actually
the point 0, b. What I want to do, and that's
what the the topic of the next few videos are going to be, I
want to find an m and a b. So I want to find these two
things that define this line. So that it minimizes
the squared error. So let me define what
the error even is. So for each of these points, the
error between it and the line is the vertical distance. So this right here we
can call error one. And then this right here
would be error two. It would be the vertical
distance between that point and the line. Or you can think of it as the y
value of this point and the y value of the line. And you just keep going all
the way to the endpoint between the y value of
this point and the y value of the line. So this error right here, error
one, if you think about it, it is this value right
here, this y value. It's equal to y1 minus
this y value. Well what's this y value
going to be? Well over here we have
x is equal to x1. And this point is the
point m x1 plus b. You take x1 into this equation
of the line and you're going to get this point
right over here. So that's literally going to
be equal to m x1 plus b. That's that first error. And we can keep doing it
with all the points. This error right over here
is going to be y2 minus m x2 plus b. And then this point right
here is m x2 plus b. The value when you take
x2 into this line. And we keep going all the
way to our nth point. This error right here is going
to be yn minus m xn plus b. Now, so if we wanted to just
take the straight up sum of the errors, we could just
some these things up. But what we want to do is a
minimize the square of the error between each of these
points, each of these n points on the line. So let me define the squared
error against this line as being equal to the sum of
these squared errors. So this error right here, or
error one we could call it, is y1 minus m x1 plus b. And we're going to square it. So this is the error
one squared. And we're going to go to
error two squared. Error two squared is y2
minus m x2 plus b. And then we're going to
square that error. And then we keep going, we're
going to go n spaces, or n points I should say. We keep going all the way
to this nth error. The nth error is going to
be yn minus m xn plus b. And then we're going
to square it. So this is the squared
error of the line. And over the next few videos, is
I want to find the m and b that minimizes the squared error
of this line right here. So if you viewed this as the
best metric for how good a fit a line is, we're going to try to
find the best fitting line for these points. And I'll continue in
the next video. Because I find that with these
very hairy math problems, it's good to kind of just deliver
one concept at a time. And it also minimizes my probability of making a mistake.