# $R^2$ - Coefficient of Determination for Test Data

When calculating the $$R^2$$ value for the coefficient of determination of a linear regression model, it is well known (Wikipedia) that

$$SS_{Total} = SS_{Explained} + SS_{Residual}$$ (1)

i.e., $$\sum_{i=1}^n (y_i - \bar{y})^2 = \sum_{i=1}^n (\hat{y_i} - \bar{y})^2 + \sum_{i=1}^n (y_i - \hat{y_i})^2$$

and $$R^2 = 1 - \frac{SS_{Residual}}{SS_{Total}}$$

Here are my questions:

1. If I apply the train/test split policy for a dataset, does/should equation (1) hold for the testing dataset? NB: I have tried this already and it does not hold for the testing dataset and I am looking for the explanation.

2. Are there any cases where equation (1) will not hold for the training dataset?

3. In some textbooks, $$R^2 = \frac{SS_{Explained}}{SS_{Total}}$$. When should this hold?

• stats.stackexchange.com/questions/569790/… could be of interest Dec 5, 2023 at 5:44
• A way to check your logic is to make sure that $R^2$ can be negative (predictions worse than random) on new data. The formulas you provided are correct. The biggest mistake analysts make is recalibrating the model in the new data by using a correlation coefficient between predicted and observed Y. Dec 5, 2023 at 13:27

1. No. That formula comes from ordinary least squares with an intercept on the training data. I go through the math here to explain why that fails to hold in general. An important point is that the OLS coefficients for the training data (probably) are not the OLS coefficients for the out-of-sample data, meaning that the $$Other$$ term in the link cannot be counted on to be zero. Consider the example below.
set.seed(2023)
N <- 10
x_train <- runif(N)
y_train <- 2*x_train + rnorm(N)
x_test <- runif(N)
y_test <- 2*x_test + rnorm(N)
L_train <- lm(y_train ~ x_train)
L_test <- lm(y_test ~ x_test)
summary(L_train)$coef[, 1] summary(L_test)$coef[, 1]


The coefficients in the test data to give this equality are $$\hat\beta_{intercept} = -2.62802$$ and $$\hat\beta_{slope} = 5.87788$$. However, the coefficients being used are from the training data, $$\hat\beta_{intercept} = 0.07146299$$ and $$\hat\beta_{slope} = 2.04047943$$.

1. This always holds for ordinary least squares linear regression with an intercept. If you use a nonlinear regression (even one fit by minimizing the sum of squared residuals), a linear regression fit by a method other than minimizing the sum of squared residuals, or use a linear regression without an intercept and fit the model by minimizing the sum of squared residuals, you no longer meet the conditions to give the decomposition of the total sum of squares into the classical "explained" and unexplained" sums of squares. Again, that $$Other$$ term from the link cannot be counted on to be zero if you deviate from ordinary least squares linear regression with an intercept. Consider the example below where a linear model $$\mathbb Ey = \beta_0 + \beta_1 x$$ is fit using a minimization of the sum of absolute residuals instead of squared residuals.
library(quantreg)
set.seed(2023)
N <- 10
x <- runif(N)
y <- 2*x + rnorm(N)
Q <- quantreg::rq(y ~ x) # Minimize absolute loss
L <- lm(y ~ x)           # Minimize square loss, as usual
predictions_Q <- predict(Q)
predictions_L <- predict(L)
SSTotal <- sum((y - mean(y))^2)
SSResidual_Q <- sum((y - predictions_Q)^2)
SSExplained_Q <- sum((predictions_Q - mean(y))^2)
SSResidual_L <- sum((y - predictions_L)^2)
SSExplained_L <- sum((predictions_L - mean(y))^2)
SSTotal - (SSResidual_Q + SSExplained_Q) # Differ by about -2
SSTotal - (SSResidual_L + SSExplained_L) # Differ by essentially zero


When we use a linear model with an intercept yet fit the coefficients using an alternative to ordinary least squares, the decomposition fails. However, in the OLS situation, the decomposition holds within what I consider acceptable bounds of doing math on a computer.

1. This will hold when the author defines $$R^2$$ this way. However, it will not coincide with other common definitions of $$R^2$$, such as the $$R^2 = 1 - \frac{SS_{Residual}}{SS_{Total}}$$ you gave, unless that $$Other$$ term from my linked answer equals zero.

Somewhat related is my usual spiel about what $$R^2$$ should mean. You can calculate whatever you want, but I find one calculation to be most useful. I'll close by quoting another post of mine, which also deals with calculating out-of-sample $$R^2$$ (though mostly with regards to what the denominator should be).

Finally, definition 4 makes sense. We have some kind of baseline model (naïvely predict $$\bar y$$ every time, always using the marginal mean as our guess of the conditional mean) and compare our predictions to the predictions made by that baseline model. To draw an analogy to flipping a coin, if someone guesses which side will land up and gets correct predictions less than half the time, that person is a poor predictor. If they are right more than half the time, they are at least improving somewhat upon the naïve “Gee, I don’t know how it’ll land, so I guess I’ll just say heads every time (or tails, or alternate between the two) and get it right about half the time.”

• thanks @Dave. So, to sum up and check my understanding, the condition $𝑆𝑆_𝑇=𝑆𝑆_𝐸+𝑆𝑆_𝑅$ holds only when Other=0 (from your linked example) and we have an OLS solution with one intercept. Also, when we used the learnt regression model on the testing set, the expectation is that the Other term is (likely) not 0, right? Dec 6, 2023 at 12:29
• @STiGMa More or less. Also remember to use a linear model. I think you meant this when you mentioned OLS, but there can be ambiguity in that (it’s possible to minimize the sum of squares residuals in nonlinear models, too). $//$ There are some goofy linear regression estimators that can yield $Other=0$, though these are more of mathematical technicalities rather than being estimators a practitioner would use in an applied problem.
– Dave
Dec 6, 2023 at 12:44
1. Borrowing from the answers here, note that: \begin{align*} \sum_{i=1}^n (y_i-\bar y)^2 &= \sum_{i=1}^n (y_i-\hat y_i)^2+\sum_{i=1}^n (\hat y_i-\bar y)^2\\ &= SSE+SSR+2\sum_{i=1}^n(y_i-\hat y_i)(\hat y_i-\bar y) \end{align*} For this equation to hold we need $$\sum_{i=1}^n(y_i-\hat y_i)(\hat y_i-\bar y) = 0.$$ This only holds if $$\sum_{i=1}^{n} (y_i - \hat{y}_i) = 0$$. While this is true for the training set because it is the first order condition for the intercept, this need not necessary hold for the test set. For simplicity of exposition, consider only a single covarite $$x_i$$ and outcome $$y_i$$. When we use OLS we want to find coefficients $$a,b$$ that minimize the following: $$\mathcal{L} = \sum_{i=1}^{n} (y_i - a - bx_i)^2 \ .$$ Say the minimizer is $$(\hat{a}, \hat{b})$$, then it must satisfy the FOC : $$\frac{\partial{\mathcal{L}}}{\partial a} = \sum_{i=1}^{n} a (y_i - a - bx_i) = 0$$ for $$a = \hat{a}, b= \hat{b}$$. Plugging this in we get and using the fact that predicted value of outcome for training sample $$i$$ is $$\hat{y}_i = \hat{a} + \hat{b}x_i$$ we get: $$\sum_{i=1}^{n} (y - \hat{a} - \hat{b} x_i) = \sum_{i=1}^{n} (y - \hat{y}_i) = 0 \ .$$

2. Equation (1) will always hold for the training set for ordinary least squares regression.

3. This always holds for the training set in the context of ordinary least squares regression with an intercept term, to see this note that $$SSE = SST - SSR$$.

• I contest that last comment. Run the following R code. set.seed(2023); N <- 10; x_train <- runif(N); y_train <- 2*x_train + rnorm(N); x_test <- runif(N); y_test <- 2*x_test + rnorm(N); L <- lm(y_train ~ x_train); preds <- predict(L, data.frame(x_train = x_test)); SSTotal <- sum((y_test - mean(y_test))^2); SSResiduals <- sum((y_test - preds)^2); SSExplained <- sum((preds - mean(y_test))^2); SSTotal - (SSResiduals + SSExplained) The story is the same if you use the in-sample mean mean(y_train).
– Dave
Dec 5, 2023 at 18:30
• @Dave you are right. I meant to write training set in part 3, not test set. Thanks for pointing this out, and I apologize for the typo Dec 6, 2023 at 3:01
• This is not true in the training sample, either, except under specific circumstances. My answer gives an example where the equation fails to hold.
– Dave
Dec 6, 2023 at 3:06
• right, I was operating under the assumption of OLS with an intercept. Dec 6, 2023 at 3:13
• @blooraven thanks; could you please elaborate on what you mean by "it is the first order condition for the intercept"? Dec 6, 2023 at 12:11