Data Science & ML
Intermediate

Coefficient of Determination (R²)

Proportion of variance in the target explained by the model.

Formula

R2=1−∑(yi−y^i)2∑(yi−yˉ)2R^2 = 1 - \frac{\sum(y_i-\hat{y}_i)^2}{\sum(y_i-\bar{y})^2}

Variables

y_iActual
\hat{y}_iPredicted
\bar{y}Mean of actual

Example

R² = 0.85 means 85% of variance is explained

How it works

R² compares the squared prediction error of a model to the total squared variation you would see if you always predicted the mean of the target. If the model fits better than the mean-only baseline, the ratio of leftover error is small and R² moves toward 1; if it fits no better than the mean, R² is 0. It is built from sums of squared deviations because squaring penalizes large misses and makes positive and negative errors count equally.

When to use it

Use R² when evaluating regression models on continuous targets, such as predicting house prices, sales, or temperatures. It is especially useful for comparing how much better a model is than a simple average-based baseline on the same dataset.

Worked example, step by step

Actual values: y = [10, 12, 9, 15] Predicted values: ŷ = [11, 11, 10, 14] Step 1: Compute the mean of actual values ȳ = (10 + 12 + 9 + 15) / 4 = 46 / 4 = 11.5 Step 2: Compute the residual sum of squares Σ(y_i - ŷ_i)² = (10-11)² + (12-11)² + (9-10)² + (15-14)² = 1 + 1 + 1 + 1 = 4 Step 3: Compute the total sum of squares Σ(y_i - ȳ)² = (10-11.5)² + (12-11.5)² + (9-11.5)² + (15-11.5)² = 2.25 + 0.25 + 6.25 + 12.25 = 21 Step 4: Plug into the formula R² = 1 - 4/21 = 1 - 0.190476... R² ≈ 0.81 Final answer: the model explains about 81% of the variance in the target values.

Common mistakes to avoid

  • Thinking R² is the same as accuracy or a percent of correct predictions
  • Assuming R² can only be between 0 and 1, even though it can be negative for poor models
  • Using R² to compare models across different datasets without considering that the target variance may differ

Did You Know?

R² can be negative if the model is worse than predicting the mean.

Share this formula

Reviewed & maintained by the FormulaWon editorial teamLast reviewed September 2026