@stdlib/stats-chi2gof

Perform a chi-square goodness-of-fit test.

https://github.com/stdlib-js/stats-chi2gof

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.1%) to scientific vocabulary

Keywords

goodness-of-fit hypothesis javascript math mathematics node node-js nodejs statistics stats stdlib summary test
Last synced: 6 months ago · JSON representation

Repository

Perform a chi-square goodness-of-fit test.

Basic Info
Statistics
  • Stars: 3
  • Watchers: 3
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
goodness-of-fit hypothesis javascript math mathematics node node-js nodejs statistics stats stdlib summary test
Created over 4 years ago · Last pushed 11 months ago
Metadata Files
Readme Changelog Contributing License Code of conduct Citation Security

README.md

About stdlib...

We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.

The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.

When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.

To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!

Chi-square goodness-of-fit test

NPM version Build Status Coverage Status <!-- dependencies -->

Perform a chi-square goodness-of-fit test.

## Installation ```bash npm install @stdlib/stats-chi2gof ``` Alternatively, - To load the package in a website via a `script` tag without installation and bundlers, use the [ES Module][es-module] available on the [`esm`][esm-url] branch (see [README][esm-readme]). - If you are using Deno, visit the [`deno`][deno-url] branch (see [README][deno-readme] for usage intructions). - For use in Observable, or in browser/node environments, use the [Universal Module Definition (UMD)][umd] build available on the [`umd`][umd-url] branch (see [README][umd-readme]). The [branches.md][branches-url] file summarizes the available branches and displays a diagram illustrating their relationships. To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.
## Usage ```javascript var chi2gof = require( '@stdlib/stats-chi2gof' ); ``` #### chi2gof( x, y\[, ...args]\[, options] ) Computes a chi-square goodness-of-fit test for the **null hypothesis** that the values of `x` come from the discrete probability distribution specified by `y`. ```javascript // Observed counts: var x = [ 30, 20, 23, 27 ]; // Expected counts: var y = [ 25, 25, 25, 25 ]; var res = chi2gof( x, y ); var o = res.toJSON(); /* returns { 'rejected': false, 'alpha': 0.05, 'pValue': ~0.5087, 'df': 3, 'statistic': ~2.32, ... } */ ``` The second argument can either be an array-like object (or 1-dimensional [`ndarray`][@stdlib/ndarray/array]) of expected frequencies, an array-like object (or 1-dimensional [`ndarray`][@stdlib/ndarray/array]) of population probabilities summing to one, or a discrete probability distribution name to test against. ```javascript // Observed counts: var x = [ 89, 37, 30, 28, 2 ]; // Expected probabilities: var y = [ 0.40, 0.20, 0.20, 0.15, 0.05 ]; var res = chi2gof( x, y ); var o = res.toJSON(); /* returns { 'rejected': true, 'alpha': 0.05, 'pValue': ~0.0187, 'df': 3, 'statistic': ~9.9901, ... } */ ``` When specifying a discrete probability distribution name, distribution parameters **must** be provided as additional arguments. ```javascript var Int32Array = require( '@stdlib/array-int32' ); var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); var res; var x; var v; var i; // Simulate expected counts... x = new Int32Array( 100 ); for ( i = 0; i < x.length; i++ ) { v = discreteUniform( 0, 99 ); x[ v ] += 1; } res = chi2gof( x, 'discrete-uniform', 0, 99 ); // returns {...} ``` The function accepts the following `options`: - **alpha**: significance level of the hypothesis test. Must be on the interval `[0,1]`. Default: `0.05`. - **ddof**: "delta degrees of freedom" adjustment. Must be a nonnegative integer. Default: `0`. - **simulate**: `boolean` indicating whether to calculate p-values by Monte Carlo simulation. Default: `false`. - **iterations**: number of Monte Carlo iterations. Default: `500`. By default, the test is performed at a significance level of `0.05`. To adjust the significance level, set the `alpha` option. ```javascript var x = [ 89, 37, 30, 28, 2 ]; var p = [ 0.40, 0.20, 0.20, 0.15, 0.05 ]; var res = chi2gof( x, p ); var table = res.toString(); /* e.g., returns Chi-square goodness-of-fit test Null hypothesis: population probabilities are equal to those in p pValue: 0.0186 statistic: 9.9901 degrees of freedom: 3 Test Decision: Reject null in favor of alternative at 5% significance level */ res = chi2gof( x, p, { 'alpha': 0.01 }); table = res.toString(); /* e.g., returns Chi-square goodness-of-fit test Null hypothesis: population probabilities are equal to those in p pValue: 0.0186 statistic: 9.9901 degrees of freedom: 3 Test Decision: Fail to reject null in favor of alternative at 1% significance level */ ``` By default, the p-value is computed using a chi-square distribution with `k-1` degrees of freedom, where `k` is the length of `x`. If provided distribution arguments are estimated (e.g., via maximum likelihood estimation), the degrees of freedom **should** be corrected. Set the `ddof` option to use `k-1-n` degrees of freedom, where `n` is the degrees of freedom adjustment. ```javascript var x = [ 89, 37, 30, 28, 2 ]; var p = [ 0.40, 0.20, 0.20, 0.15, 0.05 ]; var res = chi2gof( x, p, { 'ddof': 1 }); var o = res.toJSON(); // returns { 'pValue': ~0.0186, 'statistic': ~9.9901, 'df': 3, ... } ``` Instead of relying on chi-square approximation to calculate the p-value, one can use Monte Carlo simulation. When the `simulate` option is `true`, the simulation is performed by re-sampling from the discrete probability distribution specified by `y`. ```javascript var x = [ 89, 37, 30, 28, 2 ]; var p = [ 0.40, 0.20, 0.20, 0.15, 0.05 ]; var res = chi2gof( x, p, { 'simulate': true, 'iterations': 1000 // explicitly set the number of Monte Carlo simulations }); // returns {...} ``` The function returns a results `object` having the following properties: - **alpha**: significance level. - **rejected**: `boolean` indicating the test decision. - **pValue**: test p-value. - **statistic**: test statistic. - **df**: degrees of freedom. - **method**: test name. - **toString**: serializes results as formatted test output. - **toJSON**: serializes results as a JSON object. To print formatted test output, invoke the `toString` method. The method accepts the following options: - **digits**: number of displayed decimal digits. Default: `4`. - **decision**: `boolean` indicating whether to show the test decision. Default: `true`. ```javascript var x = [ 89, 37, 30, 28, 2 ]; var p = [ 0.40, 0.20, 0.20, 0.15, 0.05 ]; var res = chi2gof( x, p ); var table = res.toString({ 'decision': false }); /* e.g., returns Chi-square goodness-of-fit test Null hypothesis: population probabilities are equal to those in p pValue: 0.0186 statistic: 9.9901 degrees of freedom: 3 */ ```
## Notes - The chi-square approximation may be incorrect if the observed or expected frequencies in each category are too small. Common practice is to require frequencies **greater than** five.
## Examples ```javascript var poisson = require( '@stdlib/random-base-poisson' ); var Int32Array = require( '@stdlib/array-int32' ); var chi2gof = require( '@stdlib/stats-chi2gof' ); var N = 400; var lambda = 3.0; var rpois = poisson.factory( lambda ); // Draw samples from a Poisson distribution: var x = []; var i; for ( i = 0; i < N; i++ ) { x.push( rpois() ); } // Generate a frequency table: var freqs = new Int32Array( N ); for ( i = 0; i < N; i++ ) { freqs[ x[ i ] ] += 1; } // Assess whether the simulated values come from a Poisson distribution: var out = chi2gof( freqs, 'poisson', lambda ); // returns {...} console.log( out.toString() ); ```
* * * ## Notice This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. #### Community [![Chat][chat-image]][chat-url] --- ## License See [LICENSE][stdlib-license]. ## Copyright Copyright © 2016-2025. The Stdlib [Authors][stdlib-authors].

Owner

  • Name: stdlib
  • Login: stdlib-js
  • Kind: organization

Standard library for JavaScript.

GitHub Events

Total
  • Push event: 5
Last Year
  • Push event: 5

Committers

Last synced: 8 months ago

All Time
  • Total Commits: 54
  • Total Committers: 1
  • Avg Commits per committer: 54.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 2
  • Committers: 1
  • Avg Commits per committer: 2.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
stdlib-bot n****y@s****o 54
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • npm 330 last-month
  • Total dependent packages: 10
  • Total dependent repositories: 10
  • Total versions: 12
  • Total maintainers: 4
npmjs.org: @stdlib/stats-chi2gof

Perform a chi-square goodness-of-fit test.

  • Homepage: https://stdlib.io
  • License: Apache-2.0
  • Latest release: 0.2.2
    published over 1 year ago
  • Versions: 12
  • Dependent Packages: 10
  • Dependent Repositories: 10
  • Downloads: 330 Last month
Rankings
Dependent packages count: 2.1%
Dependent repos count: 3.7%
Downloads: 5.9%
Average: 8.1%
Stargazers count: 13.2%
Forks count: 15.5%
Funding
  • type: opencollective
  • url: https://opencollective.com/stdlib
Last synced: 6 months ago

Dependencies

test/fixtures/r/DESCRIPTION cran
  • R >=3.4.0 depends
  • jsonlite * imports
package.json npm
  • @stdlib/array-int32 ^0.0.x development
  • @stdlib/assert-contains ^0.0.x development
  • @stdlib/assert-is-object ^0.0.x development
  • @stdlib/bench ^0.0.x development
  • @stdlib/constants-float64-eps ^0.0.x development
  • @stdlib/math-base-special-abs ^0.0.x development
  • @stdlib/math-base-special-max ^0.0.x development
  • @stdlib/random-base-discrete-uniform ^0.0.x development
  • @stdlib/random-base-poisson ^0.0.x development
  • istanbul ^0.4.1 development
  • tap-spec 5.x.x development
  • tape git+https://github.com/kgryte/tape.git#fix/globby development
  • @stdlib/array-base-incrspace ^0.0.x
  • @stdlib/array-float64 ^0.0.x
  • @stdlib/assert-has-own-property ^0.0.x
  • @stdlib/assert-is-boolean ^0.0.x
  • @stdlib/assert-is-collection ^0.0.x
  • @stdlib/assert-is-nan ^0.0.x
  • @stdlib/assert-is-ndarray-like ^0.0.x
  • @stdlib/assert-is-nonnegative-integer ^0.0.x
  • @stdlib/assert-is-number ^0.0.x
  • @stdlib/assert-is-plain-object ^0.0.x
  • @stdlib/assert-is-positive-integer ^0.0.x
  • @stdlib/assert-is-string ^0.0.x
  • @stdlib/blas-base-daxpy ^0.0.x
  • @stdlib/blas-base-dscal ^0.0.x
  • @stdlib/blas-ext-base-dfill ^0.0.x
  • @stdlib/blas-ext-base-dsumpw ^0.0.x
  • @stdlib/constants-float64-pinf ^0.0.x
  • @stdlib/constants-float64-sqrt-eps ^0.0.x
  • @stdlib/math-base-special-roundn ^0.0.x
  • @stdlib/math-base-utils-absolute-difference ^0.0.x
  • @stdlib/random-sample ^0.0.x
  • @stdlib/stats-base-dists-bernoulli-pmf ^0.0.x
  • @stdlib/stats-base-dists-binomial-pmf ^0.0.x
  • @stdlib/stats-base-dists-chisquare-cdf ^0.0.x
  • @stdlib/stats-base-dists-discrete-uniform-pmf ^0.0.x
  • @stdlib/stats-base-dists-geometric-pmf ^0.0.x
  • @stdlib/stats-base-dists-hypergeometric-pmf ^0.0.x
  • @stdlib/stats-base-dists-negative-binomial-pmf ^0.0.x
  • @stdlib/stats-base-dists-poisson-pmf ^0.0.x
  • @stdlib/string-format ^0.0.x
  • @stdlib/types ^0.0.x
  • @stdlib/utils-define-nonenumerable-read-only-accessor ^0.0.x
  • @stdlib/utils-define-nonenumerable-read-only-property ^0.0.x
.github/workflows/benchmark.yml actions
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
.github/workflows/cancel.yml actions
  • styfle/cancel-workflow-action 0.11.0 composite
.github/workflows/close_pull_requests.yml actions
  • superbrothers/close-pull-request v3 composite
.github/workflows/examples.yml actions
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
.github/workflows/npm_downloads.yml actions
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • actions/upload-artifact v3 composite
  • distributhor/workflow-webhook v3 composite
.github/workflows/productionize.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • stdlib-js/bundle-action main composite
  • stdlib-js/transform-errors-action main composite
.github/workflows/publish.yml actions
  • JS-DevTools/npm-publish v1 composite
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • styfle/cancel-workflow-action 0.11.0 composite
.github/workflows/test.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
.github/workflows/test_bundles.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • denoland/setup-deno v1 composite
.github/workflows/test_coverage.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • codecov/codecov-action v3 composite
  • distributhor/workflow-webhook v3 composite
.github/workflows/test_install.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite