Regression & correlation
Linear Regression Calculator
Fit a straight line to paired data, and get everything needed to judge whether the line means anything: standard errors, p-values, R² and a residual plot.
Have a spreadsheet? Drop a CSV here
Or . CSV, TSV or plain text. You pick which column is which after it loads — the names in your file do not have to match ours. The file is read in your browser and never uploaded.
How to read this result
The slope is the finding. It says how much y changes for a one-unit rise in x — in the data's own units, which makes it the number to quote. Its p-value tests whether that change is distinguishable from zero, and its confidence interval says how precisely you have pinned it down.
R² is the share of variance explained. It is a measure of fit, not of correctness: a curved relationship can produce an impressive R² from a straight line, and a low R² in a noisy field can still be a real and useful finding.
Read the residual plot before believing anything above it. Residuals should look like structureless noise around zero. A curve means the relationship is not linear. A funnel means the spread changes with x. Either one invalidates the standard errors and therefore the p-value.
Association, not causation. A significant slope is equally consistent with x causing y, y causing x, and a third variable causing both. Only the design of the study distinguishes them.
Do not extrapolate. The fit describes the range of x you observed. Beyond it you are assuming the relationship continues, which the data cannot support — the calculator flags a prediction that leaves the observed range.
The formula
- the slope — the change in y per unit of x
- the intercept — the fitted y when x is zero
- the two means
These are the values that minimise the sum of squared vertical distances from the points to the line, which is what “least squares” means.
Is the slope real?
Compared against the t distribution on n − 2 degrees of freedom. Two are used up estimating the slope and the intercept.
Worked example
Advertising spend against sales
Ten months of advertising spend, in thousands, against sales in the same units. Sales clearly rise with spend — but by how much per thousand, and how confident can we be in that number?
Spend: 1 … 10 · Sales: 2.1, 4.3, 6.2, 8.9, 10.1, 12.8, 14.2, 16.7, 18.1, 20.4
- Take both means.x̄ = 5.5, ȳ = 11.38
- Sum the products of the deviations, and the squared x deviations.Σ(x−x̄)(y−ȳ) = 166.2 Σ(x−x̄)² = 82.5
- Divide for the slope.b = 166.2 / 82.5 = 2.0145
- The intercept follows from the means.a = 11.38 − 2.0145 × 5.5 = 0.30
- The residual standard error, on n − 2 = 8 degrees of freedom.s = 0.3238
- The standard error of the slope, and t.SE_b = 0.3238 / √82.5 = 0.03564 → t = 2.0145 / 0.03564 = 56.5
- R²: the share of variance the line accounts for.R² = 0.9975
Every extra thousand spent is associated with about 2.01 more in sales, and the interval says between 1.93 and 2.10. Whether spending causes the sales is a question this arithmetic cannot answer — the months with high spend may simply be the months with high demand.
Checked against R's lm(y ~ x) and summary(lm).
The calculator above is loaded with these numbers by the Load the worked example button.
Assumptions, and when to use something else
The four classic assumptions, in the order they matter:
- Linearity. The relationship really is a straight line over the range observed. The residual plot shows this better than R² does.
- Independent errors. One observation's deviation says nothing about the next one's. Time series routinely violate this.
- Constant variance. The spread of the residuals does not change with x. A funnel shape in the residual plot is the symptom, and it makes the standard errors wrong.
- Roughly normal residuals, which matters mainly for small samples and mainly for the p-values, not for the slope estimate itself.
Also worth checking: influential points. A single observation far out along x can pivot the whole line, and it will not look like an outlier in either variable on its own.
- You have more than one predictorMultiple regressionFits several predictors at once, with each coefficient adjusted for the others.
- The outcome is yes/noLogistic regressionLinear regression on a binary outcome produces impossible predictions; logistic regression is the right model.
- You only want the strength of the relationshipCorrelation coefficientOne number, symmetric in x and y, with no equation attached.
- You only need the line through two pointsSlope and intercept calculatorGeometry rather than statistics — exact, with no uncertainty.
Questions people ask
What does the slope mean?
The average change in y associated with a one-unit increase in x. If x is advertising in thousands and y is sales in thousands, a slope of 2.01 means each extra thousand spent goes with about 2.01 thousand more in sales — in this data, over this range, without any claim about cause.
What is a good R-squared?
It depends entirely on the field. Physics experiments routinely exceed 0.99; individual human behaviour rarely passes 0.3. R² measures how much variance the model explains, not whether the model is right — check the residual plot for that.
Can I use regression to predict beyond my data?
You can compute it, and you should not trust it. The fit describes the range of x you observed; outside it you are assuming the relationship continues unchanged. The calculator warns when a prediction leaves the observed range.
What is the difference between correlation and regression?
Correlation is one symmetric number describing strength. Regression is an asymmetric model that predicts y from x and gives you an equation with units. For a single predictor R² is exactly r², so they are closely related — but only regression gives you something to use.
How many data points do I need?
Technically three; realistically enough to see the pattern. Ten to fifteen is workable for a single predictor, thirty is comfortable, and below about eight the confidence interval for the slope is so wide as to be uninformative.