Find the least-squares line of best fit, correlation and R² for your data.
Calculated instantly in your browser.
How do you calculate a linear regression line of best fit?
Slope m = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)², and intercept b = ȳ − m·x̄. Correlation r = Σ(x − x̄)(y − ȳ) ÷ √(Σ(x − x̄)²·Σ(y − ȳ)²), with R² = r². Least-squares fitting finds the line minimising total squared vertical distance to the points. Example: for x = 1–5 and y = 2,4,5,4,5 the best-fit line is y = 0.6x + 2.2 with R² = 0.6.
Understanding your result
Least-squares regression finds the line that minimises the total squared vertical distance to the points. The slope is the change in y per unit of x; R² (0 to 1) is the fraction of the variation in y explained by x.
Formula and method
Slope m = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)². Intercept b = ȳ − m·x̄. Correlation r = Σ(x − x̄)(y − ȳ) ÷ √(Σ(x − x̄)²·Σ(y − ȳ)²); R² = r².
Assumptions and limitations
This tool fits one straight line by ordinary least squares, so it captures only linear trends; curved relationships are summarised poorly and may show a low R squared despite a clear pattern. It needs at least two distinct x-values and is sensitive to outliers, which can pull the line. Correlation here does not prove that x causes y.
Worked example
For x = 1–5 and y = 2,4,5,4,5 the best-fit line is y = 0.6x + 2.2 with R² = 0.6.
How to use this tool
- Enter your x values.
- Enter a y value for each x, in the same order.
- Read the equation, r, R² and any prediction.
Common mistakes to avoid
- Entering a different number of x and y values.
- Assuming a high R² proves x causes y — correlation is not causation.
About the Linear Regression Calculator
The Linear Regression Calculator fits a straight line to your data using the least-squares method. Enter paired x and y values to get the equation of the line, the slope and intercept, the correlation coefficient r and R², and a prediction for any x.
Who should use this tool
Students, researchers, analysts and anyone finding the trend between two variables.
Benefits
- Equation of the line of best fit (y = mx + b).
- Correlation coefficient r and R² goodness-of-fit.
- Predict y for a value of x.
- Shows the working behind the result.
Practical use cases
- Finding the trend in sales over time.
- Relating study hours to test scores.
- Checking how strongly two variables move together.
Frequently asked questions
What does R² mean?
R² is the proportion of the variation in y that the line explains, from 0 (no fit) to 1 (perfect fit).
What is the difference between r and R²?
r is the correlation coefficient (−1 to 1) showing direction and strength; R² is simply r squared and measures fit quality.
What if my data isn't a straight line?
Least-squares regression always fits the best straight line, even when the true relationship is curved, so the result can be misleading. A low R squared, or an obvious pattern in the residuals, signals a poor linear fit. In that case a non-linear model or a transformation of the data usually describes the trend far better.