R-squared › The R-squared Formula

Michael BrenndoerferApril 4, 20251 min read
Machine Learning from ScratchAll chapters
Part 3 of 9Foundations
Chapter 24R-squared › The R-squared Formula
Reading section 3 of 12 · The R-squared Formula · 1 minSection 3 of 12 · 1 min

Book contents

Section 3 of 12 · 1 min read

R-squared › The R-squared FormulaOne section · Sections in this chapter3 / 12 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

R-squared is defined as:

R2=1−SSresSStotR^2 = 1 - \frac{SS_{\text{res}}}{SS_{\text{tot}}}

Where:

  • SSres=∑i=1n(yi−y^i)2SS_{\text{res}} = \sum_{i=1}^n (y_i - \hat{y}_i)^2: Residual sum of squares (unexplained variance)
  • SStot=∑i=1n(yi−yˉ)2SS_{\text{tot}} = \sum_{i=1}^n (y_i - \bar{y})^2: Total sum of squares (total variance)
  • yiy_i: Actual value
  • y^i\hat{y}_i: Predicted value from the model
  • yˉ\bar{y}: Mean of the actual values

Mathematical Intuition

The formula can be understood through variance decomposition. The total variance in the dependent variable can be split into explained variance (how much the model explains) and unexplained variance (how much remains). R-squared represents the proportion of total variance that is explained by the model. For simple linear regression, R-squared equals the square of the correlation coefficient between the predictor and response variables (R2=r2R^2 = r^2).