Linear Regression Calculator
Paste x and y data to get the least-squares line, r, R², slope and intercept p-values, residual plots and a prediction with confidence and prediction intervals.
The interactive tool loads here when JavaScript runs.
About this toolWhat is the Linear Regression Calculator?
Paste pairs of numbers and the Linear Regression Calculator fits the straight line that best predicts y from x by ordinary least squares. It returns the equation ŷ = a + bx, the correlation coefficient r, R² and adjusted R², the residual standard error, and a full coefficient table with standard errors, t statistics, p-values and confidence intervals for the slope and the intercept. The same numbers you would get from Excel's LINEST, R's lm() or a TI-84 LinReg(a+bx), with the working shown.
Below the summary you get a scatter plot with the fitted line and shaded confidence and prediction bands, a residual plot for checking the straight-line assumption, the regression ANOVA table, and a residual for every point with its standardized value. Type an x value to predict y and the tool gives both intervals people mix up: the confidence interval for the average y at that x and the wider prediction interval for one new observation. Data can be pasted as two spreadsheet columns, CSV pairs or two separate lists, and nothing leaves your browser.
- Equation, slope, intercept, r, R², adjusted R² and residual standard error
- Coefficient table with standard errors, t, p-values and confidence intervals
- Prediction at any x with both the confidence interval and the prediction interval
- Scatter plot with confidence and prediction bands, plus a residual plot
- Residual table and CSV download; verified against the NIST StRD Norris data
- Paste from a spreadsheet; your data stays in your browser
How to use the Linear Regression Calculator
- Paste your data. In "x, y pairs" mode put one pair per line, separated by a comma, tab or space; a header line such as "hours, score" is skipped. In "Two lists" mode paste the x values and the y values separately, in matching order.
- Pick the confidence level for the intervals (95% is the usual choice).
- Read the equation, slope, intercept, r and R² at the top. The slope card shows its standard error and the p-value for the test that the true slope is zero.
- Optionally type an x value in "Predict y at x" to get ŷ with a confidence interval for the mean and a prediction interval for a single new point. A warning appears if that x lies outside your data.
- Check the residual plot for curves or funnels, then copy the summary, share a link or download every residual as CSV.
Worked examples
Study hours and exam scores (the default data)
Input
x = 1 to 10 hours, y = 52, 55, 61, 60, 68, 70, 74, 79, 81, 88
Output
ŷ = 47.4667 + 3.87879x, r = 0.99189, R² = 0.98384, s = 1.5964, slope p = 1.88e-8
Each extra hour is worth about 3.9 points. With x̄ = 5.5 and ȳ = 68.8, Sxy = 320 and Sxx = 82.5, so b = 320 / 82.5 = 3.87879 and a = 68.8 - 3.87879 × 5.5 = 47.4667.
Predicting beyond the data
Input
Same data, predict at x = 12
Output
ŷ = 94.012; 95% CI for the mean 91.13 to 96.89; 95% prediction interval 89.34 to 98.69
The prediction interval is wider because it adds the scatter of a single student (s = 1.6) to the uncertainty in the line. x = 12 is outside the 1 to 10 range, so the tool flags it as extrapolation.
Anscombe's quartet, set I
Input
x = 10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5; y = 8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68
Output
ŷ = 3.00009 + 0.500091x, r = 0.81642, R² = 0.66654, slope p = 0.00217
Set II of the quartet is a smooth curve and set III a perfect line with one outlier, yet both give almost exactly these numbers. Only the plots tell them apart.
Background and insight
Least squares picks the line that makes the sum of squared vertical distances from the points to the line as small as possible. Adrien-Marie Legendre published the method in 1805 and Carl Friedrich Gauss, who said he had used it since 1795, connected it to normally distributed errors in 1809. The word regression came later from Francis Galton, who noticed in the 1880s that tall parents had children who were tall but closer to the average, a "regression towards mediocrity". The slope formula is b = Sxy / Sxx, where Sxy is the sum of (x - x̄)(y - ȳ) and Sxx the sum of (x - x̄)²; the intercept is a = ȳ - b·x̄, so the line always passes through the point of means.
r measures how tightly the points hug a straight line, from -1 to 1. R² is its square in simple regression and reads as the share of the variance in y that the line accounts for. Neither says the relationship is causal or even that a straight line is the right shape: Anscombe's 1973 quartet is four data sets with the same slope (0.500), intercept (3.00) and r (0.816) that look nothing alike when plotted. That is why the calculator always draws the scatter plot and the residuals.
The arithmetic centres the data before multiplying, which matters more than it sounds. The textbook shortcut Σxy - n·x̄·ȳ subtracts two large, nearly equal numbers when x and y sit far from zero, and in floating point that can wipe out the significant digits. The regression tests run the NIST Statistical Reference Dataset "Norris" (36 ozone monitor calibrations) and match its certified slope 1.00211681802045, intercept -0.262323073774029 and R² 0.999993745883712 to at least 10 significant figures.
Common use cases
Homework and lab reports
Get the line of best fit with r, R² and the slope test, and see Sxx, Sxy and each residual to check a hand calculation.
Calibration curves
Fit instrument readings against known standards, read the residual standard error and convert a new reading with a prediction interval.
Business forecasting
Relate sales to advertising spend or costs to volume, and see how wide the uncertainty is before quoting a forecast.
Checking a spreadsheet
Confirm what Excel's SLOPE, INTERCEPT, RSQ and LINEST return, with the standard errors LINEST hides in its second row.
Tips and limitations
- Always look at the scatter and residual plots. A curved residual pattern means a straight line is the wrong model even when R² is high.
- A small p-value for the slope says the slope is unlikely to be exactly zero, not that the relationship is strong or useful. With thousands of points a tiny slope can be "significant".
- Do not predict far outside the range of x you measured. The line is only supported where you have data, and the intervals widen as you move away from x̄.
- Swapping x and y gives a different line (unless r is ±1), because least squares minimises vertical distances. Put the variable you want to predict on the y side.
- One outlier can tilt the whole line. Points with standardized residuals beyond ±2 are highlighted; check whether they are data errors before deleting anything.
- This tool fits simple linear regression with one predictor. Curved fits and several predictors need multiple regression software.
Frequently asked questions
What is the formula for the least squares regression line?
The slope is b = Σ(x - x̄)(y - ȳ) / Σ(x - x̄)² and the intercept is a = ȳ - b·x̄, giving ŷ = a + bx. These values make the sum of squared vertical distances from the points to the line as small as possible, and the line always passes through (x̄, ȳ).
What is the difference between r and R²?
r is the correlation coefficient, between -1 and 1, and its sign gives the direction of the relationship. R² is r squared in simple regression, between 0 and 1, and is the share of the variation in y explained by the line. r = 0.9 means R² = 0.81, so 81% of the variance is accounted for.
What is a good R² value?
It depends on the field. Physical calibration curves often reach 0.99 or more, while models of human behaviour can be useful at 0.2 or 0.3. R² alone does not show whether a straight line is the right shape, so judge it together with the residual plot.
What is the difference between a confidence interval and a prediction interval?
The confidence interval covers the average y for all cases at a given x. The prediction interval covers a single new observation at that x, so it adds the scatter of individual points and is always wider. Use the prediction interval when you want to say where one new measurement will land.
What does the p-value for the slope mean?
It tests the null hypothesis that the true slope is zero, meaning y does not change linearly with x. It comes from t = b / SE(b) with n - 2 degrees of freedom. In simple regression it equals the p-value of the regression F test, since F = t².
How many data points do I need for linear regression?
The calculator needs at least 3 pairs, because two points fit a line exactly and leave no information about the error. For trustworthy intervals and p-values, aim for 20 or more, and more if the data are noisy.
What is the residual standard error?
It is s = √(SSE / (n - 2)), the typical vertical distance between a point and the line, in the units of y. Excel calls it the standard error of the estimate, STEYX. About 95% of points should lie within roughly 2s of the line if the errors are normal.
Does correlation mean causation?
No. A strong fit says x and y move together in this data. A third variable, reverse causation or coincidence can produce the same pattern. Causal claims need a controlled experiment or a careful study design.
Is there an in-depth guide to the Linear Regression Calculator?
Yes. “Linear Regression Explained: Slope, R² and Residuals” covers the background, how it works, common pitfalls and worked examples. Read the full guide.
People also search for
- line of best fit calculator with steps
- least squares regression line from data
- how to find the slope of a regression line
- prediction interval vs confidence interval regression
- r squared calculator from x and y
- standard error of the slope calculator
- residual plot for linear regression online
- excel linest equivalent online
Related Tools You May Like
Free, in-browser tools that pair well with this one.