How To Calculate The Sum Of Squared Residuals

9 min read

How to Calculate the Sum of Squared Residuals
The sum of squared residuals (SSR) is a fundamental statistic used to assess how well a regression model fits observed data. By quantifying the total deviation between actual outcomes and model predictions, SSR provides a clear numerical basis for comparing models, estimating parameters, and diagnosing fit quality. Understanding how to compute SSR is essential for anyone working with linear regression, econometrics, machine learning, or any field that relies on predictive modeling.


Introduction

Every time you fit a model to data, each observation yields a residual—the difference between the observed value and the value predicted by the model. Squaring these differences eliminates negative signs and emphasizes larger errors, then summing them produces the sum of squared residuals. This metric serves as the core of the ordinary least squares (OLS) criterion, which seeks the parameter set that minimizes SSR. In the sections that follow, we break down the concept, walk through a step‑by‑step calculation, explain the statistical rationale, and answer common questions.


Understanding Residuals

What Is a Residual?

A residual (eᵢ) for observation i is defined as

[ e_i = y_i - \hat{y}_i ]

where

  • yᵢ is the observed response,
  • (\hat{y}_i) is the model’s predicted value for that observation.

Residuals can be positive (under‑prediction) or negative (over‑prediction) Took long enough..

Why Square the Residuals?

Squaring serves two purposes:

  1. Eliminates sign cancellation – positive and negative residuals would otherwise offset each other if summed directly.
  2. Penalizes large errors – the square grows faster than the absolute value, making the metric sensitive to outliers, which is often desirable in least‑squares estimation.

Step‑by‑Step Calculation

Below is a practical workflow you can follow with any dataset and any linear model (simple or multiple regression).

Step 1: Fit the Model

Estimate the regression coefficients (β̂) using your preferred method (OLS, gradient descent, statistical software, etc.).
Example: For a simple linear model ( \hat{y} = \beta_0 + \beta_1 x ), obtain β̂₀ and β̂₁ from the data.

Step 2: Compute Predicted Values

Plug each predictor value into the fitted equation to get (\hat{y}_i).

[ \hat{y}_i = \hat{\beta}_0 + \hat{\beta}1 x{i1} + \hat{\beta}2 x{i2} + \dots + \hat{\beta}p x{ip} ]

Step 3: Calculate Residuals

Subtract each prediction from the actual observation:

[ e_i = y_i - \hat{y}_i ]

Create a column (or vector) of residuals for all n observations And it works..

Step 4: Square Each Residual

Compute (e_i^2) for every observation. This step removes signs and amplifies larger discrepancies.

Step 5: Sum the Squared Residuals

Add all squared residuals together:

[ \text{SSR} = \sum_{i=1}^{n} e_i^2 = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2 ]

The resulting scalar is the sum of squared residuals The details matter here..


Scientific Explanation

Relationship to Least Squares Estimation

The ordinary least squares (OLS) estimator chooses β̂ that minimizes SSR. Mathematically, OLS solves

[ \hat{\beta} = \arg\min_{\beta} \sum_{i=1}^{n} (y_i - X_i\beta)^2 ]

Because SSR is a convex quadratic function of β, the minimization yields a unique closed‑form solution (when (X^\top X) is invertible):

[ \hat{\beta} = (X^\top X)^{-1} X^\top y ]

Thus, SSR is not just a diagnostic; it is the objective function that drives parameter estimation in OLS regression.

Connection to Variance and Standard Error

An unbiased estimator of the error variance σ² is

[ \hat{\sigma}^2 = \frac{\text{SSR}}{n - p - 1} ]

where p is the number of predictors (excluding the intercept). The denominator n − p − 1 represents the degrees of freedom left after estimating the model. The square root of (\hat{\sigma}^2) gives the standard error of the regression, a key ingredient in confidence intervals and hypothesis tests for β̂ The details matter here. Surprisingly effective..

Not the most exciting part, but easily the most useful.

Why SSR Matters for Model Comparison

When comparing nested models (e.g., a model with fewer predictors vs. the same model with additional terms), a reduction in SSR indicates that the extra variables explain additional variation in the response.

partial F‑test for comparing nested specifications. The F‑statistic is constructed as

[ F = \frac{(\text{SSR}{\text{restricted}} - \text{SSR}{\text{unrestricted}}) / q}{\text{SSR}_{\text{unrestricted}} / (n - p - 1)} ]

where (q) is the number of restrictions (i., the number of extra predictors in the unrestricted model). e.A large F value relative to its critical value signals that the additional variables jointly improve the fit enough to justify their inclusion Turns out it matters..

Coefficient of Determination ((R^2))

SSR also forms the basis of the most widely reported goodness‑of‑fit measure. The total sum of squares (SST) partitions into explained (SSR) and unexplained (SSE, often confusingly also called SSR in some texts—here we retain SSR for sum of squared residuals) components:

[ \text{SST} = \sum_{i=1}^{n} (y_i - \bar{y})^2 = \text{SSR} + \text{SSE} ]

where SSE is the sum of squared errors (explained variation). The coefficient of determination is then

[ R^2 = 1 - \frac{\text{SSR}}{\text{SST}} ]

(R^2) represents the proportion of total variance in (y) accounted for by the model. While intuitive, (R^2) never decreases when predictors are added, even if they are irrelevant.

Adjusted (R^2) and Information Criteria

To penalize model complexity, analysts use adjusted (R^2):

[ \bar{R}^2 = 1 - \frac{\text{SSR}/(n-p-1)}{\text{SST}/(n-1)} ]

Adjusted (R^2) increases only if the new predictor reduces SSR more than expected by chance. For non‑nested comparisons or when the goal is prediction rather than inference, information criteria such as AIC and BIC incorporate SSR directly:

[ \text{AIC} = n \ln\left(\frac{\text{SSR}}{n}\right) + 2(p+1), \qquad \text{BIC} = n \ln\left(\frac{\text{SSR}}{n}\right) + (p+1)\ln(n) ]

Both criteria balance fit (via SSR) against parsimony, with BIC imposing a harsher penalty for additional parameters.


Practical Diagnostics Using Residuals

SSR aggregates information, but the individual residuals (e_i) reveal where the model struggles. Standard diagnostic plots include:

  1. Residuals vs. Fitted Values – Checks linearity and homoscedasticity. A funnel shape suggests non‑constant variance; a curved pattern indicates missed nonlinearity.
  2. Normal Q‑Q Plot – Assesses the normality assumption required for exact (t) and (F) tests in small samples.
  3. Scale‑Location Plot – A square‑root transformation of standardized residuals against fitted values to spotlight heteroscedasticity more clearly.
  4. take advantage of and Influence (Cook’s Distance) – Identifies observations that disproportionately pull the regression surface. Points with high apply and large residuals can inflate SSR and distort (\hat{\beta}).

If diagnostics reveal violations, remedies include variable transformations (log, Box‑Cox), weighted least squares, solid regression, or adding polynomial/interaction terms—each of which will change the SSR and, ideally, improve the model’s validity Not complicated — just consistent. Nothing fancy..


Limitations and Caveats

Despite its centrality, SSR has notable limitations:

  • Scale Dependence – SSR is expressed in the squared units of (y), making it incomparable across datasets with different response scales. (R^2) or standardized metrics are needed for cross‑study comparison.
  • Sensitivity to Outliers – Because residuals are squared, a single extreme outlier can dominate SSR, pulling the OLS fit toward itself. strong alternatives (Huber, Tukey’s biweight) minimize a less aggressive loss function.
  • In‑Sample Optimism – SSR is computed on the training data. A model that minimizes in‑sample SSR may overfit and generalize poorly. Cross‑validated prediction error (e.g., PRESS statistic) or a hold‑out test set provides a more honest assessment of predictive performance.
  • No Causal Interpretation – A low SSR only indicates close numerical proximity between (\hat{y}) and (y); it does not imply that the predictors cause changes in the response.

Computational Note

In modern practice, SSR is rarely computed by hand. Statistical languages expose it directly:

  • R: sum(residuals(lm_model)^2) or deviance(lm_model)
  • Python (statsmodels): results.ssr or np.sum(results.resid**2)
  • Python (scikit-learn): mean_squared_error(y_true, y_pred) * n_samples

These functions handle matrix algebra efficiently and guard against numerical instability (e.g., using QR decomposition rather than explicitly forming ((X^\top X)^{-1})).


Conclusion

The sum of squared residuals is far more than a bookkeeping artifact; it is the **objective

The sum of squared residuals is far more than a bookkeeping artifact; it is the objective function that drives ordinary least‑squares (OLS) estimation. This minimization yields the closed‑form solution (\hat\beta = (X^\top X)^{-1}X^\top y) when the design matrix (X) has full column rank, and it also coincides with the maximum‑likelihood estimator for the regression coefficients when the error terms are assumed independent and normally distributed with constant variance. By minimizing SSR, the fitted line is positioned so that the vertical distances between observed and predicted values are as small as possible in a squared sense. Because of this, SSR underpins the derivation of standard errors, t‑statistics, and F‑tests, because the estimated error variance (\hat\sigma^2 = \text{SSR}/(n-p)) feeds directly into the covariance matrix of (\hat\beta).

Beyond inference, SSR serves as the backbone for many model‑selection criteria. Also, the coefficient of determination, (R^2 = 1 - \text{SSR}/\text{TSS}), expresses the proportion of total variation explained, while adjusted (R^2) penalizes unnecessary complexity by incorporating the degrees of freedom. Information‑theoretic measures such as AIC and BIC embed SSR (or the log‑likelihood derived from it) alongside penalty terms for the number of parameters, enabling comparisons across non‑nested models. In a predictive framework, SSR is transformed into metrics like the mean squared error (MSE) or the root‑mean‑square error (RMSE), which are central to cross‑validation and to the computation of the PRESS statistic—a leave‑one‑out estimate of out‑of‑sample error.

Although SSR is indispensable, its raw magnitude can be misleading. Even so, because it is expressed in the squared units of the response, direct comparisons across datasets with differing scales are not informative without standardization. On top of that, the quadratic nature of SSR makes it vulnerable to extreme observations; dependable regression approaches replace the squared loss with more forgiving functions (e.Because of that, g. , Huber or Tukey’s biweight) to reduce the influence of outliers. Finally, a low SSR on training data does not guarantee good generalization; practitioners therefore complement SSR with validation techniques such as k‑fold cross‑validation or an independent test set to gauge predictive performance.

In practice, statistical software abstracts away the algebraic heavy lifting, providing SSR through concise commands (sum(residuals(lm_model)^2) in R, results.ssr in statsmodels, or mean_squared_error(y_true, y_pred) * n_samples in scikit‑learn). These implementations often employ numerically stable algorithms (e.g., QR decomposition) to compute the fit and the associated residuals without explicitly inverting large matrices That alone is useful..

Conclusion
The sum of squared residuals is a unifying thread that

Out the Door

New Content Alert

Similar Territory

You May Enjoy These

Thank you for reading about How To Calculate The Sum Of Squared Residuals. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home