St · Statistics & Probability

How to Compute SSreg and SSres in Regression

Published By the 9to5Science editorial team

At a glance

  1. 1. Find the mean

    Calculate the mean of the observed response values.

  2. 2. Get fitted values

    Use the regression equation to predict each observed response.

  3. 3. Sum squared deviations

    Square and add fitted-value deviations for SSreg, and residuals for SSres.

  4. 4. Check the identity

    With an intercept, verify that total sum of squares equals SSreg plus SSres.

In regression, SSreg measures the variation in the response values accounted for by the model, while SSres measures the variation left in the residuals. The exact abbreviations differ among textbooks and software, so it helps to write down the definitions rather than rely on labels such as SSR or SSE.

For ordinary least-squares regression that includes an intercept, the total variation splits into these two parts: SS⁡total=SS⁡reg+SS⁡res\operatorname{SS}_{\text{total}}=\operatorname{SS}_{\text{reg}}+\operatorname{SS}_{\text{res}}. The formulas and example below show how to calculate and check them.

Definitions and formulas

Suppose you have nn observed responses y1,…,yny_1,\ldots,y_n and fitted values y^1,…,y^n\hat y_1,\ldots,\hat y_n from a regression model. Let yˉ\bar y be the mean of the observed responses. A common convention is:

SS⁡reg=∑i=1n(y^i−yˉ)2,SS⁡res=∑i=1n(yi−y^i)2,SS⁡total=∑i=1n(yi−yˉ)2.\begin{aligned} \operatorname{SS}_{\text{reg}} &= \sum_{i=1}^{n}(\hat y_i-\bar y)^2,\\ \operatorname{SS}_{\text{res}} &= \sum_{i=1}^{n}(y_i-\hat y_i)^2,\\ \operatorname{SS}_{\text{total}} &= \sum_{i=1}^{n}(y_i-\bar y)^2. \end{aligned}

The residual for observation ii is ei=yi−y^ie_i=y_i-\hat y_i. Thus, SSres is the sum of squared residuals. SSreg is the sum of squared differences between the fitted values and the overall response mean.

Some sources call the explained sum of squares SSR and the residual sum of squares SSE; other sources use those abbreviations differently. The definitions above specify which quantity is meant. The word “sum” matters: these quantities add squared differences. They are not the corresponding averages or mean squares.

Step-by-step calculation

  1. Find the mean response, yˉ\bar y.
  2. Calculate the fitted response y^i\hat y_i for every data point using the regression equation.
  3. For SSreg, subtract yˉ\bar y from each fitted value, square each difference, and add them.
  4. For SSres, subtract each fitted value from its observed response, square each residual, and add them.
  5. If the least-squares model includes an intercept, check that SSreg plus SSres equals SStotal, calculated from the observed responses.

Worked example

Consider the data x=(1,2,3,4)x=(1,2,3,4) and y=(2,3,5,4)y=(2,3,5,4), fitted by a simple linear regression with an intercept. The least-squares line is

y^=1.5+0.8x.\hat y=1.5+0.8x.

The observed response mean is yˉ=(2+3+5+4)/4=3.5\bar y=(2+3+5+4)/4=3.5. The fitted values and residuals are:

xix_iObserved yiy_iFitted y^i\hat y_iResidual yi−y^iy_i-\hat y_i
122.3-0.3
233.1-0.1
353.91.1
444.7-0.7

For the regression sum of squares, compare each fitted value with 3.53.5:

SS⁡reg=(2.3−3.5)2+(3.1−3.5)2+(3.9−3.5)2+(4.7−3.5)2=1.44+0.16+0.16+1.44=3.20.\begin{aligned} \operatorname{SS}_{\text{reg}} &=(2.3-3.5)^2+(3.1-3.5)^2+(3.9-3.5)^2+(4.7-3.5)^2\\ &=1.44+0.16+0.16+1.44=3.20. \end{aligned}

For the residual sum of squares, square and add the residuals:

SS⁡res=(−0.3)2+(−0.1)2+(1.1)2+(−0.7)2=0.09+0.01+1.21+0.49=1.80.\begin{aligned} \operatorname{SS}_{\text{res}} &=(-0.3)^2+(-0.1)^2+(1.1)^2+(-0.7)^2\\ &=0.09+0.01+1.21+0.49=1.80. \end{aligned}

The total sum of squares from the observed responses is

SS⁡total=(2−3.5)2+(3−3.5)2+(5−3.5)2+(4−3.5)2=5.00.\operatorname{SS}_{\text{total}}=(2-3.5)^2+(3-3.5)^2+(5-3.5)^2+(4-3.5)^2=5.00.

The identity checks: 3.20+1.80=5.003.20+1.80=5.00. In this example, the coefficient of determination is R2=SS⁡reg/SS⁡total=3.20/5.00=0.64R^2=\operatorname{SS}_{\text{reg}}/\operatorname{SS}_{\text{total}}=3.20/5.00=0.64. So the fitted model accounts for 64% of the observed variation around the response mean, under this standard definition of R2R^2.

Why the sums add up

For least squares with an intercept, residuals sum to zero and are orthogonal to the fitted values. Since yi−yˉ=(y^i−yˉ)+(yi−y^i)y_i-\bar y=(\hat y_i-\bar y)+(y_i-\hat y_i), squaring and summing gives the total as the sum of the fitted-value variation and residual variation; the cross-product term is zero. This is why the identity holds for this model.

For a model with pp linearly independent predictors and an intercept, the usual regression degrees of freedom are pp, and the residual degrees of freedom are n−p−1n-p-1. Dividing SSreg and SSres by their respective degrees of freedom gives the regression and residual mean squares. These mean squares are different quantities from the sums of squares themselves.

Common mistakes

  • Using observed values in the wrong sum: SSreg uses fitted values relative to yˉ\bar y; SSres uses observed minus fitted values.
  • Forgetting to square: Residuals can be positive or negative. Squaring prevents them from canceling out.
  • Confusing sums with mean squares: Do not divide by degrees of freedom unless you are specifically asked for a mean square.
  • Assuming the identity always applies: The standard centered decomposition assumes an ordinary least-squares fit with an intercept. In a regression through the origin, the residuals need not sum to zero, and the centered identity may fail. Some software reports an uncentered R2R^2 for such models, so check its convention.
  • Mixing up abbreviations: State the defining formula, especially when comparing results from different books or software.

References

  1. Introduction to Linear Regression Analysis, Douglas C. Montgomery, Elizabeth A. Peck, and G. Geoffrey Vining
  2. Applied Linear Statistical Models, Michael H. Kutner, Christopher J. Nachtsheim, John Neter, and William Li

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.