Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Understanding Residuals and R-squared

Hello! Welcome back.

In our last lesson, we proved the Gauss-Markov theorem, which established that the OLS estimator is BLUE—the Best Linear Unbiased Estimator. This gives us confidence in the statistical properties of our estimated coefficients, . However, knowing our estimator is optimal doesn't tell us the full story. We also need to evaluate how well the resulting model fits the actual data.

Today's lesson addresses exactly that. Our learning outcome is to define residuals and derive the coefficient of determination (R-squared) as a measure of model fit. We will dissect the variation in our dependent variable and quantify the proportion of that variation our model successfully explains.

1. Defining Residuals

The first step in assessing model fit is to quantify the error for each data point. We call this error the residual.

The residual for the -th observation, denoted , is the difference between the actual observed value and the value predicted by our model, :

In vector notation, the vector of residuals is:

Residuals are fundamental because they represent the portion of the data that the model fails to explain. A key property, which follows directly from the OLS normal equations, is that if the model includes an intercept term, the sum of the residuals is exactly zero.

Lectures 9 & 10 - Ordinary Least Squares Estimation

The resource 'Lectures 9 & 10 - Ordinary Least Squares Estimation' provides a concise definition of residuals and this property.

Please read the short section titled 'Residuals' on page 5. It formally defines the residual and notes the property that their sum equals zero when a constant is in the model.

This property, , implies that the average residual is also zero. This means our regression line passes through the "center" of our data in a way that the positive and negative errors cancel each other out.

2. The Decomposition of Variance

To measure "goodness of fit," we need a way to compare the errors of our model to the total variability of the data. The core idea is to partition the total variation in the dependent variable, , into two parts: the variation explained by our regression model and the variation that remains unexplained (the residuals).

This is done using sums of squares:

  • Total Sum of Squares (TSS): Measures the total variation of the values around their sample mean, . This is the total amount of variation we aim to explain.
  • Explained Sum of Squares (ESS): Measures the variation of the fitted values, , around the sample mean, . This represents the variation in that is captured or "explained" by our model.
  • Residual Sum of Squares (RSS): Measures the variation of the data points around the fitted regression line. This is the sum of our squared residuals and represents the variation left "unexplained" by the model. This is also often called the Sum of Squared Errors (SSE).

The following image provides a helpful visual breakdown of these three components for a single data point.

R-Squared Explanation with Visual Components
This diagram illustrates the three key components of variance for a single data point \(y_i\). The Total Sum of Squares (TSS) relates to the total deviation from the mean (\(y_i - \bar{y}\)). The Explained Sum of Squares (ESS) relates to the deviation of the prediction from the mean (\(\hat{y}_i - \bar{y}\)). The Residual Sum of Squares (RSS) relates to the prediction error (\(y_i - \hat{y}_i\)).

These three quantities are linked by a fundamental identity:

This equation states that the total variation in can be perfectly decomposed into the variation explained by the model and the variation that remains unexplained. Proving this identity is the next crucial step.

3. Proof: TSS = ESS + RSS

Your desire for mathematical rigor will be well-served here. The proof is a classic piece of statistical derivation that demonstrates how the properties of OLS lead to this clean decomposition.

We start with the definition of TSS and algebraically manipulate it:

The entire proof now hinges on showing that the cross-product term, , is equal to zero. This is a non-trivial result that follows from the properties of the OLS estimates.

TSS = RSS + ESS | Simple Linear Regression

The video 'TSS = RSS + ESS' provides a clear, step-by-step algebraic proof that this cross-product term is zero.

Watch from 00:21 to the end (07:37). The presenter walks through the algebraic expansion we just did and then provides a rigorous proof that the cross-term vanishes. Follow the derivation closely; it relies on the definition of the OLS coefficients that we derived in earlier lessons.

The key steps in the proof that are:

  1. Expand the term: .
  2. The second part, , is zero because .
  3. The first part, , is also zero. This is a fundamental property of OLS residuals: they are uncorrelated with the fitted values. The video demonstrates this algebraically. In matrix form, this is because , since the residual-maker matrix and projection matrix are orthogonal ().

With the cross-term proven to be zero, the identity is established.

4. The Coefficient of Determination ()

Now that we have partitioned the total variance, we can define a standardized measure of fit. By dividing the identity by TSS, we get:

The coefficient of determination, or , is defined as the proportion of the total sum of squares that is explained by the model.

Using the identity above, we can also express it as:

This second form is often more intuitive: is 1 minus the proportion of variance that is unexplained.

To explore the definition and interpretation of in more detail, let's turn to another resource.

[Econometrics] Coefficient of Determination r2 | TSS = ESS + RSS | 10 |

The video '[Econometrics] Coefficient of Determination r2' provides a great overview of the R-squared statistic, including its interpretation and some alternative formulas.

Please watch from 06:45 to 16:02. This covers: The formal definition of R-squared (06:45-09:58). The interpretation of R-squared values of 0 and 1 (09:58-12:26). Alternative mathematical expressions for R-squared, connecting it to the regression coefficients and sample variances (12:26-16:02). This part should be particularly interesting given your background and goals.

Key properties of :

  • It is a proportion, so its value ranges from 0 to 1 (assuming the model includes an intercept).
  • : A perfect fit. All variation in is explained by the model. RSS = 0, meaning all data points lie perfectly on the regression line.
  • : The model has no explanatory power. ESS = 0, which means for all . The best prediction for is simply its average, and the independent variables provide no additional information.
  • An of, for example, 0.65 means that 65% of the variance in the dependent variable can be explained by the independent variables in the model.
Test your understanding!

A data scientist fits a simple linear regression model and finds an of 0.70. They believe they can improve the model, so they add five new independent variables and re-fit the regression. The new is 0.75.

Does this increase from 0.70 to 0.75 definitively prove that the second model is a better representation of the underlying relationship between the variables? Why or why not?

Show answer

No, it does not definitively prove the second model is better.

The coefficient of determination, , has a mathematical property that it can never decrease when new variables are added to the model. Since RSS is the value being minimized, adding more variables gives the OLS procedure more flexibility to reduce the sum of squared residuals, . At worst, if a new variable is completely useless, its coefficient will be estimated as zero (or very close to it), and the RSS will remain unchanged. More likely, the model will fit to random noise in the new variables, causing RSS to decrease slightly and to increase, even if the added variables have no true relationship with the dependent variable.

Therefore, the increase from 0.70 to 0.75 might simply be a mathematical artifact of adding more variables, not an indication of a genuinely better model. This phenomenon is a key reason why must be interpreted with caution and why other metrics like Adjusted or techniques like regularization are needed.

Conclusion

In this lesson, we transitioned from analyzing the properties of the OLS estimator to evaluating the performance of the overall regression model.

Key Takeaways:

  • Residuals () represent the unexplained error for each observation.
  • The total variation in the dependent variable can be decomposed into explained and unexplained parts: TSS = ESS + RSS. This identity is a direct consequence of the properties of OLS.
  • The coefficient of determination () is the ratio , representing the proportion of variance in that is explained by the model.
  • While useful, has a major limitation: it always increases (or stays the same) when more variables are added, making it a potentially misleading metric for model comparison.

Preview of the Next Lesson:
The tendency of to reward model complexity, even when that complexity is not justified, is a symptom of a larger problem known as overfitting. Our next lesson will introduce a powerful technique to combat this: regularization. We will formulate L2 (Ridge) regularization for linear regression and derive its solution, showing how it penalizes large coefficients to create simpler, more robust models.

Can't find a good explanation? Sign up and we'll make it for you

Sign up