All files / stats/bartlett-test/lib main.js

100% Statements 151/151
100% Branches 22/22
100% Functions 1/1
100% Lines 151/151

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 1521x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 15x 15x 15x 15x 8x 8x 15x 31x 3x 3x 3x 3x 1x 1x 3x 3x 3x 31x 20x 49x 49x 20x 21x 21x 21x 21x 21x 21x 31x 45x 45x 8x 8x 45x 1x 1x 36x 36x 36x 36x 36x 36x 36x 12x 31x 9x 31x 3x 3x 31x 2x 2x 10x 10x 10x 10x 10x 10x 10x 31x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var isCollection = require( '@stdlib/assert/is-collection' );
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
var objectKeys = require( '@stdlib/utils/keys' );
var format = require( '@stdlib/string/format' );
var chisqCDF = require( '@stdlib/stats/base/dists/chisquare/cdf' );
var group = require( '@stdlib/utils/group' );
var ln = require( '@stdlib/math/base/special/ln' );
var Int32Array = require( '@stdlib/array/int32' );
var Float64Array = require( '@stdlib/array/float64' );
var dvariance = require( '@stdlib/stats/base/dvariance' );
var validate = require( './validate.js' );
var Results = require( './results.js' );
 
 
// MAIN //
 
/**
* Computes Bartlett’s test for equal variances.
*
* @param {...NumericArray} arguments - either two or more number arrays or a single numeric array if an array of group indicators is supplied as an option
* @param {Options} [options] - function options
* @param {number} [options.alpha=0.05] - significance level
* @param {Array} [options.groups] - array of group indicators
* @throws {TypeError} must provide array-like arguments
* @throws {RangeError} alpha option has to be a number in the interval `[0,1]`
* @throws {Error} must provide at least two array-like arguments if `groups` is not set
* @throws {TypeError} options must be an object
* @throws {TypeError} must provide valid options
* @returns {Object} test results
*
* @example
* // Data from Hollander & Wolfe (1973), p. 116:
* var x = [ 2.9, 3.0, 2.5, 2.6, 3.2 ];
* var y = [ 3.8, 2.7, 4.0, 2.4 ];
* var z = [ 2.8, 3.4, 3.7, 2.2, 2.0 ];
*
* var out = bartlett( x, y, z );
* // returns {...}
*/
function bartlett() {
	var options;
	var ngroups;
	var ninvSum;
	var levels;
	var table;
	var alpha;
	var nSum;
	var vSum;
	var args;
	var opts;
	var pval;
	var stat;
	var arg;
	var err;
	var lnv;
	var df;
	var n;
	var v;
	var i;
 
	args = [];
	ngroups = arguments.length;
	opts = {};
	if ( isPlainObject( arguments[ ngroups - 1 ] ) ) {
		options = arguments[ ngroups - 1 ];
		ngroups -= 1;
		err = validate( opts, options );
		if ( err ) {
			throw err;
		}
	}
	if ( opts.groups ) {
		table = group( arguments[ 0 ], opts.groups );
		levels = objectKeys( table );
		ngroups = levels.length;
		if ( ngroups < 2 ) {
			throw new Error( format( 'invalid option. `%s` option must be an array containing at least two unique elements. Option: `%s`.', 'groups', levels ) );
		}
		for ( i = 0; i < ngroups; i++ ) {
			args.push( table[ levels[ i ] ] );
		}
	} else {
		for ( i = 0; i < ngroups; i++ ) {
			args.push( arguments[ i ] );
		}
	}
	nSum = 0;
	ninvSum = 0.0;
	vSum = 0.0;
	lnv = 0.0;
	n = new Int32Array( ngroups );
	v = new Float64Array( ngroups );
	for ( i = 0; i < ngroups; i++ ) {
		arg = args[ i ];
		if ( !isCollection( arg ) ) {
			throw new TypeError( format( 'invalid argument. Must provide array-like arguments. Value: `%s`.', arg ) );
		}
		if ( arg.length === 0 ) {
			throw new Error( format( 'invalid argument. Supplied arrays cannot be empty. Value: `%s`.', arg ) );
		}
		n[ i ] = arg.length - 1;
		nSum += n[ i ];
		ninvSum += 1.0 / n[ i ];
		v[ i ] = dvariance( arg.length, 1, arg, 1 );
		vSum += ( n[ i ] * v[ i ] );
		lnv += n[ i ] * ln( v[ i ] );
	}
	vSum /= nSum;
	if ( opts.alpha === void 0 ) {
		alpha = 0.05;
	} else {
		alpha = opts.alpha;
	}
	if ( alpha < 0.0 || alpha > 1.0 ) {
		throw new RangeError( format( 'invalid option. `%s` option must be a number on the interval: [0, 1]. Option: `%f`.', 'alpha', alpha ) );
	}
 
	stat = ( ( nSum * ln( vSum ) ) - lnv );
	stat /= ( 1.0 + ( ( ninvSum - ( 1.0 / nSum ) ) / ( 3 * ( ngroups-1 ) ) ) );
	df = ngroups - 1;
	pval = 1.0 - chisqCDF( stat, df );
 
	return new Results( pval, alpha, stat, df );
}
 
 
// EXPORTS //
 
module.exports = bartlett;