This regression calculator fits a straight line to a set of (x, y) points by least squares. It returns the intercept, the slope and R², so you can see the equation of the line and how well it fits.
Regression Calculator
Simple linear regression (y = a + bx) and the correlation coefficient for a list of x, y data points.
Starting values: the calculator opens with the points (1, 2), (2, 3), (3, 5), (4, 4) and (5, 6). It reports an intercept of 1.3, a slope of 0.9, R² = 0.81 and n = 5, so the line is y = 1.3 + 0.9x.
What it calculates
The tool performs simple linear regression: one predictor x and one response y. It reports the intercept, the slope, R² and the number of points. It computes the correlation coefficient r internally but displays R² only. For a straight-line fit, R² is simply r squared, so you can recover the size of r as the square root of R²; the sign of r matches the sign of the slope. You need at least two points, and the x values cannot all be identical, because a vertical set of points has no defined slope.
The formulas
Slope (b) = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)²
Intercept (a) = ȳ − b × x̄
Line: y = a + b x
Where:
- x̄ and ȳ = the means of the x values and the y values
- b = the slope, the change in y for each one-unit increase in x
- a = the intercept, the predicted y when x is 0
- R² = the share of the variation in y that the line accounts for, from 0 to 1
An equivalent form of the slope is r × (sy ÷ sx), the correlation multiplied by the ratio of the standard deviations. Least squares chooses the line that makes the sum of squared vertical distances from the points to the line as small as possible.
Worked example: the default data
- x values: 1, 2, 3, 4, 5, so x̄ = 3. y values: 2, 3, 5, 4, 6, so ȳ = 4.
- Deviations in x: −2, −1, 0, 1, 2. Deviations in y: −2, −1, 1, 0, 2.
- Products: 4, 1, 0, 0, 4, which sum to 9. Squared x deviations: 4, 1, 0, 1, 4, which sum to 10.
- Slope = 9 ÷ 10 = 0.9.
- Intercept = 4 − 0.9 × 3 = 1.3.
The line is y = 1.3 + 0.9x. To predict y at x = 6, compute 1.3 + 0.9 × 6 = 6.7.
Checking R²
Squared y deviations: 4, 1, 1, 0, 4, which sum to 10. The correlation is r = 9 ÷ √(10 × 10) = 0.9, so R² = 0.81. About 81 percent of the variation in y is explained by the straight-line relationship with x.
A second example with a negative slope
Take the points (1, 8), (2, 6), (3, 5), (4, 3). Here x̄ = 2.5 and ȳ = 5.5. The x deviations are −1.5, −0.5, 0.5, 1.5, and the y deviations are 2.5, 0.5, −0.5, −2.5. The products sum to −3.75 − 0.25 − 0.25 − 3.75 = −8. The squared x deviations sum to 5. The slope is −8 ÷ 5 = −1.6 and the intercept is 5.5 − (−1.6)(2.5) = 9.5. A negative slope means y falls as x rises.
Using the result well
- Do not predict far outside your data. The line describes the range you entered, not beyond it.
- A high R² does not prove that x causes y. It only shows a strong linear association.
- Look at the points. A curved pattern can give a decent R² and still be poorly described by a straight line.
- Watch for outliers. Least squares gives extra weight to points far from the line, so one unusual point can tilt the whole fit. Robust methods such as Theil–Sen exist for that situation but are not part of this tool.
- The method treats x as measured without error and puts all the uncertainty in y.
Entering points
List each point as an x value and a y value in matching order. A different count of x and y values, or non-numeric text, will give an error. Keep the units consistent, and note that swapping x and y changes the line, because the fit minimises vertical, not horizontal, distances. To explore the spread of one variable on its own, use the statistics calculator.
Residuals: seeing how the line misses each point
A residual is the actual y minus the y the line predicts. For the default data the line predicts 2.2, 3.1, 4.0, 4.9 and 5.8 at x = 1 to 5. The residuals are therefore −0.2, −0.1, 1.0, −0.9 and 0.2. They add up to zero, which is always true for a least-squares line that has an intercept.
Square them and add: 0.04 + 0.01 + 1.00 + 0.81 + 0.04 = 1.90. The total variation in y, the sum of squared deviations from its mean, is 10. R² is one minus the unexplained share: 1 − 1.9 ÷ 10 = 0.81. That is the same figure the calculator shows, and it makes clear what R² means in practice: the line removes 81 percent of the squared variation.
What to do if R² is low
A low R² means a straight line explains little of the variation. Before drawing conclusions, check whether the pattern is curved, whether an outlier is pulling the fit, or whether a second variable is involved. A low value is information, not an error in the calculation.
Frequently asked questions
What do the slope and intercept mean?
The slope is the predicted change in y for each one-unit increase in x. The intercept is the predicted y when x is 0, which may or may not be meaningful for your data.
Why is only R² shown, not r?
The calculator displays R². For a straight-line fit r is the square root of R², with the same sign as the slope.
How many points do I need?
At least two, and the x values must not all be the same. Two points define the line exactly, so R² is only informative with more.
Does a high R² mean x causes y?
No. R² measures how well a line fits the points. It does not show that one variable causes the other.
