Skip to content

Correlation

A correlation coefficient puts one number on how closely two numeric variables move together. In bioinformatics the question comes up with co-expressed genes, a marker against a phenotype, or a technical variable against a biological one. This page computes Pearson’s r and Spearman’s rho for two co-expressed genes, in R and Python.

Pearson’s r measures how closely the points follow a straight line. It runs from -1 to 1, and it reaches either end only when every point lies exactly on a line. It does not measure how steep that line is.

Spearman’s rho is Pearson’s r computed on the ranks of the values. It measures how consistently one variable increases, or decreases, as the other increases, whatever the shape of that trend. Because it uses ranks, a log transform of either variable leaves it unchanged, and one extreme sample cannot move it far.

The two coefficients answer different questions, so choose by the question. Pearson asks about a straight line on the scale you measured. Spearman asks whether higher goes with higher. On raw expression values with a long right tail, a few extreme samples can decide Pearson’s r, which is one reason to compute it on log values.

Download coexpression.csv into your working directory. The invented table has 60 rows, one per independent sample, and two columns, gene_x and gene_y, each the log2 expression of one gene.

library(ggplot2)
coexpression <- read.csv("coexpression.csv")
dir.create("outputs", showWarnings = FALSE)
cat(sprintf("%d samples, columns: %s\n", nrow(coexpression),
paste(names(coexpression), collapse = ", ")))
60 samples, columns: gene_x, gene_y

cor.test() in R and pearsonr() in SciPy return r, a p-value against a true correlation of zero, and a 95% confidence interval from Fisher’s z transformation. SciPy keeps the interval behind the confidence_interval() method of the result.

pearson <- cor.test(coexpression$gene_x, coexpression$gene_y,
method = "pearson")
cat(sprintf("Pearson r = %.3f, 95%% CI [%.3f, %.3f]\n", pearson$estimate,
pearson$conf.int[1], pearson$conf.int[2]))
cat(sprintf("p = %.2g, r squared = %.3f\n", pearson$p.value,
pearson$estimate^2))
Pearson r = 0.777, 95% CI [0.652, 0.861]
p = 2.8e-13, r squared = 0.604

The estimate is r = 0.777, and correlations from 0.652 to 0.861 are compatible with these 60 samples. The p-value of 2.8e-13 says that a true correlation of zero fits the data poorly. It does not say how strong the correlation is; the interval does that. Both the interval and the p-value treat the 60 pairs as independent draws from a roughly bivariate normal population.

Squaring r gives 0.604, so a straight line in gene X accounts for about 60% of the variance of gene Y.

A coefficient cannot show the shape of a relationship. A curve, two separate clusters, or one outlying sample can each produce r = 0.78, so draw the scatter before you report the number. The code adds the least-squares line and writes r with its interval on the plot.

label <- sprintf("r = %.2f, 95%% CI %.2f to %.2f", pearson$estimate,
pearson$conf.int[1], pearson$conf.int[2])
scatter <- ggplot(coexpression, aes(gene_x, gene_y)) +
geom_point(color = "#69b3a2", alpha = 0.8, size = 2) +
geom_smooth(method = "lm", formula = y ~ x, se = FALSE,
color = "#1f4e5f") +
annotate("text", x = -Inf, y = Inf, hjust = -0.1, vjust = 2,
label = label) +
labs(x = "Gene X (log2 expression)", y = "Gene Y (log2 expression)") +
theme_classic(base_size = 12)
ggsave("outputs/correlation-scatter-r.png", scatter, width = 5, height = 4,
dpi = 150)

Scatter of gene Y against gene X for 60 samples, drawn with ggplot2. The points rise along a straight least-squares line, and the plot carries the label r = 0.78, 95% CI 0.65 to 0.86.

The points follow a straight line with even scatter on both sides, and no single sample sets the trend. A straight-line summary is fair for this table.

The same functions compute Spearman’s rho. Two samples share one gene_y value, and R cannot compute an exact p-value when ranks are tied. exact = FALSE asks R for the t approximation, which is also what SciPy uses, and it avoids a warning about the tie.

spearman <- cor.test(coexpression$gene_x, coexpression$gene_y,
method = "spearman", exact = FALSE)
cat(sprintf("Spearman rho = %.3f, p = %.2g\n", spearman$estimate,
spearman$p.value))
Spearman rho = 0.780, p = 2.1e-13

Rho is 0.780, next to r = 0.777. Two close coefficients do not show that the relationship is a straight line, because a gently curved trend can give them similar values, and they do not rule out one sample that drives both. The scatter above answers both questions for this table. Neither function reports an interval for rho, and a bootstrap over the samples is the usual way to get one.

A correlation of 0.78 between two genes does not show that one regulates the other. A shared transcription factor produces the same number, and so does a batch in which every gene reads higher.

Neither coefficient gives the size of the change. The amount by which gene Y rises per unit of gene X is a regression slope, and two gene pairs with the same r can have slopes that differ tenfold.

Read r squared, and not r, as the share of variance. An r of 0.5 corresponds to 25% of the variance, and an r of 0.3 to 9%.

A coefficient near zero does not show that two variables are unrelated. A symmetric U-shaped relationship gives a Pearson’s r and a Spearman’s rho close to zero, because neither a line nor a steady increase describes it.

The interval above is valid for 60 independent samples. If the rows were 60 cells from three donors, the cells of one donor would resemble each other, and the same interval would be too narrow for a claim about donors.

The R correlation test reference documents the Pearson, Spearman, and Kendall tests, and it states when each p-value is exact and when the confidence interval is available.

The SciPy pearsonr reference describes the result object and the confidence_interval() method, including the option to compute the interval by bootstrap.

The Simple Linear Regression lesson fits the line through a scatter like this one. It reports the slope with its interval and predicts a new sample with a prediction interval.