R-Squared (Coefficient of Determination) Calculator

R-Squared (Coefficient of Determination) Calculator

Emerging AI Skill PRO

Enter Calculation

Formula

R2=1−SSresSStotwhere{SSres=∑(Y−Y^)2SStot=∑(Y−Y‾)2Y‾=1n∑Y\begin{gathered} R^{2} = 1 - \frac{SS_{res}}{SS_{tot}} \\[1.5em] \text{where}\quad \left\{ \begin{aligned} SS_{res} &= \sum \left( \text{Y} - {{\hat{\text{Y}}}} \right)^{2} \\ SS_{tot} &= \sum \left( \text{Y} - \bar{\text{Y}} \right)^{2} \\ \bar{\text{Y}} &= \frac{1}{n}\sum \text{Y} \end{aligned} \right. \end{gathered}
Usage stats

Advertisement

Results

R² (Coefficient of Determination)
0.9486
fraction of variance explained
Residual sum of squares (SS_res) 1.5
Total sum of squares (SS_tot) 29.1875
Number of data points (n) 4

Report an error on this page

What Is R-Squared?

R-squared (R²), also called the coefficient of determination, measures how well a model's predictions match the observed data. It expresses the proportion of the variance in the dependent variable that is explained by the model. R² ranges from 0 to 1 for a sensible model: a value of 1 means the predictions perfectly fit the data, while 0 means the model explains none of the variability and is no better than predicting the mean. Negative values can occur when a model fits worse than a horizontal line at the mean.

Scatter plot with regression line showing residuals as vertical gaps between points and line
R-squared measures how well the regression line fits the observed data points; the vertical gaps are the residuals.

How to Use This Calculator

Enter your observed values (the actual Y data) and your predicted values (the model's outputs Ŷ) as comma-separated lists. The two lists should be in the same order and the same length. Click calculate to get R² along with the residual sum of squares (\(SS_{res}\)), the total sum of squares (\(SS_{tot}\)), and the number of data points used.

The Formula Explained

The calculator first computes the mean of the observed values. The total sum of squares is the sum of squared differences between each observation and that mean: $$SS_{tot} = \sum (y_i - \bar{y})^2.$$ The residual sum of squares is the sum of squared differences between observed and predicted values: $$SS_{res} = \sum (y_i - \hat{y}_i)^2.$$ R² is then $$R^2 = 1 - \frac{SS_{res}}{SS_{tot}}.$$ When residuals are small relative to total variation, \(SS_{res}/SS_{tot}\) is near zero and R² approaches 1.

Advertisement
Diagram comparing total variation around the mean versus residual variation around the regression line
R² compares residual variation (\(SS_{res}\)) around the fitted line to total variation (\(SS_{tot}\)) around the mean.

Worked Example

Suppose the observed values are 3, −0.5, 2, 7 and the predicted values are 2.5, 0.0, 2, 8. The mean of the observed values is $$\frac{3 - 0.5 + 2 + 7}{4} = 2.875.$$ $$SS_{res} = 0.5^2 + (-0.5)^2 + 0^2 + (-1)^2 = 0.25 + 0.25 + 0 + 1 = 1.5.$$ $$SS_{tot} = (0.125)^2 + (-3.375)^2 + (-0.875)^2 + (4.125)^2 \approx 0.015625 + 11.390625 + 0.765625 + 17.015625 = 29.1875.$$ $$R^2 = 1 - \frac{1.5}{29.1875} \approx 0.9486.$$

Advertisement

R² Across Different Fit Scenarios

All rows use the same observed values: 2, 4, 6, 8. The mean is \(\bar{Y} = (2+4+6+8)/4 = 5\), so \(SS_{tot} = (-3)^2 + (-1)^2 + 1^2 + 3^2 = 9+1+1+9 = 20\). Only the predictions change.

Scenario Predicted values SS_res SS_tot R²
Perfect fit 2, 4, 6, 8 0 20 1
Close fit 3, 4, 6, 7 (-1)² + 0² + 0² + 1² = 2 20 0.9
Moderate fit 4, 4, 6, 6 (-2)² + 0² + 0² + 2² = 8 20 0.6
Predicting the mean 5, 5, 5, 5 9 + 1 + 1 + 9 = 20 20 0
Poor fit (reversed trend) 8, 6, 4, 2 (-6)² + (-2)² + 2² + 6² = 80 20 -3

Worked check for the close fit: \(R^2 = 1 - 2/20 = 1 - 0.1 = 0.9\). For the poor fit: \(R^2 = 1 - 80/20 = 1 - 4 = -3\). Whenever \(SS_{res}\) exceeds \(SS_{tot}\), R² goes below zero.

FAQ

Can R² be negative? Yes. When the model fits worse than simply predicting the mean of the data, \(SS_{res}\) exceeds \(SS_{tot}\) and R² becomes negative.

Does a high R² mean a good model? Not always. A high R² indicates a close fit to this data but can result from overfitting. Always validate with out-of-sample data.

What's the difference between R² and adjusted R²? Adjusted R² penalizes adding predictors that don't improve the model, which is useful for multiple regression. This calculator reports the unadjusted R².

Last updated:

Most popular in Math and Statistics

View all Math and Statistics calculators