Calculate Correlation
What is this
A simple node package that calculates the Pearson correlation coefficient of two arrays. No dependencies needed.
Installation
As with any other node package:
npm install calculate-correlation
Usage
The package exports a function.
The function takes two required arguments: two arrays of numbers, with equal length.
And the function returns the correlation coefficient of the two arrays of numbers.
The function also accepts a third optional argument, a config object. More info about it below.
Examples
Use it as follows:
const calculateCorrelation = require("calculate-correlation");
const x = [2, 5, 4, 1];
const y = [3, 3, 6, 7];
const correlation = calculateCorrelation(x, y);
console.log(correlation);
console.log(typeof correlation);
Example with config options:
const calculateCorrelation = require("calculate-correlation");
const x = [2, 5, 4, 1];
const y = [3, 3, 6, 7];
const config = {
string: true,
decimals: 5,
};
const correlation = calculateCorrelation(x, y, config);
console.log(correlation);
console.log(typeof correlation);
Be careful
- Passing less than 2 or more than three arguments will return an error.
- Passing two arrays with non-numerical values in them will return an error.
- Passign two arrays with different length will also return an error.
If the input is correct, the default result will be a Number
between 1 and -1, rounded up to 9 decimal positions.
Config options
The function accepts an object as the third argument. This object can setup two options:
Option | Type | Default | Description |
---|
string | Boolean | false | Return the result as a String . |
decimals | Number | 9 | The maximum number of decimals to round the result to. |
More details
As of now, the formula used by the package to calculate correlation is the one for samples.
Future
Some kind of possible roadmap: