Correlation coefficient calculator: Pearson and Spearman
This correlation coefficient calculator finds Pearson's r and Spearman's ρ for paired data. Paste two columns of numbers and it gives Pearson's r and Spearman's ρ side by side: r with a confidence interval and p-value, r², ρ, the least-squares line, and a scatter plot that flags points far from the line. Everything runs in your browser.
Result
r = 0.816, 95% CI [0.424, 0.951], n = 11
- Pearson r
- 0.816
- 95% CI (Fisher z)
- [0.424, 0.951]
- r²
- 0.667
- Pairs n
- 11
- t statistic
- 4.241 (df 9)
- p-value (two-sided)
- 0.002
- Spearman's ρ
- 0.818
- ρ p-value (approximate)
- 0.002
Least-squares line: ŷ = 3.0001 + 0.5001x (slope 0.5001, intercept 3.0001)
Every point: fitted values, residuals and leverage
| Line | x | y | Fitted ŷ | Residual | Stud. residual | Leverage | Flag |
|---|---|---|---|---|---|---|---|
| 1 | 10 | 8.04 | 8.001 | 0.039 | 0.03 | 0.100 | |
| 2 | 8 | 6.95 | 7.001 | −0.051 | −0.04 | 0.100 | |
| 3 | 13 | 7.58 | 9.501 | −1.921 | −2.08 | 0.236 | |
| 4 | 9 | 8.81 | 7.501 | 1.309 | 1.13 | 0.091 | |
| 5 | 11 | 8.33 | 8.501 | −0.171 | −0.14 | 0.127 | |
| 6 | 14 | 9.96 | 10.001 | −0.041 | −0.04 | 0.318 | |
| 7 | 6 | 7.24 | 6.001 | 1.239 | 1.12 | 0.173 | |
| 8 | 4 | 4.26 | 5.000 | −0.740 | −0.70 | 0.318 | |
| 9 | 12 | 10.84 | 9.001 | 1.839 | 1.84 | 0.173 | |
| 10 | 7 | 4.82 | 6.501 | −1.681 | −1.57 | 0.127 | |
| 11 | 5 | 5.68 | 5.501 | 0.179 | 0.16 | 0.236 |
Numbers are rounded for display; the calculation keeps full precision. The ρ p-value uses the t approximation, so treat it as approximate in small samples.
What the correlation coefficient measures
Pearson's correlation coefficient r measures how closely paired values of x and y follow a straight line. It lies between −1 and 1: a positive r means y tends to rise as x rises, a negative r means it tends to fall, and r = 1 or −1 means every point lies on one line (Illowsky and Dean, 2023). It has no units, and swapping x and y gives the same value. An r near 0 means no linear association: a curve can have r close to 0, which is why Illowsky and Dean add, "It is important to view the scatterplot". Nor does r say anything about cause; in the words of the NIST/SEMATECH e-Handbook, "association does NOT imply causality": a scatter plot "can never 'prove' cause and effect."
The Pearson formula
Here n is the number of pairs, x̄ and ȳ are the means, Sxx and Syy are the sums of squared deviations from the means, and Sxy is the sum of cross-products, the form given in NIST's Dataplot reference manual. A pair with both values above their means, or both below, adds to Sxy; a pair on opposite sides subtracts from it.
Spearman's rank correlation
Spearman's ρ (rho) is Pearson's r computed on ranks: replace each x by its rank among the x values (1 for the
smallest), do the same for y, and correlate the ranks, with tied values sharing the average of the ranks they
span. R's documentation says its Spearman method "basically computes cor(R(x), R(y))", and the calculator matches
R's cor(x, y, method = "spearman"). Because only the order counts, ρ = 1 whenever y strictly increases with x, even along a curve, and an extreme value counts only as
the largest (or smallest) rank. R's documentation calls the rank-based measures "more robust" and says they "have been
recommended if the data do not necessarily come from a bivariate normal distribution"; it also notes that they
"make sense for ordered inputs". So prefer ρ for ordinal data such as ratings, for a relationship that is
monotonic but curved, or when one outlier would dominate r.
The p-value for ρ is approximate: it uses the t formula below with ρ in place of r, as R's
cor.test(x, y, method = "spearman", exact = FALSE) does. R's default (exact = NULL) and
exact = TRUE use algorithm AS 89 instead for fewer than 1,290 pairs without ties, so plain
cor.test(x, y, method = "spearman") can give a slightly different p-value.
Confidence interval and t test
The test asks whether the population correlation is 0. Illowsky and Dean (2023) give this statistic, on
n − 2 degrees of freedom; the p-value is the area in both tails. R's cor.test() documentation
states that it follows a t distribution "if the samples follow independent normal distributions".
The sampling distribution of r is skewed, more so the closer the population correlation is to ±1, so the
interval is built on Fisher's z transformation of r, which is roughly normal with variance 1/(n − 3): the calculator adds
and subtracts the normal critical value times 1/√(n − 3) on the z scale and transforms back. This is the
"asymptotic confidence interval" that R's cor.test() gives "based on Fisher's Z transform" when
there are at least 4 pairs. It is not symmetric around r.
Both are derived for independent pairs from a normal population: the t distribution holds when the two variables are independent and normal, and the Fisher interval is an approximation that can be poor for non-normal data. For data that "do not necessarily come from a bivariate normal distribution", R offers the rank-based tests. For the t test, Illowsky and Dean (2023) list a linear pattern, y values normally distributed about the line with the same spread at every x, and independent residuals from a random sample or randomised experiment.
Worked example
Worked example: Anscombe's data set I
The calculator's default data are the 11 pairs of Anscombe's set I. The means are x̄ = 9.000 and ȳ = 7.501, with Sxx = 110.000, Syy = 41.273 and Sxy = 55.010, so r = 55.010 / √(110.000 × 41.273) = 0.816.
The test statistic is t = 4.241 on 9 degrees of freedom, p = 0.002. For the 95% interval, z = atanh(r) = 1.146 with standard error 1/√(11 − 3) = 0.354; z ± 1.960 × 0.354 runs from 0.453 to 1.839 (from unrounded values), and tanh turns that into 0.424 to 0.951: 0.392 below r but only 0.135 above it.
Spearman's ρ = 0.818 (approximate p = 0.002), and the least-squares line is ŷ = 3.0001 + 0.5001x, with r² = 0.667.
r and r²
The square of r is the coefficient of determination. Illowsky and Dean (2023) describe r², as a percentage, as "the percent of variation in the dependent (predicted) variable y that can be explained by variation in the independent (explanatory) variable x using the regression (best-fit) line". In set I, r² = 0.667, so about 66.7% of the variation in y goes with the line. The calculator shows both, but r² drops the sign, so report r when the direction matters.
Reading the scatter plot and the flags
Look at the plot before trusting r. Among the questions a scatter plot answers, the NIST/SEMATECH e-Handbook lists "Are variables X and Y linearly related?" and "Are there outliers?". Anscombe's quartet (Anscombe, 1973) is four data sets that, in the words of R's documentation, "have the same traditional statistical properties (mean, variance, correlation, regression line, etc.), yet are quite different". Their r values are 0.816, 0.816, 0.816 and 0.817. Load them from the example menu and compare the plots.
In set III the points lie almost exactly on a line except line 3. With it, r = 0.816; without it, r = 0.999997. One point moves r that far. Spearman's ρ for set III is 0.991, because on the rank scale the odd point is only one place out of order. In set IV every x is 8 except line 8, so that single point has leverage 1 and alone decides the slope: r = 0.817 comes from one point.
The standardised residual ri is the residual ei divided by its estimated standard
deviation (s is the residual standard error, hii the leverage); the studentised residual
ti estimates s without point i. They are R's rstandard(lm(y ~ x)) and
rstudent(lm(y ~ x)), which "re-normalize the residuals to have unit variance, using an overall and
leave-one-out measure of the error variance respectively". The calculator flags a point when its studentised
residual is beyond ±3. It does not use ri for this, because ri can never
exceed √(n − 2): with 11 or fewer pairs no point could pass 3. Line 3 of set III shows the difference: its
standardised residual is 2.999992, at the ceiling √9 = 3.000000, while its studentised residual is
1203.539464, so it is flagged. The cut-off is a prompt to look, not a test. Points with leverage 1 are flagged
too, and samples of fewer than 10 pairs get a warning (our own rule of thumb, not a published cut-off).
Flagged points are drawn as open diamonds with their line number, not by colour alone.
How to report it
Across the 11 pairs, x and y were positively correlated, r(9) = 0.82, 95% CI [0.42, 0.95], p = 0.002 (Pearson's r, Fisher z interval); Spearman's ρ = 0.82.
Give n or the degrees of freedom, the coefficient, its interval and method, and the p-value. Show the scatter plot, and say whether any flagged point was removed. For a correlation used as an effect size, our effect size calculator converts between r and Cohen's d, and our guide to effect size explains how to interpret it.
Our guide to how to calculate the correlation coefficient works through r by hand, and correlation vs causation explains why a strong r does not show that one variable changes the other.
References
- Anscombe, F. J. (1973). Graphs in statistical analysis. The American Statistician, 27(1), 17–21. doi.org/10.1080/00031305.1973.10478966
- Illowsky, B. & Dean, S. (2023). Introductory Statistics 2e, sections 12.3 and 12.4. OpenStax.
- NIST/SEMATECH. e-Handbook of Statistical Methods, section 1.3.3.26, Scatter plot. itl.nist.gov
- NIST. Dataplot Reference Manual: CORRELATION. itl.nist.gov
- R Core Team. R documentation: cor.test, cor, influence.measures (rstandard, rstudent) and anscombe. R Foundation for Statistical Computing.