Linear Regression Explained: Line of Best Fit, r, and R²
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:
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
Step 2: Compute the slope
Step 3: Compute the intercept
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:
- 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:
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:
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.