npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

statistical

v1.0.2

Published

A library that provides a set of functions to calculate various statistical measures for a given dataset.

Downloads

5

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:

  1. 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.
  2. 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!