R-Squared

The coefficient of determination is the fraction of the variation in that a model’s predictions account for. It uses three sums of squares over points:

Here is the mean of the . SST is the total sum of squares and SSE the error sum of squares. SSR is the regression sum of squares. The definition is

It compares the model’s MSE with the MSE of always predicting . The adjusted divides each sum of squares by its degrees of freedom. With predictors plus an intercept,

It falls when a new predictor reduces SSE too little to make up for the lost degree of freedom. The headline facts are these:

  • Decomposition: for least squares with an intercept, on its training data, . Then lies in .
  • Correlation: in simple regression, , the squared sample correlation of and . With several predictors and an intercept, is the squared correlation between and .
  • Never decreasing: adding a predictor to a least squares fit never lowers the training , which motivates the adjusted version.
  • Negative values: on test data, or for a fit without an intercept, SSE can exceed SST and make . The model then predicts worse than the constant .
  • No units: is a ratio, so it compares the quality of fit across targets on different scales.

is the scale-free companion of MSE. On one dataset it ranks models in the same order as MSE, and it adds a comparison across targets.

Names of the Sums of Squares

Texts disagree on the abbreviations. This vault writes SSE for the error sum of squares and SSR for the regression sum of squares. Many texts write RSS for the residual sum of squares, and some use SSR for it. STAT 153 writes for SSE. Some texts also let count every coefficient, including the intercept, and then write in place of .

Intuition

The baseline for any regression model is the constant prediction , which ignores the inputs. Its squared error is SST. A model that uses the inputs lowers the squared error to SSE. reports the fraction of SST that the model removed. An of means the model’s squared error is of the baseline’s. The name comes from simple regression, where equals the square of the correlation coefficient .

For least squares the removed part has a geometric meaning. After centering, the fitted vector and the residual vector are orthogonal. Their squared lengths therefore add like the sides of a right triangle. SST is the squared hypotenuse, and SSR and SSE are the squared legs. is the squared cosine of the angle between the centered and the centered . The triangle gives the bound and the correlation formula at once.

The animation compares the spread around the mean (SST) with the spread around the fit (SSE). On new test data, a poor model misses more than the mean does, so its is negative.

\usepackage{tikz}
\usetikzlibrary{animations, arrows.meta}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\newcommand{\pt}[1]{\filldraw[draw=black, fill=mid] (#1) circle (2.6pt);}
\newcommand{\pq}[1]{\draw[thick] (#1) circle (2.6pt);}
\begin{tikzpicture}[font=\small, >={Stealth[length=2.2mm]}]
\draw[->] (0,0) -- (7.73,0) node[right] {$x$};
\draw[->] (0,0) -- (0,6.05) node[above] {$y$};
\draw (8.50,0) -- (11.00,0);
\node[anchor=north] at (9.15,-0.05) {SST};
\node[anchor=north] at (10.35,-0.05) {SSE};
\draw (1.50,-0.08) -- (1.50,0.08) node[pos=0, below] {\footnotesize $2$};
\draw (3.00,-0.08) -- (3.00,0.08) node[pos=0, below] {\footnotesize $4$};
\draw (4.50,-0.08) -- (4.50,0.08) node[pos=0, below] {\footnotesize $6$};
\draw (6.00,-0.08) -- (6.00,0.08) node[pos=0, below] {\footnotesize $8$};
\draw (-0.08,1.10) -- (0.08,1.10) node[pos=0, left] {\footnotesize $2$};
\draw (-0.08,2.20) -- (0.08,2.20) node[pos=0, left] {\footnotesize $4$};
\draw (-0.08,3.30) -- (0.08,3.30) node[pos=0, left] {\footnotesize $6$};
\draw (-0.08,4.40) -- (0.08,4.40) node[pos=0, left] {\footnotesize $8$};
\draw (-0.08,5.50) -- (0.08,5.50) node[pos=0, left] {\footnotesize $10$};
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 13.28s = "false", 20.08s = "false", repeats}}}]
\pt{0.32,2.06}
\pt{1.23,2.02}
\pt{1.75,2.90}
\pt{2.60,3.80}
\pt{3.34,2.78}
\pt{3.90,3.93}
\pt{4.63,3.89}
\pt{5.32,5.25}
\pt{5.96,4.10}
\pt{6.78,4.91}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8s = "true", 13.28s = "false", 20.08s = "false", repeats}}}]
\filldraw[draw=black, fill=cold] (8.75,0.000) rectangle (9.55,0.525);
\filldraw[draw=black, fill=cold] (8.75,0.525) rectangle (9.55,1.078);
\filldraw[draw=black, fill=cold] (8.75,1.078) rectangle (9.55,1.180);
\filldraw[draw=black, fill=cold] (8.75,1.180) rectangle (9.55,1.193);
\filldraw[draw=black, fill=cold] (8.75,1.193) rectangle (9.55,1.336);
\filldraw[draw=black, fill=cold] (8.75,1.336) rectangle (9.55,1.366);
\filldraw[draw=black, fill=cold] (8.75,1.366) rectangle (9.55,1.391);
\filldraw[draw=black, fill=cold] (8.75,1.391) rectangle (9.55,2.054);
\filldraw[draw=black, fill=cold] (8.75,2.054) rectangle (9.55,2.121);
\filldraw[draw=black, fill=cold] (8.75,2.121) rectangle (9.55,2.538);
\node[anchor=south] at (9.15,2.538) {\footnotesize $36.2$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 13.28s = "false", 20.08s = "false", repeats}}}]
\draw[cold, dashed, thick] (0.00,3.56) -- (7.43,3.56);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 13.28s = "false", 20.08s = "false", repeats}}}]
\node at (9.75,6.0) {training data};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 2.6s = "false", 20.08s = "false", repeats}}}]
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = \;?$};
\node[anchor=west] at (-0.6,-1.05) {Ten training points. The flat line at the mean, $\bar y = 6.48$, is the baseline.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.6s = "true", 2.88s = "false", 20.08s = "false", repeats}}}]
\draw[cold, very thick] (0.32,2.15) -- (0.32,3.56);
\filldraw[draw=black, fill=cold] (8.75,0.000) rectangle (9.55,0.525);
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 7.48$};
\node[anchor=west] at (-0.6,-1.05) {SST adds up the squared distance from each point to the mean.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.88s = "true", 3.16s = "false", 20.08s = "false", repeats}}}]
\draw[cold, very thick] (0.32,2.15) -- (0.32,3.56);
\draw[cold, very thick] (1.23,2.11) -- (1.23,3.56);
\filldraw[draw=black, fill=cold] (8.75,0.000) rectangle (9.55,0.525);
\filldraw[draw=black, fill=cold] (8.75,0.525) rectangle (9.55,1.078);
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 15.37$};
\node[anchor=west] at (-0.6,-1.05) {SST adds up the squared distance from each point to the mean.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.16s = "true", 3.44s = "false", 20.08s = "false", repeats}}}]
\draw[cold, very thick] (0.32,2.15) -- (0.32,3.56);
\draw[cold, very thick] (1.23,2.11) -- (1.23,3.56);
\draw[cold, very thick] (1.75,2.99) -- (1.75,3.56);
\filldraw[draw=black, fill=cold] (8.75,0.000) rectangle (9.55,0.525);
\filldraw[draw=black, fill=cold] (8.75,0.525) rectangle (9.55,1.078);
\filldraw[draw=black, fill=cold] (8.75,1.078) rectangle (9.55,1.180);
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 16.81$};
\node[anchor=west] at (-0.6,-1.05) {SST adds up the squared distance from each point to the mean.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.44s = "true", 3.72s = "false", 20.08s = "false", repeats}}}]
\draw[cold, very thick] (0.32,2.15) -- (0.32,3.56);
\draw[cold, very thick] (1.23,2.11) -- (1.23,3.56);
\draw[cold, very thick] (1.75,2.99) -- (1.75,3.56);
\draw[cold, very thick] (2.60,3.71) -- (2.60,3.56);
\filldraw[draw=black, fill=cold] (8.75,0.000) rectangle (9.55,0.525);
\filldraw[draw=black, fill=cold] (8.75,0.525) rectangle (9.55,1.078);
\filldraw[draw=black, fill=cold] (8.75,1.078) rectangle (9.55,1.180);
\filldraw[draw=black, fill=cold] (8.75,1.180) rectangle (9.55,1.193);
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 17.00$};
\node[anchor=west] at (-0.6,-1.05) {SST adds up the squared distance from each point to the mean.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.72s = "true", 4s = "false", 20.08s = "false", repeats}}}]
\draw[cold, very thick] (0.32,2.15) -- (0.32,3.56);
\draw[cold, very thick] (1.23,2.11) -- (1.23,3.56);
\draw[cold, very thick] (1.75,2.99) -- (1.75,3.56);
\draw[cold, very thick] (2.60,3.71) -- (2.60,3.56);
\draw[cold, very thick] (3.34,2.87) -- (3.34,3.56);
\filldraw[draw=black, fill=cold] (8.75,0.000) rectangle (9.55,0.525);
\filldraw[draw=black, fill=cold] (8.75,0.525) rectangle (9.55,1.078);
\filldraw[draw=black, fill=cold] (8.75,1.078) rectangle (9.55,1.180);
\filldraw[draw=black, fill=cold] (8.75,1.180) rectangle (9.55,1.193);
\filldraw[draw=black, fill=cold] (8.75,1.193) rectangle (9.55,1.336);
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 19.03$};
\node[anchor=west] at (-0.6,-1.05) {SST adds up the squared distance from each point to the mean.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 4s = "true", 4.28s = "false", 20.08s = "false", repeats}}}]
\draw[cold, very thick] (0.32,2.15) -- (0.32,3.56);
\draw[cold, very thick] (1.23,2.11) -- (1.23,3.56);
\draw[cold, very thick] (1.75,2.99) -- (1.75,3.56);
\draw[cold, very thick] (2.60,3.71) -- (2.60,3.56);
\draw[cold, very thick] (3.34,2.87) -- (3.34,3.56);
\draw[cold, very thick] (3.90,3.84) -- (3.90,3.56);
\filldraw[draw=black, fill=cold] (8.75,0.000) rectangle (9.55,0.525);
\filldraw[draw=black, fill=cold] (8.75,0.525) rectangle (9.55,1.078);
\filldraw[draw=black, fill=cold] (8.75,1.078) rectangle (9.55,1.180);
\filldraw[draw=black, fill=cold] (8.75,1.180) rectangle (9.55,1.193);
\filldraw[draw=black, fill=cold] (8.75,1.193) rectangle (9.55,1.336);
\filldraw[draw=black, fill=cold] (8.75,1.336) rectangle (9.55,1.366);
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 19.47$};
\node[anchor=west] at (-0.6,-1.05) {SST adds up the squared distance from each point to the mean.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 4.28s = "true", 4.56s = "false", 20.08s = "false", repeats}}}]
\draw[cold, very thick] (0.32,2.15) -- (0.32,3.56);
\draw[cold, very thick] (1.23,2.11) -- (1.23,3.56);
\draw[cold, very thick] (1.75,2.99) -- (1.75,3.56);
\draw[cold, very thick] (2.60,3.71) -- (2.60,3.56);
\draw[cold, very thick] (3.34,2.87) -- (3.34,3.56);
\draw[cold, very thick] (3.90,3.84) -- (3.90,3.56);
\draw[cold, very thick] (4.63,3.80) -- (4.63,3.56);
\filldraw[draw=black, fill=cold] (8.75,0.000) rectangle (9.55,0.525);
\filldraw[draw=black, fill=cold] (8.75,0.525) rectangle (9.55,1.078);
\filldraw[draw=black, fill=cold] (8.75,1.078) rectangle (9.55,1.180);
\filldraw[draw=black, fill=cold] (8.75,1.180) rectangle (9.55,1.193);
\filldraw[draw=black, fill=cold] (8.75,1.193) rectangle (9.55,1.336);
\filldraw[draw=black, fill=cold] (8.75,1.336) rectangle (9.55,1.366);
\filldraw[draw=black, fill=cold] (8.75,1.366) rectangle (9.55,1.391);
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 19.83$};
\node[anchor=west] at (-0.6,-1.05) {SST adds up the squared distance from each point to the mean.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 4.56s = "true", 4.84s = "false", 20.08s = "false", repeats}}}]
\draw[cold, very thick] (0.32,2.15) -- (0.32,3.56);
\draw[cold, very thick] (1.23,2.11) -- (1.23,3.56);
\draw[cold, very thick] (1.75,2.99) -- (1.75,3.56);
\draw[cold, very thick] (2.60,3.71) -- (2.60,3.56);
\draw[cold, very thick] (3.34,2.87) -- (3.34,3.56);
\draw[cold, very thick] (3.90,3.84) -- (3.90,3.56);
\draw[cold, very thick] (4.63,3.80) -- (4.63,3.56);
\draw[cold, very thick] (5.32,5.16) -- (5.32,3.56);
\filldraw[draw=black, fill=cold] (8.75,0.000) rectangle (9.55,0.525);
\filldraw[draw=black, fill=cold] (8.75,0.525) rectangle (9.55,1.078);
\filldraw[draw=black, fill=cold] (8.75,1.078) rectangle (9.55,1.180);
\filldraw[draw=black, fill=cold] (8.75,1.180) rectangle (9.55,1.193);
\filldraw[draw=black, fill=cold] (8.75,1.193) rectangle (9.55,1.336);
\filldraw[draw=black, fill=cold] (8.75,1.336) rectangle (9.55,1.366);
\filldraw[draw=black, fill=cold] (8.75,1.366) rectangle (9.55,1.391);
\filldraw[draw=black, fill=cold] (8.75,1.391) rectangle (9.55,2.054);
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 29.27$};
\node[anchor=west] at (-0.6,-1.05) {SST adds up the squared distance from each point to the mean.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 4.84s = "true", 5.12s = "false", 20.08s = "false", repeats}}}]
\draw[cold, very thick] (0.32,2.15) -- (0.32,3.56);
\draw[cold, very thick] (1.23,2.11) -- (1.23,3.56);
\draw[cold, very thick] (1.75,2.99) -- (1.75,3.56);
\draw[cold, very thick] (2.60,3.71) -- (2.60,3.56);
\draw[cold, very thick] (3.34,2.87) -- (3.34,3.56);
\draw[cold, very thick] (3.90,3.84) -- (3.90,3.56);
\draw[cold, very thick] (4.63,3.80) -- (4.63,3.56);
\draw[cold, very thick] (5.32,5.16) -- (5.32,3.56);
\draw[cold, very thick] (5.96,4.01) -- (5.96,3.56);
\filldraw[draw=black, fill=cold] (8.75,0.000) rectangle (9.55,0.525);
\filldraw[draw=black, fill=cold] (8.75,0.525) rectangle (9.55,1.078);
\filldraw[draw=black, fill=cold] (8.75,1.078) rectangle (9.55,1.180);
\filldraw[draw=black, fill=cold] (8.75,1.180) rectangle (9.55,1.193);
\filldraw[draw=black, fill=cold] (8.75,1.193) rectangle (9.55,1.336);
\filldraw[draw=black, fill=cold] (8.75,1.336) rectangle (9.55,1.366);
\filldraw[draw=black, fill=cold] (8.75,1.366) rectangle (9.55,1.391);
\filldraw[draw=black, fill=cold] (8.75,1.391) rectangle (9.55,2.054);
\filldraw[draw=black, fill=cold] (8.75,2.054) rectangle (9.55,2.121);
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 30.22$};
\node[anchor=west] at (-0.6,-1.05) {SST adds up the squared distance from each point to the mean.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.12s = "true", 5.4s = "false", 20.08s = "false", repeats}}}]
\draw[cold, very thick] (0.32,2.15) -- (0.32,3.56);
\draw[cold, very thick] (1.23,2.11) -- (1.23,3.56);
\draw[cold, very thick] (1.75,2.99) -- (1.75,3.56);
\draw[cold, very thick] (2.60,3.71) -- (2.60,3.56);
\draw[cold, very thick] (3.34,2.87) -- (3.34,3.56);
\draw[cold, very thick] (3.90,3.84) -- (3.90,3.56);
\draw[cold, very thick] (4.63,3.80) -- (4.63,3.56);
\draw[cold, very thick] (5.32,5.16) -- (5.32,3.56);
\draw[cold, very thick] (5.96,4.01) -- (5.96,3.56);
\draw[cold, very thick] (6.78,4.82) -- (6.78,3.56);
\filldraw[draw=black, fill=cold] (8.75,0.000) rectangle (9.55,0.525);
\filldraw[draw=black, fill=cold] (8.75,0.525) rectangle (9.55,1.078);
\filldraw[draw=black, fill=cold] (8.75,1.078) rectangle (9.55,1.180);
\filldraw[draw=black, fill=cold] (8.75,1.180) rectangle (9.55,1.193);
\filldraw[draw=black, fill=cold] (8.75,1.193) rectangle (9.55,1.336);
\filldraw[draw=black, fill=cold] (8.75,1.336) rectangle (9.55,1.366);
\filldraw[draw=black, fill=cold] (8.75,1.366) rectangle (9.55,1.391);
\filldraw[draw=black, fill=cold] (8.75,1.391) rectangle (9.55,2.054);
\filldraw[draw=black, fill=cold] (8.75,2.054) rectangle (9.55,2.121);
\filldraw[draw=black, fill=cold] (8.75,2.121) rectangle (9.55,2.538);
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 36.18$};
\node[anchor=west] at (-0.6,-1.05) {SST adds up the squared distance from each point to the mean.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.4s = "true", 8s = "false", 20.08s = "false", repeats}}}]
\draw[cold, very thick] (0.32,2.15) -- (0.32,3.56);
\draw[cold, very thick] (1.23,2.11) -- (1.23,3.56);
\draw[cold, very thick] (1.75,2.99) -- (1.75,3.56);
\draw[cold, very thick] (2.60,3.71) -- (2.60,3.56);
\draw[cold, very thick] (3.34,2.87) -- (3.34,3.56);
\draw[cold, very thick] (3.90,3.84) -- (3.90,3.56);
\draw[cold, very thick] (4.63,3.80) -- (4.63,3.56);
\draw[cold, very thick] (5.32,5.16) -- (5.32,3.56);
\draw[cold, very thick] (5.96,4.01) -- (5.96,3.56);
\draw[cold, very thick] (6.78,4.82) -- (6.78,3.56);
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 36.18$};
\node[anchor=west] at (-0.6,-1.05) {$\mathrm{SST} = 36.18$ is the total squared spread of $y$ around its mean.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8s = "true", 8.28s = "false", 20.08s = "false", repeats}}}]
\draw[hot, very thick] (0.00,3.47) -- (7.43,3.66);
\draw[hot, very thick] (0.32,2.15) -- (0.32,3.48);
\draw[hot, very thick] (1.23,2.11) -- (1.23,3.50);
\draw[hot, very thick] (1.75,2.99) -- (1.75,3.52);
\draw[hot, very thick] (2.60,3.71) -- (2.60,3.54);
\draw[hot, very thick] (3.34,2.87) -- (3.34,3.56);
\draw[hot, very thick] (3.90,3.84) -- (3.90,3.57);
\draw[hot, very thick] (4.63,3.80) -- (4.63,3.59);
\draw[hot, very thick] (5.32,5.16) -- (5.32,3.61);
\draw[hot, very thick] (5.96,4.01) -- (5.96,3.62);
\draw[hot, very thick] (6.78,4.82) -- (6.78,3.64);
\filldraw[draw=black, fill=hot] (9.95,0) rectangle (10.75,2.324);
\node[anchor=south] at (10.35,2.324) {\footnotesize $33.1$};
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 36.18$};
\node[anchor=west] at (11.35,4.5) {$\mathrm{SSE} = \sum_i e_i^2 = 33.12$};
\node[anchor=west] at (-0.6,-1.05) {The line turns toward the least squares fit. The residuals $e_i$ shrink.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.28s = "true", 8.56s = "false", 20.08s = "false", repeats}}}]
\draw[hot, very thick] (0.00,3.24) -- (7.43,3.91);
\draw[hot, very thick] (0.32,2.15) -- (0.32,3.27);
\draw[hot, very thick] (1.23,2.11) -- (1.23,3.35);
\draw[hot, very thick] (1.75,2.99) -- (1.75,3.40);
\draw[hot, very thick] (2.60,3.71) -- (2.60,3.47);
\draw[hot, very thick] (3.34,2.87) -- (3.34,3.54);
\draw[hot, very thick] (3.90,3.84) -- (3.90,3.59);
\draw[hot, very thick] (4.63,3.80) -- (4.63,3.66);
\draw[hot, very thick] (5.32,5.16) -- (5.32,3.72);
\draw[hot, very thick] (5.96,4.01) -- (5.96,3.78);
\draw[hot, very thick] (6.78,4.82) -- (6.78,3.85);
\filldraw[draw=black, fill=hot] (9.95,0) rectangle (10.75,1.827);
\node[anchor=south] at (10.35,1.827) {\footnotesize $26.0$};
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 36.18$};
\node[anchor=west] at (11.35,4.5) {$\mathrm{SSE} = \sum_i e_i^2 = 26.04$};
\node[anchor=west] at (-0.6,-1.05) {The line turns toward the least squares fit. The residuals $e_i$ shrink.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.56s = "true", 8.84s = "false", 20.08s = "false", repeats}}}]
\draw[hot, very thick] (0.00,2.92) -- (7.43,4.26);
\draw[hot, very thick] (0.32,2.15) -- (0.32,2.97);
\draw[hot, very thick] (1.23,2.11) -- (1.23,3.14);
\draw[hot, very thick] (1.75,2.99) -- (1.75,3.23);
\draw[hot, very thick] (2.60,3.71) -- (2.60,3.39);
\draw[hot, very thick] (3.34,2.87) -- (3.34,3.52);
\draw[hot, very thick] (3.90,3.84) -- (3.90,3.62);
\draw[hot, very thick] (4.63,3.80) -- (4.63,3.75);
\draw[hot, very thick] (5.32,5.16) -- (5.32,3.88);
\draw[hot, very thick] (5.96,4.01) -- (5.96,3.99);
\draw[hot, very thick] (6.78,4.82) -- (6.78,4.14);
\filldraw[draw=black, fill=hot] (9.95,0) rectangle (10.75,1.279);
\node[anchor=south] at (10.35,1.279) {\footnotesize $18.2$};
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 36.18$};
\node[anchor=west] at (11.35,4.5) {$\mathrm{SSE} = \sum_i e_i^2 = 18.23$};
\node[anchor=west] at (-0.6,-1.05) {The line turns toward the least squares fit. The residuals $e_i$ shrink.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.84s = "true", 9.12s = "false", 20.08s = "false", repeats}}}]
\draw[hot, very thick] (0.00,2.56) -- (7.43,4.63);
\draw[hot, very thick] (0.32,2.15) -- (0.32,2.65);
\draw[hot, very thick] (1.23,2.11) -- (1.23,2.91);
\draw[hot, very thick] (1.75,2.99) -- (1.75,3.05);
\draw[hot, very thick] (2.60,3.71) -- (2.60,3.29);
\draw[hot, very thick] (3.34,2.87) -- (3.34,3.49);
\draw[hot, very thick] (3.90,3.84) -- (3.90,3.65);
 
\draw[hot, very thick] (5.32,5.16) -- (5.32,4.05);
\draw[hot, very thick] (5.96,4.19) -- (5.96,4.22);
\draw[hot, very thick] (6.78,4.82) -- (6.78,4.45);
\filldraw[draw=black, fill=hot] (9.95,0) rectangle (10.75,0.856);
\node[anchor=south] at (10.35,0.856) {\footnotesize $12.2$};
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 36.18$};
\node[anchor=west] at (11.35,4.5) {$\mathrm{SSE} = \sum_i e_i^2 = 12.19$};
\node[anchor=west] at (-0.6,-1.05) {The line turns toward the least squares fit. The residuals $e_i$ shrink.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.12s = "true", 9.4s = "false", 20.08s = "false", repeats}}}]
\draw[hot, very thick] (0.00,2.24) -- (7.43,4.98);
\draw[hot, very thick] (0.32,2.15) -- (0.32,2.36);
\draw[hot, very thick] (1.23,2.11) -- (1.23,2.70);
 
\draw[hot, very thick] (2.60,3.71) -- (2.60,3.20);
\draw[hot, very thick] (3.34,2.87) -- (3.34,3.47);
\draw[hot, very thick] (3.90,3.84) -- (3.90,3.68);
 
\draw[hot, very thick] (5.32,5.16) -- (5.32,4.20);
\draw[hot, very thick] (5.96,4.19) -- (5.96,4.44);
\draw[hot, very thick] (6.78,4.82) -- (6.78,4.74);
\filldraw[draw=black, fill=hot] (9.95,0) rectangle (10.75,0.625);
\node[anchor=south] at (10.35,0.625) {\footnotesize $8.9$};
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 36.18$};
\node[anchor=west] at (11.35,4.5) {$\mathrm{SSE} = \sum_i e_i^2 = 8.91$};
\node[anchor=west] at (-0.6,-1.05) {The line turns toward the least squares fit. The residuals $e_i$ shrink.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.4s = "true", 9.68s = "false", 20.08s = "false", repeats}}}]
\draw[hot, very thick] (0.00,2.01) -- (7.43,5.23);
 
\draw[hot, very thick] (1.23,2.11) -- (1.23,2.54);
\draw[hot, very thick] (1.75,2.81) -- (1.75,2.77);
\draw[hot, very thick] (2.60,3.71) -- (2.60,3.14);
\draw[hot, very thick] (3.34,2.87) -- (3.34,3.46);
\draw[hot, very thick] (3.90,3.84) -- (3.90,3.70);
\draw[hot, very thick] (4.63,3.98) -- (4.63,4.02);
\draw[hot, very thick] (5.32,5.16) -- (5.32,4.32);
\draw[hot, very thick] (5.96,4.19) -- (5.96,4.59);
 
\filldraw[draw=black, fill=hot] (9.95,0) rectangle (10.75,0.553);
\node[anchor=south] at (10.35,0.553) {\footnotesize $7.9$};
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 36.18$};
\node[anchor=west] at (11.35,4.5) {$\mathrm{SSE} = \sum_i e_i^2 = 7.88$};
\node[anchor=west] at (-0.6,-1.05) {The line turns toward the least squares fit. The residuals $e_i$ shrink.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.68s = "true", 13.28s = "false", 20.08s = "false", repeats}}}]
\draw[hot, very thick] (0.00,1.92) -- (7.43,5.33);
 
\draw[hot, very thick] (1.23,2.11) -- (1.23,2.48);
\draw[hot, very thick] (1.75,2.81) -- (1.75,2.72);
\draw[hot, very thick] (2.60,3.71) -- (2.60,3.11);
\draw[hot, very thick] (3.34,2.87) -- (3.34,3.45);
\draw[hot, very thick] (3.90,3.84) -- (3.90,3.71);
\draw[hot, very thick] (4.63,3.98) -- (4.63,4.04);
\draw[hot, very thick] (5.32,5.16) -- (5.32,4.36);
\draw[hot, very thick] (5.96,4.19) -- (5.96,4.65);
\draw[hot, very thick] (6.78,5.00) -- (6.78,5.03);
\filldraw[draw=black, fill=hot] (9.95,0) rectangle (10.75,0.547);
\node[anchor=south] at (10.35,0.547) {\footnotesize $7.8$};
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 36.18$};
\node[anchor=west] at (11.35,4.5) {$\mathrm{SSE} = \sum_i e_i^2 = 7.80$};
\node[anchor=west] at (11.35,3.7) {$R^2 = 1 - \mathrm{SSE}/\mathrm{SST} = 0.78$};
\draw[dashed] (9.95,0.547) rectangle (10.75,2.538);
\node at (10.35,1.543) {\footnotesize SSR};
\node[anchor=west] at (-0.6,-1.05) {The least squares fit leaves $\mathrm{SSE} = 7.80$. So $R^2 = 1 - 7.80/36.18 = 0.78$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.28s = "true", 16.08s = "false", 20.08s = "false", repeats}}}]
\pq{0.57,1.28}
\pq{1.41,2.38}
\pq{2.06,3.39}
\pq{2.76,3.10}
\pq{3.58,3.06}
\pq{4.38,4.09}
\pq{5.09,4.03}
\pq{5.71,4.64}
\pq{6.47,4.50}
\pq{7.08,4.47}
\draw[cold, dashed, thick] (0.00,3.49) -- (7.43,3.49);
\node at (9.75,6.0) {test data};
\draw[cold, very thick] (0.57,1.37) -- (0.57,3.49);
\draw[cold, very thick] (1.41,2.47) -- (1.41,3.49);
\draw[cold, very thick] (2.06,3.48) -- (2.06,3.49);
\draw[cold, very thick] (2.76,3.19) -- (2.76,3.49);
\draw[cold, very thick] (3.58,3.15) -- (3.58,3.49);
\draw[cold, very thick] (4.38,4.00) -- (4.38,3.49);
\draw[cold, very thick] (5.09,3.94) -- (5.09,3.49);
\draw[cold, very thick] (5.71,4.55) -- (5.71,3.49);
\draw[cold, very thick] (6.47,4.41) -- (6.47,3.49);
\draw[cold, very thick] (7.08,4.38) -- (7.08,3.49);
\filldraw[draw=black, fill=cold] (8.75,0) rectangle (9.55,2.416);
\node[anchor=south] at (9.15,2.416) {\footnotesize $34.4$};
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 34.43$};
\node[anchor=west] at (-0.6,-1.05) {New test points. SST measures their spread around their own mean.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 16.08s = "true", 20.08s = "false", 20.08s = "false", repeats}}}]
\pq{0.57,1.28}
\pq{1.41,2.38}
\pq{2.06,3.39}
\pq{2.76,3.10}
\pq{3.58,3.06}
\pq{4.38,4.09}
\pq{5.09,4.03}
\pq{5.71,4.64}
\pq{6.47,4.50}
\pq{7.08,4.47}
\draw[cold, dashed, thick] (0.00,3.49) -- (7.43,3.49);
\node at (9.75,6.0) {test data};
\draw[hot, very thick] (0.00,4.68) -- (7.43,4.13);
\draw[hot, very thick] (0.57,1.37) -- (0.57,4.63);
\draw[hot, very thick] (1.41,2.47) -- (1.41,4.57);
\draw[hot, very thick] (2.06,3.48) -- (2.06,4.52);
\draw[hot, very thick] (2.76,3.19) -- (2.76,4.47);
\draw[hot, very thick] (3.58,3.15) -- (3.58,4.41);
\draw[hot, very thick] (4.38,4.18) -- (4.38,4.35);
\draw[hot, very thick] (5.09,4.12) -- (5.09,4.30);
\draw[hot, very thick] (5.71,4.55) -- (5.71,4.26);
\draw[hot, very thick] (6.47,4.41) -- (6.47,4.20);
\draw[hot, very thick] (7.08,4.38) -- (7.08,4.16);
\filldraw[draw=black, fill=cold] (8.75,0) rectangle (9.55,2.416);
\node[anchor=south] at (9.15,2.416) {\footnotesize $34.4$};
\filldraw[draw=black, fill=hot] (9.95,0) rectangle (10.75,5.000);
\node[anchor=south] at (10.35,5.000) {\footnotesize $71.3$};
\node[anchor=west] at (11.35,5.3) {$\mathrm{SST} = \sum_i (y_i - \bar y)^2 = 34.43$};
\node[anchor=west] at (11.35,4.5) {$\mathrm{SSE} = \sum_i e_i^2 = 71.26$};
\node[anchor=west] at (11.35,3.7) {$R^2 = 1 - \mathrm{SSE}/\mathrm{SST} = -1.07$};
\node[anchor=west] at (-0.6,-1.05) {A poor model misses more than the mean does. $\mathrm{SSE} > \mathrm{SST}$, so $R^2 = -1.07$.};
\end{scope}
\end{tikzpicture}
\end{document}

Properties

(Training Grows With Size) Pure-noise predictors raise the training by about on average.

Suppose is unrelated to predictors and the noise is Gaussian. The training then follows a distribution with mean . With houses and random predictors, the expected is , although the predictors carry no information. A least squares fit with predictors interpolates the data and reaches . In the same setting the adjusted has expected value . An analyst therefore cannot pick the number of predictors from the training alone. Adjusted , cross-validation or a test set should guide that choice.

(Reading Adjusted ) Adjusted rises when a new predictor has and falls otherwise.

Here is the new predictor’s -statistic in the larger least squares fit. A predictor with between and raises . A test at the level would not call that predictor significant. Adjusted is therefore a lenient filter for predictors. It is negative when , that is, when the predictors explain less than pure noise would on average. In the STAT 153 finger-tapping regression, observations and predictors gave . The adjusted value was .

(Negative Out of Sample) On new data has no lower bound, and a negative value means the model loses to a constant.

On a test set, , with taken over the test set. This is the convention of scikit-learn’s r2_score. Nothing ties the model’s errors to SST on new data, so can fall below . A model that predicts a constant , such as the training mean, gets

Overfit models and models applied after a distribution shift often give negative test . Return forecasting in finance computes out-of-sample against the training mean instead (Campbell and Thompson, 2008). A report should state which mean defines the baseline.

(Without an Intercept) A fit through the origin breaks the decomposition, and software then reports a different .

Without an intercept the residuals need not sum to zero, and fails. The ordinary can then be negative even on the training data. Statsmodels switches to the uncentered , which compares the model with predicting . In STAT 153 lecture 5, a line through the origin for monthly chicken prices reported an uncentered of . Adding the intercept gave an ordinary of . The two numbers use different baselines and cannot be compared. When is far from zero, the uncentered version is close to for almost any model.

(What Does Not Measure) A high does not certify the model, and a low does not make it useless.

Anscombe (1973) built four datasets with the same fitted line and . One is linear with noise, one is a clean curve, and two are driven by single outliers. Only a plot of the data or the residuals separates them. In the other direction, a slope can be precise and important while is small, because the noise is large. also depends on the spread of the inputs. For with noise variance , the population value is

With and , it is when and when . The slope and the noise are the same in both cases. Two unrelated trending time series can also give a high , the spurious regression described in 3.1a. Multiple Linear Regression.

Derivation

We show that for least squares with an intercept. Write and for the vector of ones. The normal equations say that is orthogonal to every column of (10. Projection Theorem). The intercept column is , so . Since is a combination of the columns, as well. Split each deviation as and square:

The cross term equals , which leaves . The fact used the intercept column, and the identity fails without it.

For simple regression, write , and . The fitted values satisfy with . Then

The last expression is the square of the sample correlation coefficient.

Examples

(Easy) Price Against Floor Area

Five houses have floor areas hundred square feet and prices thousand dollars. The least squares line is .

(Medium) Choosing Between Two Price Models

An analyst fits two models to apartment rents. Model A has predictors and . Model B adds five more predictors and reaches .

(Hard) A Model Moved to a New City

A delivery-time model trained in one city is tested in another. The five test deliveries took minutes, and the model predicted . The training mean was minutes.

See Also