Linear Regression Calculator
Fit the best straight line through the relationship between two data sets.
Enter your values and press Calculate to see the result and the working.
What does this calculator do?
Finds the straight line that best describes the relationship between two variables, giving its slope, intercept and how well it fits, with a chart showing the points and the line.
When would you use it?
When studying a relationship such as ad spend against sales, or temperature against consumption, or extracting a trend from time-series data.
How the formula works
The method is least squares: find the line that minimises the sum of the squared vertical distances between the points and the line. Squaring stops positive and negative errors cancelling, and gives large deviations more weight.
The slope tells you how much y changes for each unit increase in x — the substance of what the line says. The intercept is y when x is zero, which may have no real meaning if x = 0 lies outside your data.
R² runs from zero to one and tells you how much of y's variation the line explains: 0.85 means the line accounts for 85% of the variation, with the rest down to other factors or noise.
What does the result mean?
Read the slope as a rate of change and R² as confidence in the line — but two limits matter.
First, correlation is not causation. Two variables can track each other closely because a third drives both.
Second, the line is only valid within the range of the data it was fitted to. Extrapolating beyond it assumes the relationship stays linear, which is rarely safe.
Common mistakes
Treating a high R² as evidence of causation is the most dangerous interpretive error. Another is fitting a straight line to an obviously curved relationship — the maths will succeed while the model is wrong.
Worked example
An upward relationship
Five points to fit a trend line through.
Calculation method
Ordinary least-squares regression using SciPy.
Last reviewed: