R-squared, Adjusted R-squared and Pseudo-R-squared

Learn how to use these measures to evaluate the goodness of fit of Linear and certain Nonlinear regression models

One of the most used and therefore misused measures in Regression Analysis is R² (pronounced R-squared). It’s sometimes called by its long name: coefficient of determination and it’s frequently confused with the coefficient of correlation r² . See it’s getting baffling already!

The technical definition of R² is that it is the proportion of variance in the response variable y that your regression model is able to “explain” via the introduction of regression variables.

Clearly, that doesn’t do a whole lot to clear the air.

Hence we appeal to the familiar visual of a linear regression line superimposed on a cloud of (y,x) points:

The Linear Model and the Mean Model (Data set: House prices in New Taipei City, Taiwan)
The Linear Model and the Mean Model (Image by Author) (Data set: House prices in New Taipei City, Taiwan)

The flat horizontal orange line represents the Mean Model. The Mean Model is the simplest model that you can build for your data. For every x value, the mean model predicts the same y value and that value is the mean of your y vector. In this case, it happens to be 38.81 x 10000 New Taiwan Dollar/Ping where one Ping is 3.3 meter².

We can do better than the Mean Model at explaining the variance in y. For that, we need to add one or more regression variables. We will start with one such variable — the age of the house. The red line in the plot represents the predictions of a Linear Regression Model when it’s fitted on the (y, X) data set where y=HOUSE PRICE and X=HOUSE AGE. As you can see, the Linear Model with one variable fits a little better than the Mean Model.

R² lets you quantify just how much better the Linear model fits the data as compared to the Mean Model.

Let’s zoom into a portion of the above graph:

The prediction error of the Mean Model
The prediction error of the Mean Model (Image by Author)

In the above plot, (y_i — y_mean) is the error made by the Mean Model in predicting y_i. If you calculate this error for each value of y and then calculate the sum of the square of each error, you will get a quantity that is proportional to the variance in y. It is known as the Total Sum of Square TSS.

Total Sum of Squares (TSS)
Total Sum of Squares (TSS) (Image by Author)

The Total Sum of Squares is proportional to the variance in your data. It is the variance that the Mean Model wasn’t able to explain.

Because TSS/N is the actual variance in y, the TSS is proportional to the total variance in your data.

Being the sum of squares, the TSS for any data set is always non-negative.

The Mean Model is a very simple model. It contains only one parameter which is the mean of the dependent variable y and it is represented as follows:

The Mean Model
The Mean Model (Image by Author)

The Mean Model is also sometimes known as the Null Model or the Intercept only Model. But this interchangeability of definitions is appropriate only when the Null or Intercept Only model is fitted, i.e. trained on the data set. That’s the only situation in which the Intercept will become the unconditional mean of y.

As mentioned earlier, if you want to better than the Mean Model in explaining the variance in y, you need to add one or more regression variables. Let’s look closely at how adding the regression variable HOUSE AGE has helped the Linear Model in reducing the prediction error:

The prediction error of the Linear Model
The prediction error of the Linear Model (Image by Author)

In the above plot, (y_i — y_pred_i) is the error made by the linear regression model in predicting y_i. This quantity is known as the residual error or simply the residual.

Residual error
Residual error (Image by Author)

In the above plot, the residual error is clearly less than the prediction error of the Mean Model. Such improvement is not guaranteed. In a sub-optimal or badly constructed Linear Model, the residual error could be more than the prediction error of the Mean Model.

If you calculate this residual error for each value of y and then calculate the sum of the square of each such residual error, you will get a quantity that is proportional to the prediction error of the Linear Model. It is known as the Residual Sum of Square RSS.

Residual Sum of Squares (RSS)
Residual Sum of Squares (RSS) (Image by Author)

The Residual Sum of Squares captures the prediction error of your custom Regression Model.

Being the sum of squares, the RSS for a regression model is always non-negative.

Thus, (Residual Sum of Squares)/(Total Sum of Squares) is the fraction of the total variance in y, that your regression model wasn’t able to explain.

Conversely:

1 — (Residual Sum of Squares)/(Total Sum of Squares) is the fraction of the variance in y that your regression model was able to explain.

We will now state the formula for in terms of RSS and TSS as follows:

Formula for R-squared
Formula for R-squared (Image by Author)

Here is the Python code that produced the above plot:

import pandas as pd
from matplotlib import pyplot as plt
from statsmodels.regression.linear_model import OLS as OLS
import statsmodels.api as sm
df = pd.read_csv('taiwan_real_estate_valuation_curated.csv', header=0)
y = df['HOUSE_PRICE_PER_UNIT_AREA']
X = df['HOUSE_AGE_YEARS']
X = sm.add_constant(X)
olsr_model = OLS(endog=y, exog=X)
olsr_results = olsr_model.fit()
y_pred = olsr_results.predict()
fig = plt.figure()
fig.suptitle('Real estate valuation against house age (years)')
plt.xlabel('House Age (years)')
plt.ylabel('House price (10000 New Taiwan Dollar/Ping)')
plt.scatter(df['HOUSE_AGE_YEARS'], df['HOUSE_PRICE_PER_UNIT_AREA'])
mean_y = df['HOUSE_PRICE_PER_UNIT_AREA'].mean()
mean_model, = plt.plot([0.0, 20.0], [mean_y, mean_y], color='orange', linewidth=2, label='Mean Model')
linear_model, = plt.plot(df['HOUSE_AGE_YEARS'], y_pred, marker='o', linestyle='dashed', linewidth=1, markersize=6, color='red', label='OLS Model')
plt.legend(handles=[mean_model, linear_model])
plt.show()
The Mean Model and the Linear Model

And here is the link to the data set.


Range of R²

For Linear Regression Models that are fitted (i.e. trained) using the Ordinary Least Squares (OLS) Estimation technique, the range of R² is 0 to 1. Consider the following plot:

The Total Error and the Residual Error
The Total Error and the Residual Error (Image by Author)

In the above plot, one can see that the residual error (y_i — y_pred_i)² is less than the total error (y_i — y_mean)². It can be shown that if you fit a Linear Regression Model to the above data by using the OLS technique, i.e. by minimizing the sum of squares of residual errors (RSS), the worst that you can do is the Mean Model. But the sum of squares of residual errors of the Mean Model is simply TSS, i.e. for the Mean Model, RSS = TSS.

Hence for OLS linear regression models, RSS ≤ TSS.

Since R² =1 — RSS/TSS, in the case of a perfect fit, RSS=0 and R² =1. In the worst case, RSS=TSS and R² = 0.

For Ordinary Least Squares Linear Regression Models, R² ranges from 0 to 1


Applicability of R² to Nonlinear Regression models

Many non-linear regression models do not use the Ordinary Least Squares Estimation technique to fit the model. Examples of such nonlinear models include:

  • The exponential, gamma and inverse-Gaussian regression models used for continuously varying y in the range (-∞, ∞).
  • Binary choice models such as the Logit (a.k.a. Logistic) and Probit and their variants such as Ordered Probit used for y = 0 or 1, and the general class of Binomial regression models.
  • The Poisson, Generalized Poisson and the Negative Binomial regression models for discrete non-negative y ϵ [0, 1, 2, …, ∞). i.e. models for counts based data sets.

The model fitting procedure of these nonlinear models is not based on progressively minimizing the sum of squares of residual errors (RSS) and therefore the optimally fitted model could have a residual sum of squares that is greater than total sum of squares. That means, R² for such models can be a negative quantity. As such, R² is not a useful goodness-of-fit measure for most nonlinear models.

R-squared is not a useful goodness-of-fit measure for most nonlinear regression models.

A notable exception is regression models that are fitted using the Nonlinear Least Squares (NLS) estimation technique. The NLS estimator seeks to minimizes the sum of squares of residual errors thereby making R² applicable to NLS regression models.

Later, we’ll look at some alternatives to R-squared for nonlinear regression models.


Alternate formula for R-squared for Linear Models

Let’s look at the following figure again:

Explained Error (ESS)
Explained Error (ESS) (Image by Author)

In the above plot, (y_pred_i — y_mean) is the reduction in prediction error that we achieved by adding a regression variable HOUSE_AGE_YEARS to our model.

If you calculate this difference for each value of y and then calculate the sum of the square of each difference, you will get a quantity that is proportional to the variance in y that the Linear Regression model was able to explain. It is known as the Explained Sum of Square ESS.

Explained Sum of Squares (ESS)
Explained Sum of Squares (ESS) (Image by Author)

The Explained Sum of Squares is proportional to the variance in your data that your regression model was able to explain.

Let’s do some math.

From the above plot, one can see that:

Derivation of the alternate formula for R-Squared for OLSR models
Derivation of the alternate formula for R-Squared for OLSR models (Image by Author)

It can be shown that when the Least Squares Estimation technique is used to fit a linear regression model, the term 2*(y_i — y_pred)*(y_pred — y_mean) is 0.

So for the special case of OLS Regression Model:

The identity applicable to OLSR models
The identity applicable to OLSR models (Image by Author)

In other words:

The alternate formula for R-Squared for OLSR models
The alternate formula for R-Squared for OLSR models (Image by Author)

Example

The linear regression model that we have used to illustrate the concepts has been fitted on a curated version of the New Taipei City Real Estate data set. Let’s see how to build this linear model and find the R² score for it.

We’ll begin by importing all the required libraries:

import pandas as pd
from matplotlib import pyplot as plt
from statsmodels.regression.linear_model import OLS as OLS
import statsmodels.api as sm

Next, let’s read in the data file using Pandas. You can download the data set from here.

df = pd.read_csv('taiwan_real_estate_valuation_curated.csv', header=0)

Print the top 10 rows:

Top 10 rows in the New Taipei City Real Estate data set
Top 10 rows in the New Taipei City Real Estate data set (Image by Author)

Our dependent y variable is HOUSE_PRICE_PER_UNIT_AREA and our explanatory a.k.a. regression a.k.a. X variable is HOUSE_AGE_YEARS.

We’ll carve out the y and X matrices:

y = df['HOUSE_PRICE_PER_UNIT_AREA']
X = df['HOUSE_AGE_YEARS']

Since houses of age zero years, i.e. new houses will also have some non-zero price, we need to add a y intercept. This is the ‘β0’ in the equation of the straight line: y_pred = β1*X + β0

X = sm.add_constant(X)

Next, we build and fit the OLS regression model and print the training summary:

olsr_model = OLS(endog=y, exog=X)
olsr_results = olsr_model.fit()

print(olsr_results.summary())

Here’s the output we get:

Output of the OLSR model
Output of the OLSR model (Image by Author)

We see that the R² is 0.238. R² is not very large indicating a weak linear relationship between HOUSE_PRICE_PER_UNIT_AREA and HOUSE_AGE_YEARS.

The equation of the fitted model is as follows:

HOUSE_PRICE_PER_UNIT_AREA_pred = -1.0950*HOUSE_AGE_YEARS + 50.6617.

There is a somewhat weak and negative relationship between the age of the house and its price. And houses of zero age are predicted to have a mean price per unit area of 50.6617 x 10000 New Taiwan Dollar/Ping.


How to increase R-squared

In a way, this is like asking how to become rich or how to reduce weight? As the saying goes, be careful what you ask for, because you just might get it!

The naive way to increase R² in an OLS linear regression model is to throw in more regression variables but this can also lead to an over-fitted model.

To see why adding regression variables to an OLS Regression model does not reduce R², consider two linear models fitted using the OLS technique:

y_pred = β1*X1 + β0

y_pred = β2*X2 + β1*X1 + β0

The OLS estimation technique minimizes the residual sum of squares (RSS). If the second model does not improve the value of R² over the first model, the OLS estimation technique will set β2 to zero or to some value that is statistically insignificant which would essentially get us back to the first model. Generally speaking, each time you add a new regression variable and refit the model using OLS, you will either get a model with a better R² or essentially the same R² as the more constrained model.

This property of OLS estimation can work against you. If you go on adding more and more variables, the model will become increasingly unconstrained and the risk of over-fitting to your training data set will correspondingly increase.

On the other hand, the addition of correctly chosen variables will increase the goodness of fit of the model without increasing the risk of over-fitting to the training data.

This tussle between our desire to increase R² and the need to minimize over-fitting has led to the creation of another goodness-of-fit measure called the Adjusted-R².

Adjusted-R²

The concept behind Adjusted-R² is simple. To get Adjusted-R², we penalize R² each time a new regression variable is added.

Specifically, we scale (1-R²) by a factor that is directly proportional to the number of regression variables. Greater is the number of regression variables in the model, greater is this scaling factor and greater is the downward adjustment to R².

The formula for Adjusted-R² is:

Formula for Adjusted-R²
Formula for Adjusted-R² (Image by Author)

df_mean_model is the degrees of freedom of the mean model. For a training data set of size N, df_mean_model=(N-1).

df_model is the degrees of freedom of the regression model. For a model with p regression variables, df_model=(N-1-p).

Substituting:

Formula for Adjusted-R²
Formula for Adjusted-R² (Image by Author)

One can see that as the model acquires more variables, p increases and the factor (N-1)/(N-1-p) increases which has the effect of depressing R².

Drawbacks of Adjusted-R²

Adjusted-R² has some problems, notably:

  1. It treats the effect of all regression variables equally. In reality, some variables are more influential than others in their ability to make the model fit (or over-fit) the training data.
  2. The formula for Adjusted-R² yields negative values when R² falls below p/(N-1) thereby limiting the use of Adjusted-R² to only values of R² that are above p/(N-1).

Comparing the goodness-of-fit of two linear models using Adjusted-R²

We will illustrate the process of using Adjusted-R² using our example data set. To do so, let’s introduce another regression variable NUM_CONVENIENCE_STORES_IN_AREA and refit our OLS regression model on the data set:

y = df['HOUSE_PRICE_PER_UNIT_AREA']
X = df[['HOUSE_AGE_YEARS', 'NUM_CONVENIENCE_STORES_IN_AREA']]
X = sm.add_constant(X)

olsr_model = OLS(endog=y, exog=X)
olsr_results = olsr_model.fit()

Let’s print the model training summary:

Output of the OLSR model with two regression variables
Output of the OLSR model with two regression variables (Image by Author)

Notice that both R² and Adjusted-R² of the model with two regression variables is more than double that of the model with one variable:

Comparison of R² and Adjusted-R² of the one variable and two variable OLSR models
Comparison of R² and Adjusted-R² of the one variable and two variable OLSR models (Image by Author)

On balance, the addition of the new regression variable has increased the goodness-of-fit. This conclusion is further supported by the coefficients by the p-values of the regression parameter coefficients. We see from the regression output that the p-values of all three coefficients in the 2-variable OLSR model are essentially zero indicating that all parameters are statistically significant:

Parameters of the OLSR model with two regression variables
Parameters of the OLSR model with two regression variables (Image by Author)

The equation of the fitted two-variable model is as follows:

HOUSE_PRICE_PER_UNIT_AREA_pred = -0.7709*HOUSE_AGE_YEARS + 2.6287*NUM_CONVENIENCE_STORES_IN_AREA + 36.9925


R² in nonlinear regression models

Nonlinear models often use model fitting techniques such as Maximum Likelihood Estimation (MLE) which do not necessarily minimize the Residual Sum of Squares (RSS). Thus, given two nonlinear models that have been fitted using MLE, the one with the greater goodness-of-fit may turn out to have a lower R² or Adjusted-R². Another consequence of this fact is that adding regression variables to nonlinear models can reduce R². Overall, R² or Adjusted-R² should not be used for judging the goodness-of-fit of nonlinear regression model.

For nonlinear models, there have been a range of alternatives proposed for the humble R². We’ll look at one such alternative that is based on the following identity that we have come to know so well:

Total Sum of Squares (TSS) = Residual Sum of Squares (RSS) + Explained Sum of Squares (ESS).

While this identity works for OLS Linear Regression Models a.k.a. Linear Models, for nonlinear regression models, it turns out that a similar kind of triangle identity works using the concept of Deviance. We’ll explain the concept of Deviance in a bit but for now, let’s look at this identity for nonlinear regression models:

Deviance of the Intercept-only model = Deviance of the fitted nonlinear model + Deviance explained by the fitted nonlinear model.

Notation-wise:

The Deviance identity for nonlinear regression models
The Deviance identity for nonlinear regression models (Image by Author)

Where:

D(y, y_mean) = Deviance of the Intercept-only model

D(y, y_pred) = Deviance of the fitted nonlinear model

D(y_pred, y_mean) = Deviance explained by the fitted nonlinear model

Using the above identity, Cameron and Windmeijer have described (see paper links at the end of this page) the following Deviance based formula for R² that is applicable to nonlinear models, especially for Generalized Linear Regression Models (known as GLMs) that are fitted on discrete data. Commonly occurring examples of such nonlinear models are the Poisson and Generalized Poisson models, the Negative Binomial Regression model and the Logistic Regression Model:

Pseudo-R² for nonlinear models for discrete data
Pseudo-R² for nonlinear models for discrete data (Image by Author)

Before we go any further, some terms deserve an explanation.

Deviance

Deviance of a regression model measures by how much the Log-Likelihood (more about Log-Likelihood in a bit) of the fitted regression model is greater than the Log-Likelihood of the saturated model. Specifically,

Deviance of a fitted regression model
Deviance of a fitted regression model (Image by Author)

So this begs two questions: What is a saturated model and what is Likelihood?

Saturated Model

A saturated regression model is one in which the number of regression variables is equal to the number of unique y values in the sample data set. What a saturated model gives you is essentially N equations in N variables, and we know from college algebra that a system of N equations in N variables yields an exact solution for each variable. Thus, a saturated model can be built to perfectly fit each y value. A saturated model thereby yields the maximum possible fit on your training data set.

Likelihood

Now let’s tackle Likelihood. The Likelihood of a fitted regression model is the probability (or probability density) of jointly observing all y values in the training data set using the predictions of the fitted model as the mean parameter of the probability distribution function. The procedure for calculating Likelihood is as follows:

  • Say your training data set contains 100 y observations. What you want to calculate is the joint probability of observing y1 and y2 and y3 and…up to y100 with your fitted regression model.
  • So you start with fitting your regression model on this training data.
  • Next, you feed the 100 rows in your training set through the fitted model to get 100 predictions from this model. These 100 y_pred_i values are your 100 conditional means (the ith mean is conditioned upon the corresponding ith row in your X matrix).
  • Now you set the 100 observed y values and the 100 conditional means (the predictions) in the probability (density) function of y to get 100 probability values. One probability for each y_i in y.
  • Finally, you multiply together these 100 probabilities to get the Likelihood value. This is the likelihood of observing your training data set given your fitted model.

To get a feel for the calculation, I’d encourage you to refer to the section on the Poisson regression model which contains a sample calculation for Likelihood for a Poisson Model.

The Log-Likelihood is simply the natural logarithm of the Likelihood of the fitted model.

With these concepts under our belt, let’s circle back to our Deviance based formula for Pseudo-R²:

Pseudo-R² for nonlinear models for discrete data
Pseudo-R² for nonlinear models for discrete data (Image by Author)

As mentioned earlier:

D(y, y_pred) = Deviance of the fitted nonlinear model

D(y, y_mean) = Deviance of the Intercept-only model (a.k.a. the null-model). The null model contains only the intercept i.e. no regression variables.

Using our formula for Deviance:

Deviance of a fitted regression model
Deviance of a fitted regression model (Image by Author)

And,

Deviance of the null model
Deviance of the null model (Image by Author)

Therefore:

Pseudo-R² for nonlinear models for discrete data
Pseudo-R² for nonlinear models for discrete data (Image by Author)

Sometimes, the following simpler version of Pseudo- R² proposed by McFadden is used (see paper link below for details):

McFadden’s Pseudo R²
McFadden’s Pseudo R² (Image by Author)

McFadden’s Pseudo-R² is implemented by the Python statsmodels library for discrete data models such as Poisson or NegativeBinomial or the Logistic (Logit) regression model. If you call DiscreteResults.prsquared() , you will get the value of McFadden’s R-squared value on your fitted nonlinear regression model.


References, Citations and Copyrights

Data set

The Taiwan House prices data set retrieved from UCI Machine Learning repository. Curated data set download link

Data set citation: I-Cheng Yeh, Tzu-Kuang Hsu, Building real estate valuation models with comparative approach through case-based reasoning, Applied Soft Computing, Volume 65, 2018, Pages 260-271, ISSN 1568-4946, https://doi.org/10.1016/j.asoc.2018.01.0

Paper and Book Links

Cameron A. Colin, Trivedi Pravin K., Regression Analysis of Count Data, Econometric Society Monograph №30, Cambridge University Press, 1998. ISBN: 0521635675

McCullagh P., Nelder John A., Generalized Linear Models, 2nd Ed., CRC Press, 1989, ISBN 0412317605, 9780412317606

McFadden, D. (1974), Conditional logit analysis of qualitative choice behaviour, in: P. Zarembka (ed.), Frontiers in Econometrics, Academic Press, New York, 105–142. PDF Download Link

Cameron, A., & Frank A. G. Windmeijer. (1996). R-Squared Measures for Count Data Regression Models with Applications to Health-Care Utilization. Journal of Business & Economic Statistics, 14(2), 209–220. doi:10.2307/1392433 PDF Download Link

A. Colin Cameron, Frank A.G. Windmeijer, An R-squared measure of goodness of fit for some common nonlinear regression models, Journal of Econometrics, Volume 77, Issue 2, 1997, Pages 329–342, ISSN 0304–4076,
https://doi.org/10.1016/S0304-4076(96)01818-0. PDF Download link

N. J. D. Nagelkerke, A note on a general definition of the coefficient of determination, Biometrika, Volume 78, Issue 3, September 1991, Pages 691–692, https://doi.org/10.1093/biomet/78.3.691. PDF Download link

Images

All images are copyright Sachin Date under CC-BY-NC-SA, unless a different source and copyright are mentioned underneath the image.


I write about topics in data science, with a specific focus on time series analysis, regression and forecasting.

If you liked this content, please subscribe to receive new content in your email:


UP: Table of Contents


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.