correlation test demo
statinfer (opens in a new tab) is a JavaScript package for statistical tests, with TypeScript types included. It runs in Node.js and in the browser.
This page demonstrates its correlationTest function, which measures how strongly two measurements move together. Enter your own pairs of numbers below: statinfer calculates the result right here in your browser, explains what it means, and shows the JavaScript code that produces it.
On this page: When to use a correlation test · Demo data · Demo results · What the results mean · How to use it · Further reading
When to use a correlation test
A correlation test asks whether two measurements of the same things move together. For example: do taller people tend to weigh more, or do students who study longer tend to score higher? Each x value must be paired with the y value in the same position.
The result, the correlation, runs from −1 to 1. A value near 1 means y rises steadily as x rises, near −1 means y falls steadily, and near 0 means no consistent relationship. The test asks whether the correlation in your data is strong enough to be more than chance. There are two methods:
- Pearson measures how closely the points follow a straight line. It's the usual choice, but a single extreme value can distort it.
- Spearman first converts the values to ranks (1st, 2nd, 3rd…), then measures how consistently y rises or falls with x, even along a curve. That makes it robust to outliers.
Correlation doesn't show that one thing causes the other: both might be driven by something else. A correlation test also needs at least three pairs, and more pairs make the result far more reliable.
Demo data
Demo results
Pearson correlation test ======================== statistic 7.315 p-value 8.266e-5 df 8 estimate 0.9327 95% CI [0.7343, 0.9843] alternative two-sided
The statistic in the table is a t-statistic, which statinfer calculates from the correlation to get the p-value. The correlation itself is the estimate.
What the results of a correlation test mean
The estimate from the demo data entered above is 0.9327.
If the true correlation (Pearson's r) really were 0, the chance of getting a result at least this extreme is less than 0.001. That's the p-value.
That's below 0.05, so at the common 5% level this counts as evidence that the true correlation (Pearson's r) differs from 0.
The 95% confidence interval, 0.7343 to 0.9843, is the range of values for the true correlation (Pearson's r) that are consistent with the demo data entered above.
"True" here means the value for the whole population that the demo data entered above are a sample from, rather than just for those numbers.
How to use statinfer's correlation test in your JavaScript code
Install statinfer from npm:
npm install statinfer
This JavaScript code reproduces the result above. It also works unchanged in TypeScript, where statinfer's built-in types give you autocompletion and type checking.
import { correlationTest, summarize } from "statinfer";
const result = correlationTest({
x: [2.1, 3.4, 1.9, 5.6, 4.2, 3.3, 6.1, 2.8, 4.9, 3.7],
y: [1.8, 3.9, 2.2, 5.1, 3.6, 3.5, 6.4, 2.5, 4.1, 4.4],
});
console.log(summarize(result));For every option correlationTest accepts, see its API documentation (opens in a new tab).
The raw result
correlationTest returns a plain JavaScript object, which your code can use directly. For example, result.estimate is the correlation. Below, the same object is shown as JSON (JavaScript Object Notation), a standard text format for data, which you can save or send to other programs. For Spearman's method, confidenceInterval is null, because statinfer doesn't calculate one.
Show the raw result as JSON
{
"method": "Pearson correlation test",
"statistic": 7.315253218094181,
"pValue": 0.00008266060253636755,
"df": 8,
"estimate": 0.9327089800616777,
"confidenceInterval": [
0.7343152777677147,
0.9842981907383277
],
"confidenceLevel": 0.95,
"alternative": "two-sided"
}Further reading
- Pearson correlation coefficient (opens in a new tab), on Wikipedia
- Spearman's rank correlation coefficient (opens in a new tab), on Wikipedia
- Fisher transformation (opens in a new tab), on Wikipedia, which is how statinfer calculates Pearson's confidence interval