Gathering detailed insights and metrics for @stdlib/stats-ttest2
Gathering detailed insights and metrics for @stdlib/stats-ttest2
Gathering detailed insights and metrics for @stdlib/stats-ttest2
Gathering detailed insights and metrics for @stdlib/stats-ttest2
@stdlib/stats-base-meanpn
Calculate the arithmetic mean of a strided array using a two-pass error correction algorithm.
@stdlib/stats-base-dists-chi-cdf
Chi distribution cumulative distribution function (CDF).
@stdlib/stats-base-mean
Calculate the arithmetic mean of a strided array.
@stdlib/stats-base-dists-degenerate-cdf
Degenerate distribution cumulative distribution function (CDF).
npm install @stdlib/stats-ttest2
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
4 Stars
60 Commits
3 Watching
5 Branches
10 Contributors
Updated on 01 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
71%
6,722
Compared to previous day
Last week
6.1%
33,661
Compared to previous week
Last month
2%
138,742
Compared to previous month
Last year
844.4%
1,591,695
Compared to previous year
22
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!
Two-sample Student's t-Test.
1npm install @stdlib/stats-ttest2
Alternatively,
script
tag without installation and bundlers, use the ES Module available on the esm
branch (see README).deno
branch (see README for usage intructions).umd
branch (see README).The branches.md 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.
1var ttest2 = require( '@stdlib/stats-ttest2' );
By default, the function performs a two-sample t-test for the null hypothesis that the data in arrays or typed arrays x
and y
is independently drawn from normal distributions with equal means.
1// Student's sleep data: 2var x = [ 0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0 ]; 3var y = [ 1.9, 0.8, 1.1, 0.1, -0.1, 4.4, 5.5, 1.6, 4.6, 3.4 ]; 4 5var out = ttest2( x, y ); 6/* e.g., returns 7 { 8 'rejected': false, 9 'pValue': ~0.079, 10 'statistic': ~-1.861, 11 'ci': [ ~-3.365, ~0.205 ], 12 // ... 13 } 14*/
The returned object comes with a .print()
method which when invoked will print a formatted output of the results of the hypothesis test. print
accepts a digits
option that controls the number of decimal digits displayed for the outputs and a decision
option, which when set to false
will hide the test decision.
1console.log( out.print() ); 2/* e.g., => 3 Welch two-sample t-test 4 5 Alternative hypothesis: True difference in means is not equal to 0 6 7 pValue: 0.0794 8 statistic: -1.8608 9 95% confidence interval: [-3.3655,0.2055] 10 11 Test Decision: Fail to reject null in favor of alternative at 5% significance level 12*/
The function accepts the following options
:
number
in the interval [0,1]
giving the significance level of the hypothesis test. Default: 0.05
.two-sided
, less
or greater
. Indicates whether the alternative hypothesis is that x
has a larger mean than y
(greater
), x
has a smaller mean than y
(less
) or the means are the same (two-sided
). Default: two-sided
.number
denoting the difference in means under the null hypothesis. Default: 0
.string
indicating if the test should be conducted under the assumption that the unknown variances of the normal distributions are equal
or unequal
. Default: unequal
.By default, the hypothesis test is carried out at a significance level of 0.05
. To choose a different significance level, set the alpha
option.
1var x = [ 0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0 ]; 2var y = [ 1.9, 0.8, 1.1, 0.1, -0.1, 4.4, 5.5, 1.6, 4.6, 3.4 ]; 3 4var out = ttest2( x, y, { 5 'alpha': 0.1 6}); 7var table = out.print(); 8/* e.g., returns 9 Welch two-sample t-test 10 11 Alternative hypothesis: True difference in means is not equal to 0 12 13 pValue: 0.0794 14 statistic: -1.8608 15 90% confidence interval: [-3.0534,-0.1066] 16 17 Test Decision: Reject null in favor of alternative at 10% significance level 18*/
By default, a two-sided test is performed. To perform either of the one-sided tests, set the alternative
option to less
or greater
.
1// Student's sleep data: 2var x = [ 0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0 ]; 3var y = [ 1.9, 0.8, 1.1, 0.1, -0.1, 4.4, 5.5, 1.6, 4.6, 3.4 ]; 4 5var out = ttest2( x, y, { 6 'alternative': 'less' 7}); 8var table = out.print(); 9/* e.g., returns 10 Welch two-sample t-test 11 12 Alternative hypothesis: True difference in means is less than 0 13 14 pValue: 0.0397 15 statistic: -1.8608 16 df: 17.7765 17 95% confidence interval: [-Infinity,-0.1066] 18 19 Test Decision: Reject null in favor of alternative at 5% significance level 20*/ 21 22out = ttest2( x, y, { 23 'alternative': 'greater' 24}); 25table = out.print(); 26/* e.g., returns 27 Welch two-sample t-test 28 29 Alternative hypothesis: True difference in means is greater than 0 30 31 pValue: 0.9603 32 statistic: -1.8608 33 df: 17.7765 34 95% confidence interval: [-3.0534,Infinity] 35 36 Test Decision: Fail to reject null in favor of alternative at 5% significance level 37*/
As a default choice, the ttest2
function carries out the Welch test (using the Satterthwaite approximation for the degrees of freedom), which does not have the requirement that the variances of the underlying distributions are equal. If the equal variances assumption seems warranted, set the variance
option to equal
.
1var x = [ 2, 3, 1, 4 ]; 2var y = [ 1, 2, 3, 1, 2, 5, 3, 4 ]; 3 4var out = ttest2( x, y, { 5 'variance': 'equal' 6}); 7var table = out.print(); 8/* e.g., returns 9 Two-sample t-test 10 11 Alternative hypothesis: True difference in means is not equal to 0 12 13 pValue: 0.8848 14 statistic: -0.1486 15 df: 10 16 95% confidence interval: [-1.9996,1.7496] 17 18 Test Decision: Fail to reject null in favor of alternative at 5% significance level 19*/
To test whether the difference in the population means is equal to some other value than 0
, set the difference
option.
1var normal = require( '@stdlib/random-base-normal' ).factory; 2 3var rnorm = normal({ 4 'seed': 372 5}); 6 7var x = []; 8var i; 9for ( i = 0; i < x.length; i++ ) { 10 x.push( rnorm( 2.0, 3.0 ) ); 11} 12 13var y = []; 14for ( i = 0; i < x.length; i++ ) { 15 y.push( rnorm( 1.0, 3.0 ) ); 16} 17 18var out = ttest2( x, y, { 19 'difference': 1.0, 20 'variance': 'equal' 21}); 22/* e.g., returns 23 { 24 'rejected': false, 25 'pValue': ~0.642, 26 'statistic': ~-0.466, 27 'ci': [ ~-0.0455, ~1.646 ], 28 // ... 29 } 30*/ 31 32var table = out.print(); 33/* e.g., returns 34 Two-sample t-test 35 36 Alternative hypothesis: True difference in means is not equal to 1 37 38 pValue: 0.6419 39 statistic: -0.4657 40 df: 198 41 95% confidence interval: [-0.0455,1.646] 42 43 Test Decision: Fail to reject null in favor of alternative at 5% significance level 44*/
1var incrspace = require( '@stdlib/array-base-incrspace' ); 2var ttest2 = require( '@stdlib/stats-ttest2' ); 3 4var a = incrspace( 1, 11, 1 ); 5var b = incrspace( 7, 21, 1 ); 6 7var out = ttest2( a, b ); 8var table = out.print(); 9/* e.g., returns 10 Welch two-sample t-test 11 12 Alternative hypothesis: True difference in means is not equal to 0 13 14 pValue: 0 15 statistic: -5.4349 16 95% confidence interval: [-11.0528,-4.9472] 17 18 Test Decision: Reject null in favor of alternative at 5% significance level 19*/
@stdlib/stats-ttest
: one-sample and paired Student's t-Test.This package is part of 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, see the main project repository.
See LICENSE.
Copyright © 2016-2024. The Stdlib Authors.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
3 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 2
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
no SAST tool detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More