Hello and welcome back.
Today we're talking about the R-squared.
A very important concept when it comes to evaluating the goodness
of fit of our models.
Now, in order to understand
r squared we're going to need to look at two versions of our chart.
So here is our data set.
And what we're going to do here is plot the regression as we were doing before.
And here's our data set again.
And this time we're going to just plot an average line.
And we'll see that in a second. So we'll start with the regression.
Let's draw our regression line as usual.
Let's project vertically our data points onto it.
And for each data point we're going to look at the difference between the y
the actual value and y hat the predicted value.
Now as we discussed before, the way we built this line is
we're minimizing this sum over here.
And that is the ordinary least squares method.
Well actually this sum has a name.
It's called the residual sum of squares.
And it's indicated by this abbreviation.
Now on the right here we're going to draw an average line.
And this is simply taking all of the y values.
So the actual values of our data set the y values
and taking their average.
Again we're going to project vertically our data points onto this line.
And for each data point we're going to look at y I.
And here we're going to calculate another total.
And this one is called
the total sum of squares and is indicated by this abbreviation.
And it's similar to the residual sum of squares.
But instead of using y I hat we're looking at the difference
between y I, the actual value and y average the average value
of of our data set.
And now we can calculate r squared.
R squared is defined as one minus the ratio
between the residual sum of squares and the total sum of squares.
Now let's pause here for a second and discuss this a bit.
So we know that we're minimizing the residual sum of squares.
We want to make it as small as possible.
And from these two images you can already see just by judging
based on the lengths of these
blue dotted blue dashed lines, we can see that
they're generally longer on the right with averages
and they're shorter where the regression is.
And that's because, we've designed our line to minimize these lengths.
So the sum of squared is smaller.
And so what that means is that the, residual
sum of squares is usually in most cases it's less than the total sum of squares.
So the way to think about it is in the total sum of squares on the right.
You're just putting an average line. You're not modeling anything.
This is the most rudimentary thing that we can do. is
just put our average line and approximate our data with that average line.
Of course it will be.
And it should be worse than any,
thought out model that we create, which is the example on the left.
So thereby, unless our regression model is facing the absolutely
the wrong way, for example, a downward slope on the left over here,
if our model was sloping downwards, then the residual
sum of squares would be huge because our model's just incorrect.
But in all other cases, the residual sum of squares is less than the total
sum of squares.
And what that means is that the ratio is less than one.
So r squared is somewhere between 0 and 1.
And the better our model fits the data, the smaller
the residual sum of squares will be, and that means the greater r squared will be.
So here's a quick rule of thumb for r squared.
Now bear in mind that it highly depends on the context.
And this rule of thumb is just for the practical tutorials
that we're looking at in this section of the course.
And here we go.
So if you have A1R squared of one that's a perfect fit.
That means, the residual sum of squares is zero.
And basically your line is going through
all the data points, which is virtually impossible.
So it's very suspicious if your r squared is about 0.9.
That's a very good model.
If your r squared is less than zero point.
So it's not a great model.
It's not the end of the world but not great.
If it's less than 0.4, it's quite a terrible model.
And if it's less than zero then the model makes no sense for this data.
As we discussed.
So there we go.
That's how the r squared works a very important concept to understand
because it's used quite a lot to evaluate models.
I look forward to seeing you next time.
And until then, enjoy machine learning.