How to Calculate Adjusted R-Squared in R


R-squared, often written R2, is the proportion of the variance in the response variable that can be explained by the predictor variables in a linear regression model.

The value for R-squared can range from 0 to 1. A value of 0 indicates that the response variable cannot be explained by the predictor variable at all while a value of 1 indicates that the response variable can be perfectly explained without error by the predictor variables.

The adjusted R-squared is a modified version of R-squared that adjusts for the number of predictors in a regression model. It is calculated as:

Adjusted R2 = 1 – [(1-R2)*(n-1)/(n-k-1)]

where:

  • R2: The R2 of the model
  • n: The number of observations
  • k: The number of predictor variables

Because R2 always increases as you add more predictors to a model, adjusted R2 can serve as a metric that tells you how useful a model is, adjusted for the number of predictors in a model.

This tutorial explains how to calculate adjusted R2 for a regression model in R.

Related: What is a Good R-squared Value?

Example: How to Calculate Adjusted R-Squared in R

We can use the following code to build a multiple linear regression model in R using the built-in dataset called mtcars:

model <- lm(hp ~ mpg + wt + drat + qsec, data=mtcars)

And we can use one of the following three methods to find the adjusted R-squared of the model:

Method 1: Use the summary() function

We can view both the R-squared and the adjusted R-squared of the model by simply using the summary() function:

summary(model)

Call:
lm(formula = hp ~ mpg + wt + drat + qsec, data = mtcars)

Residuals:
    Min      1Q  Median      3Q     Max 
-48.801 -16.007  -5.482  11.614  97.338 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  473.779    105.213   4.503 0.000116 ***
mpg           -2.877      2.381  -1.209 0.237319    
wt            26.037     13.514   1.927 0.064600 .  
drat           4.819     15.952   0.302 0.764910    
qsec         -20.751      3.993  -5.197 1.79e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 32.25 on 27 degrees of freedom
Multiple R-squared:  0.8073,	Adjusted R-squared:  0.7787 
F-statistic: 28.27 on 4 and 27 DF,  p-value: 2.647e-09

At the bottom of the output we can see the following:

  • Multiple R-squared: 0.8073
  • Adjusted R-squared: 0.7787

Method 2: Use summary(model)$adj.r.squared

If we simply wanted to obtain the adjusted R-squared of the model, we could use the following function:

summary(model)$adj.r.squared

[1] 0.7787005

Method 3: Use a custom function

Yet another way to find the adjusted R-squared of the model is to write a custom function:

#define function to calculate adjusted R-squared
adj_r2 <- function(x) {
   return (1 - ((1-x$adj.r.squared)*(nobs(x)-1)/(nobs(x)-length(x$coefficients)-1)))
}

#use function to calculate adjusted R-squared of the model
adj_r2(model)

[1] 0.7787005
numeric(0)

Notice that each of the three methods shared here result in the same value for adjusted R-squared.

Additional Resources

How to Perform Simple Linear Regression in R
How to Perform Multiple Linear Regression in R
How to Perform Polynomial Regression in R

Leave a Reply

Your email address will not be published.