Skip to content
simstat.com Statistics by simulation

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.

Separate x and y with a comma, tab, semicolon or space, so two columns copied from a spreadsheet work. Use a point for decimals. A text header on the first line is skipped. The example is data set I of Anscombe's quartet (Anscombe, 1973), as in R's datasets::anscombe.

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)

    Scatter plot of y against x with the least-squares line Scatter plot of 11 points with the least-squares line y = 3.0001 + 0.50009x. The table below the plot lists every point. 46810121446810xyline 1: x = 10, y = 8.04line 2: x = 8, y = 6.95line 3: x = 13, y = 7.58line 4: x = 9, y = 8.81line 5: x = 11, y = 8.33line 6: x = 14, y = 9.96line 7: x = 6, y = 7.24line 8: x = 4, y = 4.26line 9: x = 12, y = 10.84line 10: x = 7, y = 4.82line 11: x = 5, y = 5.68
    Dots: your pairs. Line: the least-squares line over the range of x. Open diamonds with a line number: points whose studentised residual is beyond ±3, or that alone fix the slope (leverage 1).
    Every point: fitted values, residuals and leverage
    Each pair with its fitted value, residual, studentised residual and leverage
    LinexyFitted ŷResidualStud. residualLeverageFlag
    1108.048.0010.0390.030.100
    286.957.001−0.051−0.040.100
    3137.589.501−1.921−2.080.236
    498.817.5011.3091.130.091
    5118.338.501−0.171−0.140.127
    6149.9610.001−0.041−0.040.318
    767.246.0011.2391.120.173
    844.265.000−0.740−0.700.318
    91210.849.0011.8391.840.173
    1074.826.501−1.681−1.570.127
    1155.685.5010.1790.160.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

    r=∑i=1n(xi−xˉ)(yi−yˉ)∑i=1n(xi−xˉ)2 ∑i=1n(yi−yˉ)2r = \frac{\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})}{\sqrt{\sum_{i=1}^{n}(x_i-\bar{x})^2}\,\sqrt{\sum_{i=1}^{n}(y_i-\bar{y})^2}}
    r=SxySxx Syyr = \frac{S_{xy}}{\sqrt{S_{xx}\,S_{yy}}}

    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

    t=rn−21−r2,df=n−2t = \frac{r\sqrt{n-2}}{\sqrt{1-r^2}}, \qquad \text{df} = n-2

    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".

    z=atanh⁡(r)=12ln⁡1+r1−rz = \operatorname{atanh}(r) = \frac{1}{2}\ln\frac{1+r}{1-r}
    limits=tanh⁡ ⁣(z±z1−α/2n−3)\text{limits} = \tanh\!\left(z \pm \frac{z_{1-\alpha/2}}{\sqrt{n-3}}\right)

    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.

    ri=eis1−hii,s=∑ei2n−2r_i = \frac{e_i}{s\sqrt{1-h_{ii}}}, \qquad s = \sqrt{\frac{\sum e_i^2}{n-2}}
    ti=rin−3n−2−ri2t_i = r_i\sqrt{\frac{n-3}{n-2-r_i^2}}
    hii=1n+(xi−xˉ)2Sxxh_{ii} = \frac{1}{n} + \frac{(x_i-\bar{x})^2}{S_{xx}}

    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