Linear Regression Calculator: Formula & How to Use
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
This linear regression calculator finds the line of best fit (y = a + bx) for a set of paired data points you enter. You input your x and y values, and the calculator returns the slope (b), intercept (a), correlation coefficient (r), and R-squared (r²) so you can see how strongly the two variables relate. It also shows the equation of the fitted line, which you can use to predict y from a new x value.
Formula
The least-squares regression line is written as:
y = a + b·x
where:
- y = dependent variable (what you are predicting)
- x = independent variable (what you measure or control)
- b = slope of the line, calculated as b = [n·Σ(xy) − Σx·Σy] / [n·Σ(x²) − (Σx)²]
- a = intercept, calculated as a = (Σy − b·Σx) / n
- n = number of data pairs
- Σ means “sum of”
The correlation coefficient r uses the same sums:
r = [n·Σ(xy) − Σx·Σy] / √([n·Σ(x²) − (Σx)²]·[n·Σ(y²) − (Σy)²])
R-squared is simply r², expressed as a decimal between 0 and 1. Multiply by 100 to get a percentage.
Worked Example
Using the default data: x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5.
- Compute the sums: Σx = 15, Σy = 20, Σ(xy) = 1·2 + 2·4 + 3·5 + 4·4 + 5·5 = 64, Σ(x²) = 1 + 4 + 9 + 16 + 25 = 55, Σ(y²) = 4 + 16 + 25 + 16 + 25 = 86.
- Slope b = [5·64 − 15·20] / [5·55 − 15²] = (320 − 300) / (275 − 225) = 20 / 50 = 0.4.
- Intercept a = (20 − 0.4·15) / 5 = (20 − 6) / 5 = 14 / 5 = 2.8.
- Correlation r = (5·64 − 15·20) / √[(5·55 − 225)·(5·86 − 400)] = 20 / √[(275−225)·(430−400)] = 20 / √(50·30) = 20 / √1500 ≈ 0.5164.
- R² = 0.5164² ≈ 0.2667.
So the regression line is y = 2.8 + 0.4x. For x = 6, the predicted y is 2.8 + 0.4·6 = 5.2.
When to Use Linear Regression
Use linear regression when you have two continuous numerical variables and want to see if a straight line describes their relationship. Common examples include:
- Predicting a student’s exam score (y) from hours studied (x).
- Estimating a house’s sale price (y) from its square footage (x).
- Forecasting sales (y) from advertising spend (x).
The method works best when the scatterplot shows a roughly straight pattern and there are no obvious curves or outliers. It also assumes that the spread of points around the line is similar across all x values (homoscedasticity) and that the residuals (differences between observed and predicted y) are approximately normally distributed if you plan to use confidence intervals or hypothesis tests.
How to Read the Results
The slope (b) tells you the average change in y for a one-unit increase in x. In the example, each extra unit of x adds 0.4 to y. The intercept (a) is the predicted y when x = 0; sometimes it has no practical meaning if x=0 is outside your data range.
The correlation (r) ranges from −1 to +1. Values near +1 indicate a strong positive linear relationship, near −1 a strong negative one, and near 0 little to no linear association. R-squared is the proportion of variance in y explained by x. An r² of 0.27 means about 27% of the variation in y is explained by the line; the rest is due to other factors or random noise.
Always plot your data before trusting the numbers. A single outlier can drastically change the slope and intercept, and a curved relationship might produce a low r even if a clear pattern exists.
Common Mistakes and Misconceptions
- Confusing correlation with causation: A high r does not prove that x causes y. Both could be influenced by a third variable.
- Using the line outside your data range: Predictions for x values far beyond your observed minimum or maximum are risky; the relationship may not stay linear.
- Mixing up x and y: Regression is not symmetric. Swapping x and y gives a different line if the units or variances differ.
- Rounding too early: Rounding intermediate sums can change the final slope noticeably. Keep several decimal places until the end.
- Ignoring outliers: A few extreme points can pull the line toward them. Check a scatterplot and consider whether those points are data errors.
Limits of the Simple Linear Model
Simple linear regression only handles one predictor. If you have multiple independent variables, you need multiple regression, which requires matrix algebra or software. Also, the least-squares method is sensitive to outliers because it squares the residuals; a single bad point gets extra weight. For data with a clear curve, polynomial regression or data transformation (e.g., taking logs) may fit better.
Finally, the standard errors and p-values that often accompany regression output depend on the assumption that the residuals are independent and normally distributed. If your data is time series or has repeated measures, those assumptions may be violated, and you should use more specialized methods.
Frequently Asked Questions
What is the difference between linear regression and correlation?
Correlation (r) measures the strength and direction of a linear relationship between two variables, without giving a prediction equation. Linear regression finds the line that best predicts y from x, and its slope and intercept describe that prediction. A high correlation often accompanies a good regression fit, but they answer different questions.
Can I use linear regression for non-linear data?
You can fit a straight line to any data, but if the true relationship is curved, the line will be a poor summary and predictions will be biased. Check a scatterplot first. If the pattern is clearly curved, try transforming a variable (e.g., log x) or using polynomial regression.
What does R-squared really mean?
R-squared is the proportion of the total variation in y that is explained by the regression line. For example, an r² of 0.80 means 80% of the variability in y is accounted for by x; the other 20% is unexplained. It does not tell you whether the slope is statistically significant or whether the model is appropriate.
How many data points do I need?
Technically you need at least two points to draw a line, but with only two points you have no idea whether the relationship is truly linear. For any meaningful inference, more points are better; a common rule of thumb is at least 10 to 20 pairs, but the exact number depends on the variability and the effect size you want to detect.
What if my data has outliers?
Outliers can heavily influence the least-squares line. First verify the data entry is correct. If the outlier is a real observation, consider running the analysis with and without it to see how much the slope changes. Robust regression methods exist but are beyond a simple calculator.
Can I use this calculator for multiple regression?
No, this calculator handles only one independent variable (x). For two or more predictors, you need statistical software like Excel’s LINEST, R, or Python. The formulas become matrix-based and are not practical to compute by hand.
Related
Formula used
in x | X values | - | 1, 2, 3, 4, 5 | 0 in y | Y values | - | 2, 4, 5, 4, 5 | 0 out slope | Slope (b) | - | (count(x)*sum(x*y)-sum(x)*sum(y))/(count(x)*sum(x^2)-sum(x)^2) | 4 out intercept | Intercept (a) | - | (sum(y)-slope*sum(x))/count(x) | 4 out r | Correlation (r) | - | (count(x)*sum(x*y)-sum(x)*sum(y))/sqrt((count(x)*sum(x^2)-sum(x)^2)*(count(x)*sum(y^2)-sum(y)^2)) | 4 out r2 | R-squared (r²) | - | r^2 | 4
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.