Linear Regression Calculator
Fit a least-squares straight line to your x and y data, with every step shown.
Your data
🔒 Your numbers stay in your browser. Nothing is uploaded or stored.
Result
Fit summary
Worked example: the default data, fitted point by point
The same five points you see in the calculator, with the fitted value and the residual (actual minus fitted) for each one. Residuals of a least-squares fit always sum to zero.
| x | Actual y | Fitted y | Residual |
|---|---|---|---|
| 1 | 2 | 2.8 | -0.8 |
| 2 | 4 | 3.4 | 0.6 |
| 3 | 5 | 4.0 | 1.0 |
| 4 | 4 | 4.6 | -0.6 |
| 5 | 5 | 5.2 | -0.2 |
Predictions from the same line: x = 4 gives 4.6, x = 5 gives 5.2, x = 6 gives 5.8, and x = 7 gives 6.4. The step from one x to the next is always the slope, 0.6.
Related tools
Correlation Coefficient Calculator
Find r for the same data, and check it against R-squared here.
→ 🔍P-Value Calculator
Test whether a result like your slope is statistically significant.
→ 📊Statistics Calculator
Mean, median, variance, and standard deviation for any list.
→ 📖Linear Regression Equation Explained: Slope, Intercept, and Line of Best Fit
The regression line is y = a + bx, and a and b come from the data by least squares: the line that makes the…
→📖Simple vs Multiple Linear Regression: What’s the Difference?
Simple regression has one predictor and one slope. Multiple regression has several predictors and one slope…
→- Least squares minimises the sum of squared residuals — the line is the best compromise, not a perfect description.
- Slope is the rate, intercept is the value at x = 0, R-squared is the share of variation explained, and r carries the sign of the slope.
- Predictions inside the data range are interpolations; outside, they are extrapolations, and reliability falls with distance.
- How it works in 3 steps
- The formula, plainly
- Deep dive
- The line that fits best, and what best means
- Reading slope, intercept, R-squared and r
- A perfect fit and a flat line
- Predicting at a new x
- What the line cannot tell you
- Entering data so the fit means something
- Simple vs multiple regression at a glance
- Regression in Excel: the LINEST habit
- Frequently asked questions
How it works in 3 steps
Enter your pairs
Type your x list and your y list, with the same number of values in each. Use commas or one value per line.
Read the line
The calculator finds the slope and intercept that make the squared residuals as small as possible, and reports R-squared with them.
Predict
Enter any x to get the fitted y on the same line. Predictions inside your data range are safer than ones far outside it.
The formula, plainly
Worked check on the default data. The mean of x is 3 and the mean of y is 4. The numerator Σ((x − x̄)(y − ȳ)) is (−2 × −2) + (−1 × 0) + (0 × 1) + (1 × 0) + (2 × 1) = 6. The denominator Σ(x − x̄)² is 4 + 1 + 0 + 1 + 4 = 10. So the slope is 6 ÷ 10 = 0.6, the intercept is 4 − 0.6 × 3 = 2.2, and the line is y = 2.2 + 0.6x. At x = 6, the prediction is 2.2 + 3.6 = 5.8.
R-squared check. R-squared is the square of the correlation r. Here r = 6 ÷ √(10 × 6) = 0.7746, so R-squared = 0.60. That matches the calculator, and it is the same data the correlation calculator uses for its 0.7746 result.
Deep dive
For any candidate line, each point has a residual: the gap between its actual y and the fitted y. Ordinary least squares picks the one line that makes the sum of the squared residuals as small as possible. Squaring matters, because it stops a positive gap from cancelling a negative one, and it punishes large misses more than small ones. The formulas above are the exact solution to that problem, not a guess or an approximation, so there is no iteration and no starting value involved.
R-squared is the share of the variation in y that the line accounts for, on a scale from 0 to 1. A value of 0.60 means 60 percent is explained by the line and 40 percent remains as scatter. It does not prove that x causes y, it does not say the straight line is the right shape, and it can look impressive on a tiny data set purely by chance. Always look at the points themselves, not only at the single number.
Fit a least-squares straight line to your x and y data. Get the slope, intercept, R-squared, and a prediction at any x value, with every step shown.
The line that fits best, and what best means
A linear regression fits a straight line to paired data, and “best” has a precise meaning: the line that minimises the sum of the squared vertical distances between the data points and the line. Those distances are the residuals, the gaps between what the line predicts and what was actually observed, and squaring them punishes large misses more than small ones, so the best line is the one that keeps the total squared miss as small as possible. That criterion, least squares, is what this calculator implements.
The default data shows the result. With x values 1, 2, 3, 4, 5 and y values 2, 4, 5, 4, 5, the fitted line is y = 2.2 + 0.6x. The slope is 0.6, the intercept is 2.2, R-squared is 0.60, r is 0.7746, n is 5, mean x is 3 and mean y is 4. The line passes through the mean point (3, 4), as least-squares lines always do, and the prediction at x = 6 is 5.8, from 2.2 + 0.6 × 6. Every figure on the result follows from the same least-squares computation, so the line, the prediction and the fit statistics hang together as one result.
Best does not mean perfect. The default points do not lie on the line: at x = 3 the line predicts 4.0 while the observed y is 5, and at x = 4 it predicts 4.6 while the observed y is 4. Those gaps are the residuals, and their squares sum to the quantity the line minimises. A different line could reduce one residual only by increasing others, and the least-squares line is the balance point. Understanding that the line is a compromise, not a description of any single point, prevents the most common misreading of regression output.
The calculator shows every intermediate figure, means included, because the slope and intercept are computed from the means and the deviations around them. Mean x of 3 and mean y of 4 anchor the line, and the slope measures how y’s deviations track x’s deviations. When you enter your own data, check the means first: if they look wrong, a data-entry error is more likely than a computation error, because the rest of the output inherits whatever the means contain.
Reading slope, intercept, R-squared and r
The slope is the line’s rate of change: how much y moves when x moves by one. A slope of 0.6, as in the defaults, means each additional unit of x is associated with 0.6 more units of y, on average across the data. Slope is not a guarantee for any single step, and it is not a causal claim; it is the average association the line encodes, and individual points deviate from it by their residuals.
The intercept is the line’s value at x = 0: the predicted y when x is zero. The default intercept is 2.2, so the line predicts y = 2.2 at x = 0. Intercepts are often extrapolations, because real data rarely include x = 0, and an intercept can be meaningless as a description even when it is exact as arithmetic. If x = 0 lies far outside the data range, read the intercept as the line’s anchor, not as a prediction about a real case.
R-squared measures how much of y’s variation the line accounts for. The default R-squared of 0.60 means the line explains 60% of the variation in y around its mean, leaving 40% in the residuals. R-squared of 1.00 would mean the line passes through every point exactly, and R-squared of 0.00 would mean the line explains nothing beyond the mean. It is a proportion, not a grade: 0.60 can be excellent for noisy human data and poor for precise physical data, and the judgement depends on the field, not on the number alone.
The correlation coefficient r measures the strength and direction of the linear association, and its sign always follows the slope. The default r is 0.7746, positive because the slope is positive, and its square is R-squared: 0.7746 squared is 0.60, matching the reported R-squared. A positive r near 1 means points rise tightly with x, a negative r near −1 means they fall tightly, and an r near 0 means no linear pattern. Read r for direction and tightness, R-squared for proportion explained, and slope for the rate, and the three together describe the fit completely.
A perfect fit and a flat line
Two extreme cases show what the statistics look like at the boundaries. A perfect fit has every point on the line: with x values 1, 2, 3, 4 and y values 2, 4, 6, 8, the fitted line is y = 0 + 2x, the slope is 2, the intercept is 0, R-squared is 1.00, r is 1.0000, n is 4, mean x is 2.5 and mean y is 5, and the prediction at x = 6 is 12. There are no residuals, so the line explains everything, and R-squared and r both attain their maxima. Perfect fits are rare with real data, and a perfect fit on noisy data should prompt suspicion about the data rather than celebration about the model.
A flat line is the opposite extreme: with x values 1, 2, 3 and y values 5, 5, 5, the fitted line is y = 5 + 0x, the slope is 0, the intercept is 5, R-squared is 0.00, r is 0.0000, n is 3, mean x is 2 and mean y is 5, and the prediction at any x is 5. The line is the mean itself, because x carries no information about y when y never varies. R-squared of 0.00 here does not mean the calculation failed; it means the best line is no better than guessing the mean, which is the correct conclusion when the data show no pattern.
Most real data sit between these extremes, like the defaults at R-squared 0.60. The extremes are useful as checks: if your data look perfectly linear but the page reports R-squared far from 1, an entry error is likely, such as a transposed pair or a mistyped value. If your data look patternless but the page reports a high R-squared, check for a dominant outlier pulling the line, or for too few points, where a line can fit closely by chance. The extremes calibrate what to expect, and deviations from expectation are diagnostics.
Sample size matters to both extremes. A perfect fit on 3 points is less impressive than on 30, because fewer points constrain the line less, and a flat line on 3 identical values says little about a population. The page reports n with every result, and n should temper every interpretation: small n means wide uncertainty, whatever R-squared says, and the line should be treated as a summary of the sample, not as an established relationship.
Predicting at a new x
Prediction is the line’s most-used output: enter a new x, and the page returns the line’s y at that x. For the defaults, predicting at x = 6 gives 5.8, from 2.2 + 0.6 × 6, and predicting at x = 10 gives 8.2, from 2.2 + 0.6 × 10. The arithmetic is the same in both cases, but the reliability is not: x = 6 sits just beyond the data range of 1 to 5, while x = 10 sits twice as far out, and the further a prediction strays from the observed range, the more it assumes the linear pattern continues where no data confirm it.
Interpolation, predicting inside the observed range, is the safest use. The line was fitted to points across that range, so its values between them are supported by neighbouring data, even though no point may sit exactly on the line. Extrapolation, predicting outside the range, assumes the relationship holds where it was never observed, and that assumption fails whenever the true relationship curves, saturates, or changes regime beyond the data. A prediction at x = 10 from data spanning 1 to 5 is an extrapolation of twice the range, and should be reported with that caveat, not as a fact.
The prediction also inherits the fit’s uncertainty. With R-squared 0.60, individual points deviate substantially from the line, so a prediction of 5.8 at x = 6 describes the line’s average expectation, not the value any single new observation will take. A new observation at x = 6 could plausibly land well above or below 5.8, within the scatter the residuals display. Reporting a prediction without acknowledging that scatter overstates what regression can do, and the residuals, visible in the gap between points and line, are the honest measure of it.
Use predictions comparatively when extrapolation is unavoidable. Predict at several x values, note how the predictions move with the slope, and compare them with domain knowledge about plausible limits: a line that predicts impossible values, such as negative counts or percentages above 100, at the x you care about is signalling that the linear model does not extend that far. The page will compute the number regardless; judging whether the number means anything is the user’s part of the work.
Residuals: the story the line leaves out
Residuals are the gaps between observed values and the line’s predictions, and they carry information the summary statistics hide. For the default data, the line is y = 2.2 + 0.6x, and the residuals work out point by point. At x = 1, the line predicts 2.8 while the observed y is 2, a residual of −0.8. At x = 2, it predicts 3.4 against an observed 4, a residual of +0.6. At x = 3, it predicts 4.0 against an observed 5, a residual of +1.0. At x = 4, it predicts 4.6 against an observed 4, a residual of −0.6. At x = 5, it predicts 5.2 against an observed 5, a residual of −0.2. The residuals sum to zero, as least-squares residuals always do, which is a useful check that the line was fitted correctly.
The pattern of residuals matters more than any single one. Here the signs alternate, negative, positive, positive, negative, negative, with no obvious curve, which is consistent with random scatter around a genuinely linear pattern. A curved pattern, such as residuals positive at the ends and negative in the middle, would suggest the true relationship bends and the line is the wrong model. A widening pattern, with larger residuals at larger x, would suggest the scatter grows with x and the line’s predictions are less reliable at the top end. Plotting residuals, or simply listing them in x order as above, reveals patterns that R-squared alone cannot show.
Residual size sets the scale for prediction uncertainty. The default residuals range from −0.8 to +1.0, so a new observation at a given x might plausibly land about that far from the line’s prediction, even when the line itself is well estimated. A prediction of 5.8 at x = 6 should therefore be read as “around 5.8, within roughly a unit either way for an individual observation”, not as “exactly 5.8”. The distinction between the line’s average expectation and an individual’s likely value is the distinction between a confident trend and an uncertain instance, and residuals quantify it.
Large residuals deserve individual attention, because they may be outliers, errors, or genuine extremes. The largest default residual is +1.0 at x = 3, where the observed 5 exceeds the predicted 4.0 by a full unit. That point pulls the line upward more than the others, and refitting without it would change the slope and intercept noticeably. Whether to keep it depends on whether it is a valid observation: a genuine data point stays, whatever its pull, while a measurement error should be corrected or removed with documentation. The decision belongs to the analyst, but the residual is what flags the question.
Use residuals as the diagnostic habit for every fit. After reading slope, intercept, R-squared and r, list the residuals in x order, check their sum is near zero, look for curves or widening, note the largest ones, and ask whether any point needs investigation. A fit whose residuals look random is a fit worth trusting within its range; a fit whose residuals show structure is a fit asking for a better model. The page reports the summaries; the residuals, derivable from the line and the data as shown here, complete the picture.
What the line cannot tell you
A regression line describes association, not causation. A slope of 0.6 says y tends to rise with x in these data; it does not say x causes y, and it does not rule out a third factor driving both, or y driving x, or coincidence. Causal claims need design, experiment, or theory beyond the fit, and no R-squared, however high, supplies them. The perfect-fit case makes the point sharply: y = 0 + 2x fits its four points exactly, but exact fit alone says nothing about why y doubles with x, and a causal story invented from the fit would be speculation.
Outliers deserve special attention, because least squares squares the residuals and therefore lets a single distant point pull the line disproportionately. One point far from the rest can change the slope substantially, inflate or deflate R-squared, and shift predictions across the range. When a fit looks surprising, plot the data or inspect the pairs for a point that does not belong with the rest, and consider whether it reflects a measurement error, a different regime, or a genuine extreme. The page fits whatever you enter, including errors, so data inspection belongs before interpretation.
Linearity itself is an assumption, not a finding. The page fits a straight line whether or not the true relationship is straight, and a curved relationship fitted with a line will show a middling R-squared and a pattern in the residuals: positive at the ends and negative in the middle, or the reverse, rather than random scatter. If the residuals show a curve, the line is the wrong model, and transforming the data or fitting a curve is the remedy, not a more emphatic reading of the line’s statistics. The page’s model is linear by design, and recognising when linearity fails is part of using it well.
Finally, the line summarises the sample, not the population, and small samples summarise unreliably. With n = 5, as in the defaults, the slope and R-squared carry wide uncertainty, and a different sample of 5 from the same source could give noticeably different figures. Treat small-sample fits as exploratory: useful for spotting a pattern worth investigating with more data, but not as established results. The page reports n prominently for this reason, and n should be the first qualifier on any claim drawn from the fit.
Entering data so the fit means something
Enter paired values, one x list and one y list, with the same number of entries in each, so that every x has its y partner and the pairs line up in order. The first x pairs with the first y, the second with the second, and so on, and the pairing is what the fit analyses: shuffling one list without the other destroys the pairs and produces a fit for a different dataset. Check the pairing before reading any result, because a pairing error is invisible in the summary statistics and visible only in the data itself.
Use numbers only, in consistent units, and watch for the entry errors that most distort fits: a decimal point slipped by one place, a value entered twice, a missing value that shifts all later pairs by one position. The means are a quick check: mean x of 3 and mean y of 4 for the defaults are easy to verify by eye, and means that look wrong point to entry errors before any deeper diagnosis is needed. A fit built on mistyped data is exact arithmetic on wrong numbers, and no statistic computed from it can warn you.
Include enough points for the question you are asking. Two points always fit a line exactly, but the fit says nothing, because any two points define a line. Three points constrain slightly more, five points as in the defaults begin to show scatter, and larger samples show patterns more reliably. If the question is whether a linear pattern exists at all, gather more data before fitting; if the question is summarising an existing dataset, fit what you have and report n alongside every figure, so readers can judge the summary’s weight.
Keep the prediction x honest as well. Enter the x you actually want predicted, check that it sits where you think it sits relative to the data range, and note whether the prediction is interpolation or extrapolation. A prediction at x = 6 from data spanning 1 to 5 is a small step beyond the range; a prediction at x = 10 is a leap. The page computes both identically, and the distinction, with its implications for reliability, is yours to keep.
Simple vs multiple regression at a glance
Simple regression has one predictor and one slope: y = a + bx. Multiple regression has several predictors, one slope each, fitted together: y = a + b1x1 + b2x2, and so on. The fitting method is the same in both cases, least squares, and the interpretation of R-squared is the same, the share of variation explained. What changes is the meaning of the slopes.
A multiple slope is the effect of one predictor holding the others fixed. That holding-fixed is the whole point, and it is what simple regression cannot do. Adding predictors always raises R-squared, so multiple models are judged by adjusted R-squared, which penalises extra predictors.
Regression in Excel: the LINEST habit
Excel fits the same least-squares line as the calculator on this page, through the LINEST function and the Data Analysis regression tool. LINEST takes the y range and the x range and returns the slope and intercept, with the option of extra statistics including R-squared and the standard errors. The Data Analysis tool adds the full output: the coefficients, their significance, the residuals, and the analysis of variance table.
The habit that matters is checking the output against the definitions: the slope is the rate, the intercept is the fitted y at x = 0, and the R-squared is the share of variation explained. A line fitted in Excel and a line fitted here should agree to the rounding, because the method is the same, and a disagreement means the ranges were selected differently, usually by including a header row or a blank cell. Keep the ranges clean, one column each, with the same number of values, and the two fits will match.
Frequently asked questions
What is a linear regression calculator?
It fits a straight line to paired x and y data using least squares, and reports the slope, intercept, R-squared, and predictions from that line.
What does the slope mean?
The slope is how much the fitted y changes when x rises by one unit. A slope of 0.6 means each extra unit of x adds 0.6 to the predicted y.
What does the intercept mean?
The intercept is the fitted y when x equals zero. It is only meaningful when x equal to zero makes sense for your data.
What is a good R-squared value?
There is no universal good value. In noisy real world data, 0.60 can be useful, while in a controlled experiment the same value would look weak. Judge it against your field and your sample size.
Why does my slope show as undefined?
When every x value is the same, there is no horizontal spread to define a slope, so the calculator reports that the slope is undefined instead of inventing a number.
Can I trust a prediction outside my data range?
Only with caution. The line is fitted to your observed range, and the true relationship may bend or flatten beyond it. Predictions far outside the range are extrapolation, not measurement.
What does an R-squared of 0.60 mean?
It means the line accounts for 60% of the variation in y around its mean, leaving 40% in the residuals. It is a proportion, not a grade: 0.60 can be excellent for noisy data and poor for precise data, and the judgement depends on the field, not on the number alone.
Why is r positive here?
Because r always carries the sign of the slope. The default slope is 0.6, positive, so r is positive at 0.7746, and its square is R-squared, 0.60. A negative slope would give a negative r of the same magnitude pattern.
Can I predict far beyond my data?
Mathematically yes, reliably no. The line extends to any x, but a prediction far outside the observed range assumes the linear pattern continues where no data confirm it. Predicting at x = 10 from data spanning 1 to 5 is a leap, and should be reported as an extrapolation with that caveat.
What if my x and y lists have different lengths?
Then the pairs do not line up, and the fit cannot mean what you intend. Enter the same number of x and y values, in the same order, so that every x has its y partner, and check the pairing before reading any result.
How do you find the regression equation from data?
Compute the mean of x and the mean of y, then the slope as the sum of the products of the x-deviations and y-deviations divided by the sum of the squared x-deviations, and the intercept as the mean of y minus the slope times the mean of x. The line is y = a + bx, and it always passes through the point of the two means.
What is the difference between correlation and regression?
Correlation measures the strength and direction of a linear relationship, as a single number r between -1 and 1. Regression fits a line and gives a slope and an intercept, which predict y from x. R-squared is the square of r, and it is the share of the variation in y that the line explains. Correlation describes; regression predicts.
When should I not use linear regression?
When the relationship is not straight, when the x values have no variation, when the data are too few for the predictors, or when the question is causal and the data are only observational. A pattern in the residuals, a slope that changes greatly when one point is removed, or an extrapolation far outside the data range are all warnings that the line should not be trusted.
What does a negative slope mean?
A negative slope means the fitted y falls as x rises: each extra unit of x subtracts the slope from the predicted y. The correlation r is negative too, and R-squared is unchanged, because it squares r. A negative slope is not a bad fit; it is a falling relationship, and it is read the same way as a rising one, with the sign reversed.
More calculators
Average Calculator
Means for any list of numbers.
%Percentage Calculator
Shares and changes, like R-squared as a percent.
🎯Sample Size Calculator
How many points a study needs before you fit a line.
🎲Binomial Distribution Calculator
Probabilities for repeated yes or no trials.
ΣSummation Calculator
Sums like the Σ terms inside the slope formula.