- [Charlie] All right, in this screencast
I'm going to tell you all about
the adjusted R squared parameter.
I think a lot of you are probably familiar
with just the R squared
the correlation coefficient R squared.
So when you perform a fit, you can get the R squared
and you want that to be close to one.
Now, a lot of people just use R squared
and they think that making a model
that maximizes R squared is the best thing to do.
So right here I've got some data
some xy data, and I fit this the experimental data in blue
to a second order model.
And we get a pretty good model.
We can keep adding terms into the model.
So we can make a third order, polynomial,
a fourth order polynomial, and so on and we can keep going.
And if we continue to do that
and we went up to a sixth order polynomial
you would see that the R squared is exactly one.
In fact, whenever you have seven data points
and seven parameters a sixth order polynomial
actually has seven parameters
because it has an intercept also.
So when the number of data points matches
the number of parameters
then the model exactly fits your data.
And the R squared which is the correlation coefficient,
is identically one.
So the model would perfectly fit the data.
However, if you're not being careful,
you wouldn't realize that if you're not
on one of the experimental data points.
For example here, if I'm at about 0.3 here
that the fit is way off of what you would expect
from the rest of the data, sort of it doesn't really fit in.
So the model does really weird things between data points,
experimental data points, but then it always matches exactly
when we have n data points and an n parameter model.
So obviously relying upon R squared
is not the best thing to do if you're trying
to create a general model that fits the data well.
So obviously, this is not a good fit.
Instead, what we use
is this parameter known as R squared adjusted.
I'll show you a little bit about this in a minute.
So here I've got just first order through fifth order
polynomial and I've plotted the R squared
also the adjusted R squared here.
As I just mentioned the R squared continues to climb
and it'll reach 1.0.
But if we look at the adjusted R squared
which is what you wanna base your goodness of fit
for the models off of.
So you want to choose a model
that has a high adjusted R squared.
We see that the second or third order polynomial
is the best for the adjusted R squared.
And that's what you wanna use
for your regression toolbox project.
You wanna output the adjusted R squared.
How do you calculate R squared and adjusted R squared?
Well, on the left here
this should be the last time you ever see R squared
because we're not gonna want to use that.
But it's just one minus something known as sum squared
the error over the total sum of squares.
And I'll explain what those mean.
But the adjusted R squared is equal to one minus
and we sort of normalize the SSE
and the SST to the number of parameters.
So as the number of parameters increases,
as P increases what we end up doing
is actually we decrease adjusted R square.
So you penalize the user
for increasing the number of parameters in the model.
So the adjusted R squared for statisticians
is preferred because it penalizes for more terms
in order to calculate SSE, again,
that's known as the error sum of squared.
We need to calculate something known as the residuals.
The residuals
are the difference between the model predictions
the model predictions here in the green.
So that's basically our our y hat.
And then the yi are the actual observations.
So we might say, for example, that might be y2
but if we plugged in the model
and came up with a prediction,
we would end up with y2 hat.
So the residual is the difference
between the prediction in green
and the experimentally observed value.
So this would be e2 and in this case is gonna be negative.
For the next one, this is y3
and the experimental prediction there
would be y hat of three.
And in that case, the residual would be e3.
That's the difference
between the predicted and the observed.
And that's gonna be a positive value.
So it's sort of like this little difference.
And obviously for you to have a really good model
what you want are these residuals as a whole to be small.
Now, to eliminate the effects of positive
and negative residuals, we wanna sort of just take
the absolute value or you can square it.
So what we do is we square the residuals
and sum those up for all of the observations,
and that's known as this SSE.
To calculate this SST value, we sort of do a similar thing
but what we do is we look to see how far each observation.
So that is y went one up there is from the average
which is y average you just take the average
of all the y values.
So this would be the component of SST attributable
to the first data point.
We would then have another component,
which would be y2 minus the average.
And we keep doing that similar
to what we did with the residuals.
We're gonna take the difference and we're gonna square it
just so the positive and negatives
don't cancel each other out.
And when we sum all of those differences
when they're squared,
this is known as the total sum of squares SST.
So we can take SST and SSE
along with the number of parameters.
So for a second order polynomial
we have something of the form y equals
beta not plus beta one x plus beta two x squared.
That's a three parameter model.
So in this case, p would equal three.
And so we'd plug that in all those values
into this equation here and that's how you can calculate
adjusted R squared.