statistical
v1.0.2
Published
A library that provides a set of functions to calculate various statistical measures for a given dataset.
Downloads
3
Readme
Statistical
Statistical is a library that provides a set of functions to calculate various statistical measures for a given dataset. It supports measures such as mean, median, mode, variance, standard deviation, skewness, kurtosis, quartiles, and many more.
Installation
You can install this package using npm:
npm i statistical
Usage
Importing
You can import the library using ES6 modules:
import Statistic from 'statistical'
Creating an instance
You can then create an instance of the Statistic
class, passing in your dataset and its type (either 'population' or 'sample'):
const dataset = [1, 2, 3, 4, 5];
const datasetType = 'population';
const statistic = new Statistic(dataset, datasetType)
Setting properties
You can use the setDatasetType
method to change dataset type in already created instance of the Statistic
class:
const statistic = new Statistic(dataset, 'population');
statistic.setDatasetType('sample');
This method takes a single argument, either 'population' or 'sample', and changes the type of the dataset accordingly.
You can use the setDecimalPlaces
method to change the number of decimal places in the calculated measure values. The default value is 5. For example, to set the number of decimal places to 10, you can use the following code:
const n = 10; // any number between 0 and 20
statistic.setDecimalPlaces(10);
Calculating measures
You can calculate all measures by calling the calc
method without any arguments:
statistic.calc();
Alternatively, you can calculate specific measures by passing in an array of measure names:
statistic.calc(['mean', 'mode']);
If you want to calculate a single measure, you can pass in its name as a string:
statistic.calc('skewness');
Good to know:
- Some measures depend on other measures being calculated first. For example, to calculate the mean value, the sum and count must be calculated first. These values will be calculated automatically when you call calc for the first time.
- The kurtosis and skewness values cannot be calculated for datasets with a size less than 4 and 3, respectively.
Getting measures and other properties
You can use the get
method to retrieve the calculated measures from the Statistic
instance. The get
method takes an optional parameter that works similarly to the calc
method parameter.
Here's an example:
// Calculate all measures
statistic.calc();
// Get all measures
const measures = statistic.get();
// Get specific measure(s)
const { mean } = statistic.get('mean');
const { median, mode } = statistic.get(['median', 'mode']);
Note that the calc
method supports chaining, so you can calculate measures and get their values in a single line, like this:
const allMeasures = statistic.calc().get();
// or
const x = 'skewness';
const { skewness } = statistic.calc(x).get(x)
You can use the getFrequencies
method to get the dataset frequencies object:
const statistic = new Statistic([1, 5, 5], datasetType);
console.log(statistic.getFrequencies());
/*
{
'1': 1,
'5': 2,
}
*/
You can use the getDataset
method to get the dataset values as an array:
const dataset = statistic.getDataset()
Working with dataset outliers
You can check if a dataset contains outliers using the checkOutliers
method. This method returns true
if the dataset contains outliers and false
otherwise. If the dataset contains outliers, you can remove them using the removeOutliers
method. This method returns an array of the removed values.
Here's an example code:
const containsOutliers = statistic.checkOutliers() // true or false
if (containsOutliers) {
const removedValues = statistic.removeOutliers()
// Note that after removing outliers all measures will be reset as well as frequencies object.
console.log(removedValues)
}
statistic.calc() // calculations without outliers
If you just want to get outliers you can use the getOutliers
method:
const statistic = new Statistic([1, 2, 3, 2356], datasetType)
const outliers = statistic.getOutliers() // [2356]
Contributing
If you'd like to contribute to this package, please open an issue or a pull request on the GitHub repository. Any contributions are welcome!