Usage
var ttest2 = require( '@stdlib/stats-ttest2' );
ttest2( x, y[, opts] )
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.
var x = [ 0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0 ];
var y = [ 1.9, 0.8, 1.1, 0.1, -0.1, 4.4, 5.5, 1.6, 4.6, 3.4 ];
var out = ttest2( x, y );
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.
console.log( out.print() );
The function accepts the following options
:
- alpha:
number
in the interval [0,1]
giving the significance level of the hypothesis test. Default: 0.05
. - alternative: Either
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
. - difference:
number
denoting the difference in means under the null hypothesis. Default: 0
. - variance:
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.
var x = [ 0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0 ];
var y = [ 1.9, 0.8, 1.1, 0.1, -0.1, 4.4, 5.5, 1.6, 4.6, 3.4 ];
var out = ttest2( x, y, {
'alpha': 0.1
});
var table = out.print();
By default, a two-sided test is performed. To perform either of the one-sided tests, set the alternative
option to less
or greater
.
var x = [ 0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0 ];
var y = [ 1.9, 0.8, 1.1, 0.1, -0.1, 4.4, 5.5, 1.6, 4.6, 3.4 ];
var out = ttest2( x, y, {
'alternative': 'less'
});
var table = out.print();
out = ttest2( x, y, {
'alternative': 'greater'
});
table = out.print();
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
.
var x = [ 2, 3, 1, 4 ];
var y = [ 1, 2, 3, 1, 2, 5, 3, 4 ];
var out = ttest2( x, y, {
'variance': 'equal'
});
var table = out.print();
To test whether the difference in the population means is equal to some other value than 0
, set the difference
option.
var normal = require( '@stdlib/random-base-normal' ).factory;
var table;
var rnorm;
var out;
var x;
var y;
var i;
rnorm = normal({
'seed': 372
});
x = new Array( 100 );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = rnorm( 2.0, 3.0 );
}
y = new Array( 100 );
for ( i = 0; i < x.length; i++ ) {
y[ i ] = rnorm( 1.0, 3.0 );
}
out = ttest2( x, y, {
'difference': 1.0,
'variance': 'equal'
});
table = out.print();