Linear Regression Calculator
Fit a least-squares regression line to any paired X-Y data. Get the regression equation, slope, intercept, R², r, RMSE, residuals and outliers, with a full step-by-step solution and live scatter plot.
Enter your data
Separate numbers with commas, spaces, or new lines. The X list is the predictor (independent variable); the Y list is what you are trying to model.
Residuals should look like a structureless cloud around y = 0. A clear curve means the relationship isn't linear; a funnel means the variance grows with X (heteroscedasticity).
Step-by-Step Solution
Computed from the 10 pairs you entered using the least-squares method.
Step 1Tabulate paired values and running products
| i | xᵢ | yᵢ | xᵢ·yᵢ | xᵢ² |
|---|---|---|---|---|
| 1 | 1 | 52 | 52.00 | 1.00 |
| 2 | 2 | 60 | 120.00 | 4.00 |
| 3 | 3 | 65 | 195.00 | 9.00 |
| 4 | 4 | 70 | 280.00 | 16.00 |
| 5 | 5 | 73 | 365.00 | 25.00 |
| 6 | 6 | 78 | 468.00 | 36.00 |
| 7 | 7 | 82 | 574.00 | 49.00 |
| 8 | 8 | 85 | 680.00 | 64.00 |
| 9 | 9 | 90 | 810.00 | 81.00 |
| 10 | 10 | 94 | 940.00 | 100.00 |
| Σ | 55.00 | 749.00 | 4484.00 | 385.00 |
Step 2Compute the running sums
- n = 10
- Σx = 55.0000
- Σy = 749.0000
- Σxy = 4484.0000
- Σx² = 385.0000
- Σy² = 57727.0000
- x̄ = 5.5000
- ȳ = 74.9000
Step 3Compute the slope (m)
Step 4Compute the intercept (b)
Step 5Write the regression equation
Step 6Compute R² (goodness of fit)
About 99.0% of the variation in Y is explained by X under this linear model.
Step 7Compute RMSE
A typical prediction from this line will be off by about 1.28 units of Y.
Step 8Use the equation to predict
To predict Y for any new X value, substitute it into the fitted equation. For example, at X = x̄ = 5.50 the line predicts Y = ȳ = 74.90 - a sanity check that always holds for ordinary least squares with an intercept.
The Linear Regression Formulas
Ordinary least squares minimises - the sum of squared vertical distances between each point and the fitted line. Solving that minimisation gives closed-form expressions for the slope and intercept.
Slope
Equivalent to . Units of m are units of Y per unit of X.
Intercept
The predicted Y when X = 0. Only meaningful if X = 0 is inside or near the range of your data - extrapolating outside that range is risky.
R² (coefficient of determination)
Proportion of variance in Y explained by the model. Equal to r² for simple linear regression.
RMSE
Typical prediction error, in the original units of Y. Use alongside R² to judge fit quality.
Assumptions Behind Linear Regression
Least-squares regression always returns a slope and intercept - even when the model is the wrong shape for the data. The following assumptions decide whether those numbers can be trusted for inference and prediction.
Linearity
The true relationship between X and Y is a straight line. Check by plotting Y against X (the calculator does this for you) and looking for curvature.
Independence of errors
Each residual is independent of the others. Time-series data often violates this; use specialised models when it does.
Constant variance (homoscedasticity)
The spread of residuals is roughly the same across the range of X. A funnel shape in the residual plot means heteroscedasticity - consider transforming Y (log, square root) or fitting weighted least squares.
Normally distributed errors
Residuals are approximately normal. This matters for confidence intervals and p-values, less so for the point estimates of slope and intercept.
No influential outliers
A single extreme point with high leverage can swing the line dramatically. Examine flagged outliers and check whether they are recording errors or genuinely informative.
Worked Example: Five Students by Hand
Five students recorded hours studied (X) and test score (Y). Compute the regression line by hand.
| Student | X | Y | X·Y | X² |
|---|---|---|---|---|
| 1 | 1 | 52 | 52 | 1 |
| 2 | 2 | 60 | 120 | 4 |
| 3 | 3 | 68 | 204 | 9 |
| 4 | 4 | 73 | 292 | 16 |
| 5 | 5 | 82 | 410 | 25 |
| Σ | 15 | 335 | 1078 | 55 |
Each additional hour of study is associated with roughly 7.3 extra points. Paste 1, 2, 3, 4, 5 and 52, 60, 68, 73, 82 into the calculator above to reproduce the result.
Where Linear Regression is Used
Economics & forecasting
Demand curves, Phillips curves, and short-term price models start as linear regressions before fancier dynamics are added. They remain the baseline against which complex models are judged.
Real estate price modelling
Square footage, bedrooms, neighbourhood - each predictor enters as a coefficient in a multiple regression. Even single-predictor versions (price per sq ft) are useful sanity checks.
Lab calibration curves
Chemistry instruments are calibrated by fitting a line through known standards. The slope becomes the conversion factor; the R² confirms the linear range.
A/B testing - covariate adjustment
Regressing the outcome on pre-experiment covariates (CUPED, MLRATE) reduces variance and shrinks the confidence interval on the treatment effect.
Machine learning baseline
Before deep learning, before random forests, fit a linear regression. If a simple line explains most of the variance, the gain from complexity may not be worth it.
Education research
Predicting test performance from hours of instruction, attendance, or prior scores typically starts as a linear regression.
Common Mistakes to Avoid
- Extrapolating beyond the data. A regression fit on house sizes from 800 to 2,500 sq ft tells you nothing reliable about a 6,000 sq ft mansion. The line is only validated inside the range of X you trained on.
- Reading the intercept literally when X = 0 is unrealistic. For a "years of experience vs salary" regression, the intercept is just an arithmetic anchor - not a meaningful starting salary, because the data doesn't include negative experience.
- Mistaking correlation for causation. Regression gives you a slope, not a causal effect. A significant slope can still come from confounders or reverse causation.
- Trusting R² alone to judge fit. R² can be high for a fundamentally wrong model. Always look at the residual plot for curvature, funnels, or clusters.
- Letting one point dominate. A single high-leverage point can change slope, intercept and R² substantially. The calculator flags large standardised residuals to make this visible.
- Forgetting that slope has units. A slope of 0.4 means nothing without knowing it is "0.4 dollars per additional minute of ad time", not 0.4 of something abstract.
Frequently Asked Questions
What is linear regression?
How do you calculate the slope and intercept by hand?
What does the R² value mean?
What is the difference between r and R²?
What is RMSE and why is it different from R²?
How is the line of best fit calculated?
What is a residual?
How are outliers detected in regression?
When should I not use linear regression?
How many data points do I need for linear regression?
Does the regression line always go through the mean point?
When Ordinary Least Squares Is Not the Right Tool
The calculator above fits ordinary least squares (OLS), which assumes a single linear predictor, roughly constant error variance, and an unbounded continuous response. When those assumptions fail, the right move is not to torture the OLS output. It is to pick a different model.
Polynomial regression
Use when the scatter plot shows a clear curve. Fit y on x, x², (sometimes x³). Stop at the lowest degree that fixes the residual curve - higher degrees overfit fast.
Ridge & Lasso regression
Use when you have many correlated predictors and a small n. Both shrink coefficients; Lasso also drives some to zero, doubling as feature selection.
Logistic regression
Use when Y is binary (clicked or not, churned or not). OLS on a 0/1 outcome produces probabilities outside [0, 1] and badly biased standard errors.
Poisson / negative binomial
Use when Y is a non-negative integer count (number of bugs, customer arrivals). The variance grows with the mean, breaking the constant-variance assumption.
Robust regression
Use when one or two influential points dominate the slope. Methods like Huber or RANSAC down-weight outliers instead of letting them swing the line.
Weighted least squares
Use when the residuals form a funnel (heteroscedasticity). Each point is weighted by the inverse of its estimated variance, restoring valid inference.
References and Further Reading
- Galton, F. (1886) and Gauss / Legendre on the origin of least-squares - Ordinary least squares (Wikipedia).
- Anscombe's quartet shows why a high R² alone is not enough - Anscombe's quartet.
- NIST/SEMATECH e-Handbook on linear models and residual analysis - NIST handbook: linear regression.
- Deng, A. et al. (2013). Improving the sensitivity of online controlled experiments by utilizing pre-experiment data (CUPED) - practical use of regression in A/B testing.
- For an intuition primer on the difference between correlation and a regression slope, see Correlation vs causation on this site.