Correlation & Linear Regression Calculator
Enter pairs of numbers (x, y) to find the correlation coefficient and the regression line that explains y from x.
Correlation and the regression line
- The correlation coefficient r (Pearson's product-moment correlation) measures the strength of the linear relationship between two values. It is between −1 and 1. r = Σ(x − x̄)(y − ȳ) ÷ (√Σ(x − x̄)² × √Σ(y − ȳ)²)
- The regression line is the line y = slope × x + intercept that explains y from x (least squares). slope = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)², intercept = ȳ − slope × x̄
- The coefficient of determination R² equals r squared when there is one explanatory variable x.
The calculation uses exact fractions. The square root in r is an approximation unless it comes out exactly, and then the value is labeled "rounded value".
When x never changes, or y never changes
| Situation | Correlation and R² | Regression line |
|---|---|---|
| All the x values are the same (the variance of x is 0) | Not available | Not available (the slope is not determined) |
| All the y values are the same (the variance of y is 0), and x varies | Not available (the correlation coefficient is not defined when y has no variance) | Available (slope 0, intercept = the mean of y: a horizontal line) |
If both x and y are all the same, it is treated as the case where x has no variance. With only 2 pairs, r is always exactly ±1 (two points always lie on a line), so with very little data the correlation says little.
Notes
- The correlation coefficient measures only linear relationships. A curved relationship can exist even when r is close to 0.
- Correlation does not show causation. Two values that rise and fall together do not necessarily cause each other (a third factor may affect both, or it may be chance).
- Outliers can have a large effect. One unusual pair can change r and the regression line a lot.
- Predicting outside the range of your data (extrapolation) can be unreliable.
- Regression explains y from x. If you swap x and y, the slope and intercept change (r does not).
The last three points are general statistical cautions. They are not claims taken from a specific source.
Input rules
- Put one pair per line, with two numbers: x and y (decimals and negatives are allowed; up to 9 digits before the decimal point and 6 decimal places). Empty lines are ignored.
- Separate x and y with a space, a tab, a semicolon, or a comma followed by a space. A comma with no space after it (1,000, 1,5, 3,5) is rejected with the line number, instead of being guessed. Do not use thousands separators.
- The numbers of x and y values always match, because each line holds one x and one y. A line with only one number, or with three, is reported as an error with its line number.
- You can enter 2 to 1,000 pairs.
Examples
| Data | Result |
|---|---|
| (1, 2), (2, 3), (3, 5), (4, 4), (5, 6) | r = 0.9, R² = 0.81, slope 0.9, intercept 1.3 (y = 0.9 x + 1.3) |
| (1, 2), (2, 4), (3, 6) | r = 1, slope 2, intercept 0 |
What this calculator does not do
Spearman's rank correlation, significance tests (p-values) and confidence intervals, multiple regression and curve fitting, automatic outlier detection, scatter plots and predicted values are not covered. For the spread of one set of numbers, use the Standard Deviation Calculator, and for the average, the Mean & Median Calculator.
Frequently asked questions
Does a correlation close to 0 mean there is no relationship?
It means there is little linear relationship. A curved relationship may still exist.
If r is close to 1, does x cause y?
No. Correlation does not show causation.
Why does it say the slope cannot be calculated?
If all the x values are the same, the slope of the line is not determined.