Correlation and Linear Regression Calculator
Paste paired numeric observations to calculate Pearson correlation, its square, and the least-squares straight line predicting y from x. The calculator reports slope and intercept, draws the fitted line, and lists observed values, predictions, and residuals so the numerical summary can be checked against the original pairs.
By Dr. Hassan Khames EliwaLast updated
Result
- r²
- 0.6
- Slope
- 0.6
- Intercept
- 2.2
- Pair count
- 5
- Observations
- Least-squares fit
Observed and fitted values; residual = observed minus fitted
| x | y | Fitted y | Residual |
|---|---|---|---|
| 1 | 2 | 2.8 | -0.8 |
| 2 | 4 | 3.4 | 0.6 |
| 3 | 5 | 4 | 1 |
| 4 | 4 | 4.6 | -0.6 |
| 5 | 5 | 5.2 | -0.2 |
How it works
Pairs must stay paired
Enter exactly one x,y pair per line, separated by a comma, without a header. Spaces around the numbers are allowed, as is scientific notation. Thousands separators are not allowed because the comma already separates coordinates. The parser accepts 2–500 rows, at most 50,000 characters, with finite coordinate magnitudes no greater than one billion. Blank interior rows and malformed records are errors, not silently discarded observations.
Center the sums before fitting
Let x̄ and ȳ denote the coordinate means. Compute Sxx = Σ(x−x̄)², Syy = Σ(y−ȳ)², and Sxy = Σ(x−x̄)(y−ȳ). Pearson r = Sxy / √(Sxx × Syy). The slope b = Sxy/Sxx, and intercept a = ȳ − b × x̄. The fitted equation is y-hat = a + b × x. Each residual is the observed y minus its fitted value.
Centering is more numerically stable than subtracting two large raw sums. It cannot recover distinctions that were already lost when extremely close decimal values were converted to machine numbers. Rescale measurements or subtract a meaningful reference value before fitting awkward data. The results use up to six displayed decimal places; calculations retain additional precision internally.
Correlation is a linear summary
The sign of r describes the direction of a linear association. Its magnitude is bounded by one. For this ordinary least-squares model with an intercept, r² equals the fraction of centered y variation accounted for by the fitted line when y varies. It does not establish a causal mechanism or measure prediction accuracy in original units.
Constant x makes a unique slope impossible, so the calculator rejects that dataset with a specific explanation. Constant y permits a horizontal fitted line with slope zero and intercept equal to that constant, but Pearson r and r² are undefined because Syy is zero. No significance test, confidence band, or prediction interval is implied by the fitted line.
Worked example
Scenario 1: a small paired dataset
Paste 1,2; 2,4; 3,5; 4,4; and 5,5 on separate lines, replacing each semicolon with a newline. The means are x̄ = 3 and ȳ = 4. Centered sums are Sxx = 10, Syy = 6, and Sxy = 6. The slope is 0.6, the intercept is 2.2, Pearson r is 0.774597, and r² is 0.6 after display rounding. At x = 3, fitted y is 4 and the observed y is 5, giving residual 1.
Scenario 2: a decreasing relationship
Enter the three rows 1,5; 2,3; and 3,1. The line is y-hat = 7 − 2x, so the reported slope is −2 and intercept is 7. Every residual is zero, Pearson r is −1, and r² is 1. The negative sign describes direction, not a poor fit. A perfect fit to three constructed points is arithmetic evidence only; it does not guarantee future observations will obey the same relationship.
Expert tips
Common mistakes
- Do not sort x and y independently before pasting. That destroys the original pairing and can manufacture an impressive correlation that was absent from the observations.
- Do not extrapolate far beyond the smallest and largest observed x. A fitted line is a description within the observed range, not a guarantee that physical or commercial constraints disappear elsewhere.
- Do not treat repeated measurements from the same subject as independent evidence for inference. This tool computes descriptive coefficients and makes no adjustment for grouped observations or time dependence.
Frequently asked questions
Can r be zero for related variables?
Yes. A curved relationship can have little or no linear correlation. Pearson r summarizes straight-line association, so inspecting the observations is necessary before concluding that two variables have no relationship.
What happens when y is constant?
The fitted line is horizontal and predictions equal that constant. Correlation and its square are labeled undefined rather than zero, because the correlation denominator contains the zero variation of y.
What happens when x is constant?
The calculator returns an error. There is no observed x variation from which to estimate a unique slope. Collect observations across different x values before interpreting a regression of y on x.
Does switching x and y preserve the line?
No. Pearson r remains the same, but least squares minimizes vertical residuals in the chosen response variable. Regressing x on y usually produces a different predictive equation, not a simple rearrangement of the original fit.
Does a high r prove causation?
No. Confounding, selection effects, shared trends, and chance can all produce correlation. A causal claim needs a defensible design and subject-matter argument beyond these descriptive coefficients.
Related calculators
Related guides
MathSample size and confidence intervals without the jargonSeparate sample planning from interval estimation, and make the assumptions visible before reporting a margin of error.15 November 2026 · 8 min read
MathStandard deviation without the jargonThe average tells you the centre. Standard deviation helps describe the spread, provided you know what the data represents.14 November 2026 · 7 min read
MathPercentages people get wrong: change, difference and pointsThe denominator determines the answer. Learn to distinguish percentage change, symmetric difference and percentage-point movements.14 November 2026 · 7 min readSources and standards
About the author
Dr. Hassan Khames Eliwa holds a PhD in Construction & Built Environment and has more than 20 years of international project controls experience. He is Director of Eliwa Project Controls Limited in Auckland and founder of PMMilestone.org and PMMilestone.com.
Buy me a coffee