Linear Regression and Correlation Calculator
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 calculator works out the linear regression equation (y = a + bx) and the correlation coefficient (r) for a set of paired data you enter. You type or paste your x and y values, and it returns the slope, intercept, and r, along with the coefficient of determination (r²). It uses the standard least-squares formulas, so you can check your homework or quickly analyze a small dataset without a spreadsheet.
Formula
The linear regression line is written as y = a + bx, where:
- y is the dependent variable (what you are predicting).
- x is the independent variable (the input).
- b is the slope, the change in y for each one-unit change in x.
- a is the intercept, the predicted y when x = 0.
The slope is calculated as:
b = [n·Σ(xy) − Σx·Σy] / [n·Σ(x²) − (Σx)²]
The intercept is:
a = (Σy − b·Σx) / n
The correlation coefficient r is:
r = [n·Σ(xy) − Σx·Σy] / √([n·Σ(x²) − (Σx)²]·[n·Σ(y²) − (Σy)²])
Here, n is the number of data pairs, and Σ means “sum of.” All sums are taken over all data points. The value of r always falls between −1 and 1. A positive r means y tends to increase as x increases; a negative r means y tends to decrease as x increases. The closer |r| is to 1, the stronger the linear relationship.
Worked Example
Using the default data: x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5.
- Count: n = 5.
- Σx = 1+2+3+4+5 = 15, Σy = 2+4+5+4+5 = 20.
- Σ(xy) = (1·2)+(2·4)+(3·5)+(4·4)+(5·5) = 2+8+15+16+25 = 66.
- Σ(x²) = 1+4+9+16+25 = 55, Σ(y²) = 4+16+25+16+25 = 86.
- Slope b = (5·66 − 15·20) / (5·55 − 15²) = (330 − 300) / (275 − 225) = 30 / 50 = 0.6.
- Intercept a = (20 − 0.6·15) / 5 = (20 − 9) / 5 = 11 / 5 = 2.2.
- r = (5·66 − 15·20) / √((5·55 − 225)·(5·86 − 400)) = 30 / √(50·30) = 30 / √1500 ≈ 0.7746.
- r² ≈ 0.6.
So the regression line is y = 2.2 + 0.6x, and the correlation coefficient is about 0.7746. This indicates a moderately strong positive linear relationship.
When to Use Linear Regression and Correlation
Use this calculator when you have two numerical variables and you want to know whether a straight line describes their relationship well. Common situations:
- Checking the association between study hours and exam scores.
- Looking at the relationship between temperature and energy use.
- Estimating a simple trend in sales or production data.
- Verifying a calibration curve in a lab (e.g., absorbance vs. concentration).
Remember that correlation does not imply causation. A high r only means the two variables move together linearly, not that one causes the other. Also, linear regression is only appropriate if the relationship is roughly linear; if the scatterplot shows a curve, a straight line may mislead.
How to Read the Results
The slope (b) tells you the direction and steepness. For example, b = 0.6 means for each additional unit of x, y increases by 0.6 on average. The intercept (a) is the predicted y when x is zero, which may not be meaningful if x=0 is outside the range of your data. The correlation coefficient r tells you strength and direction: 0.9 or higher is a very strong relationship, 0.7–0.9 is strong, 0.5–0.7 is moderate, 0.3–0.5 is weak, and below 0.3 is very weak. r² is the proportion of variance in y explained by x. In the example, r² = 0.6 means 60% of the variation in y is explained by x.
Common Mistakes and Limitations
- Mixing up x and y: Reversing the variables changes the regression line (unless you swap both), but r stays the same.
- Ignoring outliers: A single extreme point can drastically change the slope and r. Always plot the data first.
- Extrapolating: The line is only valid within the range of your data. Predicting y for x far outside that range is risky.
- Assuming linearity: If the true relationship is curved, r may be low even if a strong nonlinear relationship exists.
- Rounding too early: Keep several decimal places during calculations to avoid rounding errors in the final answer.
The calculator assumes you have at least two distinct x values. If all x values are identical, the denominator becomes zero and the slope is undefined. Also, this is simple linear regression with one predictor; if you have multiple predictors, you need multiple regression.
How to Choose Between Correlation and Regression
Correlation and regression answer different questions. Correlation (r) measures the strength and direction of the linear association, with no assumption about which variable causes the other. Regression (y = a + bx) gives a predictive equation, so you must decide which variable is the predictor (x) and which is the response (y). If you only want to know if two variables move together, report r. If you want to predict one from the other, use the regression equation. In many fields, both are reported together.
Frequently Asked Questions
What is the difference between Pearson and Spearman correlation?
This calculator uses Pearson correlation, which measures linear relationships. Spearman correlation uses ranks and measures monotonic relationships (whether y consistently increases or decreases with x, even if not at a constant rate). Use Spearman if your data is not normally distributed or if the relationship is curved but still monotonic.
Can I use this calculator with only a few data points?
Technically yes, but with fewer than about 10 points the results are very sensitive to individual values. With just two points, r is always 1 or −1, which is meaningless. More data generally gives more reliable estimates of the true relationship.
What does r² mean in simple terms?
r² is the proportion of the total variation in y that is explained by the linear relationship with x. For example, if r² = 0.6, then 60% of the variation in y is accounted for by x, and 40% is due to other factors or random variation.
Why is my correlation coefficient not exactly 1 even though the points look close to a line?
Because r measures how perfectly the points fall on a straight line. If the points are close but not exactly on the line, r will be less than 1. The value depends on the scatter, not just the visual impression. A perfect r = 1 only happens when all points lie exactly on a straight line.
What if my data has missing values?
Most calculators, including this one, ignore blank entries or require complete pairs. If a pair has a missing x or y, that pair is excluded from the calculation. Make sure your x and y lists have the same number of values; otherwise, the calculator will not work correctly.
How do I know if the regression line is a good fit?
Look at r². A higher r² means a better fit. Also, plot the residuals (the differences between observed and predicted y). If they show a random pattern, the line is appropriate. If they show a curve or funnel shape, a linear model may not be suitable.
Related
Formula used
in x | X values (comma separated) | - | 1, 2, 3, 4, 5 | 0 in y | Y values (comma separated) | - | 2, 4, 5, 4, 5 | 0 out slope | Slope (b) | - | (count(x)*sum(x*y)-sum(x)*sum(y))/(count(x)*sum(x*x)-sum(x)^2) | 4 out intercept | Intercept (a) | - | (sum(y)-slope*sum(x))/count(x) | 4 out r | Correlation coefficient (r) | - | (count(x)*sum(x*y)-sum(x)*sum(y))/sqrt((count(x)*sum(x*x)-sum(x)^2)*(count(x)*sum(y*y)-sum(y)^2)) | 4 out r2 | Coefficient of determination (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.