Calculating R-Square from Line Fit Equation- Tutorial

What R-Squared Actually Is

R-squared (R²) measures how well your regression line fits the data. It's the proportion of variance in the dependent variable explained by your independent variable(s). A value of 0.85 means 85% of the variation is explained by your model. The remaining 15% is noise you can't account for.

Most people calculate R² using software and never think about the math underneath. But when you have a line fit equation already, you can derive R² directly without rerunning your regression. Here's how.

The R-Squared Formula

The standard formula uses two sums of squares:

R² = 1 - (SSres / SStot)

Where:

SStot is straightforward. It's the sum of squared differences between each y-value and the mean of all y-values:

SStot = Σ(yi - ȳ)²

SSres is the sum of squared differences between actual y-values and predicted y-values from your line:

SSres = Σ(yi - ŷi)²

Calculating R² from Your Line Fit Equation

Your line fit equation looks like this:

ŷ = mx + b

Where m is slope and b is intercept. Here's the step-by-step process:

Step 1: Gather Your Data

You need the original x and y data points. The line equation alone isn't enough—you need the actual y-values to calculate residuals.

Step 2: Calculate the Mean of Y

Add all your y-values and divide by n (number of data points). This is ȳ.

Step 3: Calculate SStot

For each data point, subtract the mean from the actual y-value, square it, and sum all results.

Step 4: Calculate SSres

For each x-value, plug it into your line equation to get the predicted ŷ. Subtract that from the actual y-value, square it, and sum all results.

Step 5: Plug Into the Formula

Divide SSres by SStot, subtract from 1, and you have R².

Worked Example

Say you have five data points and your line fit is ŷ = 2.5x + 10.

x y (actual) ŷ (predicted) (y - ŷ)² (y - ȳ)²
1 15 12.5 6.25 25
2 18 15 9 64
3 20 17.5 6.25 100
4 22 20 4 144
5 25 22.5 6.25 225

Mean of y (ȳ) = 20

SSres = 6.25 + 9 + 6.25 + 4 + 6.25 = 31.75

SStot = 25 + 64 + 100 + 144 + 225 = 558

R² = 1 - (31.75 / 558) = 1 - 0.0569 = 0.943

Your line explains 94.3% of the variance. That's a strong fit.

Quick Method: Correlation Coefficient

If your regression is simple linear (one x variable), you can skip the residuals calculation entirely. R² equals the square of the correlation coefficient (r):

R² = r²

Calculate r between your x and y values, square it, and you're done. This only works for simple linear regression with a single predictor.

What R² Can't Tell You

R² has limits. A high R² doesn't mean your model is correct—it means it fits the data you have. You could be overfitting. It also doesn't tell you if you chose the right functional form. A quadratic relationship forced into a linear model will give you a misleading R².

Low R² isn't always bad either. In noisy domains like economics or social science, R² of 0.3 might be meaningful. Context matters.

Adjusted R²

Adding more predictors always increases R², even if the new variables are useless. Adjusted R² penalizes you for adding predictors that don't contribute meaningful explanatory power. If you're comparing models with different numbers of variables, use adjusted R²:

Adjusted R² = 1 - [(1 - R²)(n - 1) / (n - k - 1)]

Where n is sample size and k is number of predictors.

Tools That Do This Automatically

You don't need to calculate by hand unless you're verifying work or stuck without software.

Bottom Line

Calculating R² from a line fit equation requires your original data points. Plug x values into your equation to get predicted y-values, compute the residuals, sum the squared residuals, divide by total sum of squares, and subtract from 1. Or just square the correlation coefficient if you're working with simple linear regression. That's it.