Back to All Blogs
StatisticsFeatured

Linear Regression Explained: Line of Best Fit, r, and R²

Solver360 Team
February 5, 2025
8 min read

Linear Regression Explained: Line of Best Fit, r, and R²

Linear regression finds the straight line that best describes the relationship between two variables — an independent variable x and a dependent variable y. It is the foundation of forecasting, trend analysis, and much of data science. This guide shows how to compute the line of best fit by hand, interpret the correlation coefficient, and make predictions.

You can verify every step with our free Linear Regression & Correlation Calculator.

The Regression Line

The least-squares regression line has the form:

📐 Formula
ŷ = a + b·x Slope: b = (nΣxy − ΣxΣy) / (nΣx² − (Σx)²) Intercept: a = ȳ − b·x̄

The method is called "least squares" because it chooses the line that minimizes the sum of the squared vertical distances between the data points and the line.

Step-by-Step Example

Consider these five (x, y) pairs: (1, 2), (2, 4), (3, 5), (4, 4), (5, 5).

Step 1: Compute the sums

📐 Formula
n = 5 Σx = 15, Σy = 20, Σxy = 66, Σx² = 55, Σy² = 86 x̄ = 3, ȳ = 4

Step 2: Compute the slope

📐 Formula
b = (5×66 − 15×20) / (5×55 − 15²) = (330 − 300) / (275 − 225) = 30 / 50 = 0.6

Step 3: Compute the intercept

📐 Formula
a = ȳ − b·x̄ = 4 − 0.6×3 = 4 − 1.8 = 2.2

So the regression line is ŷ = 2.2 + 0.6·x.

The Correlation Coefficient (r)

Pearson's correlation coefficient measures the strength and direction of the linear relationship:

📐 Formula
r = (nΣxy − ΣxΣy) / √[(nΣx² − (Σx)²)(nΣy² − (Σy)²)] = 30 / √(50 × 30) ≈ 0.775
  • r = +1: perfect positive linear relationship
  • r = −1: perfect negative linear relationship
  • r = 0: no linear relationship

An r of about 0.775 indicates a moderate-to-strong positive relationship.

R²: How Much Variance Is Explained

R² (the coefficient of determination) is simply r squared:

📐 Formula
R² = 0.775² ≈ 0.60

This means about 60% of the variation in y is explained by its linear relationship with x. The remaining 40% is due to other factors or random noise.

Making Predictions

To predict y for a new x, plug it into the line. For x = 6:

📐 Formula
ŷ = 2.2 + 0.6×6 = 2.2 + 3.6 = 5.8

Interpreting Results Carefully

  • Correlation is not causation. A high r means the variables move together, not that one causes the other.
  • Extrapolation is risky. Predictions far outside the range of your data can be unreliable.
  • Check the scatter. Linear regression assumes a roughly linear pattern; always look at your data first.

Try It Yourself

Enter your x and y values into the Linear Regression & Correlation Calculator to get the equation, r, R², covariance, and predictions instantly — with a downloadable PDF report. To project a time series forward instead, use the Forecasting Calculator; to summarize a single variable, use the Standard Deviation & Statistics Calculator.

Key Takeaways

  • The least-squares line ŷ = a + b·x minimizes squared prediction errors.
  • For the example data, ŷ = 2.2 + 0.6·x, r ≈ 0.775, R² ≈ 0.60.
  • r measures strength and direction; R² is the fraction of variance explained.
  • Never confuse correlation with causation, and avoid extrapolating too far.
Tags:
Linear RegressionCorrelationStatisticsForecasting