**R-squared**, often written R^{2}, 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 R ^{2} = 1 – [(1-R^{2})*(n-1)/(n-k-1)]**

where:

**R**: The R^{2}^{2}of the model**n**: The number of observations**k**: The number of predictor variables

Because R^{2} always increases as you add more predictors to a model, adjusted R^{2} 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 R^{2} 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-summary(x)$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

More clear and understandable, it helps me as well people interested with a fit script. Thank you.

correct the custom function:

adj_r2 <- function(x)

{

r2 <- summary(x)$r.squared

n <- nobs(x)

p <- length(x$coefficients) – 1

1 – (1 – r2) * (n – 1) / (n – p – 1)

# or

# r2 – p * (1 – r2) / (n – (p + 1))

}