Quick Answer
Linear regression finds the straight line y = a + bx that best fits your data. The slope is b = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²) and the intercept is a = (Σy − bΣx) ÷ n. For the sample data above, the line is y = 2.2 + 0.6x, with r = 0.7746.
Embed this calculator
Adjust the size if needed, then copy the code below into your page or blog. The calculator will load in an iframe and stay updated automatically.
Table of Contents
- Linear Regression Calculator
- Quick Answer
- Related Calculators
- How This Linear Regression Calculator Works
- How to Use the Linear Regression Calculator
- Core Formulas
- Key Terms
- Worked Example
- Real-Life Examples
- Correlation vs Causation
- Sample Data and Fitted Values
- Reading r and R²
- Common Mistakes to Avoid
- Frequently Asked Questions
- References & Sources
How This Linear Regression Calculator Works
Enter your data as x, y pairs, one per line. The calculator adds up x, y, xy and x², then uses the least squares formulas to find the slope and intercept of the best-fit line. It also gives the correlation coefficient r, R², and a predicted y value for any x you enter.
Quick explanation: the best-fit line makes the squared vertical distances from the points as small as possible.
- Slope: how much y changes for each 1 unit increase in x.
- Intercept: the value of y when x is 0.
- r: how closely the points follow a straight line, from −1 to 1.
How to Use the Linear Regression Calculator
- Type or paste your data, one x, y pair per line.
- Optionally, enter an x value to predict y.
- Press Calculate.
- Read the line of best fit, r and R².
- Follow the steps to check the math by hand.
Core Formulas
- Slope: b = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²)
- Intercept: a = (Σy − bΣx) ÷ n
- Correlation: r = (nΣxy − ΣxΣy) ÷ √((nΣx² − (Σx)²)(nΣy² − (Σy)²))
- R²: r × r, the share of the variation in y explained by x
Key Terms
- Regression line: the straight line that best fits the data.
- Least squares: the method that minimizes the sum of squared residuals.
- Residual: the actual y minus the predicted y.
- Correlation coefficient (r): a measure from −1 to 1 of how well points fit a line.
- Coefficient of determination (R²): the share of variation in y explained by x.
Worked Example
Hours studied and test scores: (2, 65), (3, 70), (5, 80), (6, 82), (8, 90).
- Slope = 4.1404 points per hour, intercept = 57.5263
- Line: score = 57.53 + 4.14 × hours, with r = 0.9945
- Predicted score after 7 hours = 86.5
Real-Life Examples
- Study time: the example above predicts about 87 points after 7 hours.
- Sales trends: fit monthly sales against month number to see the trend per month.
- Science labs: fit measurements to find a rate, such as speed from distance and time.
- Pricing: fit house price against square footage to estimate price per square foot.
Correlation vs Causation
- Correlation: two things move together.
- Causation: one thing causes the other.
- Caution: a strong r does not prove cause. A third factor may drive both.
Sample Data and Fitted Values
Line: y = 2.2 + 0.6x. Residuals add up to zero.
| x | y | Fitted y | Residual |
|---|---|---|---|
| 1 | 2 | 2.80 | -0.80 |
| 2 | 4 | 3.40 | 0.60 |
| 3 | 5 | 4.00 | 1.00 |
| 4 | 4 | 4.60 | -0.60 |
| 5 | 5 | 5.20 | -0.20 |
Reading r and R²
- r near 1: a strong upward straight-line pattern.
- r near −1: a strong downward straight-line pattern.
- r near 0: little straight-line relationship.
- R² of 0.8: 80% of the variation in y is explained by the line.
Common Mistakes to Avoid
- Swapping x and y. x is the input or cause you control; y is the outcome.
- Predicting far outside the data. The line may not hold beyond your range of x.
- Fitting a line to a curve. Plot the data first to check it looks straight.
- Trusting a few points. More data gives a more reliable line.
- Ignoring outliers. One unusual point can tilt the line.
Frequently Asked Questions
Find Σx, Σy, Σxy and Σx², then use b = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²) and a = (Σy − bΣx) ÷ n.
How much y changes on average for each 1 unit increase in x.
The predicted y when x is 0, which may not make sense if 0 is outside your data.
It depends on the field. In physics, above 0.9 is common. In social sciences, much lower values can still be useful.
r shows strength and direction. R² is r squared and shows the share of variation explained.
At least 3, but more points give a more reliable result.
Yes. Copy two columns and paste them. Tabs between values work.
A straight line will fit poorly. Consider a different model, such as exponential or polynomial.
No. It always falls between −1 and 1.
A method that chooses the line with the smallest sum of squared vertical distances to the points.
No. It only shows that two variables move together.
Residuals show how far each point is from the line. Plotting them helps you spot a curve, outliers or a spread that grows with x, which suggest a straight line is not the best model.
Put the x value into the equation y = a + bx, or enter it in the prediction box.
References & Sources
| Source | Used For | Link |
|---|---|---|
| Wikipedia, Simple linear regression | Least squares formulas for slope and intercept. | https://en.wikipedia.org/wiki/Simple_linear_regression |
| NIST/SEMATECH e-Handbook, Linear least squares regression | How least squares fitting works. | https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd141.htm |